Example #1
0
class ExtensionTreeModelBuilder:
    """Tree model of available extensions."""
    def __init__(self):
        self.mainNodes = dict()
        self.model = TreeModel(ModuleNode)
        for prefix in (
                "display",
                "database",
                "general",
                "imagery",
                "misc",
                "postscript",
                "paint",
                "raster",
                "raster3D",
                "sites",
                "temporal",
                "vector",
                "wxGUI",
                "other",
        ):
            node = self.model.AppendNode(parent=self.model.root, label=prefix)
            self.mainNodes[prefix] = node

    def GetModel(self):
        return self.model

    def _emptyTree(self):
        """Remove modules from tree keeping the main structure"""
        for node in self.mainNodes.values():
            for child in reversed(node.children):
                self.model.RemoveNode(child)

    def _expandPrefix(self, c):
        name = {
            "d": "display",
            "db": "database",
            "g": "general",
            "i": "imagery",
            "m": "misc",
            "ps": "postscript",
            "p": "paint",
            "r": "raster",
            "r3": "raster3D",
            "s": "sites",
            "t": "temporal",
            "v": "vector",
            "wx": "wxGUI",
            "": "other",
        }

        if c in name:
            return name[c]

        return c

    def Load(self, url, full=True):
        """Load list of extensions"""
        self._emptyTree()

        if full:
            flags = "g"
        else:
            flags = "l"
        retcode, ret, msg = RunCommand("g.extension",
                                       read=True,
                                       getErrorMsg=True,
                                       url=url,
                                       flags=flags,
                                       quiet=True)
        if retcode != 0:
            raise GException(_("Unable to load extensions. %s") % msg)

        currentNode = None
        for line in ret.splitlines():
            if full:
                try:
                    key, value = line.split("=", 1)
                except ValueError:
                    key = "name"
                    value = line

                if key == "name":
                    try:
                        prefix, name = value.split(".", 1)
                    except ValueError:
                        prefix = ""
                        name = value
                    mainNode = self.mainNodes[self._expandPrefix(prefix)]
                    currentNode = self.model.AppendNode(parent=mainNode,
                                                        label=value)
                    currentNode.data = {"command": value}
                else:
                    if currentNode is not None:
                        currentNode.data[key] = value
            else:
                try:
                    prefix, name = line.strip().split(".", 1)
                except ValueError:
                    prefix = ""
                    name = line.strip()

                if self._expandPrefix(prefix) == prefix:
                    prefix = ""
                module = prefix + "." + name
                mainNode = self.mainNodes[self._expandPrefix(prefix)]
                currentNode = self.model.AppendNode(parent=mainNode,
                                                    label=module)
                currentNode.data = {
                    "command": module,
                    "keywords": "",
                    "description": "",
                }
Example #2
0
class ExtensionTreeModelBuilder:
    """Tree model of available extensions."""
    def __init__(self):
        self.mainNodes = dict()
        self.model = TreeModel(ModuleNode)
        for prefix in ('display', 'database', 'general', 'imagery', 'misc',
                       'postscript', 'paint', 'raster', 'raster3D', 'sites',
                       'vector', 'wxGUI', 'other'):
            node = self.model.AppendNode(parent=self.model.root, label=prefix)
            self.mainNodes[prefix] = node

    def GetModel(self):
        return self.model

    def _emptyTree(self):
        """Remove modules from tree keeping the main structure"""
        for node in self.mainNodes.values():
            for child in reversed(node.children):
                self.model.RemoveNode(child)

    def _expandPrefix(self, c):
        name = {
            'd': 'display',
            'db': 'database',
            'g': 'general',
            'i': 'imagery',
            'm': 'misc',
            'ps': 'postscript',
            'p': 'paint',
            'r': 'raster',
            'r3': 'raster3D',
            's': 'sites',
            'v': 'vector',
            'wx': 'wxGUI',
            '': 'other'
        }

        if c in name:
            return name[c]

        return c

    def Load(self, url, full=True):
        """Load list of extensions"""
        self._emptyTree()

        if full:
            flags = 'g'
        else:
            flags = 'l'
        retcode, ret, msg = RunCommand('g.extension',
                                       read=True,
                                       getErrorMsg=True,
                                       url=url,
                                       flags=flags,
                                       quiet=True)
        if retcode != 0:
            raise GException(_("Unable to load extensions. %s") % msg)

        currentNode = None
        for line in ret.splitlines():
            if full:
                try:
                    key, value = line.split('=', 1)
                except ValueError:
                    key = 'name'
                    value = line

                if key == 'name':
                    try:
                        prefix, name = value.split('.', 1)
                    except ValueError:
                        prefix = ''
                        name = value
                    mainNode = self.mainNodes[self._expandPrefix(prefix)]
                    currentNode = self.model.AppendNode(parent=mainNode,
                                                        label=value)
                    currentNode.data = {'command': value}
                else:
                    if currentNode is not None:
                        currentNode.data[key] = value
            else:
                try:
                    prefix, name = line.strip().split('.', 1)
                except ValueError:
                    prefix = ''
                    name = line.strip()

                if self._expandPrefix(prefix) == prefix:
                    prefix = ''
                module = prefix + '.' + name
                mainNode = self.mainNodes[self._expandPrefix(prefix)]
                currentNode = self.model.AppendNode(parent=mainNode,
                                                    label=module)
                currentNode.data = {
                    'command': module,
                    'keywords': '',
                    'description': ''
                }