コード例 #1
0
ファイル: registry.py プロジェクト: sjl421/djblets
    def register(self, item):
        """Register an item.

        Args:
            item (object):
                The item to register with the class.

        Raises:
            djblets.registries.errors.RegistrationError:
                Raised if the item is missing one of the required attributes.

            djblets.registries.errors.AlreadyRegisteredError:
                Raised if the item is already registered or if the item shares
                an attribute name, attribute value pair with another item in
                the registry.
        """
        self.populate()
        attr_values = {}

        if item in self._items:
            raise self.already_registered_error_class(self.format_error(
                ALREADY_REGISTERED,
                item=item))

        for attr_name in self.lookup_attrs:
            attr_map = self._registry[attr_name]

            try:
                attr_value = getattr(item, attr_name)

                if attr_value in attr_map:
                    raise self.already_registered_error_class(
                        self.format_error(ATTRIBUTE_REGISTERED,
                                          item=item,
                                          duplicate=attr_map[attr_value],
                                          attr_name=attr_name,
                                          attr_value=attr_value))

                attr_values[attr_name] = attr_value
            except AttributeError:
                raise RegistrationError(self.format_error(
                    MISSING_ATTRIBUTE,
                    item=item,
                    attr_name=attr_name))

        for attr_name, attr_value in six.iteritems(attr_values):
            self._registry[attr_name][attr_value] = item

        self._items.add(item)
コード例 #2
0
ファイル: base.py プロジェクト: ParikhKadam/reviewboard
    def register(self, item):
        """Register a callback.

        Args:
            item (callable):
                The item to register.

        Raises:
            djblets.registries.errors.RegistrationError:
                Raised if the item is not a callable.

            djblets.registries.errors.AlreadyRegisteredError:
                Raised if the item is already registered.
        """
        self.populate()

        if not callable(item):
            raise RegistrationError(self.format_error(NOT_CALLABLE, item=item))

        super(CallbackRegistry, self).register(item)
コード例 #3
0
    def register(self, page_class):
        """Register a configuration page class.

        A page ID is considered unique and can only be registered once.

        This will also register all form classes on the page. If registration
        for any form fails, registration for the entire class will fail. In
        this case, the page will become unregistered, as well as any forms on
        that page that were successfully registered.

        Args:
            page_class (type):
                The page class to register, as a subclass of
                :py:class:`djblets.configforms.pages.ConfigPage`.

        Raises:
            djblets.registries.errors.AlreadyRegisteredError:
                Raised if the page has already been registered.

            djblets.registries.errors.RegistrationError:
                Raised if the page shares an attribute with an already
                registered page or if any of its forms share an attribute
                with an already registered form.
        """
        super(ConfigPageRegistry, self).register(page_class)

        # Set the form_classes to an empty list by default if it doesn't
        # explicitly provide its own, so that entries don't go into
        # DynamicConfigurationPageMixin's global list.
        if page_class.form_classes is None:
            page_class.form_classes = []

        # Set _default_form_classes when an account page class first registers.
        if page_class._default_form_classes is None:
            page_class._default_form_classes = list(page_class.form_classes)

        # If form_classes is empty, reload the list from _default_form_classes.
        if not page_class.form_classes:
            page_class.form_classes = list(page_class._default_form_classes)

        # We keep track of the pages that are successfully registered so that
        # if registration of any page fails, we can rollback those during
        # page unregistration.
        registered_forms = []

        for form_class in page_class.form_classes:
            try:
                self._forms.register(form_class)
                self._form_to_page[form_class.form_id] = page_class.page_id
            except RegistrationError:
                duplicate = self.get('page_id',
                                     self._form_to_page[form_class.form_id])

                self.unregister(page_class, registered_forms)
                raise RegistrationError(
                    self.format_error(FORM_ALREADY_REGISTERED,
                                      page=page_class,
                                      form=form_class,
                                      duplicate=duplicate))

            registered_forms.append(form_class)