def main(): """entry point""" args = docopt(USAGE) path = os.path.expanduser(args['<config.yaml>']) if args['--true']: value = True if args['--false']: value = False ignores = args['--ignore'] with open(path, 'r') as file: content = yaml(typ='safe').load(file) for k, val in content[KEY].items(): if k in ignores: continue val[ENTRY] = value output = io.StringIO() data = yaml() data.default_flow_style = False data.indent = 2 data.typ = 'rt' data.dump(content, output) print(output)
def _yaml_load(self, path): """load from yaml""" with open(path, 'r') as f: y = yaml() y.typ = 'rt' content = y.load(f) return content
def yaml_dump(content, path): with open(path, 'w') as f: y = yaml() y.default_flow_style = False y.indent = 2 y.typ = 'safe' y.dump(content, f)
def _yaml_dump(self, content, where): """dump to yaml""" y = yaml() y.default_flow_style = False y.indent = 2 y.typ = 'rt' y.dump(content, where)
def test_load_configuration(logging_mixin, basic_config): """ Test that loading yaml goes according to expectations. This may be somewhat trivial, but it is still important to check in case ruamel.yaml changes APIs or defaults. NOTE: We can only compare at the YAML level because the dumped string does not preserve anchors that are not actually referenced, as well as some trivial variation in quote types and other similarly trivial formatting issues. """ (basic_config, yaml_string) = basic_config classes_to_register = [dataclasses.make_dataclass("TestClass", ["hello", "world"])] yaml_string += """ hello: - !TestClass hello: "str" world: "str2" """ basic_config["hello"] = [classes_to_register[0](hello = "str", world = "str2")] yml = yaml.yaml(classes_to_register = classes_to_register) import tempfile with tempfile.NamedTemporaryFile() as f: # Write and move back to the start of the file f.write(yaml_string.encode()) f.seek(0) # Then get the config from the file retrieved_config = generic_config.load_configuration(yaml = yml, filename = f.name) assert retrieved_config == basic_config
def _yaml_dump(self, content, path): """dump to yaml""" with open(self.path, 'w') as f: y = yaml() y.default_flow_style = False y.indent = 2 y.typ = 'rt' y.dump(content, f)
def main(): args = docopt(USAGE) path = os.path.expanduser(args['<config.yaml>']) if args['--true']: value = True if args['--false']: value = False ignores = args['--ignore'] with open(path, 'r') as f: content = yaml(typ='safe').load(f) for k, v in content[key].items(): if k in ignores: continue v[entry] = value output = io.StringIO() y = yaml() y.default_flow_style = False y.indent = 2 y.typ = 'rt' y.dump(content, output) print(output)
def __init__(self, yaml_file): if not os.path.exists(yaml_file): raise IOError("yaml file: {} is not existed".format(yaml_file)) super().__init__() self.d = collections.OrderedDict() with open(yaml_file) as fp: for _, v in yaml().load(fp).items(): for k1, v1 in v.items(): try: if self.get(k1): self.set_hparam(k1, v1) else: self.add_hparam(k1, v1) self.d[k1] = v1 except Exception: import traceback print(traceback.format_exc())
def main(): args = docopt(USAGE) path = os.path.expanduser(args['<config.yaml>']) if args['--true']: value = True if args['--false']: value = False ignores = args['--ignore'] with open(path, 'r') as f: content = yaml(typ='safe').load(f) for k, v in content[key].items(): if k in ignores: continue v[entry] = value ret = yaml.dump(content, default_flow_style=False, indent=2) print(ret)
def __init__(self, configfile): self.configfile = configfile with open(configfile) as f: yaml_decoder = yaml() self.config = yaml_decoder.load(f) self.cpu_version = self.config[self.CPU_VER] self.ram_addr_w = self.config[self.RAM_ADDR_W] self.ram_d_w = self.config[self.RAM_D_W] self.opcodes = self.config[self.OPD] self.as_g_tr = self.config[self.AS_G_TRANSFORMS] # self.asm = rply.LexerGenerator() # # ignore comments # self.asm.ignore(";(.+)?\n") # # ignore white spaces # self.asm.ignore("\s+") self.aliases = [op[self.ALIAS] for op in self.opcodes] self.num_args = [op[self.NUM_ARG] for op in self.opcodes] self.is_args = self.config[self.IS_ARG] # self.label_addr_map = {} self.ram = []
def __init__(self, config_file): self.config_file = config_file # try loading the configuration from the file with open(config_file) as f: yaml_decoder = yaml() self.config = yaml_decoder.load(f) self.cpu_version = self.config[self.CPU_VER] self.rom_addr_w = self.config[self.ROM_ADDR_W] self.rom_d_w = self.config[self.ROM_D_W] self.max_ts = self.config[self.MAX_TS] self.micro_code_sym_def = self.config[self.MCI] # generate a control code for each of the microcode instructions self.mci_ctrlc_map = {} for i in range(len(self.micro_code_sym_def)): self.mci_ctrlc_map[self.micro_code_sym_def[ i]] = 0x1 << i # (i % (self.rom_d_w - 1)) if self.mci_ctrlc_map[self.micro_code_sym_def[i]] > ( pow(2, self.rom_d_w) - 1): self.mci_ctrlc_map[self.micro_code_sym_def[ i]] = self.mci_ctrlc_map[self.micro_code_sym_def[i]] >> ( self.rom_d_w * self._rom_bank_num(self.micro_code_sym_def[i])) # self.mci_ctrlc_map['T_W'] = 0 # prepare the rom banks max_rom_banks = ((len(self.micro_code_sym_def) - 1) // self.rom_d_w) self.rom_banks = [[] for i in range(max_rom_banks + 1)] # load the fetch cycle self.fc = self.config[self.FCD] # load the opcodes self.opcodes = self.config[self.OPD] # load opcodes to rom banks for op in self.opcodes: for i in self.fc: self._add_mci(i) for i in op[self.OPD_DEF]: self._add_mci(i) for i in range(len(self.rom_banks)): self.rom_banks[i].append( str(self.max_ts - (len(self.fc) + len(op[self.OPD_DEF]))) + '*0' + '\n')
def yaml_load(path): with open(path, 'r') as f: content = yaml(typ='safe').load(f) return content
"""Get output from pwrstat program and send results to REST or MQTT clients.""" from threading import Thread import time import logging from subprocess import DEVNULL, Popen, PIPE from typing import Any, Dict, List, Optional from ruamel.yaml import YAML as yaml from ruamel.yaml import YAMLError import pwrstat_mqtt import pwrstat_rest from pwrstat_schemas import PWRSTAT_API_SCHEMA, MQTT_SCHEMA, REST_SCHEMA _LOGGER = logging.getLogger("PwrstatApi") YAML = yaml(typ="safe") class PwrstatApi: """Get output from pwrstat program and send results to REST or MQTT clients.""" def __init__(self) -> None: """Initilize Pwrstat class.""" _start_pwrstatd_watchdog() _process_config() def _process_config() -> None: """Process YAML config file. Starts servers if configured.""" with open("pwrstat.yaml") as file: try: yaml_config: Dict[str, Any] = YAML.load(file)
def yaml_load(f): return yaml(typ='safe').load(f)