form.build.map module

class form.build.map.BuildManager(type_map)

Bases: object

A class for managing builds of Containers

build(container, source=None)

Build the GroupBuilder for the given Container

Parameters:
  • container (Container) – the container to convert to a Builder
  • source (str) – the source of container being built i.e. file path
construct(builder)

Construct the Container represented by the given builder

Parameters:builder (DatasetBuilder or GroupBuilder) – the builder to construct the Container from
get_builder_name(container)

Get the name a Builder should be given

Parameters:container (Container) – the container to convert to a Builder
Returns:The name a Builder should be given when building this container
Return type:str
get_cls(builder)

Get the class object for the given Builder

Parameters:builder (Builder) – the Builder to get the class object for
get_subspec(spec, builder)

Get the specification from this spec that corresponds to the given builder

Parameters:
prebuilt(container, builder)

Save the Builder for a given Container for future use

Parameters:
class form.build.map.ObjectMapper(spec)

Bases: object

A class for mapping between Spec objects and Container attributes

Create a map from Container attributes to NWB specifications

Parameters:spec (DatasetSpec or GroupSpec) – The specification for mapping objects to builders
build(container, manager, parent=None, source=None)

Convert an Container to a Builder representation

Parameters:
  • container (Container) – the container to convert to a Builder
  • manager (BuildManager) – the BuildManager to use for managing this build
  • parent (Builder) – the parent of the resulting Builder
  • source (str) – the source of container being built i.e. file path
Returns:

the Builder representing the given Container

Return type:

Builder

construct(builder, manager)

Construct an Container from the given Builder

Parameters:
static constructor_arg(name)

Decorator to override the default mapping scheme for a given constructor argument.

Decorate ObjectMapper methods with this function when extending ObjectMapper to override the default scheme for mapping between Container and Builder objects. The decorated method should accept as its first argument the Builder object that is being mapped. The method should return the value to be passed to the target Container class constructor argument given by name.
Parameters:name (str) – the name of the constructor argument
constructor_args = {'name': <function ObjectMapper.get_container_name>}
classmethod convert_dt_name(spec)

Get the attribute name corresponding to a specification

Parameters:spec (Spec) – the specification to get the name for
classmethod get_attr_names(spec)

Get the attribute names for each subspecification in a Spec

Parameters:spec (Spec) – the specification to get the object attribute names for
get_attr_spec(attr_name)

Return the Spec for a given attribute

Parameters:attr_name (str) – the name of the attribute
get_attr_value(spec, container)

Get the value of the attribute corresponding to this spec from the given container

Parameters:
  • spec (Spec) – the spec to get the attribute value for
  • container (Container) – the container to get the attribute value from
get_attribute(spec)

Get the object attribute name for the given Spec

Parameters:spec (Spec) – the spec to get the attribute for
Returns:the attribute name
Return type:str
get_builder_name(container)

Get the name of a Builder that represents a Container

Parameters:container (Container) – the Container to get the Builder name for
get_carg_spec(carg_name)

Return the Spec for a given constructor argument

Parameters:carg_name (str) – the name of the constructor argument
get_const_arg(spec)

Get the constructor argument for the given Spec

Parameters:spec (Spec) – the spec to get the constructor argument for
Returns:the name of the constructor argument
Return type:str
get_container_name(builder)
map_attr(attr_name, spec)

Map an attribute to spec. Use this to override default behavior

Parameters:
  • attr_name (str) – the name of the object to map
  • spec (Spec) – the spec to map the attribute to
map_const_arg(const_arg, spec)

Map an attribute to spec. Use this to override default behavior

Parameters:
  • const_arg (str) – the name of the constructor argument to map
  • spec (Spec) – the spec to map the attribute to
map_spec(attr_carg, spec)

Map the given specification to the construct argument and object attribute

Parameters:
  • attr_carg (str) – the constructor argument/object attribute to map this spec to
  • spec (Spec) – the spec to map the attribute to
obj_attrs = {}
static object_attr(name)

Decorator to override the default mapping scheme for a given object attribute.

Decorate ObjectMapper methods with this function when extending ObjectMapper to override the default scheme for mapping between Container and Builder objects. The decorated method should accept as its first argument the Container object that is being mapped. The method should return the child Builder object (or scalar if the object attribute corresponds to an AttributeSpec) that represents the attribute given by name.
Parameters:name (str) – the name of the constructor argument
spec

the Spec used in this ObjectMapper

unmap(spec)

Removing any mapping for a specification. Use this to override default mapping

Parameters:spec (Spec) – the spec to map the attribute to
class form.build.map.TypeMap(namespaces, mapper_cls=<class 'form.build.map.ObjectMapper'>)

Bases: object

A class to maintain the map between ObjectMappers and Container classes

Parameters:
  • namespaces (NamespaceCatalog) – the NamespaceCatalog to use
  • mapper_cls (type) – the ObjectMapper class to use
build(container, manager=None, source=None)

Build the GroupBuilder for the given Container

Parameters:
  • container (Container) – the container to convert to a Builder
  • manager (BuildManager) – the BuildManager to use for managing this build
  • source (str) – the source of container being built i.e. file path
construct(builder, manager=None)

Construct the Container represented by the given builder

Parameters:
classmethod default()

The default TypeMap for FORM to use

get_builder_name(container)

Get the name a Builder should be given

Parameters:container (Container) – the container to convert to a Builder
Returns:The name a Builder should be given when building this container
Return type:str
get_cls(builder)

Get the class object for the given Builder

Parameters:builder (Builder) – the Builder object to get the corresponding Container class for
get_container_classes(namespace=None)
Parameters:namespace (str) – the namespace to get the container classes for
get_container_cls(namespace, data_type)

Get the container class from data type specification

If no class has been associated with the data_type from namespace, a class will be dynamically created and returned.
Parameters:
  • namespace (str) – the namespace containing the data_type
  • data_type (str) – the data type to create a Container class for
Returns:

the class for the given namespace and data_type

Return type:

type

get_map(obj)

Return the ObjectMapper object that should be used for the given container

Parameters:obj (Container or Builder) – the object to get the ObjectMapper for
Returns:the ObjectMapper to use for mapping the given object
Return type:ObjectMapper
get_subspec(spec, builder)

Get the specification from this spec that corresponds to the given builder

Parameters:
load_namespaces(namespace_path, resolve=True)

Load namespaces from a namespace file.

This method will call load_namespaces on the NamespaceCatalog used to construct this TypeMap. Additionally, it will process the return value to keep track of what types were included in the loaded namespaces. Calling load_namespaces here has the advantage of being able to keep track of type dependencies across namespaces.
Parameters:
  • namespace_path (str) – the path to the file containing the namespaces(s) to load
  • resolve (bool) – whether or not to include objects from included/parent spec objects
Returns:

the namespaces loaded from the given file

Return type:

tuple

register_container_type(namespace, data_type, container_cls)

Map a container class to a data_type

Parameters:
  • namespace (str) – the namespace containing the data_type to map the class to
  • data_type (str) – the data_type to mape the class to
  • container_cls (TypeSource or type) – the class to map to the specified data_type
classmethod register_default(default_map)

Register a default TypeMap for FORM to use

Parameters:default_map (TypeMap) – the default TypeMap instance to use
register_map(container_cls, mapper_cls)

Map a container class to an ObjectMapper class

Parameters:
  • container_cls (type) – the Container class for which the given ObjectMapper class gets used for
  • mapper_cls (type) – the ObjectMapper class to use to map
class form.build.map.TypeSource(namespace, data_type)

Bases: object

A class to indicate the source of a data_type in a namespace.

This class should only be used by TypeMap

Parameters:
  • namespace (str) – the namespace the from, which the data_type originated
  • data_type (str) – the name of the type
data_type
namespace