def partially_expand(f, wildcards): """Expand the wildcards in f from the ones present in wildcards This is done by replacing all wildcard delimiters by `{{` or `}}` that are not in `wildcards.keys()`. """ # perform the partial expansion from f's string representation s = str(f).replace("{", "{{").replace("}", "}}") for key in wildcards.keys(): s = s.replace("{{{{{}}}}}".format(key), "{{{}}}".format(key)) # build result anno_s = AnnotatedString(s) anno_s.flags = f.flags return IOFile(anno_s, f.rule)
def partially_expand(f, wildcards): """Expand the wildcards in f from the ones present in wildcards This is done by replacing all wildcard delimiters by `{{` or `}}` that are not in `wildcards.keys()`. """ # perform the partial expansion from f's string representation s = str(f).replace('{', '{{').replace('}', '}}') for key in wildcards.keys(): s = s.replace('{{{{{}}}}}'.format(key), '{{{}}}'.format(key)) # build result anno_s = AnnotatedString(s) anno_s.flags = f.flags return IOFile(anno_s, f.rule)
def modify(self, path, property=None): modified_path = self.apply_default_remote(self.replace_prefix(path, property)) # Important, update with previous flags in case of AnnotatedString #596 if hasattr(path, "flags"): if not hasattr(modified_path, "flags"): modified_path = AnnotatedString(modified_path) modified_path.flags.update(path.flags) return modified_path
def format_annotated(self, item, expand_args): """Wrapper for :meth:format preserving *AnnotatedString* flags Calls :meth:format to format *item* into a new string and copies flags from original item. This is used by :meth:expand """ updated = self.format(item, **expand_args) if isinstance(item, AnnotatedString): updated = AnnotatedString(updated) try: updated.flags = deepcopy(item.flags) except TypeError: log.debug("Failed to deepcopy flags for item {} with flags{}" "".format(item, item.flags)) updated.flags = copy(item.flags) return updated
def cleanup(f): prefix = self.rule.workflow.default_remote_prefix # remove constraints and turn this into a plain string cleaned = strip_wildcard_constraints(f) if (self.rule.workflow.default_remote_provider is not None and f.startswith(prefix) and not is_flagged(f, "local")): cleaned = f[len(prefix) + 1:] cleaned = IOFile(cleaned, rule=self.rule) else: cleaned = IOFile(AnnotatedString(cleaned), rule=self.rule) cleaned.clone_remote_object(f) return cleaned
def modify(self, path, property=None): if get_flag_value(path, PATH_MODIFIER_FLAG) is self: # Path has been modified before and is reused now, no need to modify again. return path modified_path = self.apply_default_remote( self.replace_prefix(path, property)) if modified_path == path: # nothing has changed return path # Important, update with previous flags in case of AnnotatedString #596 if hasattr(path, "flags"): if not hasattr(modified_path, "flags"): modified_path = AnnotatedString(modified_path) modified_path.flags.update(path.flags) if is_flagged(modified_path, "multiext"): modified_path.flags["multiext"] = self.apply_default_remote( self.replace_prefix(modified_path.flags["multiext"], property)) # Flag the path as modified and return. modified_path = flag(modified_path, PATH_MODIFIER_FLAG, self) return modified_path
def expand(self, item, fields, rec=-1): if isinstance(item, str): updated = self.format(item, **fields) if isinstance(item, AnnotatedString): updated = AnnotatedString(updated) try: updated.flags = deepcopy(item.flags) except TypeError as e: log.debug( "Failed to deepcopy flags for item {} with flags{}" "".format(item, item.flags)) updated.flags = copy(item.flags) item = updated elif hasattr(item, '__call__'): # function _item = item def late_expand(*args, **kwargs): return self.expand(_item(*args, **kwargs), {'wc': args[0]}, rec=rec + 1) item = late_expand elif isinstance(item, int): pass elif isinstance(item, dict): for key, value in item.items(): item[key] = self.expand(value, fields, rec=rec + 1) elif isinstance(item, list): for i, subitem in enumerate(item): item[i] = self.expand(subitem, fields, rec=rec + 1) elif isinstance(item, tuple): return (self.expand(subitem, fields, rec=rec + 1) for subitem in item) else: raise ValueError("unable to expand item '{}' with fields '{}'" "".format(repr(item), repr(fields))) return item
def cleanup(f): prefix = self.rule.workflow.default_remote_prefix # remove constraints and turn this into a plain string cleaned = strip_wildcard_constraints(f) modified_by = get_flag_value(f, PATH_MODIFIER_FLAG) if (self.rule.workflow.default_remote_provider is not None and f.startswith(prefix) and not is_flagged(f, "local")): cleaned = f[len(prefix) + 1:] cleaned = IOFile(cleaned, rule=self.rule) else: cleaned = IOFile(AnnotatedString(cleaned), rule=self.rule) cleaned.clone_remote_object(f) if modified_by is not None: cleaned.flags[PATH_MODIFIER_FLAG] = modified_by return cleaned