def inference_rules(self, prefix, rule_spec): ''' Generator yielding inference functions from `rule_spec`. Each yielded function accepts a path and returns an iterable of `Tag`s or other values. Because some functions are implemented as lambdas it is reasonable to return an iterable conatining `None` values to be discarded by the consumer of the rule. ''' with Pfx(r(rule_spec)): if isinstance(rule_spec, str): if rule_spec.startswith('/'): rule = RegexpTagRule(rule_spec[1:]) yield lambda path, rule=rule: rule.infer_tags( basename(path)).as_tags() else: tag_name, _ = get_dotted_identifier(rule_spec) if tag_name and tag_name == rule_spec: # return the value of tag_name or None, as a 1-tuple yield lambda path: (self.fstags[path].get(tag_name), ) else: warning("skipping unrecognised pattern") elif isinstance(rule_spec, (list, tuple)): for subspec in rule_spec: yield from self.inference_rules(prefix, subspec) else: warning("skipping unhandled type")
def __call__(self, context, namespaces, *param_values): with Pfx("$(%s)", self.name): assert type(namespaces) is list, "namespaces = %s" % r(namespaces) if len(param_values) != len(self.params): raise ValueError( "mismatched Macro parameters: self.params = %r (%d items) but got %d param_values: %r" % (self.params, len( self.params), len(param_values), param_values)) if self.params: namespaces = [dict(zip(self.params, param_values)) ] + namespaces return self.mexpr(context, namespaces)
def _parse_value(value_s): try: value, offset = pfx_call(Tag.parse_value, value_s) except ValueError as e: warning("EditValue._parse_value(%s): %s", r(value_s), e) value = value_s else: if offset < len(value_s): warning("unparsed: %r", value_s[offset:]) if isinstance(value, str): value += value_s[offset:] else: value = value_s return value
def cmd_ont(self, argv): ''' Usage: {cmd} type_name ''' tagger = self.options.tagger if not argv: raise GetoptError("missing type_name") type_name = argv.pop(0) with Pfx("type %r", type_name): if argv: raise GetoptError("extra arguments: %r" % (argv, )) print(type_name) for type_value in tagger.ont_values(type_name): ontkey = f"{type_name}.{type_value}" with Pfx("ontkey = %r", ontkey): print(" ", r(type_value), tagger.ont[ontkey])
def fspath(self, new_fspath): self._fspath = new_fspath self.configure(text=new_fspath or r(new_fspath)) def ev_set_image(ev): ''' Set the image once visible, fired at idle time. It is essential that this is queued with `after_idle`. If this ran directly during widget construction the `wait_visibility` call would block the follow construction. ''' if self._image_for == self._fspath: return if not self.is_visible(): return imgpath = self._fspath if imgpath is None: display_fspath = None else: size = self.fixed_size or (self.width, self.height) try: display_fspath = pngfor(expanduser(imgpath), size) except (OSError, ValueError) as e: warning("%r: %s", imgpath, e) display_fspath = None if display_fspath is None: self._image_for = None self.image = None self.configure(image=None) else: img = Image.open(display_fspath) image = ImageTk.PhotoImage(img) self.configure( text=basename(imgpath), image=image, width=size[0], height=size[1], ) self.image = image self._image_for = self._fspath self.bind('<Configure>', ev_set_image) self.bind('<Map>', ev_set_image) self.bind('<Unmap>', ev_set_image)
def __str__(self): return "%s[%r](prefix=%r,mapping=%s)" % ( type(self).__name__, type(self).__mro__, self.prefix, r(self.mapping) )