Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
tev
plugin_event
Commits
87cf1c06
Commit
87cf1c06
authored
Jan 28, 2015
by
LE GAC Renaud
Browse files
Polish the script to hnadle properly json type field.
parent
e23a2689
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
10 deletions
+28
-10
scripts/export-to-csv.py
scripts/export-to-csv.py
+9
-2
scripts/import-from-csv.py
scripts/import-from-csv.py
+14
-3
scripts/run
scripts/run
+5
-5
No files found.
scripts/export-to-csv.py
View file @
87cf1c06
...
...
@@ -26,6 +26,7 @@
if
__name__
==
"__main__"
:
import
csv
import
json
import
os
import
sys
...
...
@@ -53,9 +54,15 @@ if __name__ == "__main__":
headers
[
el
]
=
el
writer
.
writerow
(
headers
)
# write table content
# write table content
dumping properly json field
for
row
in
db
(
db
[
args
.
table
]).
select
():
writer
.
writerow
(
row
.
as_dict
())
di
=
{}
for
el
in
headers
:
if
db
[
args
.
table
][
el
].
type
==
'json'
:
di
[
el
]
=
json
.
dumps
(
row
[
el
])
else
:
di
[
el
]
=
row
[
el
]
writer
.
writerow
(
di
)
# close
print
'Table "%s" exported to "%s"'
%
(
args
.
table
,
args
.
file
.
name
)
...
...
scripts/import-from-csv.py
View file @
87cf1c06
...
...
@@ -26,6 +26,7 @@
if
__name__
==
"__main__"
:
import
csv
import
json
import
os
import
sys
...
...
@@ -43,11 +44,21 @@ if __name__ == "__main__":
args
=
parser
.
parse_args
()
# load table content
# json fields
json_fields
=
[]
for
field
in
db
[
args
.
table
]:
if
field
.
type
==
'json'
:
json_fields
.
append
(
field
.
name
)
# load table content loading properly json fields
reader
=
csv
.
DictReader
(
args
.
file
)
for
di
in
reader
:
db
[
args
.
table
][
0
]
=
di
for
el
in
json_fields
:
di
[
el
]
=
json
.
loads
(
di
[
el
])
db
[
args
.
table
].
insert
(
**
di
)
db
.
commit
()
# close
...
...
scripts/run
View file @
87cf1c06
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" NAME
run -- run a script using the
track_lhcbfrance
model
run -- run a script using the
application
model
SYNOPSIS
run [options] script.py
DESCRIPTION
run the script in the web2py context when the
track_lhcbfrance
model is instantiated.
run the script in the web2py context when the
application
model is instantiated.
OPTIONS
-h, --help
...
...
@@ -16,7 +16,7 @@
EXAMPLE
> cd ...
track_lhcbfrance
/scripts
> cd ...
myapp
/scripts
> ./run myscript.py
AUTHOR
...
...
@@ -31,7 +31,7 @@ if __name__ == "__main__":
from
argparse
import
ArgumentParser
APP
=
'track_lhcbfrance'
APP
=
os
.
getcwd
().
split
(
os
.
sep
)[
-
2
]
WEB2PY_DIR
=
'../../web2py'
WEB2PY
=
os
.
path
.
join
(
WEB2PY_DIR
,
'web2py.py'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment