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 path_to_dict(path): path = str(path) # path = os.path.normcase(path) path = path.replace(os.sep, '/') templates = [] for name, pattern in six.iteritems(path_templates): template = Template( name, pattern, anchor=lucidity.Template.ANCHOR_BOTH, default_placeholder_expression= '[^/]*', # needed to use '#' for a path duplicate_placeholder_mode=lucidity.Template.STRICT) template.template_resolver = resolvers templates.append(template) try: data, template = lucidity.parse(path, templates) # print 'found', data, template except Exception as e: warn(e) return None, None if not data: return None, None for key, value in six.iteritems(data): if path_mapping.get(key): value = path_mapping.get(key).get(value, value) data[key] = value for key in list(data.keys()): if key not in shot_keys + asset_keys: data.pop(key) return template.name, data # need the name ?