def _make_path_loader_and_dumper(): global MyLoader, MyDumper class MyLoader(yaml.Loader): pass class MyDumper(yaml.Dumper): pass yaml.add_path_resolver('!root', [], Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver('!root/scalar', [], str, Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver('!root/key11/key12/*', ['key11', 'key12'], Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver('!root/key21/1/*', ['key21', 1], Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver('!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict, Loader=MyLoader, Dumper=MyDumper) return MyLoader, MyDumper
def __init__(self, config_file): yaml.add_path_resolver('!Scope', ['scope'], dict) yaml.add_path_resolver('!Context', ['contexts', None], dict) self.data = yaml.load(config_file, Loader=yaml.Loader) self.scope = self.data["scope"] self.contexts = [] for ct_bldr in self.data.get('contexts', []): self.contexts.append(ct_bldr.context(scope=self.scope))
def load_conf() -> Application: with open(CONFIG_PATH) as file: yaml.add_path_resolver(tag='!application', path=['application'], kind=dict) conf = yaml.load(file, Loader=yaml.FullLoader) print(conf) cfg: Application = parse(conf) return cfg
def inject(vclass): def from_yaml(loader, node): #print "from_yaml" v = vclass(node) return v.visit(node) """ visit every thing! """ yaml.add_constructor(u"Root", from_yaml) yaml.add_path_resolver(u"Root", [], dict) yaml.add_path_resolver(u"Root", [], list)
def _make_path_loader_and_dumper(): global MyLoader, MyDumper class MyLoader(yaml.Loader): pass class MyDumper(yaml.Dumper): pass yaml.add_path_resolver(u"!root", [], Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver(u"!root/scalar", [], str, Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver(u"!root/key11/key12/*", ["key11", "key12"], Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver(u"!root/key21/1/*", ["key21", 1], Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver( u"!root/key31/*/*/key14/map", ["key31", None, None, "key14"], dict, Loader=MyLoader, Dumper=MyDumper ) return MyLoader, MyDumper
def _make_path_loader_and_dumper(): class MyLoader(yaml.Loader): pass class MyDumper(yaml.Dumper): pass yaml.add_path_resolver('!root', [], Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver('!root/scalar', [], str, Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver('!root/key11/key12/*', ['key11', 'key12'], Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver('!root/key21/1/*', ['key21', 1], Loader=MyLoader, Dumper=MyDumper) yaml.add_path_resolver('!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict, Loader=MyLoader, Dumper=MyDumper) return MyLoader, MyDumper
klasses = ', '.join(find_keyword_cls(key)) self.yaml_warning(yaml.constructor.ConstructorError( None, None, "Found reserved Zenoss keyword '{}' from {}".format(key, klasses), start_mark)) return False def find_name_from_node(self): '''determine zenpack name''' for k in self.recursive_objects.keys(): for v in k.value: if isinstance(v, tuple) and len(v) == 2: key, value = str(v[0].value), str(v[1].value) if key == 'name': return value return None class WarningLoader(ZenPackSpecLoader): """ These subclasses exist so that each copy of zenpacklib installed on a zenoss system provide their own loader (for add_constructor and yaml.load) and its own dumper (for add_representer) so that the proper methods will be used for this specific zenpacklib. """ warnings = True yaml_errored = False yaml.add_path_resolver(u'!ZenPackSpec', [], Loader=ZenPackSpecLoader)
name = ''.join([part.capitalize() for part in key.split('-')]) cls = getattr(yaml, '%s%s' % (name, Class)) value = domain[key] if not value: continue start = value.get('start') end = value.get('end') if start: self.substitutions[cls, -1] = start if end: self.substitutions[cls, +1] = end def __setstate__(self, state): self.__init__(**state) yaml.add_path_resolver(u'tag:yaml.org,2002:python/object:__main__.Style', [None], dict) yaml.add_path_resolver(u'tag:yaml.org,2002:pairs', [None, u'replaces'], list) class YAMLHighlight: def __init__(self, options): config = yaml.load(file(options.config, 'rb').read()) self.style = config[options.style] if options.input: self.input = file(options.input, 'rb') else: self.input = sys.stdin if options.output: self.output = file(options.output, 'wb') else:
if key not in valid_headers: raise BugValidationError('Line %s is not a known line.' % key) if val == '': self.__dict__[key] = None else: self.__dict__[key] = val else: if not line.startswith('#'): self.description += line + '\n' # Clean up the description of trailing and leading whitespace. self.description = self.description.strip(u' \t\r\f\v\n') if self.description == '': raise WorklogValidationError('Missing worklog body.') if self.poster: try: self.poster = emailaddress.EmailAddress(self.poster) except emailaddress.EmailValidationError, e: raise WorklogValidationError('Poster is not a valid email address.') else: raise WorklogValidationError('Missing poster email address.') # Make sure that when we emit stuff that the worklog shows up properly without # extra annoying tags. yaml.add_path_resolver(u'!worklog', (u'worklog', [list, False]))
def __init__(self, extra): if extra: some_attr = extra @classmethod def from_yaml(cls, loader, node): data = loader.construct_scalar(node) return cls(data) class SecondResolver(FirstResolver): yaml_tag = "!second" some_attr = "barfoo" #teach PyYAML that any untagged scalar with the path [a] has an implict tag !first yaml.add_path_resolver("!first", ["a"], str) #teach PyYAML that any untagged scalar with the path [a,b,c] has an implicit tag !second. yaml.add_path_resolver("!second", ["a", "b", "c"], str) #teach PyYAML that any untagged plain scalar that looks like XdY has the implicit tag !dice. for cls in [Dice, Foo]: yaml.add_implicit_resolver(cls.yaml_tag, cls.yaml_pattern) def load(foo): return yaml.load(foo) def dump(foo): return yaml.dump(foo, default_flow_style=False)
# This works around https://bitbucket.org/xi/pyyaml/issue/12 def myresolve(self, kind, value, implicit): if self.yaml_path_resolvers: exact_paths = self.resolver_exact_paths[-1] if kind in exact_paths: return exact_paths[kind] if None in exact_paths: return exact_paths[None] if kind is yaml.ScalarNode and implicit[0]: if value == u'': resolvers = self.yaml_implicit_resolvers.get(u'', []) else: resolvers = self.yaml_implicit_resolvers.get(value[0], []) resolvers += self.yaml_implicit_resolvers.get(None, []) for tag, regexp in resolvers: if regexp.match(value): return tag implicit = implicit[1] if kind is yaml.ScalarNode: return self.DEFAULT_SCALAR_TAG elif kind is yaml.SequenceNode: return self.DEFAULT_SEQUENCE_TAG elif kind is yaml.MappingNode: return self.DEFAULT_MAPPING_TAG yaml.resolver.BaseResolver.resolve = myresolve yaml.add_path_resolver(u'tag:yaml.org,2002:str', ['Version'], str) yaml.add_path_resolver(u'tag:yaml.org,2002:str', ['Release'], str)
class FirstResolver(yaml.YAMLObject): yaml_tag="!first" some_attr="foobars" def __init__(self, extra): if extra: some_attr=extra @classmethod def from_yaml(cls, loader, node): data = loader.construct_scalar(node) return cls(data) class SecondResolver(FirstResolver): yaml_tag="!second" some_attr="barfoo" #teach PyYAML that any untagged scalar with the path [a] has an implict tag !first yaml.add_path_resolver("!first", ["a"], str) #teach PyYAML that any untagged scalar with the path [a,b,c] has an implicit tag !second. yaml.add_path_resolver("!second", ["a", "b", "c"], str) #teach PyYAML that any untagged plain scalar that looks like XdY has the implicit tag !dice. for cls in [Dice, Foo]: yaml.add_implicit_resolver(cls.yaml_tag, cls.yaml_pattern) def load(foo): return yaml.load(foo) def dump(foo): return yaml.dump(foo, default_flow_style=False) print "loading '2d6' turns into: ", load('2d6')
cls = getattr(yaml, '%s%s' % (name, Class)) value = domain[key] if not value: continue start = value.get('start') end = value.get('end') if start: self.substitutions[cls, -1] = start if end: self.substitutions[cls, +1] = end def __setstate__(self, state): self.__init__(**state) yaml.add_path_resolver(u'tag:yaml.org,2002:python/object:__main__.Style', [None], dict) yaml.add_path_resolver(u'tag:yaml.org,2002:pairs', [None, u'replaces'], list) class YAMLHighlight: def __init__(self, options): config = yaml.full_load(file(options.config, 'rb').read()) self.style = config[options.style] if options.input: self.input = file(options.input, 'rb') else: self.input = sys.stdin if options.output: self.output = file(options.output, 'wb') else: self.output = sys.stdout
def add_path_resolver(tag, keys): yaml.add_path_resolver(tag, keys, dict, Loader=PrettySafeLoader)
self.name = name self.widgets = widgets self.controls = controls self.template = template def render(self) -> str: html = [] for w in self.widgets: dev = get_device(w['device']) config = w.get('config', {}) html.append(dev.dev.render_widget(**config)) return html # Set up YAML object instantiation yaml.add_path_resolver('!device', ['Device'], dict) yaml.add_path_resolver('!multidevice', ['MultiDevice'], dict) yaml.add_path_resolver('!action', ['ActionGroup'], dict) yaml.add_path_resolver('!interface', ['Interface'], dict) yaml.add_path_resolver('!display', ['Display'], dict) def add_scheduled_job(job: Dict): if job.get('action'): scheduler.add_job(get_action(job.pop('action')).run, trigger=job.pop('trigger', 'cron'), kwargs=dict(jitter=True), **job) elif job.get('device'): device = get_device(job.pop('device')) method = method_from_name(device.dev, job.pop('method'))
import humps import yaml @dataclass(frozen=True, eq=True) class App: """Class for recording app names with write keys.""" write_key: str name: str def schema(self): return humps.decamelize(self.name) yaml.add_path_resolver("!app", ["App"], dict) @dataclass(frozen=True, eq=True) class AppConf: """Top level configuration class""" apps: List[App] warehouses: List[dict] skip_fields: List[str] extra_timestamps: dict def from_yaml(file_path: str): apps = set() skip_fields = []