def __init__(self, type_, fget=None, fset=None, fdel=None, fvalidate=None, store=None): """Initialises :class:`ArrayWrapper` :param type_: The type of array :type type_: type :param fget: The getter for the array :type fget: func :param fset: The setter for the array :type fset: func :param fdel: The deleter for the array :type fdel: func :param fvalidate: The validator for the array :type fvalidate: func :param store: The store for the array :type store: list """ validate_type(store, list, 'store') self._type = type_ self._fget = fget self._fset = fset self._fdel = fdel self._fvalidate = fvalidate self._store = store
def __getitem__(self, index): validate_type(index, int, 'index') if self._fget: return self._fget(index) if self._store is not None: self._extend(index + 1) return self._store[index] raise ValueError( 'Could not get value for property, no store and no getter specified' )
def __delitem__(self, index): validate_type(index, int, 'index') if self._fdel: self._fdel(index) elif self._store is not None: self._extend(index + 1) del self._store[index] else: raise ValueError( 'Could not deleted index for property, no store and no setter specified' )
def string_hash(string): """Returns the hash of a string :param filename: The string to compute the hash of :type filename: str :returns: The MD5 hash of the string :rtype: bytes """ validate_type(string, str, 'string') return bytes_hash(string.encode('utf8'))
def evaluate(value_proxy, *args, **kwargs): """Utility function to evaluate a :class:`ValueProxy` Determines whether `kwargs` needs to be provided :param value_proxy: The :class:`ValueProxy` to evaluate :type value_proxy: :class:`VaWlueProxy` """ validate_type(value_proxy, ValueProxy, 'value_proxy') parameters = inspect.signature(value_proxy.evaluate).parameters if 'kwargs' in parameters: return value_proxy.evaluate(*args, **kwargs) return value_proxy.evaluate(*args)
def __set__(self, obj, value): if obj is None: return if self._name is None: raise ValueError('Cannot set property, name has not been set') if self._readonly or self._abstract: raise NotImplementedError( _not_implemented_message(obj, self._name, "not implemented (abstract)")) validate_type(value, list, 'value') wrapper = self._wrapper(obj) for i, v in enumerate(value): wrapper[i] = v
def bytes_hash(value): """Returns the hash of a byte array :param value: The bytes to compute the hash of :type value: byte string :returns: The MD5 hash of the bytes :rtype: bytes """ validate_type(value, bytes, 'value') hasher = hashlib.new('md5') hasher.update(value) return hasher.hexdigest()
def __setitem__(self, index, value): validate_type(index, int, 'index') validate_type(value, self._type, 'value') if self._fvalidate: self._fvalidate(index, value) if self._fset: self._fset(index, value) elif self._store is not None: self._extend(index + 1) self._store[index] = value else: raise ValueError( 'Could not set value for property, no store and no setter specified' )
def __init__(self, parent, name, value, path, ghosts): """Initialises :class:`ListNode` :param parent: The parent node :type parent: :class:`SpecificationNode` :param name: The node name :type name: str :param value: The node value :type value: list :param path: The path to this node :type path: str :param ghosts: Ghost values :type ghosts: dict """ super().__init__(parent, name, value, path, ghosts) validate_type(value, list, 'value')
def __init__(self, parent, name, value_proxy, path, ghosts): """Initialises :class:`ValueProxyNode` :param parent: The parent node :type parent: :class:`ValueProxyNode` :param name: The node name :type name: str :param value_proxy: The node value :type value_proxy: :class:`ValueProxy` :param path: The path to this node :type path: str :param ghosts: Ghost values :type ghosts: dict """ super().__init__(parent, name, value_proxy, path, ghosts) validate_type(value_proxy, ValueProxy, 'value_proxy')
def __init__(self, parent, name, index, value, path, ghosts): """Initialises :class:`IndexedNode` :param parent: The parent node :type parent: :class:`SpecificationNode` :param name: The node name :type name: str :param value_proxy: The node value :type value_proxy: object :param path: The path to this node :type path: str :param ghosts: Ghost values :type ghosts: dict """ super().__init__(parent, name, value, path, ghosts) validate_type(index, int, 'index') self._index = index
def _convert_node(self, node): validate_type(node, SpecificationNode, 'node') if not node.has_property: return sum([self._convert_node(c) for c in node.children], []) node_dict = {'name': node.property_name, 'value': node.property_value} if isinstance(node, IndexedNode): node_dict['index'] = node.index if node.children: children = [] for child_node in node.children: next_children = self._convert_node(child_node) children += next_children node_dict['children'] = children else: node_dict['path'] = node.path if node.ghosts: node_dict['ghosts'] = node.ghosts return [node_dict]
def convert(self, spec): """Converts the given spec model into a ``dict`` :param spec: The specification model to convert :type spec: :class:`SpecificationModel` :returns: A ``dict`` represenation of the specification model :rtype: ``dict`` """ validate_type(spec, SpecificationModel, 'spec') return { 'base_file': spec.base_file, 'metadata': self._convert_metadata(spec.metadata), 'spec': sum([self._convert_node(c) for c in spec.root_node.children], []) }
def create(self, parent, name, value, path, ghosts, children=None, literal=False): """Creates a :class:`SpecificationNode`, based on the value :param parent: The parent :class:`SpecificationNode` :type parent: :class:`SpecificationNode` :param name: The name of the node :type name: str :param value: The value of the node :type value: object :param ghosts: Ghost values :type ghosts: dict :param children: The children of the new node, if any :type children: list :param literal: if True, the value is not expandable and is set literally :type literal: bool """ children = children or [] validate_type(ghosts, dict, 'ghosts') validate_type(children, list, 'children') if isinstance(value, dict) and not literal: node = DictNode(parent, name, value, path, ghosts) elif isinstance(value, list) and not literal: node = ListNode(parent, name, value, path, ghosts) elif isinstance(value, ValueProxy) and not literal: node = ValueProxyNode(parent, name, value, path, ghosts) else: name_index = self._index(name) if name_index is not None: name, index = name_index node = IndexedNode(parent, name, index, value, path, ghosts) else: node = SpecificationNode(parent, name, value, path, ghosts) for child in children: node.add_child(self._copy_tree(node, child)) return node
def prettyspec(spec, outstream=None): """Writes the given spec tree to the stream provided """ validate_type(spec, dict, 'spec') _prettyspec_impl(spec, 0, outstream or sys.stdout)
def _convert_metadata(metadata): validate_type(metadata, SpecificationMetadata, 'metadata') return { 'creation_time': metadata.creation_time, 'notes': metadata.notes }