def set_template_fields(self): for name, cfgname, fieldsname in ( ("item", "item_formats", "template_fields"), ("album", "album_formats", "album_template_fields"), ): for field, template in config[cfgname].items(): self._log.debug(f"adding {name} field {field}") templatestr = template.as_str() template = functemplate.template(templatestr) def apply_item(item, field=field, template=template): # I would like to pre-determine the field types up front, # but there's no event that's fired after the plugin queries # and fields are merged, so we wait until first use instead. if item.id in self.cache: if field in self.cache[item.id]: return self.cache[item.id][field] field_type = item._type(field) value = self.cache[item.id][field] = field_type.parse( item.evaluate_template(template)) return value fields = getattr(self, fieldsname) fields[field] = apply_item
def get_path_formats(subview=None): """Get the configuration's path formats as a list of query/template pairs. """ path_formats = [] subview = subview or config['paths'] for query, view in subview.items(): query = PF_KEY_QUERIES.get(query, query) # Expand common queries. path_formats.append((query, template(view.as_str()))) return path_formats
def evaluate_template(self, template, for_path=False): """Evaluate a template (a string or a `Template` object) using the object's fields. If `for_path` is true, then no new path separators will be added to the template. """ # Perform substitution. if isinstance(template, six.string_types): template = functemplate.template(template) return template.substitute(self.formatted(for_path=for_path), self._template_funcs())
def evaluate_template(self, template, for_path=False): """Evaluate a template (a string or a `Template` object) using the object's fields. If `for_path` is true, then no new path separators will be added to the template. """ # Perform substitution. if isinstance(template, six.string_types): template = functemplate.template(template) return template.substitute(self.formatted(for_path), self._template_funcs())
def get_path_formats(subview=None): """Get the configuration's path formats as a list of query/template pairs. """ path_formats = [] subview = subview or config['paths'] for query, view in subview.items(): query = PF_KEY_QUERIES.get(query, query) # Expand common queries. path_formats.append((query, template(view.as_str()))) return path_formats
def get_path_formats(subview=None): """Get the configuration's path formats as a list of query/template pairs. """ path_formats = [] subview = subview or config['paths'] for query, view in subview.items(): query = PF_KEY_QUERIES.get(query, query) # Expand common queries. viewstr = view.as_str() if '\n' in viewstr: viewstr = ''.join(v.strip() for v in viewstr.split('\n')).replace('\n', '') path_formats.append((query, template(viewstr))) return path_formats