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, 2007 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 3 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, see <http://www.gnu.org/licenses/>. |
---|
19 | # |
---|
20 | # $Id$ |
---|
21 | # |
---|
22 | |
---|
23 | """This modules implements a really minimalist PJL parser.""" |
---|
24 | |
---|
25 | # NOTES : QTY= is the number of collated copies for a job. |
---|
26 | # NOTES : COPIES= is the number of uncollated copies for each page of a job |
---|
27 | |
---|
28 | import sys |
---|
29 | |
---|
30 | class PJLParserError(Exception): |
---|
31 | """An exception for PJLParser related stuff.""" |
---|
32 | def __init__(self, message = ""): |
---|
33 | self.message = message |
---|
34 | Exception.__init__(self, message) |
---|
35 | def __repr__(self): |
---|
36 | return self.message |
---|
37 | __str__ = __repr__ |
---|
38 | |
---|
39 | class PJLParser : |
---|
40 | """A parser for PJL documents. |
---|
41 | |
---|
42 | Information extracted for bpl11897.pdf which was |
---|
43 | downloaded from Hewlett-Packard's website. |
---|
44 | |
---|
45 | Only extracts the PJL SET variables. Ignore other statements. |
---|
46 | """ |
---|
47 | def __init__(self, pjljob, debug=0) : |
---|
48 | """Initializes PJL Parser.""" |
---|
49 | self.debug = debug |
---|
50 | self.statements = pjljob.replace("\r\n", "\n").split("\n") |
---|
51 | self.default_variables = {} |
---|
52 | self.environment_variables = {} |
---|
53 | self.parsed = 0 |
---|
54 | self.parse() |
---|
55 | |
---|
56 | def __str__(self) : |
---|
57 | """Outputs our variables as a string of text.""" |
---|
58 | if not self.parsed : |
---|
59 | return "" |
---|
60 | mybuffer = [] |
---|
61 | if self.default_variables : |
---|
62 | mybuffer.append("Default variables :") |
---|
63 | for (k, v) in self.default_variables.items() : |
---|
64 | mybuffer.append(" %s : %s" % (k, v)) |
---|
65 | if self.environment_variables : |
---|
66 | mybuffer.append("Environment variables :") |
---|
67 | for (k, v) in self.environment_variables.items() : |
---|
68 | mybuffer.append(" %s : %s" % (k, v)) |
---|
69 | return "\n".join(mybuffer) |
---|
70 | |
---|
71 | def logdebug(self, message) : |
---|
72 | """Logs a debug message if needed.""" |
---|
73 | if self.debug : |
---|
74 | sys.stderr.write("%s\n" % message) |
---|
75 | |
---|
76 | def cleanvars(self) : |
---|
77 | """Cleans the variables dictionnaries.""" |
---|
78 | for dicname in ("default", "environment") : |
---|
79 | varsdic = getattr(self, "%s_variables" % dicname) |
---|
80 | for (k, v) in varsdic.items() : |
---|
81 | if len(v) == 1 : |
---|
82 | varsdic[k] = v[0] |
---|
83 | |
---|
84 | def parse(self) : |
---|
85 | """Parses a PJL job.""" |
---|
86 | for i in range(len(self.statements)) : |
---|
87 | statement = self.statements[i] |
---|
88 | if statement.startswith("@PJL") : |
---|
89 | parts = statement.split() |
---|
90 | nbparts = len(parts) |
---|
91 | if parts[0] == "@PJL" : |
---|
92 | # this is a valid PJL statement, but we don't |
---|
93 | # want to examine all of them... |
---|
94 | if (nbparts > 2) and (parts[1].upper() in ("SET", "DEFAULT")) : |
---|
95 | # this is what we are interested in ! |
---|
96 | try : |
---|
97 | (varname, value) = "".join(parts[2:]).split("=", 1) |
---|
98 | except : |
---|
99 | self.logdebug("Invalid PJL SET statement [%s]" % repr(statement)) |
---|
100 | else : |
---|
101 | # all still looks fine... |
---|
102 | if parts[1].upper() == "DEFAULT" : |
---|
103 | varsdic = self.default_variables |
---|
104 | else : |
---|
105 | varsdic = self.environment_variables |
---|
106 | variable = varsdic.setdefault(varname.upper(), []) |
---|
107 | variable.append(value) |
---|
108 | else : |
---|
109 | self.logdebug("Ignored PJL statement [%s]" % repr(statement)) |
---|
110 | else : |
---|
111 | self.logdebug("Invalid PJL statement [%s]" % repr(statement)) |
---|
112 | else : |
---|
113 | self.logdebug("Invalid PJL statement [%s]" % repr(statement)) |
---|
114 | self.cleanvars() |
---|
115 | self.parsed = 1 |
---|
116 | |
---|
117 | def test() : |
---|
118 | """Test function.""" |
---|
119 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
120 | sys.argv.append("-") |
---|
121 | for arg in sys.argv[1:] : |
---|
122 | if arg == "-" : |
---|
123 | infile = sys.stdin |
---|
124 | mustclose = 0 |
---|
125 | else : |
---|
126 | infile = open(arg, "rb") |
---|
127 | mustclose = 1 |
---|
128 | try : |
---|
129 | parser = PJLParser(infile.read(), debug=1) |
---|
130 | except PJLParserError, msg : |
---|
131 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
132 | sys.stderr.flush() |
---|
133 | if mustclose : |
---|
134 | infile.close() |
---|
135 | print str(parser) |
---|
136 | |
---|
137 | if __name__ == "__main__" : |
---|
138 | test() |
---|