def dict_to_path(data): if not data: raise SpilException('[dict_to_path] Data is empty') data = data.copy() debug('Data: {}'.format(data)) # setting defaults for key in data.keys(): if not data.get(key) and path_defaults.get(key): data[key] = path_defaults.get(key) # adding defaults subtype = get_sidtype(data) # reverse path mapping for key, value in six.iteritems(data): if key == "project": for project, project_sid in path_mapping[key].items(): if os.path.normcase(project_sid) == value: data[key] = project elif value and path_mapping.get(key): mapping = path_mapping.get(key) data[key] = utils.get_key(mapping, value) debug('sidtype: {}'.format(subtype)) pattern = path_templates.get(subtype) debug('pattern: {}'.format(pattern)) if not pattern: raise SpilException( '[dict_to_path] Unable to find pattern for sidtype: "{}" \nGiven data: "{}"' .format(subtype, data)) template = Template(subtype, pattern) template.template_resolver = resolvers debug('template: {}'.format(template)) if not template: raise SpilException('toe') # adding template specific defaults for key in template.keys(): if key not in data.keys() and path_defaults.get(key): data[key] = path_defaults.get(key) debug('data after path_defaults: {}'.format(data)) path = template.format(data) debug('found: {}'.format(path)) return path
def test_format(pattern, data, expected): '''Format data against pattern.''' template = Template('test', pattern) formatted = template.format(data) assert formatted == expected
def test_format_failure(pattern, data): '''Format incomplete data against pattern.''' template = Template('test', pattern) with pytest.raises(FormatError): template.format(data)
def test_format_failure(pattern, data): '''Format incomplete data against pattern.''' template = Template('test', pattern) with pytest.raises(FormatError): template.format(data)
def test_format(pattern, data, expected): '''Format data against pattern.''' template = Template('test', pattern) formatted = template.format(data) assert formatted == expected