Exemple #1
0
 def __repr__(self):
     """Return representation of self."""
     iface = get_iresource(self) or self.__class__
     iface_dotted = to_dotted_name(iface)
     oid = getattr(self, '__oid__', None)
     name = getattr(self, '__name__', None)
     identifier = str(oid)
     return '{0} oid: {1} name: {2}'.format(iface_dotted, identifier, name)
Exemple #2
0
 def test_to_string_with_oid_and_name(self):
     inst = self.make_one()
     inst.__oid__ = 1
     inst.__name__ = None
     wanted_str = '{0} oid: {1} name: {2}'.format(to_dotted_name(inst.__class__),
                                        str(1),
                                        'None')
     assert wanted_str == str(inst)
Exemple #3
0
 def test_to_string_without_oid_and_iresource(self):
     inst = self.make_one()
     inst_class = inst.__class__
     non_iresource = object()
     wanted_str = '{0} oid: {1} name: {2}'.format(to_dotted_name(non_iresource.__class__),
                                                  'None',
                                                  'None')
     assert wanted_str == inst_class.__repr__(non_iresource)
    def _describe_resources(self, resources_meta):
        """Build a description of the resources registered in the system.

        Args:
          resources_meta (dict): mapping from iresource interfaces to metadata

        Returns:
          resource_map (dict): a dict (suitable for JSON serialization) that
                               describes all the resources registered in the
                               system.
        """
        resource_map = {}

        for iresource, resource_meta in resources_meta.items():
            prop_map = {}

            # super types
            prop_map['super_types'] = _get_base_ifaces(iresource,
                                                       root_iface=IResource)
            # List of sheets
            sheets = []
            sheets.extend(resource_meta.basic_sheets)
            sheets.extend(resource_meta.extended_sheets)
            prop_map['sheets'] = [to_dotted_name(s) for s in sheets]

            # Main element type if this is a pool or item
            if resource_meta.item_type:
                prop_map['item_type'] = to_dotted_name(resource_meta.item_type)

            # Other addable element types
            if resource_meta.element_types:
                element_names = []
                for typ in resource_meta.element_types:
                    element_names.append(to_dotted_name(typ))
                prop_map['element_types'] = element_names

            resource_map[to_dotted_name(iresource)] = prop_map
        return resource_map
Exemple #5
0
    def _describe_resources(self, resources_meta):
        """Build a description of the resources registered in the system.

        Args:
          resources_meta (dict): mapping from iresource interfaces to metadata

        Returns:
          resource_map (dict): a dict (suitable for JSON serialization) that
                               describes all the resources registered in the
                               system.
        """
        resource_map = {}

        for iresource, resource_meta in resources_meta.items():
            prop_map = {}

            # super types
            prop_map['super_types'] = _get_base_ifaces(iresource,
                                                       root_iface=IResource)
            # List of sheets
            sheets = []
            sheets.extend(resource_meta.basic_sheets)
            sheets.extend(resource_meta.extended_sheets)
            prop_map['sheets'] = [to_dotted_name(s) for s in sheets]

            # Main element type if this is a pool or item
            if resource_meta.item_type:
                prop_map['item_type'] = to_dotted_name(resource_meta.item_type)

            # Other addable element types
            if resource_meta.element_types:
                element_names = []
                for typ in resource_meta.element_types:
                    element_names.append(to_dotted_name(typ))
                prop_map['element_types'] = element_names

            resource_map[to_dotted_name(iresource)] = prop_map
        return resource_map
    def _describe_sheets(self, sheet_metadata):
        """Build a description of the sheets used in the system.

        Args:
          sheet_metadata: mapping of sheet names to metadata about them, as
            returned by the registry

        Returns:
          A dict (suitable for JSON serialization) that describes the sheets
          and their fields

        """
        sheet_map = {}
        for isheet, sheet_meta in sheet_metadata.items():
            # readable and create_mandatory flags are currently defined for
            # the whole sheet, but we copy them as attributes into each field
            # definition, since this might change in the future.
            # (The _sheet_field_readable method already allows overwriting the
            # readable flag on a field-by-field basis, but it's somewhat
            # ad-hoc.)
            fields = []

            # Create field definitions
            for node in sheet_meta.schema_class().children:

                fieldname = node.name
                valuetype = type(node)
                valuetyp = type(node.typ)
                typ = to_dotted_name(valuetyp)
                containertype = None
                targetsheet = None
                readonly = getattr(node, 'readonly', False)

                if issubclass(valuetype, References):
                    empty_appstruct = node.bind().default
                    containertype = empty_appstruct.__class__.__name__
                    typ = to_dotted_name(AbsolutePath)
                elif isinstance(node, SequenceSchema):
                    containertype = 'list'
                    typ = to_dotted_name(type(node.children[0]))
                elif valuetype is not SchemaNode:
                    # If the outer type is not a container and it's not
                    # just a generic SchemaNode, we use the outer type
                    # as "valuetype" since it provides most specific
                    # information (e.g. "adhocracy_core.schema.Identifier"
                    # instead of just "SingleLine")
                    typ = to_dotted_name(valuetype)

                if hasattr(node, 'reftype'):
                    # set targetsheet
                    reftype = node.reftype
                    target_isheet = reftype.getTaggedValue('target_isheet')
                    source_isheet = reftype.getTaggedValue('source_isheet')
                    isheet_ = source_isheet if node.backref else target_isheet
                    targetsheet = to_dotted_name(isheet_)

                typ_stripped = strip_optional_prefix(typ, 'colander.')

                fielddesc = {
                    'name': fieldname,
                    'valuetype': typ_stripped,
                    'create_mandatory':
                    False if readonly else sheet_meta.create_mandatory,
                    'editable': False if readonly else sheet_meta.editable,
                    'creatable': False if readonly else sheet_meta.creatable,
                    'readable': sheet_meta.readable,
                }
                if containertype is not None:
                    fielddesc['containertype'] = containertype
                if targetsheet is not None:
                    fielddesc['targetsheet'] = targetsheet

                fields.append(fielddesc)

            super_types = _get_base_ifaces(isheet, root_iface=ISheet)

            sheet_map[to_dotted_name(isheet)] = {
                'fields': fields,
                'super_types': super_types
            }

        return sheet_map
Exemple #7
0
    def _describe_sheets(self, sheet_metadata):
        """Build a description of the sheets used in the system.

        Args:
          sheet_metadata: mapping of sheet names to metadata about them, as
            returned by the registry

        Returns:
          A dict (suitable for JSON serialization) that describes the sheets
          and their fields

        """
        sheet_map = {}
        for isheet, sheet_meta in sheet_metadata.items():
            # readable and create_mandatory flags are currently defined for
            # the whole sheet, but we copy them as attributes into each field
            # definition, since this might change in the future.
            # (The _sheet_field_readable method already allows overwriting the
            # readable flag on a field-by-field basis, but it's somewhat
            # ad-hoc.)
            fields = []

            # Create field definitions
            for node in sheet_meta.schema_class().children:

                fieldname = node.name
                valuetype = type(node)
                valuetyp = type(node.typ)
                typ = to_dotted_name(valuetyp)
                containertype = None
                targetsheet = None
                readonly = getattr(node, 'readonly', False)

                if isinstance(node, References):
                    containertype = 'list'
                    typ = to_dotted_name(AbsolutePath)
                elif isinstance(node, colander.SequenceSchema):
                    containertype = 'list'
                    typ = to_dotted_name(type(node.children[0]))
                elif valuetype not in (colander.SchemaNode, SchemaNode):
                    # If the outer type is not a container and it's not
                    # just a generic SchemaNode, we use the outer type
                    # as "valuetype" since it provides most specific
                    # information (e.g. "adhocracy_core.schema.Identifier"
                    # instead of just "SingleLine")
                    typ = to_dotted_name(valuetype)

                if hasattr(node, 'reftype'):
                    # set targetsheet
                    reftype = node.reftype
                    target_isheet = reftype.getTaggedValue('target_isheet')
                    source_isheet = reftype.getTaggedValue('source_isheet')
                    isheet_ = source_isheet if node.backref else target_isheet
                    targetsheet = to_dotted_name(isheet_)

                fielddesc = {
                    'name': fieldname,
                    'valuetype': typ,
                    'create_mandatory':
                        False if readonly else sheet_meta.create_mandatory,
                    'editable': False if readonly else sheet_meta.editable,
                    'creatable': False if readonly else sheet_meta.creatable,
                    'readable': sheet_meta.readable,
                }
                if containertype is not None:
                    fielddesc['containertype'] = containertype
                if targetsheet is not None:
                    fielddesc['targetsheet'] = targetsheet

                fields.append(fielddesc)

            super_types = _get_base_ifaces(isheet, root_iface=ISheet)

            sheet_map[to_dotted_name(isheet)] = {'fields': fields,
                                                 'super_types': super_types}

        return sheet_map