def __init__(self, inputs, parameters, outputs): """ Parameters ---------- inputs : dict Parameter name to tuple of semantic type and view type. parameters : dict Parameter name to tuple of primitive type and view type. outputs : collections.OrderedDict Named output to tuple of semantic type and view type. """ for input_name, (semantic_type, _) in inputs.items(): if not qtype.is_semantic_type(semantic_type): raise TypeError("%r for %r is not a semantic qiime type." % (semantic_type, input_name)) for param_name, (primitive_type, _) in parameters.items(): if not qtype.is_primitive_type(primitive_type): raise TypeError("%r for %r is not a primitive qiime type." % (primitive_type, param_name)) for output_name, (output_semantic_type, _) in outputs.items(): if not qtype.is_semantic_type(output_semantic_type): raise TypeError("%r for %r is not a semantic qiime type." % (output_semantic_type, output_name)) if not output_semantic_type.is_concrete(): raise TypeError("%r for %r is not a concrete type." % (output_semantic_type, output_name)) self.inputs = inputs self.parameters = parameters self.outputs = outputs
def register_type_to_data_layout(self, semantic_type, name, version): if not is_semantic_type(semantic_type): raise TypeError("%r is not a semantic type." % semantic_type) if not isinstance(semantic_type, grammar.TypeExpression): raise ValueError("%r is not a semantic type expression." % semantic_type) self.type_to_data_layouts[semantic_type] = (name, version)
def register_semantic_type(self, semantic_type): if not is_semantic_type(semantic_type): raise TypeError("%r is not a semantic type." % semantic_type) if not (isinstance(semantic_type, grammar.CompositeType) or (semantic_type.is_concrete() and not semantic_type.fields)): raise ValueError("%r is not a semantic type symbol." % semantic_type) if semantic_type.name in self.types: raise ValueError("Duplicate semantic type symbol %r." % semantic_type) self.types[semantic_type.name] = semantic_type
def register_semantic_type_to_format(self, semantic_type, artifact_format): if not issubclass(artifact_format, DirectoryFormat): raise TypeError("%r is not a directory format." % artifact_format) if not is_semantic_type(semantic_type): raise TypeError("%r is not a semantic type." % semantic_type) if not isinstance(semantic_type, grammar.TypeExpression): raise ValueError("%r is not a semantic type expression." % semantic_type) if semantic_type.predicate is not None: raise ValueError("%r has a predicate, differentiating format on" " predicate is not supported.") self.type_formats.append(TypeFormatRecord( type_expression=semantic_type, format=artifact_format, plugin=self))
def register_semantic_type(self, semantic_type): if not is_semantic_type(semantic_type): raise TypeError("%r is not a semantic type." % semantic_type) if not (isinstance(semantic_type, grammar.CompositeType) or (semantic_type.is_concrete() and not semantic_type.fields)): raise ValueError("%r is not a semantic type symbol." % semantic_type) if semantic_type.name in self.types: raise ValueError("Duplicate semantic type symbol %r." % semantic_type) self.types[semantic_type.name] = SemanticTypeRecord( semantic_type=semantic_type, plugin=self)
def register_semantic_type_to_format(self, semantic_type, artifact_format): if not issubclass(artifact_format, DirectoryFormat): raise TypeError("%r is not a directory format." % artifact_format) if not is_semantic_type(semantic_type): raise TypeError("%r is not a semantic type." % semantic_type) if not isinstance(semantic_type, grammar.TypeExpression): raise ValueError("%r is not a semantic type expression." % semantic_type) if semantic_type.predicate is not None: raise ValueError("%r has a predicate, differentiating format on" " predicate is not supported.") self.type_formats.append( TypeFormatRecord(type_expression=semantic_type, format=artifact_format, plugin=self))
def parse_type(string, expect=None): """Convert a string into a TypeExpression Parameters ---------- string : str The string type expression to convert into a TypeExpression expect : {'semantic', 'primitive', 'visualization'}, optional Will raise a TypeError if the resulting TypeExpression is not a member of `expect`. Returns ------- TypeExpression Raises ------ ValueError Raised when `expect` has an invalid value TypeError Raised when the expression contains invalid characters TypeError Raised when the expression does not result in a member of `expect` UnknownTypeError Raised when unkown types are present in the expression. """ # Avoid circular imports import qiime.sdk import qiime.core.type as qtype if expect is not None and expect not in {'semantic', 'primitive', 'visualization'}: raise ValueError("`expect` got %r, must be 'semantic', 'primitive'," " 'visualization', or None." % (expect,)) if '\n' in string or '\r' in string or ';' in string: raise TypeError("Found multiple statements in type expression %r. Will" " not evaluate to avoid arbitrary code execution." % string) pm = qiime.sdk.PluginManager() locals_ = {n: getattr(qtype, n) for n in qtype.__all__ if '_' not in n} locals_.update({k: v[1] for k, v in pm.semantic_types.items()}) try: type_expr = eval(string, {'__builtins__': {}}, locals_) if expect is None: pass elif expect == 'semantic' and qtype.is_semantic_type(type_expr): pass elif expect == 'primitive' and qtype.is_primitive_type(type_expr): pass elif expect == 'visualization' and type_expr == qtype.Visualization: pass else: raise TypeError("Type expression %r is not a %s type." % (type_expr, expect)) return type_expr except NameError as e: # http://stackoverflow.com/a/2270822/579416 name, = re.findall("name '(\w+)' is not defined", str(e)) raise UnknownTypeError("Name %r is not a defined QIIME type, a plugin" " may be needed to define it." % name)
def parse_type(string, expect=None): """Convert a string into a TypeExpression Parameters ---------- string : str The string type expression to convert into a TypeExpression expect : {'semantic', 'primitive', 'visualization'}, optional Will raise a TypeError if the resulting TypeExpression is not a member of `expect`. Returns ------- TypeExpression Raises ------ ValueError Raised when `expect` has an invalid value TypeError Raised when the expression contains invalid characters TypeError Raised when the expression does not result in a member of `expect` UnknownTypeError Raised when unkown types are present in the expression. """ if expect is not None and expect not in {'semantic', 'primitive', 'visualization'}: raise ValueError("`expect` got %r, must be 'semantic', 'primitive'," " 'visualization', or None." % (expect,)) if '\n' in string or '\r' in string or ';' in string: raise TypeError("Found multiple statements in type expression %r. Will" " not evaluate to avoid arbitrary code execution." % string) pm = qiime.sdk.PluginManager() locals_ = {n: getattr(qtype, n) for n in qtype.__all__ if '_' not in n} locals_.update({k: v.semantic_type for k, v in pm.semantic_types.items()}) try: type_expr = eval(string, {'__builtins__': {}}, locals_) if expect is None: pass elif expect == 'semantic' and qtype.is_semantic_type(type_expr): pass # TODO optimize codepath for `expect=primitive` and # `expect=visualization` since `PluginManager` is slow and isn't # necessary for these types. elif expect == 'primitive' and qtype.is_primitive_type(type_expr): pass elif expect == 'visualization' and type_expr == qtype.Visualization: pass else: raise TypeError("Type expression %r is not a %s type." % (type_expr, expect)) return type_expr except NameError as e: # http://stackoverflow.com/a/2270822/579416 name, = re.findall("name '(\w+)' is not defined", str(e)) raise UnknownTypeError("Name %r is not a defined QIIME type, a plugin" " may be needed to define it." % name)