1 | #! /usr/bin/env python |
---|
2 | # -*- coding: ISO-8859-15 -*- |
---|
3 | # |
---|
4 | # pkpgcounter : a generic Page Description Language parser |
---|
5 | # |
---|
6 | # (c) 2003, 2004, 2005, 2006 Jerome Alet <alet@librelogiciel.com> |
---|
7 | # This program is free software; you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation; either version 2 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # This program is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with this program; if not, write to the Free Software |
---|
19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | # |
---|
21 | # $Id$ |
---|
22 | # |
---|
23 | |
---|
24 | import sys |
---|
25 | |
---|
26 | class PJLParserError(Exception): |
---|
27 | """An exception for PJLParser related stuff.""" |
---|
28 | def __init__(self, message = ""): |
---|
29 | self.message = message |
---|
30 | Exception.__init__(self, message) |
---|
31 | def __repr__(self): |
---|
32 | return self.message |
---|
33 | __str__ = __repr__ |
---|
34 | |
---|
35 | class PJLParser : |
---|
36 | """A parser for PJL documents. |
---|
37 | |
---|
38 | Only extracts the PJL SET variables. Ignore other statements. |
---|
39 | """ |
---|
40 | def __init__(self, pjljob, debug=0) : |
---|
41 | """Initializes PJL Parser.""" |
---|
42 | self.debug = debug |
---|
43 | self.statements = pjljob.replace("\r\n", "\n").split("\n") |
---|
44 | self.default_variables = {} |
---|
45 | self.environment_variables = {} |
---|
46 | self.parsed = 0 |
---|
47 | self.parse() |
---|
48 | |
---|
49 | def __str__(self) : |
---|
50 | """Outputs our variables as a string of text.""" |
---|
51 | if not self.parsed : |
---|
52 | return "" |
---|
53 | mybuffer = [] |
---|
54 | if self.default_variables : |
---|
55 | mybuffer.append("Default variables :") |
---|
56 | for (k, v) in self.default_variables.items() : |
---|
57 | mybuffer.append(" %s : %s" % (k, v)) |
---|
58 | if self.environment_variables : |
---|
59 | mybuffer.append("Environment variables :") |
---|
60 | for (k, v) in self.environment_variables.items() : |
---|
61 | mybuffer.append(" %s : %s" % (k, v)) |
---|
62 | return "\n".join(mybuffer) |
---|
63 | |
---|
64 | def logdebug(self, message) : |
---|
65 | """Logs a debug message if needed.""" |
---|
66 | if self.debug : |
---|
67 | sys.stderr.write("%s\n" % message) |
---|
68 | |
---|
69 | def cleanvars(self) : |
---|
70 | """Cleans the variables dictionnaries.""" |
---|
71 | for dicname in ("default", "environment") : |
---|
72 | varsdic = getattr(self, "%s_variables" % dicname) |
---|
73 | for (k, v) in varsdic.items() : |
---|
74 | if len(v) == 1 : |
---|
75 | varsdic[k] = v[0] |
---|
76 | |
---|
77 | def parse(self) : |
---|
78 | """Parses a PJL job.""" |
---|
79 | for i in range(len(self.statements)) : |
---|
80 | statement = self.statements[i] |
---|
81 | if statement.startswith("@PJL") : |
---|
82 | parts = statement.split() |
---|
83 | nbparts = len(parts) |
---|
84 | if parts[0] == "@PJL" : |
---|
85 | # this is a valid PJL statement, but we don't |
---|
86 | # want to examine all of them... |
---|
87 | if (nbparts > 2) and (parts[1].upper() in ("SET", "DEFAULT")) : |
---|
88 | # this is what we are interested in ! |
---|
89 | try : |
---|
90 | (varname, value) = "".join(parts[2:]).split("=", 1) |
---|
91 | except : |
---|
92 | self.logdebug("Invalid PJL SET statement [%s]" % repr(statement)) |
---|
93 | else : |
---|
94 | # all still looks fine... |
---|
95 | if parts[1].upper() == "DEFAULT" : |
---|
96 | varsdic = self.default_variables |
---|
97 | else : |
---|
98 | varsdic = self.environment_variables |
---|
99 | variable = varsdic.setdefault(varname.upper(), []) |
---|
100 | variable.append(value) |
---|
101 | else : |
---|
102 | self.logdebug("Ignored PJL statement [%s]" % repr(statement)) |
---|
103 | else : |
---|
104 | self.logdebug("Invalid PJL statement [%s]" % repr(statement)) |
---|
105 | else : |
---|
106 | self.logdebug("Invalid PJL statement [%s]" % repr(statement)) |
---|
107 | self.cleanvars() |
---|
108 | self.parsed = 1 |
---|
109 | |
---|
110 | def test() : |
---|
111 | """Test function.""" |
---|
112 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
113 | sys.argv.append("-") |
---|
114 | for arg in sys.argv[1:] : |
---|
115 | if arg == "-" : |
---|
116 | infile = sys.stdin |
---|
117 | mustclose = 0 |
---|
118 | else : |
---|
119 | infile = open(arg, "rb") |
---|
120 | mustclose = 1 |
---|
121 | try : |
---|
122 | parser = PJLParser(infile.read(), debug=1) |
---|
123 | except PJLParserError, msg : |
---|
124 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
125 | sys.stderr.flush() |
---|
126 | if mustclose : |
---|
127 | infile.close() |
---|
128 | print str(parser) |
---|
129 | |
---|
130 | if __name__ == "__main__" : |
---|
131 | test() |
---|