Exemplo n.º 1
0
    def _prepare_config(self):
        """Prepare the module's configuration file for class registration.

        To make Mage aware that the module has one or more global
        classes of type self.type, the module's configuration file
        must have a <global> element. Furthermore, this <global>
        element must have a sub element whose tag matches the type of
        the global class. If these elements don't exist, we create
        them.

        """
        find_or_create(find_or_create(self.config, "global"), self.type_tag)
Exemplo n.º 2
0
 def register(self, name):
     """Make Mage aware that the module supplies a layout file."""
     config = self.get_config()
     frontend = find_or_create(config, "frontend")
     layout = find_or_create(frontend, "layout")
     updates = find_or_create(layout, "updates")
     # Only update the file if the following element doesn't exist.
     # This way we avoid inadvertently creating duplicate layout
     # updates.
     group = updates.find(self.module.name.lower())
     if group is None:
         group = etree.SubElement(updates, self.module.name.lower())
         file_ = etree.SubElement(group, "file")
         file_.text = self._format_name(name)
         self.put_config(config)
Exemplo n.º 3
0
 def _override(self):
     """Tell Mage that this global class overrides self.superclass."""
     substrings = self.superclass.split("_")
     tags = {"module": substrings[1].lower(),
             "name": "_".join(substrings[3:]).lower()} # e.g., product_view
     elems = "/rewrite/".join((tags["module"], tags["name"]))
     if not self.config.xpath(self.xpath + "/" + elems):
         module = find_or_create(self.type_elem, tags["module"])
         rewrite = find_or_create(module, "rewrite")
         name = etree.SubElement(rewrite, tags["name"])
         name.text = "%s_%s_%s_%s" % (self.module.namespace,
                                      self.module.name,
                                      self.type.capitalize(),
                                      self.name)
         self.put_config(self.config)
Exemplo n.º 4
0
    def _add_override(self, elem):
        """Make the module's controller override another module's controllers.

        Given a <frontend> element, create the sub elements necessary to
        make the module's controller(s) override the controller(s) of the
        module to which self.superclass belongs.

        Args:
            elem: An lxml.etree._Element object mapping to a <frontend>
                  element.

        Return:
            An lxml.etree._Element object.

        """
        substrings = self.superclass.split("_")
        super_module = substrings[1].lower()
        super_prefix = "_".join(substrings[:2])

        routers = find_or_create(elem, "routers")
        if routers.find(super_module) is not None:
            return # Bail (assume that an override already exists).

        super_module = etree.SubElement(routers, super_module)
        args = etree.SubElement(super_module, "args")
        modules = etree.SubElement(args, "modules")
        group = etree.SubElement(modules, self.module.name.lower())
        group.set("before", super_prefix)
        group.text = "_".join((self.module.namespace, self.module.name))
        return elem
Exemplo n.º 5
0
    def _add_route(self, elem):
        """Add a <routers> element with associated sub elements to elem.

        Args:
            elem: An lxml.etree._Element object mapping to a <frontend>
                  element.

        Return:
            An lxml.etree._Element object.

        """
        route = elem.xpath("/config/%s/routers/%s"
                           % (elem.tag, self.module.name.lower()))
        if route:
            return # Bail (assume that a route already exists).

        routers = find_or_create(elem, "routers")
        group = etree.SubElement(routers, self.module.name.lower())
        use = etree.SubElement(group, "use")
        use.text = self.router
        args = etree.SubElement(group, "args")
        module = etree.SubElement(args, "module")
        module.text = "%s_%s" % (self.module.namespace, self.module.name)
        front_name = etree.SubElement(args, "frontName")
        front_name.text = self.front_name
        return elem
Exemplo n.º 6
0
    def register(self):
        """Make Mage aware that this module has controllers to dispatch to.
        
        Update the module's configuration file to configure a route,
        which will allow Mage to dispatch requests to the module's
        controller(s).

        """
        config = self.get_config()
        frontend = find_or_create(config, "frontend")
        # Now we're sure frontend exists, so we can add sub elements to it.
        if self.override:
            frontend = self._add_override(frontend)
        else:
            frontend = self._add_route(frontend)
        self.put_config(config)
Exemplo n.º 7
0
 def _register_resource(self, name):
     """Tell Mage that the module has one or more resource models."""
     GlobalClass.register(self)
     tag = self.module.name.lower()
     group = self.config.xpath(self.xpath + "/" + tag)[0]
     group_mysql4 = group.tag + "_mysql4"
     resource_model = find_or_create(group, "resourceModel")
     resource_model.text = group_mysql4
     group_mysql4 = find_or_create(group, group_mysql4)
     class_ = find_or_create(group_mysql4, "class")
     class_.text = "%s_%s_%s_Mysql4" % (self.module.namespace,
                                        self.module.name,
                                        self.type)
     entities = find_or_create(group_mysql4, "entities")
     name_lower = find_or_create(entities, name.lower())
     table = find_or_create(name_lower, "table")
     table.text = self.table or group.tag + "_" + name_lower.tag
     self.put_config(self.config)
Exemplo n.º 8
0
 def test_find_or_create(self):
     root = etree.Element("root")
     find_or_create(root, "child")
     self.failIf(root.find("child") is None)