root / pkpgcounter / trunk / pkpgpdls / pjl.py @ 472

Revision 472, 5.6 kB (checked in by jerome, 17 years ago)

Improved the detection of ESC/P2.
Made the PJL parser also handle EJL (in a limited way).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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/EJL 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
28import sys
29
30class 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       
39class 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    JL = "PJL"
46    def __init__(self, pjljob, debug=0) :
47        """Initializes JL Parser."""
48        self.debug = debug
49        self.jlmarker = "@%s" % self.JL
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 JL job."""
86        for i in range(len(self.statements)) :
87            statement = self.statements[i]
88            if statement.startswith(self.jlmarker) :
89                parts = statement.split()
90                nbparts = len(parts)
91                if parts[0] == self.jlmarker :
92                    # this is a valid JL statement, but we don't
93                    # want to examine all of them...
94                    if (nbparts > 2) \
95                         and ((parts[1].upper() in ("SET", "DEFAULT")) \
96                                  or ((self.jlmarker == "@EJL") and (parts[1].upper() == "JI"))) :
97                        # this is what we are interested in !
98                        try :   
99                            (varname, value) = "".join(parts[2:]).split("=", 1) # TODO : parse multiple assignments on the same SET/JI statement
100                        except :   
101                            self.logdebug("Invalid JL SET statement [%s]" % repr(statement))
102                        else :   
103                            # all still looks fine...
104                            if parts[1].upper() == "DEFAULT" :
105                                varsdic = self.default_variables
106                            else :   
107                                varsdic = self.environment_variables 
108                            variable = varsdic.setdefault(varname.upper(), [])
109                            variable.append(value)
110                    else :
111                        self.logdebug("Ignored JL statement [%s]" % repr(statement))
112                        self.logdebug(parts)
113                else :
114                    self.logdebug("Invalid JL statement [%s]" % repr(statement))
115            else :
116                self.logdebug("Invalid JL statement [%s]" % repr(statement))
117        self.cleanvars()
118        self.parsed = 1
119       
120class EJLParser(PJLParser) :
121    """A parser for EJL (Epson Job Language) documents."""
122    JL = "EJL"
123       
124def test() :       
125    """Test function."""
126    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
127        sys.argv.append("-")
128    for arg in sys.argv[1:] :
129        klass = PJLParser
130        if arg == "-" :
131            infile = sys.stdin
132            mustclose = 0
133        else :   
134            if arg.endswith(".ejl") :
135                klass = EJLParser
136            infile = open(arg, "rb")
137            mustclose = 1
138        try :
139            parser = klass(infile.read(), debug=1)
140        except PJLParserError, msg :   
141            sys.stderr.write("ERROR: %s\n" % msg)
142            sys.stderr.flush()
143        if mustclose :   
144            infile.close()
145        print str(parser)           
146   
147if __name__ == "__main__" :   
148    test()
Note: See TracBrowser for help on using the browser.