File: _Readers.py

package info (click to toggle)
naev 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 386,084 kB
  • sloc: ansic: 93,149; xml: 87,292; python: 2,347; sh: 904; makefile: 654; lisp: 162; awk: 4
file content (92 lines) | stat: -rw-r--r-- 2,755 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=80:

# Trying to get an etree.
try:
    from lxml import etree as ET
except ImportError:
    try:
        import xml.etree.ElementTree as ET
    except ImportError:
        print("Failed to import ElementTree from any known place")
        exit()


class readers:
    """
    Master object with verbosity support
    """

    _verbose=None
    xmlData=None

    def __init__(self, xmlFiles, verbose=False):
        """
        Set verbosity level and load xml file.
        The file must be valid.
        """
        self._verbose=verbose
        self.nameList=list()

        if self.xmlData is None:
            if type(xmlFiles) is not type(list()):
                self.xmlData = ET.parse(xmlFiles)
                return
            self.xmlData = list()
            for xfile in xmlFiles:
                self.xmlData.append(ET.parse( xfile ))

    def get_unused(self):
        """
        this method return a list containing all the unused stuff.
        use it wisely, it'll regenerate all the list. Could be quite time
        consuming with a lot of data.
        """
        tmp = self.nameList
        try:
            for name in self.used:
                # XXX TODO WARNING : this is a 'fix'. But I need to understand
                # why that name could not be in nameList
                if name in tmp:
                    tmp.remove(name)
            for name in self.unknown:
                if name in tmp:
                    tmp.remove(name)
        except ValueError:
            print('ValueError: %s not in %s used list' %
                        (name, self._componentName))
            print('Debug info:')
            print("\ntmp: %s" % (tmp))
            print("\nNameList: %s" % (self.nameList))
            print("\nUsed list: %s" % (self.used))

            raise
        return tmp

    def show_unused(self):
        if len(self.unknown) > 0:
            print('\nProbably not used %s name:' % self._componentName)
            print('\n'.join(self.unknown))
        tmp = self.get_unused()
        if len(tmp) > 0:
            print('\nUnused %s name:' % self._componentName)
            print('\n'.join(tmp))

    def set_unknown(self, name):
        """
        Set the name in an unknown status.
        Meaning it is probably used by a lua script, but this tool can't be
        certain (i.e. name used in a variable).
        """
        if name in self.nameList and name not in self.used:
            if name not in self.unknown:
                self.v("SET ''%s`` as UNKNOWN" % name)
                self.unknown.append(name)

    def v(self, msg):
        if self._verbose:
            print(msg)
        else:
            pass