1 """ |
1 """ |
2 Main entry point for the command-line interface |
2 Main entry point for the command-line interface |
3 """ |
3 """ |
4 |
4 |
5 import gallery, commands, config as config_module |
5 import gallery, commands, config |
6 |
6 |
7 from optparse import OptionParser |
7 from optparse import OptionParser |
|
8 import os.path |
8 |
9 |
9 def option_parser (command_name) : |
10 def build_config () : |
|
11 """ |
|
12 Build the default configuration to use |
|
13 """ |
|
14 |
|
15 return config.Configuration() |
|
16 |
|
17 def option_parser (exec_name) : |
10 """ |
18 """ |
11 Build the OptionParser that we use |
19 Build the OptionParser that we use |
12 """ |
20 """ |
13 |
21 |
14 # create parser using the given command |
22 parser = OptionParser(prog=exec_name, description="Degal - A photo gallery", version="???") |
15 parser = OptionParser(prog=command_name) |
|
16 |
23 |
17 # define options |
24 parser.add_option('-C', "--config", metavar='PATH', dest="_load_config_path", |
18 parser.add_option('-G', "--gallery-path", metavar='DIR', dest='gallery_path', default=None, |
25 help="Load configuration from PATH") |
19 help="Use DIR as the Gallery path [default: CWD]") |
|
20 |
26 |
21 parser.add_option('-F', "--force-update", dest='force_update', action="store_true", default=False, |
27 parser.add_option('-H', "--gallery-path", metavar='DIR', |
|
28 help="Use DIR as the Gallery path instead of the CWD") |
|
29 |
|
30 parser.add_option('-F', "--force-update", action="store_true", |
22 help="--force-thumb + --force-html") |
31 help="--force-thumb + --force-html") |
23 |
32 |
24 parser.add_option("--force-thumb", dest='force_thumb', action="store_true", default=False, |
33 parser.add_option("--force-thumb", action="store_true", |
25 help="Force-update all thumbnails") |
34 help="Force-update thumbnails") |
26 |
35 |
27 parser.add_option("--force-html", dest='force_html', action="store_true", default=False, |
36 parser.add_option("--force-html", action="store_true", |
28 help="Force-update all .html files") |
37 help="Force-update .html files") |
29 |
38 |
30 parser.add_option("--with-exif", dest='exif_enabled', action="store_true", default=None, |
39 parser.add_option("--with-exif", action="store_true", |
31 help="Include Exif metadata in updated .html files") |
40 help="Include Exif metadata in updated .html files") |
32 |
41 |
33 parser.add_option('-c', "--thread-count", dest='thread_count', type="int", default=None, |
42 parser.add_option('-c', "--thread-count", metavar='COUNT', type="int", |
34 help="Size of thread pool") |
43 help="Use COUNT threads for concurrent tasks") |
35 |
44 |
36 parser.add_option('-d', "--debug", dest='debug', action="store_true", default=False, |
45 parser.add_option('-d', "--debug", action="store_const", dest="log_level", const=config.logging.DEBUG, |
37 help="Show debug output") |
46 help="Show debug output") |
38 |
47 |
39 parser.add_option('-q', "--quiet", dest='quiet', action="store_true", default=False, |
48 parser.add_option('-q', "--quiet", action="store_const", dest="log_level", const=config.logging.WARN, |
40 help="Reduced output") |
49 help="Reduced output (only warnings)") |
41 |
50 |
42 return parser |
51 return parser |
43 |
52 |
44 def build_config (options) : |
53 def parse_args (config, parser, args) : |
45 """ |
54 """ |
46 Build a configuration object with the given options |
55 Parse command-line options/arguments. |
|
56 |
|
57 Returns the remaining positional arguments. |
47 """ |
58 """ |
48 |
59 |
49 # build default config |
60 # parse the given arguments, storing output directly in the config |
50 config = config_module.Configuration() |
61 _, args = parser.parse_args(args=args, values=config) |
|
62 |
|
63 # return the posargs |
|
64 return args |
|
65 |
|
66 def postprocess_config (config) : |
|
67 """ |
|
68 Post-process our Configuration after our command-line arguments have been parsed. |
|
69 |
|
70 This will attempt to load any additional configuration. |
|
71 """ |
51 |
72 |
52 # apply options |
73 # figure out what, if any, path to import |
53 if options.gallery_path : |
74 if hasattr(config, '_load_config_path') : |
54 config.gallery_path = options.gallery_path |
75 path = config._load_config_path |
|
76 |
|
77 elif os.path.exists('degal.cfg') : |
|
78 path = 'degal.cfg' |
55 |
79 |
56 if options.force_update : |
80 else : |
57 config.force_html = True |
81 return |
58 config.force_thumb = True |
|
59 |
|
60 if options.force_thumb : |
|
61 config.force_thumb = True |
|
62 |
82 |
63 if options.force_html : |
83 # import it |
64 config.force_html = True |
84 config.import_file(path) |
65 |
|
66 if options.exif_enabled is not None : |
|
67 config.exif_enabled = options.exif_enabled |
|
68 |
|
69 if options.thread_count is not None : |
|
70 config.thread_count = options.thread_count |
|
71 |
|
72 if options.debug : |
|
73 config.log_level = config_module.logging.DEBUG |
|
74 |
|
75 if options.quiet : |
|
76 config.log_level = config_module.logging.WARN |
|
77 |
|
78 # XXX: load config file(s) |
|
79 |
|
80 return config |
|
81 |
85 |
82 def load_gallery (config) : |
86 def load_gallery (config) : |
83 """ |
87 """ |
84 Create the Gallery object that we are manipulating |
88 Create the Gallery object that we are manipulating |
85 """ |
89 """ |
112 """ |
116 """ |
113 |
117 |
114 ## load commands |
118 ## load commands |
115 #commands = load_commands() |
119 #commands = load_commands() |
116 |
120 |
|
121 # build our default config |
|
122 config = build_config() |
|
123 |
117 # build optparser |
124 # build optparser |
118 parser = option_parser(argv[0]) |
125 parser = option_parser(argv[0]) |
119 |
|
120 # parse the given argv |
|
121 options, args = parser.parse_args(argv[1:]) |
|
122 |
126 |
123 # build our config |
127 # parse the args into our config |
124 config = build_config(options) |
128 args = parse_args(config, parser, argv[1:]) |
|
129 |
|
130 # postprocess |
|
131 postprocess_config(config) |
125 |
132 |
126 # open gallery |
133 # open gallery |
127 gallery = load_gallery(config) |
134 gallery = load_gallery(config) |
128 |
135 |
129 # figure out what command to run |
136 # figure out what command to run |
130 command, args, kwargs = load_command(config, args) |
137 command, args, kwargs = load_command(config, args) |
131 |
138 |
|
139 |
|
140 |
132 # run the selected command |
141 # run the selected command |
133 ret = run_command(config, gallery, command, args, kwargs) |
142 ret = run_command(config, gallery, command, args, kwargs) |
134 |
143 |
|
144 |
135 if ret is None : |
145 if ret is None : |
136 # success |
146 # success |
137 return 0 |
147 return 0 |
138 |
148 |
139 else : |
149 else : |