File: txtparser.py

package info (click to toggle)
dblatex 0.3.12py3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,684 kB
  • sloc: xml: 102,889; python: 8,259; makefile: 119; sh: 48
file content (108 lines) | stat: -rw-r--r-- 3,032 bytes parent folder | download | duplicates (4)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#
# DbTex configuration parser. Maybe we could use or extend ConfigParser.
#
import os
import re
from io import open

#
# Functions used by the config parsers and by the dbtex command parser
#
def texinputs_parse(strpath, basedir=None):
    """
    Transform the TEXINPUTS string to absolute normalized paths,
    but keep intact the '//' suffix if any. The absolute paths are
    computed from current one or from <basedir> when specified.
    """
    paths = []
    for p in strpath.split(os.pathsep):
        if not(os.path.isabs(p)):
            if not(basedir):
                d = os.path.realpath(p)
            else:
                d = os.path.normpath(os.path.join(basedir, p))
        else:
            d = os.path.normpath(p)
        if p.endswith("//"):
            d += "//"
        paths.append(d)
    return paths

def texstyle_parse(texstyle):
    sty = os.path.basename(texstyle)
    dir = os.path.dirname(texstyle)
    if sty.endswith(".sty"):
        path = os.path.realpath(dir)
        sty = sty[:-4]
        if not(os.path.isfile(texstyle)):
            raise ValueError("Latex style '%s' not found" % texstyle)
    elif (dir):
        raise ValueError("Invalid latex style path: missing .sty")
    else:
        path = ""
    return ("latex.style=%s" % sty, path)



class OptMap:
    def __init__(self, option):
        self.option = option

    def format(self, dir, value):
        return ["%s=%s" % (self.option, value)]

class PathMap(OptMap):
    def format(self, dir, value):
        if not(os.path.isabs(value)):
            value = os.path.normpath(os.path.join(dir, value))
        return OptMap.format(self, dir, value)

class TexMap(OptMap):
    def format(self, dir, value):
        paths = texinputs_parse(value, basedir=dir)
        return OptMap.format(self, dir, ":".join(paths))

class NoneMap(OptMap):
    def format(self, dir, value):
        return value.split()


class TextConfig:
    conf_mapping = {
        'TexInputs' : TexMap('--texinputs'),
        #'PdfInputs' : OptMap('--pdfinputs'),
        'TexPost'   : PathMap('--texpost'),
        'FigPath'   : PathMap('--fig-path'),
        'XslParam'  : PathMap('--xsl-user'),
        'TexStyle'  : OptMap('--texstyle'),
        'Options'   : NoneMap('')
    }

    def __init__(self):
        self._options = []
        self.reparam = re.compile(r"^\s*([^:=\s]+)\s*:\s*(.*)")

    def options(self):
        return self._options

    def fromfile(self, file):
        dir = os.path.dirname(os.path.realpath(file))
        f = open(file, "rt")

        for line in f:
            # Remove the comment
            line = line.split("#")[0]
            m = self.reparam.match(line)
            if not(m):
                continue
            key = m.group(1)
            value = m.group(2).strip()
            if key not in self.conf_mapping:
                continue
            o = self.conf_mapping[key]

            # The paths can be relative to the config file
            self._options += o.format(dir, value)

        f.close()