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