def test_cinder_service(os_api_conn, openstack_properties): """Test to verify that cinder service is enabled Args: os_api_conn (openstack.connection.Connection): An authorized API connection to the 'default' cloud on the OpenStack infrastructure. openstack_properties (dict): OpenStack facts and variables from Ansible which can be used to manipulate OpenStack objects. """ # Getting the right cinder service to check: # Cinder API V1 was removed in Queens release (os_version_major == 17) # (https://docs.openstack.org/releasenotes/horizon/rocky.html) # # Cinder API V2 is deprecated in Rocky (os_version_major == 18) # https://developer.openstack.org/api-ref/block-storage/ # # Cinder API V3 is already in Queens and later. if openstack_properties['os_version_major'] < 17: cinder_service = 'cinder' else: cinder_service = 'cinderv3' # Getting the list of services (return list of Munch Python dictionaries) service_list = os_api_conn.list_services() filtered_services = list( filter(lambda d: d['name'].lower() == cinder_service, service_list)) assert len(filtered_services) > 0, \ "Supported cinder service '{}' not found!\n{}\n"\ .format(cinder_service, pformat(unmunchify(service_list))) assert filtered_services[0]['enabled'],\ "Supported cinder service '{}' not enabled!\n{}\n" \ .format(cinder_service, pformat(unmunchify(service_list)))
def test_unmunchify(): b = Munch(foo=Munch(lol=True), hello=42, ponies="are pretty!") assert sorted(unmunchify(b).items()) == [("foo", {"lol": True}), ("hello", 42), ("ponies", "are pretty!")] b = Munch(foo=["bar", Munch(lol=True)], hello=42, ponies=("are pretty!", Munch(lies="are trouble!"))) assert sorted(unmunchify(b).items()) == [ ("foo", ["bar", {"lol": True}]), ("hello", 42), ("ponies", ("are pretty!", {"lies": "are trouble!"})), ]
def test_unmunchify_cycle(): # munch -> munch -> munch x = Munch(id="x") y = Munch(x=x, id="y") x.y = y d = unmunchify(x) assert d["id"] == "x" assert d["y"]["id"] == "y" assert d["y"]["x"] is d # munch -> list -> munch x = Munch(id="x") y = ["y", x] x.y = y d = unmunchify(x) assert d["id"] == "x" assert d["y"][0] == "y" assert d["y"][1] is d # munch -> tuple -> munch x = Munch(id="x") y = ("y", x) x.y = y d = unmunchify(x) assert d["id"] == "x" assert d["y"][0] == "y" assert d["y"][1] is d # munch1 -> list -> munch2 -> list z = Munch(id="z") y = ["y", z] z.y = y x = Munch(id="x", y=y) d = unmunchify(x) assert d["id"] == "x" assert d["y"][0] == "y" assert d["y"][1]["id"] == "z" assert d["y"][1]["y"] is d["y"] # munch1 -> tuple -> munch2 -> tuple z = Munch(id="z") y = ("y", z) z.y = y x = Munch(id="x", y=y) d = unmunchify(x) assert d["id"] == "x" assert d["y"][0] == "y" assert d["y"][1]["id"] == "z" assert d["y"][1]["y"] is d["y"]
def test_unmunchify(): b = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!') assert sorted(unmunchify(b).items()) == [('foo', { 'lol': True }), ('hello', 42), ('ponies', 'are pretty!')] b = Munch(foo=['bar', Munch(lol=True)], hello=42, ponies=('are pretty!', Munch(lies='are trouble!'))) assert sorted(unmunchify(b).items()) == [('foo', ['bar', { 'lol': True }]), ('hello', 42), ('ponies', ('are pretty!', { 'lies': 'are trouble!' }))]
def update_export(module, export, filesystem, system): """ Create new filesystem or update existing one""" changed = False name = module.params['name'] client_list = module.params['client_list'] if export is None: if not module.check_mode: export = system.exports.create(export_path=name, filesystem=filesystem) if client_list: export.update_permissions(client_list) changed = True else: if client_list: if set(map(transform, unmunchify( export.get_permissions()))) != set( map(transform, client_list)): if not module.check_mode: export.update_permissions(client_list) changed = True module.exit_json(changed=changed)
def set(self, key, value): conf = self.app.conf data_path = os.path.join(conf.data, 'data.yml') data = self.get() or {} data[key] = value with open(data_path, 'w') as f: yaml.dump(unmunchify(data), f, default_flow_style=False)
def to_dict(self): return { 'kind': self.kind, 'sub_kind': self.sub_kind, 'body': munch.unmunchify(self.body), 'headers': dict(self.headers), }
def test_get(): from configaro import ConfigPropertyNotFoundError, get, init init('tests.config') expected = { 'name': 'locals', 'log': { 'file': 'some-file.txt', 'level': 'DEBUG' }, 'monitoring': { 'haproxy': { 'disabled': True }, 'nginx': { 'disabled': True } } } config = get() assert config.log.level == 'DEBUG' assert config == munch.munchify(expected) log = get('log') assert log.level == 'DEBUG' log = munch.unmunchify(log) assert log == expected['log'] assert get('name') == 'locals' assert get('log.level') == 'DEBUG' assert get('monitoring.haproxy.disabled') is True assert get('monitoring.nginx.disabled') is True with pytest.raises(ConfigPropertyNotFoundError): assert get('monitoring.nginx.disable') is True assert get('monitoring.nginx.disable', default=None) is None
def coerce(self, name, value): column = self.inspector.columns[name] if isinstance(column.type, JSON): # DefaultMunch objects confuse SQLAlchemy, because it uses # `hasattr` to check for magic values. return unmunchify(value) return value
def from_file(fname=DEFAULT_CONFIG_FILENAME, testing=False): if testing: return TEST try: with open(os.path.expanduser(fname)) as fd: conf = yaml.load(fd) conf = munchify(conf) return conf except IOError: print("A configuration file named '%s' is missing" % fname) s_conf = yaml.dump(unmunchify(DEFAULT), explicit_start=True, indent=True, default_flow_style=False) print(""" Creating this file %s You still have to create directories with data and put your data in! """ % s_conf) time.sleep(3) try: with open(os.path.expanduser(fname), "w") as fd: fd.write(s_conf) except IOError: print("Can create '%s'" % fname) print("Trying anyway with default configuration") return DEFAULT
def save_tickers(cfg, meta, data, interval='1d'): data_dict = munch.unmunchify(data) pathlib.Path(cfg.prepare.cache_dir).mkdir(parents=True, exist_ok=True) save_pickle(f'{cfg.prepare.cache_dir}/{meta.name}_{interval}.pkl', data_dict) save_pickle(f'{cfg.prepare.cache_dir}/{meta.name}_{interval}_meta.pkl', meta)
def edit_tender_data_for_mnn(data, mode, data_version): id = { 1: '33600000-6', 2: '33632100-0', 3: '33632100-0', 4: '33622200-8', 5: '33600000-6', 6: '33692500-2', 7: '33600000-6', 8: '33615100-5' } dict_data = unmunchify(data) dict_data['data']['items'][0]['classification']['id'] = id[data_version] if data_version is 3: dict_data['data']['items'][0].pop('additionalClassifications', None) if data_version is 4: add_INN = invalid_INN_data() dict_data['data']['items'][0]['additionalClassifications'].append( add_INN) if data_version is 5: dict_data['data']['items'][0].pop('additionalClassifications', None) if data_version is 6: dict_data['data']['items'][0]['additionalClassifications'].pop(0) if data_version is 7: dict_data['data']['items'][0]['additionalClassifications'].pop(1) if data_version is 8: dict_data['data']['items'][0]['additionalClassifications'].pop(1) return munchify(dict_data)
def edit_release(user, password, url, debug, composed_by_bodhi, openid_api, **kwargs): """Edit an existing release.""" client = bindings.BodhiClient(base_url=url, username=user, password=password, staging=kwargs['staging'], openid_api=openid_api) csrf = client.csrf() edited = kwargs.pop('name') if edited is None: click.echo("ERROR: Please specify the name of the release to edit") return res = client.send_request(f'releases/{edited}', verb='GET', auth=True) data = munch.unmunchify(res) if 'errors' in data: print_errors(data) data['edited'] = edited data['csrf_token'] = csrf data['composed_by_bodhi'] = composed_by_bodhi new_name = kwargs.pop('new_name') if new_name is not None: data['name'] = new_name for k, v in kwargs.items(): if v is not None: data[k] = v save(client, **data)
def update_client(module, export): """Update export client list""" changed = False client = module.params['client'] access_mode = module.params['access_mode'] no_root_squash = module.params['no_root_squash'] client_list = export.get_permissions() client_not_in_list = True for index, item in enumerate(client_list): if item.client == client: client_not_in_list = False if item.access != access_mode: item.access = access_mode changed = True if item.no_root_squash is not no_root_squash: item.no_root_squash = no_root_squash changed = True # If access_mode and/or no_root_squash not passed as arguments to the module, # use access_mode with RW value and set no_root_squash to False if client_not_in_list: changed = True client_list.append(Munch(client=client, access=access_mode, no_root_squash=no_root_squash)) if changed: for index, item in enumerate(client_list): client_list[index] = unmunchify(item) if not module.check_mode: export.update_permissions(client_list) module.exit_json(changed=changed)
def edit(username, password, **kwargs): client = Bodhi2Client(username=username, password=password) csrf = client.csrf() edited = kwargs.pop('name') if edited is None: print("ERROR: Please specify the name of the release to edit") return res = client.send_request('releases/%s' % edited, verb='GET', auth=True) data = munch.unmunchify(res) if 'errors' in data: print_errors(data) data['edited'] = edited data['csrf_token'] = csrf new_name = kwargs.pop('new_name') if new_name is not None: data['name'] = new_name for k, v in kwargs.items(): if v is not None: data[k] = v save(client, **data)
def configs(ctx, list_, generate_configs, kind, prefix): """Manage configuration values and files.""" if prefix is None: prefix = dt.datetime.today().isoformat() log.debug("kind = {}".format(kind)) if list_: log.info("Listing current configuration state.") conf_str = yaml.dump(unmunchify(ctx.obj.CONFIG), default_flow_style=False) echo(conf_str) exit(0) factory_resets = Path('configs/factory_resets') default_files = { k: factory_resets / '{kind}.yaml'.format(kind=k) for k in VALID_CONFIG_KINDS[1:] } default_files["all"] = factory_resets.glob('*.yaml') if generate_configs: if 'all' in kind: for p in default_files['all']: _config.replace_config(name=p.name, factory_resets=factory_resets, prefix=prefix) else: for k in kind: p = default_files[k] _config.replace_config(name=p.name, factory_resets=factory_resets, prefix=prefix)
def test_cinder_verify_attach(os_api_conn, host): """Test to verify cinder volume attached to a server. Args: os_api_conn (openstack.connection.Connection): An authorized API connection to the 'default' cloud on the OpenStack infrastructure. host (testinfra.host.Host): Testinfra host fixture. """ # get server details: list_servers = os_api_conn.list_servers() for server in list_servers: hypervisor = server['hypervisor_hostname'].split('.')[0] instance_name = server['instance_name'] if len(server['os-extended-volumes:volumes_attached']) > 0: # Verify each attached volume for vol in server['os-extended-volumes:volumes_attached']: cmd = "{} virsh dumpxml {} | grep {}".format( ssh_pre, hypervisor, instance_name, vol['id']) assert host.run(cmd), \ "On Server: '{}'\n" \ "No cinder volume attachment is found with command: '{}' " \ "executed on the hypervisor: '{}' against instance_name: " \ "'{}'\n" \ "Server's details:\n{}\n"\ .format(pformat(unmunchify(server['id'], cmd, hypervisor, instance_name, server)))
def save_lattice(self, lattice=None, filename=None, directory='.'): if filename is None: pre, ext = os.path.splitext(os.path.basename( self.settingsFilename)) dict = OrderedDict({'elements': OrderedDict()}) latticedict = dict['elements'] if lattice is None: elements = list(self.elementObjects.keys()) filename = pre + '_lattice.yaml' else: if self.latticeObjects[lattice].elements is None: return elements = list(self.latticeObjects[lattice].elements.keys()) filename = pre + '_' + lattice + '_lattice.yaml' disallowed = [ 'allowedkeywords', 'keyword_conversion_rules_elegant', 'objectdefaults', 'global_parameters' ] for e in elements: new = unmunchify(self.elementObjects[e]) if ('subelement' in new and not new['subelement']) or not 'subelement' in new: try: latticedict[e] = { k.replace('object', ''): self.convert_numpy_types(new[k]) for k in new if not k in disallowed } # latticedict[e].update({k: self.convert_numpy_types(new[k]) for k in new}) except: print('##### ERROR IN CHANGE ELEMS: ', e, new) pass print('#### Saving Lattice - ', filename) with open(directory + '/' + filename, "w") as yaml_file: yaml.dump(dict, yaml_file, default_flow_style=False)
def detect_changes(self, elementtype=None, elements=None, function=None): start = time.time() changedict = {} if elementtype is not None: changeelements = self.getElementType(elementtype, 'objectname') elif elements is not None: changeelements = elements else: changeelements = list(self.elementObjects.keys()) # print('changeelements = ', changeelements) # print(changeelements[0]) if len(changeelements) > 0 and isinstance( changeelements[0], (list, tuple, dict)) and len(changeelements[0]) > 1: for ek in changeelements: new = None e, k = ek[:2] if e in self.elementObjects: new = unmunchify(self.elementObjects[e]) elif e in self.groupObjects: new = self.groupObjects[e] # print 'detecting group = ', e, new, new[k] if new is not None: # print (new) if e not in changedict: changedict[e] = {} changedict[e][k] = self.convert_numpy_types(new[k]) else: for e in changeelements: # print 'saving element: ', e if not self.original_elementObjects[e] == unmunchify( self.elementObjects[e]): orig = self.original_elementObjects[e] new = unmunchify(self.elementObjects[e]) try: changedict[e] = { k: self.convert_numpy_types(new[k]) for k in new if k in orig and not new[k] == orig[k] } changedict[e].update({ k: self.convert_numpy_types(new[k]) for k in new if k not in orig }) except: print('##### ERROR IN CHANGE ELEMS: ', e, new) pass return changedict
def test_unmunchify_namedtuple(): nt = namedtuple('nt', ['prop_a', 'prop_b']) b = Munch(foo=Munch(lol=True), hello=nt(prop_a=42, prop_b='yop'), ponies='are pretty!') assert sorted(unmunchify(b).items()) == [('foo', { 'lol': True }), ('hello', nt(prop_a=42, prop_b='yop')), ('ponies', 'are pretty!')]
def edit_plan_buyers(data, data_version): dict_data = unmunchify(data) if data_version is 1: add_buyer = invalid_buyers_data() dict_data['data']['buyers'].append(add_buyer) if data_version is 2: dict_data['data'].pop('buyers') return munchify(dict_data)
def savejson(obj, file): """Takes a Munch() object in and writes it to a file as JSON""" try: with open(f"{file}.json", "w") as f: f.write(json.dumps(munch.unmunchify(obj), indent=2)) f.truncate() except FileNotFoundError: print("Error: Locating config.json file. Is the config.json file present in the same directory as this script?") sys.exit()
def _todict(obj): result = dict() for key, value in obj.items(): if isinstance(value, dict): value = _todict(value) elif isinstance(value, set): value = sorted(map(str, value), key=str.lower) result[key] = value return unmunchify(result)
def take_action(self, parsed_args): result = munch.unmunchify(self._action(parsed_args)) if result is not None and (self.spec.returns is not None): result = self.spec.returns.convert(result) if result is None or result == '': result = 'Success' if not isinstance(result, dict): result = {'value': result} names = self._sorted_columns(result.keys()) return names, (self._preprocess_value(name, result[name], parsed_args) for name in names)
def __write(self, cache): c = self.app.conf s = self.app.services cache_path = path.join(c.paths.cwt, '.cache.yml') if not path.isdir(c.paths.cwt): os.makedirs(c.paths.cwt) with open(cache_path, 'w') as f: yaml.dump(unmunchify(cache), f, default_flow_style=False) s.util.chown(cache_path) return cache
def take_action(self, parsed_args): objects = [munch.unmunchify(obj) for obj in self._action(parsed_args)] # We expect the method to return a list of dicts, or a list of values. names = self._get_names(objects) if not names: return ('value', ), [(value, ) for value in objects] return names, [ [self._preprocess_value(name, obj.get(name), parsed_args) for name in names] for obj in objects ]
def example(reactor): pp = Connection() try: result = yield pp.upcoming_releases('ceph') pprint(unmunchify(result)) for release in result: url = pp.schedule_url(release.shortname) print(url) except ProductPagesException as e: print(e)
def save_pickle(pth, obj): if type(pth) == str: f = pathlib.Path(pth) else: f = pth if type(obj) == dict: obj_dict = obj else: obj_dict = munch.unmunchify(obj) with f.open('wb') as fp: pickle.dump(obj_dict, fp)
def reply_config(self, req, req_match): resp = Response() resp.content_type = 'application/json' resp.status = 200 tmp_config = copy.deepcopy(self.bot.config) tmp_config = munch.unmunchify(tmp_config) tmp_config = utils.mask_dict_password(tmp_config) resp_body = tmp_config resp_body = utils.dump_json(resp_body, pretty=True) resp.text = resp_body + "\n" return resp
def edit_tender_data_for_plan_tender(data, mode, data_version): plan_tedner_test_data = {1: '03222111-4', 2: 'UA-FIN', 3: '11112222', 4: 'aboveThresholdEU'} dict_data = unmunchify(data) if data_version is 1: dict_data['data']['items'][0]['classification']['id'] = plan_tedner_test_data[data_version] if data_version is 2: dict_data['data']['procuringEntity']['identifier']['scheme'] = plan_tedner_test_data[data_version] if data_version is 3: dict_data['data']['procuringEntity']['identifier']['id'] = plan_tedner_test_data[data_version] if data_version is 4: dict_data['data']['procurementMethodType'] = plan_tedner_test_data[data_version] return munchify(dict_data)
def params(self): """ Return a list of parameters in this task's request. If self.request is already a list, simply return it. If self.request is a raw XML-RPC string, parse it and return the params. """ if isinstance(self.request, list): return unmunchify(self.request) (params, _) = xmlrpc.loads(self.request) return params
def delete_client(module, export): """Update export client list""" changed = False client = module.params['client'] client_list = export.get_permissions() for index, item in enumerate(client_list): if item.client == client: changed = True del client_list[index] if changed: for index, item in enumerate(client_list): client_list[index] = unmunchify(item) if not module.check_mode: export.update_permissions(client_list) module.exit_json(changed=changed)
def update_export(module, export, filesystem, system): """ Create new filesystem or update existing one""" changed = False name = module.params['name'] client_list = module.params['client_list'] if export is None: if not module.check_mode: export = system.exports.create(export_path=name, filesystem=filesystem) if client_list: export.update_permissions(client_list) changed = True else: if client_list: if set(map(transform, unmunchify(export.get_permissions()))) != set(map(transform, client_list)): if not module.check_mode: export.update_permissions(client_list) changed = True module.exit_json(changed=changed)
def __str__(self): return 'LayerCake{}'.format([unmunchify(x) for x in reversed(self._stack)])
def print_mcus(): """print boards from boards.txt.""" ls = unmunchify(mcus()) print('\n'.join(ls))
def test_unmunchify(): b = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!') assert sorted(unmunchify(b).items()) == [('foo', {'lol': True}), ('hello', 42), ('ponies', 'are pretty!')] b = Munch(foo=['bar', Munch(lol=True)], hello=42, ponies=('are pretty!', Munch(lies='are trouble!'))) assert sorted(unmunchify(b).items()) == [('foo', ['bar', {'lol': True}]), ('hello', 42), ('ponies', ('are pretty!', {'lies': 'are trouble!'}))]