本文整理汇总了Python中salt.utils.odict.OrderedDict类的典型用法代码示例。如果您正苦于以下问题:Python OrderedDict类的具体用法?Python OrderedDict怎么用?Python OrderedDict使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OrderedDict类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: merge_tops
def merge_tops(self, tops):
'''
Cleanly merge the top files
'''
top = collections.defaultdict(dict)
orders = collections.defaultdict(dict)
for ctops in tops.values():
for ctop in ctops:
for saltenv, targets in ctop.items():
if saltenv == 'include':
continue
for tgt in targets:
matches = []
states = OrderedDict()
orders[saltenv][tgt] = 0
for comp in ctop[saltenv][tgt]:
if isinstance(comp, dict):
if 'match' in comp:
matches.append(comp)
if 'order' in comp:
order = comp['order']
if not isinstance(order, int):
try:
order = int(order)
except ValueError:
order = 0
orders[saltenv][tgt] = order
if isinstance(comp, string_types):
states[comp] = True
top[saltenv][tgt] = matches
top[saltenv][tgt].extend(list(states.keys()))
return self.sort_top_targets(top, orders)
示例2: _prompt_choice
def _prompt_choice(var_name, options):
'''
Prompt the user to choose between a list of options, index each one by adding an enumerator
based on https://github.com/audreyr/cookiecutter/blob/master/cookiecutter/prompt.py#L51
:param var_name: The question to ask the user
:type var_name: ``str``
:param options: A list of options
:type options: ``list`` of ``tupple``
:rtype: ``tuple``
:returns: The selected user
'''
choice_map = OrderedDict(
(u'{0}'.format(i), value) for i, value in enumerate(options, 1) if value[0] != 'test'
)
choices = choice_map.keys()
default = u'1'
choice_lines = [u'{0} - {1} - {2}'.format(c[0], c[1][0], c[1][1]) for c in choice_map.items()]
prompt = u'\n'.join((
u'Select {0}:'.format(var_name),
u'\n'.join(choice_lines),
u'Choose from {0}'.format(u', '.join(choices))
))
user_choice = click.prompt(
prompt, type=click.Choice(choices), default=default
)
return choice_map[user_choice]
示例3: list_vms
def list_vms(search=None, sort=None, order='uuid,type,ram,state,alias', keyed=True):
'''
Return a list of VMs
search : string
vmadm filter property
sort : string
vmadm sort (-s) property
order : string
vmadm order (-o) property -- Default: uuid,type,ram,state,alias
keyed : boolean
specified if the output should be an array (False) or dict (True)
For a dict the key is the first item from the order parameter.
Note: If key is not unique last vm wins.
CLI Example:
.. code-block:: bash
salt '*' vmadm.list
salt '*' vmadm.list order=alias,ram,cpu_cap sort=-ram,-cpu_cap
salt '*' vmadm.list search='type=KVM'
'''
ret = {}
vmadm = _check_vmadm()
# vmadm list [-p] [-H] [-o field,...] [-s field,...] [field=value ...]
cmd = '{vmadm} list -p -H {order} {sort} {search}'.format(
vmadm=vmadm,
order='-o {0}'.format(order) if order else '',
sort='-s {0}'.format(sort) if sort else '',
search=search if search else ''
)
res = __salt__['cmd.run_all'](cmd)
retcode = res['retcode']
result = OrderedDict() if keyed else []
if retcode != 0:
ret['Error'] = res['stderr'] if 'stderr' in res else _exit_status(retcode)
return ret
fields = order.split(',')
for vm in res['stdout'].splitlines():
vm_data = OrderedDict()
vm = vm.split(':')
if keyed:
for field in fields:
if fields.index(field) == 0:
continue
vm_data[field.strip()] = vm[fields.index(field)].strip()
result[vm[0]] = vm_data
else:
if len(vm) > 1:
for field in fields:
vm_data[field.strip()] = vm[fields.index(field)].strip()
else:
vm_data = vm[0]
result.append(vm_data)
return result
示例4: get_vms_per_type
def get_vms_per_type(target):
'''Return all vms indexed by virt_type for a special target'''
all_targets = OrderedDict()
for virt_type in VIRT_TYPES:
per_type = all_targets.setdefault(virt_type, set())
all_infos = get_targets_and_vms_for_virt_type(virt_type)
for vmname in all_infos.get(target, []):
per_type.add(vmname)
for i in [a for a in all_targets]:
all_targets[i] = [a for a in all_targets[i]]
return all_targets
示例5: gen_ini
def gen_ini(self):
yield '\n[{0}]\n'.format(self.name)
sections_dict = OrderedDict()
for name, value in self.iteritems():
if com_regx.match(name):
yield '{0}\n'.format(value)
elif isinstance(value, _Section):
sections_dict.update({name: value})
else:
yield '{0} {1} {2}\n'.format(name, self.sep, value)
for name, value in sections_dict.iteritems():
for line in value.gen_ini():
yield line
示例6: test_compare_sls_vs_yaml_with_jinja
def test_compare_sls_vs_yaml_with_jinja(self):
tpl = '{{ data }}'
env = jinja2.Environment()
src = '{foo: 1, bar: 2, baz: {qux: true}}'
sls_src = env.from_string(tpl).render(data=yamlex.deserialize(src))
yml_src = env.from_string(tpl).render(data=yaml.deserialize(src))
sls_data = yamlex.deserialize(sls_src)
yml_data = yaml.deserialize(yml_src)
# ensure that sls & yaml have the same base
assert isinstance(sls_data, dict)
assert isinstance(yml_data, dict)
# The below has been commented out because something the loader test
# is modifying the yaml renderer to render things to unicode. Without
# running the loader test, the below passes. Even reloading the module
# from disk does not reset its internal state (per the Python docs).
##
#assert sls_data == yml_data
# ensure that sls is ordered, while yaml not
assert isinstance(sls_data, OrderedDict)
assert not isinstance(yml_data, OrderedDict)
# prove that yaml does not handle well with OrderedDict
# while sls is jinja friendly.
obj = OrderedDict([
('foo', 1),
('bar', 2),
('baz', {'qux': True})
])
sls_obj = yamlex.deserialize(yamlex.serialize(obj))
try:
yml_obj = yaml.deserialize(yaml.serialize(obj))
except SerializationError:
# BLAAM! yaml was unable to serialize OrderedDict,
# but it's not the purpose of the current test.
yml_obj = obj.copy()
sls_src = env.from_string(tpl).render(data=sls_obj)
yml_src = env.from_string(tpl).render(data=yml_obj)
final_obj = yaml.deserialize(sls_src)
assert obj == final_obj
# BLAAM! yml_src is not valid !
final_obj = OrderedDict(yaml.deserialize(yml_src))
assert obj != final_obj
示例7: gen_ini
def gen_ini(self):
yield "{0}[{1}]{0}".format(os.linesep, self.name)
sections_dict = OrderedDict()
for name, value in six.iteritems(self):
if com_regx.match(name):
yield "{0}{1}".format(value, os.linesep)
elif isinstance(value, _Section):
sections_dict.update({name: value})
else:
yield "{0}{1}{2}{3}".format(
name, (" {0} ".format(self.sep) if self.sep != " " else self.sep), value, os.linesep
)
for name, value in six.iteritems(sections_dict):
for line in value.gen_ini():
yield line
示例8: get_vms
def get_vms():
'''Return all vms indexed by targets'''
data = OrderedDict()
for virt_type in VIRT_TYPES:
all_infos = get_targets_and_vms_for_virt_type(virt_type)
for t in all_infos:
target = data.setdefault(t, {})
vms = {}
vts = set()
for vmname in all_infos[t]:
vms.setdefault(vmname, virt_type)
vts.add(virt_type)
target['virt_types'] = [a for a in vts]
target['vms'] = vms
return data
示例9: test_compare_sls_vs_yaml_with_jinja
def test_compare_sls_vs_yaml_with_jinja(self):
tpl = '{{ data }}'
env = jinja2.Environment()
src = '{foo: 1, bar: 2, baz: {qux: true}}'
sls_src = env.from_string(tpl).render(data=sls.deserialize(src))
yml_src = env.from_string(tpl).render(data=yaml.deserialize(src))
sls_data = sls.deserialize(sls_src)
yml_data = yaml.deserialize(yml_src)
# ensure that sls & yaml have the same base
assert isinstance(sls_data, dict)
assert isinstance(yml_data, dict)
assert sls_data == yml_data
# ensure that sls is ordered, while yaml not
assert isinstance(sls_data, OrderedDict)
assert not isinstance(yml_data, OrderedDict)
# prove that yaml does not handle well with OrderedDict
# while sls is jinja friendly.
obj = OrderedDict([
('foo', 1),
('bar', 2),
('baz', {'qux': True})
])
sls_obj = sls.deserialize(sls.serialize(obj))
try:
yml_obj = yaml.deserialize(yaml.serialize(obj))
except SerializationError:
# BLAAM! yaml was unable to serialize OrderedDict,
# but it's not the purpose of the current test.
yml_obj = obj.copy()
sls_src = env.from_string(tpl).render(data=sls_obj)
yml_src = env.from_string(tpl).render(data=yml_obj)
final_obj = yaml.deserialize(sls_src)
assert obj == final_obj
# BLAAM! yml_src is not valid !
final_obj = yaml.deserialize(yml_src)
assert obj != final_obj
示例10: element
def element(self, root=None, abspath=None, **kwargs):
'''
kwargs contain extra information for custom methods.
This method must return a valid empty object if no vars are passed
to allow introspection to create patterns.
:param root:
:param abspath:
'''
if root is None and abspath is None:
root = os.path.abspath('.')
abspath = os.path.abspath('.')
relpath = os.path.relpath(abspath, root)
try:
element = self._elements.get(relpath, OrderedDict())
except AttributeError:
element = OrderedDict()
if not element:
for field in PATHINFO_FIELDS:
element.setdefault(field, '')
element['saltenv'] = kwargs.get('saltenv', 'base')
element['relpath'] = relpath
element['abspath'] = abspath
element['is_pillar'] = kwargs.get('is_pillar', False)
cachedir = kwargs.get('cachedir', '')
if cachedir and os.path.commonprefix([abspath, cachedir]) == cachedir:
element['cache_root'] = root
else:
element['file_root'] = root
element_hook = kwargs.get('_element_hook', None)
if element_hook:
element = element_hook(self, element, **kwargs)
return element
示例11: merge_tops
def merge_tops(self, tops):
'''
Cleanly merge the top files
'''
top = collections.defaultdict(OrderedDict)
orders = collections.defaultdict(OrderedDict)
for ctops in six.itervalues(tops):
for ctop in ctops:
for saltenv, targets in six.iteritems(ctop):
if saltenv == 'include':
continue
for tgt in targets:
matches = []
states = OrderedDict()
orders[saltenv][tgt] = 0
ignore_missing = False
for comp in ctop[saltenv][tgt]:
if isinstance(comp, dict):
if 'match' in comp:
matches.append(comp)
if 'order' in comp:
order = comp['order']
if not isinstance(order, int):
try:
order = int(order)
except ValueError:
order = 0
orders[saltenv][tgt] = order
if comp.get('ignore_missing', False):
ignore_missing = True
if isinstance(comp, six.string_types):
states[comp] = True
if ignore_missing:
if saltenv not in self.ignored_pillars:
self.ignored_pillars[saltenv] = []
self.ignored_pillars[saltenv].extend(states.keys())
top[saltenv][tgt] = matches
top[saltenv][tgt].extend(states)
return self.sort_top_targets(top, orders)
示例12: _uncomment_if_commented
def _uncomment_if_commented(self, opt_key):
# should be called only if opt_key is not already present
# will uncomment the key if commented and create a place holder
# for the key where the correct value can be update later
# used to preserve the ordering of comments and commented options
# and to make sure options without sectons go above any section
options_backup = OrderedDict()
comment_index = None
for key, value in self.iteritems():
if comment_index is not None:
options_backup.update({key: value})
continue
if '#comment' not in key:
continue
opt_match = opt_regx.match(value.lstrip('#'))
if opt_match and opt_match.group(2) == opt_key:
comment_index = key
for key in options_backup:
self.pop(key)
self.pop(comment_index, None)
super(_Section, self).update({opt_key: None})
for key, value in options_backup.iteritems():
super(_Section, self).update({key: value})
示例13: compile
def compile(labels, **patterns): # pylint: disable=W0622
'''
Compile patterns.
Args:
labels:
'''
pattern = patterns.pop('_pattern', None)
if pattern:
return pattern
regex = patterns.pop('_regex', False)
escape = patterns.pop('_escape', [])
if not patterns or not labels:
return None
for pattern in list(patterns.keys()):
if pattern not in labels:
patterns.pop(pattern)
default_pattern = get_default_pattern(regex)
escape = escape if escape else []
_escape_text = functools.partial(escape_text, regex=regex)
# Set default values and join patterns for each field
pattern = OrderedDict.fromkeys(labels, None)
for label in labels:
if label in patterns and patterns[label]:
field = patterns[label]
if isinstance(field, re._pattern_type): # pylint: disable=W0212
field = [field.pattern]
if isinstance(field, six.string_types):
field = [field]
if label in escape or not regex:
field = [_escape_text(text) for text in field]
else:
field = default_pattern
pattern[label] = r'(?:{0})'.format(r'|'.join(field))
try:
return re.compile(
r'\n'.join(six.itervalues(pattern)), re.MULTILINE | re.DOTALL
)
except NameError:
raise
示例14: __init__
def __init__(self, match_each=True, **patterns):
'''
match_each:
If True, each file path is matched which prevents uses less memory
but sacrifices performance a little bit. If False, the complete
list is matched after all the file information has been added to
pathinfo.
patterns:
Contains the patterns to match.
Example:
{ 'saltenv': 'base', 'relpath': ['*.sls'] }
'''
super(PathInfoDict, self).__init__(
fields=PATHINFO_FIELDS,
match_each=match_each,
**patterns
)
self._elements = OrderedDict()
示例15: StateRegistry
class StateRegistry(object):
"""
The StateRegistry holds all of the states that have been created.
"""
def __init__(self):
self.empty()
def empty(self):
self.states = OrderedDict()
self.requisites = []
def salt_data(self):
states = OrderedDict([
(id_, state())
for id_, state in self.states.iteritems()
])
self.empty()
return states
def add(self, id_, state):
if id_ in self.states:
raise DuplicateState("A state with id '%s' already exists" % id_)
# if we have requisites in our stack then add them to the state
if len(self.requisites) > 0:
for req in self.requisites:
if req.requisite not in state.kwargs:
state.kwargs[req.requisite] = []
state.kwargs[req.requisite].append(req())
self.states[id_] = state
def push_requisite(self, requisite):
self.requisites.append(requisite)
def pop_requisite(self):
del self.requisites[-1]