Commit 87cf1c06 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Polish the script to hnadle properly json type field.

parent e23a2689
......@@ -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)
......
......@@ -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
......
#!/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')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment