-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmain.py
305 lines (235 loc) · 10.7 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import os
import sys
import textwrap
import warnings
import deprecation
from io import StringIO
from configparser import ConfigParser
from configparser import Error as ConfigParserError
import eyed3
import eyed3.utils
import eyed3.utils.console
import eyed3.plugins
import eyed3.__about__
from eyed3.utils.log import initLogging
DEFAULT_PLUGIN = "classic"
DEFAULT_CONFIG = os.path.expandvars("${HOME}/.config/eyeD3/config.ini")
USER_PLUGINS_DIR = os.path.expandvars("${HOME}/.config/eyeD3/plugins")
DEFAULT_CONFIG_DEPRECATED = os.path.expandvars("${HOME}/.eyeD3/config.ini")
USER_PLUGINS_DIR_DEPRECATED = os.path.expandvars("${HOME}/.eyeD3/plugins")
def main(args, config):
if "list_plugins" in args and args.list_plugins:
_listPlugins(config)
return 0
args.plugin.start(args, config)
recursive = False
if "non_recursive" in args:
recursive = not args.non_recursive
elif "recursive" in args:
recursive = args.recursive
# Process paths (files/directories)
for p in args.paths:
eyed3.utils.walk(args.plugin, p, excludes=args.excludes, fs_encoding=args.fs_encoding,
recursive=recursive)
retval = args.plugin.handleDone()
return retval or 0
def _listPlugins(config):
from eyed3.utils.console import Fore, Style
def header(name):
is_default = name == DEFAULT_PLUGIN
return (Style.BRIGHT + (Fore.GREEN if is_default else '') + "* " +
name + Style.RESET_ALL)
all_plugins = eyed3.plugins.load(reload=True, paths=_getPluginPath(config))
# Create a new dict for sorted display
plugin_names = []
for plugin in set(all_plugins.values()):
plugin_names.append(plugin.NAMES[0])
print("\nType 'eyeD3 --plugin=<name> --help' for more help\n")
plugin_names.sort()
for name in plugin_names:
plugin = all_plugins[name]
alt_names = plugin.NAMES[1:]
alt_names = f" ({', '.join(alt_names)})" if alt_names else ""
print(f"{header(name)} {alt_names}:")
for txt in textwrap.wrap(plugin.SUMMARY, initial_indent=' ' * 2, subsequent_indent=' ' * 2):
print(f"{Fore.YELLOW}{txt}{Style.RESET_ALL}")
print("")
@deprecation.deprecated(deprecated_in="0.9a2", removed_in="1.0",
current_version=eyed3.__about__.__version__,
details=f"Default eyeD3 config moved to {DEFAULT_CONFIG}")
def _deprecatedConfigFileCheck(_):
"""This here to add deprecation."""
def _loadConfig(args):
config_files = []
if args.config:
config_files.append(os.path.abspath(args.config))
if args.no_config is False:
config_files.append(DEFAULT_CONFIG)
config_files.append(DEFAULT_CONFIG_DEPRECATED)
if not config_files:
return None
for config_file in config_files:
if os.path.isfile(config_file):
_deprecatedConfigFileCheck(config_file)
try:
config = ConfigParser()
config.read(config_file)
except ConfigParserError as ex:
eyed3.log.warning(f"User config error: {ex}")
return None
else:
return config
elif config_file != DEFAULT_CONFIG and config_file != DEFAULT_CONFIG_DEPRECATED:
raise IOError(f"User config not found: {config_file}")
def _getPluginPath(config):
plugin_path = [USER_PLUGINS_DIR]
if config and config.has_option("default", "plugin_path"):
val = config.get("default", "plugin_path")
plugin_path += [os.path.expanduser(os.path.expandvars(d)) for d
in val.split(':') if val]
return plugin_path
def profileMain(args, config): # pragma: no cover
"""This is the main function for profiling
http://code.google.com/appengine/kb/commontasks.html#profiling
"""
import cProfile
import pstats
eyed3.log.debug("driver profileMain")
prof = cProfile.Profile()
prof = prof.runctx("main(args)", globals(), locals())
stream = StringIO()
stats = pstats.Stats(prof, stream=stream)
stats.sort_stats("time") # Or cumulative
stats.print_stats(100) # 80 = how many to print
# The rest is optional.
stats.print_callees()
stats.print_callers()
sys.stderr.write("Profile data:\n%s\n" % stream.getvalue())
return 0
def setFileScannerOpts(arg_parser, default_recursive=False, paths_metavar="PATH",
paths_help="Files or directory paths"):
if default_recursive is False:
arg_parser.add_argument("-r", "--recursive", action="store_true", dest="recursive",
help="Recurse into subdirectories.")
else:
arg_parser.add_argument("-R", "--non-recursive", action="store_true", dest="non_recursive",
help="Do not recurse into subdirectories.")
arg_parser.add_argument("--exclude", action="append", metavar="PATTERN", dest="excludes",
help="A regular expression for path exclusion. May be specified "
"multiple times.")
arg_parser.add_argument("--fs-encoding", action="store", dest="fs_encoding",
default=eyed3.LOCAL_FS_ENCODING, metavar="ENCODING",
help="Use the specified file system encoding for filenames. "
f"Default as it was detected is '{eyed3.LOCAL_FS_ENCODING}' but "
"this option is still useful when reading from mounted file "
"systems.")
arg_parser.add_argument("paths", metavar=paths_metavar, nargs="*", help=paths_help)
def makeCmdLineParser(subparser=None):
from eyed3.utils import ArgumentParser
p = ArgumentParser(prog=eyed3.__about__.__project_name__, add_help=True)\
if not subparser else subparser
setFileScannerOpts(p)
p.add_argument("-L", "--plugins", action="store_true", default=False,
dest="list_plugins", help="List all available plugins")
p.add_argument("-P", "--plugin", action="store", dest="plugin",
default=None, metavar="NAME",
help=f"Specify which plugin to use. The default is '{DEFAULT_PLUGIN}'")
p.add_argument("-C", "--config", action="store", dest="config",
default=None, metavar="FILE",
help="Supply a configuration file. The default is "
f"'{DEFAULT_CONFIG}', although even that is optional.")
p.add_argument("--backup", action="store_true", dest="backup",
help="Plugins should honor this option such that "
"a backup is made of any file modified. The backup "
"is made in same directory with a '.orig' "
"extension added.")
p.add_argument("-Q", "--quiet", action="store_true", dest="quiet",
default=False, help="A hint to plugins to output less.")
p.add_argument("--no-color", action="store_true", dest="no_color",
help="Suppress color codes in console output. "
"This will happen automatically if the output is "
"not a TTY (e.g. when redirecting to a file)")
p.add_argument("--no-config",
action="store_true", dest="no_config",
help=f"Do not load the default user config '{DEFAULT_CONFIG}'. "
"The -c/--config options are still honored if present.")
return p
def parseCommandLine(cmd_line_args=None):
cmd_line_args = list(cmd_line_args) if cmd_line_args else list(sys.argv[1:])
# Remove any options not related to plugin/config for first parse. These
# determine the parser for the next stage.
stage_one_args = []
idx, auto_append = 0, False
while idx < len(cmd_line_args):
opt = cmd_line_args[idx]
if auto_append:
stage_one_args.append(opt)
auto_append = False
if opt in ("-C", "--config", "-P", "--plugin", "--no-config"):
stage_one_args.append(opt)
if opt != "--no-config":
auto_append = True
elif (opt.startswith("-C=") or opt.startswith("--config=") or
opt.startswith("-P=") or opt.startswith("--plugin=")):
stage_one_args.append(opt)
idx += 1
parser = makeCmdLineParser()
args = parser.parse_args(stage_one_args)
config = _loadConfig(args)
if args.plugin:
# Plugin on the command line takes precedence over config.
plugin_name = args.plugin
elif config and config.has_option("default", "plugin"):
# Get default plugin from config or use DEFAULT_CONFIG
plugin_name = config.get("default", "plugin")
if not plugin_name:
plugin_name = DEFAULT_PLUGIN
else:
plugin_name = DEFAULT_PLUGIN
PluginClass = eyed3.plugins.load(plugin_name, paths=_getPluginPath(config))
if PluginClass is None:
eyed3.utils.console.printError("Plugin not found: %s" % plugin_name)
parser.exit(1)
plugin = PluginClass(parser)
if config and config.has_option("default", "options"):
cmd_line_args.extend(config.get("default", "options").split())
if config and config.has_option(plugin_name, "options"):
cmd_line_args.extend(config.get(plugin_name, "options").split())
# Re-parse the command line including options from the config.
args = parser.parse_args(args=cmd_line_args)
args.plugin = plugin
eyed3.log.debug("command line args: %s", args)
eyed3.log.debug("plugin is: %s", plugin)
return args, parser, config
def _main():
"""Entry point"""
initLogging()
args = None
try:
args, _, config = parseCommandLine()
eyed3.utils.console.AnsiCodes.init(not args.no_color)
mainFunc = main if args.debug_profile is False else profileMain
retval = mainFunc(args, config)
except KeyboardInterrupt:
retval = 0
except (StopIteration, IOError) as ex:
eyed3.utils.console.printError(str(ex))
retval = 1
except Exception as ex:
eyed3.utils.console.printError(f"Uncaught exception: {ex}\n")
eyed3.log.exception(ex)
retval = 1
if args.debug_pdb:
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore", PendingDeprecationWarning)
# Must delay the import of ipdb as say as possible because
# of https://github.com/gotcha/ipdb/issues/48
import ipdb as pdb # noqa
except ImportError:
import pdb # noqa
e, m, tb = sys.exc_info()
pdb.post_mortem(tb)
sys.exit(retval)
if __name__ == "__main__": # pragma: no cover
_main()