Commit a3777d10 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

add the script sections with replace the old toolbox controller.

parent a1bfa8ac
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" NAME
export-to-csv -- export table content as CSV file
SYNOPSIS
export-to-csv [options] table file
DESCRIPTION
Write the full content of the table to a CSV file.
OPTIONS
-h, --help
Display the help and exit.
EXAMPLE
> cd ...track_lhcbfrance/scripts
> ./run export-to-csv.py history ~/mywap/track_lhcbfrance/scripts/history.csv
AUTHOR
R. Le Gac
"""
if __name__ == "__main__":
import csv
import os
import sys
from argparse import ArgumentParser, FileType
# command line options
parser = ArgumentParser()
parser.add_argument('table',
help='the name of the table.')
parser.add_argument('file',
type=FileType('w'),
help='the absolute path of the CSV file.')
args = parser.parse_args()
# the CSV writer
writer = csv.DictWriter(args.file,
db[args.table].fields,
quoting=csv.QUOTE_MINIMAL)
# write the header
headers = {}
for el in db[args.table].fields:
headers[el] = el
writer.writerow(headers)
# write table content
for row in db(db[args.table]).select():
writer.writerow(row.as_dict())
# close
print 'Table "%s" exported to "%s"' % (args.table, args.file.name)
args.file.close()
sys.exit(0)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" NAME
import-from-csv -- import table content from CSV file
SYNOPSIS
import-from-csv [options] file table
DESCRIPTION
Load the full content of the table from a CSV file.
OPTIONS
-h, --help
Display the help and exit.
EXAMPLE
> cd ...track_lhcbfrance/scripts
> ./run import-from-csv.py ~/mywap/track_lhcbfrance/scripts/history.csv history
AUTHOR
R. Le Gac
"""
if __name__ == "__main__":
import csv
import os
import sys
from argparse import ArgumentParser, FileType
# command line options
parser = ArgumentParser()
parser.add_argument('file',
type=FileType('r'),
help='the absolute path of the CSV file.')
parser.add_argument('table',
help='the name of the table.')
args = parser.parse_args()
# load table content
reader = csv.DictReader(args.file)
for di in reader:
db[args.table][0] = di
db.commit()
# close
print 'File "%s" import in "%s"' % (args.file.name, args.table)
args.file.close()
sys.exit(0)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" NAME
run -- run a script using the track_lhcbfrance model
SYNOPSIS
run [options] script.py
DESCRIPTION
run the script in the web2py context when the track_lhcbfrance
model is instantiated.
OPTIONS
-h, --help
Display the help and exit.
EXAMPLE
> cd ... track_lhcbfrance/scripts
> ./run myscript.py
AUTHOR
R. Le Gac
"""
if __name__ == "__main__":
import os
import subprocess
import sys
from argparse import ArgumentParser
APP = 'track_lhcbfrance'
WEB2PY_DIR = '../../web2py'
WEB2PY = os.path.join(WEB2PY_DIR, 'web2py.py')
# command line options
parser = ArgumentParser()
parser.add_argument('script',
help='the name of the script to be run.')
parser.add_argument('args',
nargs='*',
help='the arguments for the script.')
args = parser.parse_args()
# run the script in the track_publication context
script_path = os.path.join(os.getcwd(), args.script)
cmd = [WEB2PY,
'--no-banner',
'--shell', APP,
'--import_models',
'--run', script_path]
if args.args:
cmd.append('--args')
cmd.extend(args.args)
return_code = subprocess.call(cmd)
# exit
sys.exit(return_code)
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