本文整理汇总了Python中pants.option.ranked_value.RankedValue类的典型用法代码示例。如果您正苦于以下问题:Python RankedValue类的具体用法?Python RankedValue怎么用?Python RankedValue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RankedValue类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _format_record
def _format_record(self, record):
value_color = green if self.get_options().colors else lambda x: x
rank_color = self._rank_color(record.rank)
return '{value} {rank}'.format(
value=value_color(str(record.value)),
rank=rank_color('(from {rank}{details})'.format(
rank=RankedValue.get_rank_name(record.rank),
details=' {}'.format(record.details) if record.details else '',
)),
)
示例2: register_options
def register_options(cls, register):
super(ExplainOptionsTask, cls).register_options(register)
register('--scope', help='Only show options in this scope.')
register('--name', help='Only show options with this name.')
register('--rank', choices=RankedValue.get_names(),
help='Only show options with at least this importance.')
register('--show-history', action='store_true', default=False,
help='Show the previous values options had before being overridden.')
register('--only-overridden', action='store_true', default=False,
help='Only show values that overrode defaults.')
示例3: register_options
def register_options(cls, register):
super(ExplainOptionsTask, cls).register_options(register)
register('--scope', help='Only show options in this scope.')
register('--name', help='Only show options with this name.')
register('--rank', choices=RankedValue.get_names(),
help='Only show options with at least this importance.')
register('--show-history', type=bool,
help='Show the previous values options had before being overridden.')
register('--only-overridden', type=bool,
help='Only show values that overrode defaults.')
register('--skip-inherited', type=bool, default=True,
help='Do not show inherited options, unless their values differ from their parents.')
示例4: _compute_default
def _compute_default(self, dest, kwargs):
"""Compute the default value to use for an option's registration.
The source of the default value is chosen according to the ranking in RankedValue.
"""
config_section = 'DEFAULT' if self._scope == GLOBAL_SCOPE else self._scope
env_var = 'PANTS_{0}_{1}'.format(config_section.upper().replace('.', '_'), dest.upper())
value_type = kwargs.get('type', str)
env_val_str = self._env.get(env_var) if self._env else None
env_val = None if env_val_str is None else value_type(env_val_str)
config_val = self._config.get(config_section, dest, default=None) if self._config else None
hardcoded_val = kwargs.get('default')
return RankedValue.choose(None, env_val, config_val, hardcoded_val)
示例5: _format_record
def _format_record(self, record):
simple_rank = RankedValue.get_rank_name(record.rank)
if self.is_json():
return record.value, simple_rank
elif self.is_text():
simple_value = str(record.value)
value_color = green if self.get_options().colors else lambda x: x
formatted_value = value_color(simple_value)
rank_color = self._rank_color(record.rank)
formatted_rank = '(from {rank}{details})'.format(
rank=simple_rank,
details=rank_color(' {}'.format(record.details)) if record.details else '',
)
return '{value} {rank}'.format(
value=formatted_value,
rank=formatted_rank,
)
示例6: _compute_default
def _compute_default(self, dest, kwargs):
"""Compute the default value to use for an option's registration.
The source of the default value is chosen according to the ranking in RankedValue.
"""
config_section = 'DEFAULT' if self._scope == GLOBAL_SCOPE else self._scope
udest = dest.upper()
if self._scope == GLOBAL_SCOPE:
# For convenience, we allow three forms of env var for global scope options.
# The fully-specified env var is PANTS_DEFAULT_FOO, which is uniform with PANTS_<SCOPE>_FOO
# for all the other scopes. However we also allow simply PANTS_FOO. And if the option name
# itself starts with 'pants-' then we also allow simply FOO. E.g., PANTS_WORKDIR instead of
# PANTS_PANTS_WORKDIR or PANTS_DEFAULT_PANTS_WORKDIR. We take the first specified value we
# find, in this order: PANTS_DEFAULT_FOO, PANTS_FOO, FOO.
env_vars = ['PANTS_DEFAULT_{0}'.format(udest), 'PANTS_{0}'.format(udest)]
if udest.startswith('PANTS_'):
env_vars.append(udest)
else:
sanitized_env_var_scope = self._ENV_SANITIZER_RE.sub('_', config_section.upper())
env_vars = ['PANTS_{0}_{1}'.format(sanitized_env_var_scope, udest)]
value_type = self.str_to_bool if is_boolean_flag(kwargs) else kwargs.get('type', str)
env_val_str = None
if self._env:
for env_var in env_vars:
if env_var in self._env:
env_val_str = self._env.get(env_var)
break
env_val = None if env_val_str is None else value_type(env_val_str)
if kwargs.get('action') == 'append':
config_val_strs = self._config.getlist(config_section, dest) if self._config else None
config_val = (None if config_val_strs is None else
[value_type(config_val_str) for config_val_str in config_val_strs])
default = []
else:
config_val_str = (self._config.get(config_section, dest, default=None)
if self._config else None)
config_val = None if config_val_str is None else value_type(config_val_str)
default = None
hardcoded_val = kwargs.get('default')
return RankedValue.choose(None, env_val, config_val, hardcoded_val, default)
示例7: _rank_filter
def _rank_filter(self, rank):
pattern = self.get_options().rank
if not pattern:
return True
return rank >= RankedValue.get_rank_value(pattern)
示例8: _compute_value
def _compute_value(self, dest, kwargs, flag_vals):
"""Compute the value to use for an option.
The source of the default value is chosen according to the ranking in RankedValue.
"""
is_fromfile = kwargs.get('fromfile', False)
action = kwargs.get('action')
if is_fromfile and action and action != 'append':
raise ParseError('Cannot fromfile {} with an action ({}) in scope {}'
.format(dest, action, self._scope))
config_section = GLOBAL_SCOPE_CONFIG_SECTION if self._scope == GLOBAL_SCOPE else self._scope
udest = dest.upper()
if self._scope == GLOBAL_SCOPE:
# For convenience, we allow three forms of env var for global scope options.
# The fully-specified env var is PANTS_DEFAULT_FOO, which is uniform with PANTS_<SCOPE>_FOO
# for all the other scopes. However we also allow simply PANTS_FOO. And if the option name
# itself starts with 'pants-' then we also allow simply FOO. E.g., PANTS_WORKDIR instead of
# PANTS_PANTS_WORKDIR or PANTS_DEFAULT_PANTS_WORKDIR. We take the first specified value we
# find, in this order: PANTS_DEFAULT_FOO, PANTS_FOO, FOO.
env_vars = ['PANTS_DEFAULT_{0}'.format(udest), 'PANTS_{0}'.format(udest)]
if udest.startswith('PANTS_'):
env_vars.append(udest)
else:
sanitized_env_var_scope = self._ENV_SANITIZER_RE.sub('_', config_section.upper())
env_vars = ['PANTS_{0}_{1}'.format(sanitized_env_var_scope, udest)]
value_type = self.str_to_bool if is_boolean_flag(kwargs) else kwargs.get('type', str)
env_val_str = None
if self._env:
for env_var in env_vars:
if env_var in self._env:
env_val_str = self._env.get(env_var)
break
config_val_str = self._config.get(config_section, dest, default=None)
config_source_file = self._config.get_source_for_option(config_section, dest)
if config_source_file is not None:
config_source_file = os.path.relpath(config_source_file)
def expand(val_str):
if is_fromfile and val_str and val_str.startswith('@') and not val_str.startswith('@@'):
fromfile = val_str[1:]
try:
with open(fromfile) as fp:
return fp.read().strip()
except IOError as e:
raise self.FromfileError('Failed to read {} in {} from file {}: {}'.format(
dest, self._scope_str(), fromfile, e))
else:
# Support a literal @ for fromfile values via @@.
return val_str[1:] if is_fromfile and val_str.startswith('@@') else val_str
def parse_typed_list(val_str):
return None if val_str is None else [value_type(x) for x in list_option(expand(val_str))]
def parse_typed_item(val_str):
return None if val_str is None else value_type(expand(val_str))
flag_val = None
if flag_vals:
if action == 'append':
flag_val = [parse_typed_item(v) for v in flag_vals]
elif len(flag_vals) > 1:
raise ParseError('Multiple cmd line flags specified for option {} in {}'.format(
dest, self._scope_str()))
else:
flag_val = parse_typed_item(flag_vals[0])
default, parse = ([], parse_typed_list) if action == 'append' else (None, parse_typed_item)
config_val = parse(config_val_str)
env_val = parse(env_val_str)
hardcoded_val = kwargs.get('default')
config_details = 'in {}'.format(config_source_file) if config_source_file else None
# Note: ranked_vals is guaranteed to have at least one element, and none of the values
# of any of its elements will be None.
ranked_vals = list(reversed(list(RankedValue.prioritized_iter(
flag_val, env_val, config_val, hardcoded_val, default))))
choices = kwargs.get('choices')
for ranked_val in ranked_vals:
details = config_details if ranked_val.rank == RankedValue.CONFIG else None
self._option_tracker.record_option(scope=self._scope,
option=dest,
value=ranked_val.value,
rank=ranked_val.rank,
deprecation_version=kwargs.get('deprecated_version'),
details=details)
def check(val):
if choices is not None and val is not None and val not in choices:
raise ParseError('{} is not an allowed value for option {} in {}. '
'Must be one of: {}'.format(
val, dest, self._scope_str(), choices
))
return val
#.........这里部分代码省略.........
示例9: _compute_value
def _compute_value(self, dest, kwargs, flag_val_strs):
"""Compute the value to use for an option.
The source of the default value is chosen according to the ranking in RankedValue.
"""
# Helper function to convert a string to a value of the option's type.
def to_value_type(val_str):
if val_str is None:
return None
elif kwargs.get('type') == bool:
return self._ensure_bool(val_str)
else:
return self._wrap_type(kwargs.get('type', str))(val_str)
# Helper function to expand a fromfile=True value string, if needed.
def expand(val_str):
if kwargs.get('fromfile', False) and val_str and val_str.startswith('@'):
if val_str.startswith('@@'): # Support a literal @ for fromfile values via @@.
return val_str[1:]
else:
fromfile = val_str[1:]
try:
with open(fromfile) as fp:
return fp.read().strip()
except IOError as e:
raise self.FromfileError('Failed to read {} in {} from file {}: {}'.format(
dest, self._scope_str(), fromfile, e))
else:
return val_str
# Get value from config files, and capture details about its derivation.
config_details = None
config_section = GLOBAL_SCOPE_CONFIG_SECTION if self._scope == GLOBAL_SCOPE else self._scope
config_val_str = expand(self._config.get(config_section, dest, default=None))
config_source_file = self._config.get_source_for_option(config_section, dest)
if config_source_file is not None:
config_source_file = os.path.relpath(config_source_file)
config_details = 'in {}'.format(config_source_file)
# Get value from environment, and capture details about its derivation.
udest = dest.upper()
if self._scope == GLOBAL_SCOPE:
# For convenience, we allow three forms of env var for global scope options.
# The fully-specified env var is PANTS_GLOBAL_FOO, which is uniform with PANTS_<SCOPE>_FOO
# for all the other scopes. However we also allow simply PANTS_FOO. And if the option name
# itself starts with 'pants-' then we also allow simply FOO. E.g., PANTS_WORKDIR instead of
# PANTS_PANTS_WORKDIR or PANTS_GLOBAL_PANTS_WORKDIR. We take the first specified value we
# find, in this order: PANTS_GLOBAL_FOO, PANTS_FOO, FOO.
env_vars = ['PANTS_GLOBAL_{0}'.format(udest), 'PANTS_{0}'.format(udest)]
if udest.startswith('PANTS_'):
env_vars.append(udest)
else:
sanitized_env_var_scope = self._ENV_SANITIZER_RE.sub('_', self._scope.upper())
env_vars = ['PANTS_{0}_{1}'.format(sanitized_env_var_scope, udest)]
env_val_str = None
env_details = None
if self._env:
for env_var in env_vars:
if env_var in self._env:
env_val_str = expand(self._env.get(env_var))
env_details = 'from env var {}'.format(env_var)
break
# Get value from cmd-line flags.
flag_vals = [to_value_type(expand(x)) for x in flag_val_strs]
if is_list_option(kwargs):
# Note: It's important to set flag_val to None if no flags were specified, so we can
# distinguish between no flags set vs. explicit setting of the value to [].
flag_val = ListValueComponent.merge(flag_vals) if flag_vals else None
elif len(flag_vals) > 1:
raise ParseError('Multiple cmd line flags specified for option {} in {}'.format(
dest, self._scope_str()))
elif len(flag_vals) == 1:
flag_val = flag_vals[0]
else:
flag_val = None
# Rank all available values.
# Note that some of these values may already be of the value type, but type conversion
# is idempotent, so this is OK.
values_to_rank = [to_value_type(x) for x in
[flag_val, env_val_str, config_val_str, kwargs.get('default'), None]]
# Note that ranked_vals will always have at least one element, and all elements will be
# instances of RankedValue (so none will be None, although they may wrap a None value).
ranked_vals = list(reversed(list(RankedValue.prioritized_iter(*values_to_rank))))
# Record info about the derivation of each of the values.
for ranked_val in ranked_vals:
if ranked_val.rank == RankedValue.CONFIG:
details = config_details
elif ranked_val.rank == RankedValue.ENVIRONMENT:
details = env_details
else:
details = None
self._option_tracker.record_option(scope=self._scope,
option=dest,
value=ranked_val.value,
rank=ranked_val.rank,
#.........这里部分代码省略.........
示例10: _compute_default
def _compute_default(self, kwargs):
"""Compute the default value to use for an option's registration.
The source of the default value is chosen according to the ranking in RankedValue.
Note: Only call if kwargs has a 'dest' key set.
"""
dest = kwargs['dest']
is_fromfile = kwargs.get('fromfile', False)
action = kwargs.get('action')
if is_fromfile and action and action != 'append':
raise ParseError('Cannot fromfile {} with an action ({}) in scope {}'
.format(dest, action, self._scope))
config_section = 'DEFAULT' if self._scope == GLOBAL_SCOPE else self._scope
udest = dest.upper()
if self._scope == GLOBAL_SCOPE:
# For convenience, we allow three forms of env var for global scope options.
# The fully-specified env var is PANTS_DEFAULT_FOO, which is uniform with PANTS_<SCOPE>_FOO
# for all the other scopes. However we also allow simply PANTS_FOO. And if the option name
# itself starts with 'pants-' then we also allow simply FOO. E.g., PANTS_WORKDIR instead of
# PANTS_PANTS_WORKDIR or PANTS_DEFAULT_PANTS_WORKDIR. We take the first specified value we
# find, in this order: PANTS_DEFAULT_FOO, PANTS_FOO, FOO.
env_vars = ['PANTS_DEFAULT_{0}'.format(udest), 'PANTS_{0}'.format(udest)]
if udest.startswith('PANTS_'):
env_vars.append(udest)
else:
sanitized_env_var_scope = self._ENV_SANITIZER_RE.sub('_', config_section.upper())
env_vars = ['PANTS_{0}_{1}'.format(sanitized_env_var_scope, udest)]
value_type = self.str_to_bool if is_boolean_flag(kwargs) else kwargs.get('type', str)
env_val_str = None
if self._env:
for env_var in env_vars:
if env_var in self._env:
env_val_str = self._env.get(env_var)
break
config_val_str = self._config.get(config_section, dest, default=None)
config_source_file = self._config.get_source_for_option(config_section, dest)
if config_source_file is not None:
config_source_file = os.path.relpath(config_source_file)
def expand(val_str):
if is_fromfile and val_str and val_str.startswith('@') and not val_str.startswith('@@'):
fromfile = val_str[1:]
try:
with open(fromfile) as fp:
return fp.read().strip()
except IOError as e:
raise self.FromfileError('Failed to read {} from file {}: {}'.format(dest, fromfile, e))
else:
# Support a literal @ for fromfile values via @@.
return val_str[1:] if is_fromfile and val_str.startswith('@@') else val_str
def parse_typed_list(val_str):
return None if val_str is None else [value_type(x) for x in list_option(expand(val_str))]
def parse_typed_item(val_str):
return None if val_str is None else value_type(expand(val_str))
# Handle the forthcoming conversions argparse will need to do by placing our parse hook - we
# handle the conversions for env and config ourselves below. Unlike the env and config
# handling, `action='append'` does not need to be handled specially since appended flag values
# come as single items' thus only `parse_typed_item` is ever needed for the flag value type
# conversions.
if is_fromfile:
kwargs['type'] = parse_typed_item
default, parse = ([], parse_typed_list) if action == 'append' else (None, parse_typed_item)
config_val = parse(config_val_str)
env_val = parse(env_val_str)
hardcoded_val = kwargs.get('default')
config_details = 'in {}'.format(config_source_file) if config_source_file else None
choices = list(RankedValue.prioritized_iter(None, env_val, config_val, hardcoded_val, default))
for choice in reversed(choices):
details = config_details if choice.rank == RankedValue.CONFIG else None
self._option_tracker.record_option(scope=self._scope, option=dest, value=choice.value,
rank=choice.rank, details=details)
return choices[0]
示例11: _compute_value
def _compute_value(self, dest, kwargs, flag_val_strs):
"""Compute the value to use for an option.
The source of the default value is chosen according to the ranking in RankedValue.
"""
# Helper function to convert a string to a value of the option's type.
def to_value_type(val_str):
if val_str is None:
return None
elif kwargs.get('type') == bool:
return self._ensure_bool(val_str)
else:
type_arg = kwargs.get('type', str)
try:
return self._wrap_type(type_arg)(val_str)
except TypeError as e:
raise ParseError(
"Error applying type '{}' to option value '{}', for option '--{}' in {}: {}"
.format(type_arg.__name__, val_str, dest, self._scope_str(), e))
# Helper function to expand a fromfile=True value string, if needed.
# May return a string or a dict/list decoded from a json/yaml file.
def expand(val_or_str):
if (kwargs.get('fromfile', True) and val_or_str and
isinstance(val_or_str, str) and val_or_str.startswith('@')):
if val_or_str.startswith('@@'): # Support a literal @ for fromfile values via @@.
return val_or_str[1:]
else:
fromfile = val_or_str[1:]
try:
with open(fromfile, 'r') as fp:
s = fp.read().strip()
if fromfile.endswith('.json'):
return json.loads(s)
elif fromfile.endswith('.yml') or fromfile.endswith('.yaml'):
return yaml.safe_load(s)
else:
return s
except (IOError, ValueError, yaml.YAMLError) as e:
raise self.FromfileError('Failed to read {} in {} from file {}: {}'.format(
dest, self._scope_str(), fromfile, e))
else:
return val_or_str
# Get value from config files, and capture details about its derivation.
config_details = None
config_section = GLOBAL_SCOPE_CONFIG_SECTION if self._scope == GLOBAL_SCOPE else self._scope
config_default_val_or_str = expand(self._config.get(Config.DEFAULT_SECTION, dest, default=None))
config_val_or_str = expand(self._config.get(config_section, dest, default=None))
config_source_file = (self._config.get_source_for_option(config_section, dest) or
self._config.get_source_for_option(Config.DEFAULT_SECTION, dest))
if config_source_file is not None:
config_source_file = os.path.relpath(config_source_file)
config_details = 'in {}'.format(config_source_file)
# Get value from environment, and capture details about its derivation.
udest = dest.upper()
if self._scope == GLOBAL_SCOPE:
# For convenience, we allow three forms of env var for global scope options.
# The fully-specified env var is PANTS_GLOBAL_FOO, which is uniform with PANTS_<SCOPE>_FOO
# for all the other scopes. However we also allow simply PANTS_FOO. And if the option name
# itself starts with 'pants-' then we also allow simply FOO. E.g., PANTS_WORKDIR instead of
# PANTS_PANTS_WORKDIR or PANTS_GLOBAL_PANTS_WORKDIR. We take the first specified value we
# find, in this order: PANTS_GLOBAL_FOO, PANTS_FOO, FOO.
env_vars = ['PANTS_GLOBAL_{0}'.format(udest), 'PANTS_{0}'.format(udest)]
if udest.startswith('PANTS_'):
env_vars.append(udest)
else:
sanitized_env_var_scope = self._ENV_SANITIZER_RE.sub('_', self._scope.upper())
env_vars = ['PANTS_{0}_{1}'.format(sanitized_env_var_scope, udest)]
env_val_or_str = None
env_details = None
if self._env:
for env_var in env_vars:
if env_var in self._env:
env_val_or_str = expand(self._env.get(env_var))
env_details = 'from env var {}'.format(env_var)
break
# Get value from cmd-line flags.
flag_vals = [to_value_type(expand(x)) for x in flag_val_strs]
if is_list_option(kwargs):
# Note: It's important to set flag_val to None if no flags were specified, so we can
# distinguish between no flags set vs. explicit setting of the value to [].
flag_val = ListValueComponent.merge(flag_vals) if flag_vals else None
elif is_dict_option(kwargs):
# Note: It's important to set flag_val to None if no flags were specified, so we can
# distinguish between no flags set vs. explicit setting of the value to {}.
flag_val = DictValueComponent.merge(flag_vals) if flag_vals else None
elif len(flag_vals) > 1:
raise ParseError('Multiple cmd line flags specified for option {} in {}'.format(
dest, self._scope_str()))
elif len(flag_vals) == 1:
flag_val = flag_vals[0]
else:
flag_val = None
# Rank all available values.
# Note that some of these values may already be of the value type, but type conversion
#.........这里部分代码省略.........
示例12: _compute_default
def _compute_default(self, dest, kwargs):
"""Compute the default value to use for an option's registration.
The source of the default value is chosen according to the ranking in RankedValue.
"""
is_fromfile = kwargs.get('fromfile', False)
action = kwargs.get('action')
if is_fromfile and action:
raise ParseError('Cannot fromfile {} with an action ({}) in scope {}'
.format(dest, action, self._scope))
config_section = 'DEFAULT' if self._scope == GLOBAL_SCOPE else self._scope
udest = dest.upper()
if self._scope == GLOBAL_SCOPE:
# For convenience, we allow three forms of env var for global scope options.
# The fully-specified env var is PANTS_DEFAULT_FOO, which is uniform with PANTS_<SCOPE>_FOO
# for all the other scopes. However we also allow simply PANTS_FOO. And if the option name
# itself starts with 'pants-' then we also allow simply FOO. E.g., PANTS_WORKDIR instead of
# PANTS_PANTS_WORKDIR or PANTS_DEFAULT_PANTS_WORKDIR. We take the first specified value we
# find, in this order: PANTS_DEFAULT_FOO, PANTS_FOO, FOO.
env_vars = ['PANTS_DEFAULT_{0}'.format(udest), 'PANTS_{0}'.format(udest)]
if udest.startswith('PANTS_'):
env_vars.append(udest)
else:
sanitized_env_var_scope = self._ENV_SANITIZER_RE.sub('_', config_section.upper())
env_vars = ['PANTS_{0}_{1}'.format(sanitized_env_var_scope, udest)]
value_type = self.str_to_bool if is_boolean_flag(kwargs) else kwargs.get('type', str)
env_val_str = None
if self._env:
for env_var in env_vars:
if env_var in self._env:
env_val_str = self._env.get(env_var)
break
def expand(val_str):
if is_fromfile and val_str and val_str.startswith('@') and not val_str.startswith('@@'):
fromfile = val_str[1:]
try:
with open(fromfile) as fp:
return fp.read().strip()
except IOError as e:
raise self.FromfileError('Failed to read {} from file {}: {}'.format(dest, fromfile, e))
else:
# Support a literal @ for fromfile values via @@.
return val_str[1:] if is_fromfile and val_str.startswith('@@') else val_str
if is_fromfile:
kwargs['type'] = lambda flag_val_str: value_type(expand(flag_val_str)) # Expand flag values.
env_val = None if env_val_str is None else value_type(expand(env_val_str)) # Expand env values.
config_val = None
if action == 'append':
config_val_strs = self._config.getlist(config_section, dest) if self._config else None
if config_val_strs is not None:
config_val = [value_type(config_val_str) for config_val_str in config_val_strs]
default = []
else:
config_val_str = (self._config.get(config_section, dest, default=None)
if self._config else None)
if config_val_str is not None:
config_val = value_type(expand(config_val_str)) # Expand config values.
default = None
hardcoded_val = kwargs.get('default') # We don't expand hard coded defaults.
return RankedValue.choose(None, env_val, config_val, hardcoded_val, default)