2
from flexget.plugin import *
4
log = logging.getLogger('cli_config')
7
class CliConfig(object):
10
Allows specifying yml configuration values from commandline parameters.
12
Yml variables are prefixed with dollar sign ($).
13
Commandline parameter must be comma separated list of variable=values.
15
Configuration example:
24
--cli-config "url=http://some.url/, path=~/downloads"
32
# there is no way to misconfigure this ..
33
from flexget import validator
34
return validator.factory('any')
36
def replace_dict(self, d, replaces):
37
for k, v in d.items():
38
if isinstance(v, basestring):
39
for key, value in replaces.iteritems():
41
nv = v.replace('$%s' % key, value)
42
log.debug('Replacing key %s (%s -> %s)' % (k, v, nv))
44
if isinstance(v, list):
46
if isinstance(lv, dict):
47
self.replace_dict(lv, replaces)
48
elif isinstance(lv, basestring):
49
for key, value in replaces.iteritems():
51
nv = lv.replace('$%s' % key, value)
52
log.debug('Replacing list item %s (%s -> %s)' % (k, lv, nv))
55
if isinstance(v, dict):
56
self.replace_dict(v, replaces)
58
def parse_replaces(self, feed):
59
"""Parses commandline string into internal dict"""
60
s = feed.manager.options.cli_config
62
return False # nothing to process
64
return True # already parsed
68
key = item[:item.index('=')]
70
log.critical('Invalid --cli-config, no name for %s' % item)
72
value = item[item.index('=') + 1:]
73
self.replaces[key.strip()] = value.strip()
76
def on_process_start(self, feed):
77
if self.parse_replaces(feed):
78
self.replace_dict(feed.config, self.replaces)
79
log.debug(feed.config)
81
register_plugin(CliConfig, 'cli_config', builtin=True)
82
register_parser_option('--cli-config', action='store', dest='cli_config', default=False,
83
metavar='PARAMS', help='Configuration parameters trough commandline. See --doc cli_config.')