-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy path__main__.py
126 lines (98 loc) · 4.27 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""EcoData Retriever Wizard
Running this module directly will launch the download wizard, allowing the user
to choose from all scripts.
The main() function can be used for bootstrapping.
"""
import os
import platform
import sys
# sys removes the setdefaultencoding method at startup; reload to get it back
reload(sys)
if hasattr(sys, 'setdefaultencoding'):
# set default encoding to latin-1 to avoid ascii encoding issues
sys.setdefaultencoding('latin-1')
from retriever import VERSION, MASTER, SCRIPT_LIST, sample_script
from retriever.engines import engine_list
from retriever.lib.repository import check_for_updates
from retriever.lib.lists import Category, get_lists
from retriever.lib.tools import choose_engine, name_matches
from retriever.lib.get_opts import parser
def main():
"""This function launches the EcoData Retriever."""
if len(sys.argv) == 1 or (len(sys.argv) > 1 and sys.argv[1] == 'gui'):
# if no command line args are passed, launch GUI
check_for_updates(graphical=False if 'darwin' in platform.platform().lower() else True)
lists = get_lists()
from retriever.app.main import launch_app
launch_app(lists)
else:
# otherwise, parse them
script_list = SCRIPT_LIST()
args = parser.parse_args()
if args.quiet:
sys.stdout = open(os.devnull, 'w')
if args.command == 'help':
parser.parse_args(['-h'])
if hasattr(args, 'compile') and args.compile:
script_list = SCRIPT_LIST(force_compile=True)
if args.command == 'update':
check_for_updates(graphical=False)
script_list = SCRIPT_LIST()
return
elif args.command == 'citation':
if args.dataset is None:
citation_path = os.path.join(os.path.split(__file__)[0], '../CITATION')
print citation_path
with open(citation_path) as citation_file:
print citation_file.read()
else:
scripts = name_matches(script_list, args.dataset)
for dataset in scripts:
print dataset.description
return
elif args.command == 'gui':
lists = get_lists()
from retriever.app.main import launch_app
launch_app(lists)
return
elif args.command == 'new':
f = open(args.filename, 'w')
f.write(sample_script)
f.close()
return
if args.command == 'ls' or args.dataset is None:
import lscolumns
#If scripts have never been downloaded there is nothing to list
if not script_list:
print "No scripts are currently available. Updating scripts now..."
check_for_updates(graphical=False)
print "\n\nScripts downloaded.\n"
script_list = SCRIPT_LIST()
all_scripts = set([script.shortname for script in script_list])
all_tags = set(["ALL"] +
[tag.strip().upper() for script in script_list for tagset in script.tags for tag in tagset.split('>')])
print "Available datasets (%s):" % len(all_scripts)
lscolumns.printls(sorted(list(all_scripts), key=lambda s: s.lower()))
print "Groups:"
lscolumns.printls(sorted(list(all_tags)))
return
engine = choose_engine(args.__dict__)
if hasattr(args, 'debug') and args.debug: debug = True
else: debug = False
scripts = name_matches(script_list, args.dataset)
if scripts:
for dataset in scripts:
print "=> Installing", dataset.name
try:
dataset.download(engine, debug=debug)
dataset.engine.final_cleanup()
except KeyboardInterrupt:
pass
except Exception as e:
print e
print "Done!"
else:
print "The dataset %s isn't currently availabe in the Retriever" % (args.dataset)
print "Run 'retriever -ls to see a list of currently available datasets"
if __name__ == "__main__":
main()