File: xmlcontrol.py

package info (click to toggle)
python-plwm 2.6a%2B20080530-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 1,060 kB
  • ctags: 1,781
  • sloc: python: 7,574; ansic: 1,075; xml: 109; makefile: 83
file content (47 lines) | stat: -rw-r--r-- 1,487 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

from os.path import split, join

try:
        # Python 2.5 bundled elementtree
        from xml.etree import ElementTree, ElementInclude
except ImportError:
        # Pre-2.5 third party elementtree
        from elementtree import ElementTree, ElementInclude


class XML_controller(dict):
    """Controller part of the MVC model for a user selection facility."""

    def __init__(self, node, actions):
        """Verify that everything is labeled and has an action."""

        for child in node:
            if not child.get('label'):
                raise ValueError, 'Element %s has no label' % child.tag
            if not actions.has_key(child.tag):
                raise ValueError, 'Element tag %s not in actions' % child.tag
            self[child.get('label')] = child
        self.actions = dict(actions)

    def choose(self, label):
        """Run the code that goes with labe."""

        self.actions[self[label].tag](self[label])

class Loader(object):
    """Custmom loader for XInclude processing to fix relative addressing."""

    def __init__(self, name):
        self.base = split(name)[0]

    def __call__(self, href, typ, encoding=None):
        """Fix relative references and call the default loader."""

        return ElementInclude.default_loader(join(self.base, href), typ, encoding)

def load_menus(path):
    """Load the file path."""

    menus = ElementTree.parse(path)
    ElementInclude.include(menus.getroot(), loader=Loader(path))
    return menus