Changeset 3436 for pkpgcounter/trunk/pkpgpdls/pjl.py
- Timestamp:
- 10/06/08 00:22:07 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
pkpgcounter/trunk/pkpgpdls/pjl.py
r3410 r3436 8 8 # the Free Software Foundation, either version 3 of the License, or 9 9 # (at your option) any later version. 10 # 10 # 11 11 # This program is distributed in the hope that it will be useful, 12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 # GNU General Public License for more details. 15 # 15 # 16 16 # You should have received a copy of the GNU General Public License 17 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. … … 35 35 return self.message 36 36 __str__ = __repr__ 37 37 38 38 class PJLParser : 39 39 """A parser for PJL documents. 40 40 41 41 Information extracted for bpl11897.pdf which was 42 42 downloaded from Hewlett-Packard's website. … … 52 52 self.parsed = 0 53 53 self.parse() 54 55 def __str__(self) : 54 55 def __str__(self) : 56 56 """Outputs our variables as a string of text.""" 57 57 if not self.parsed : … … 62 62 for (k, v) in self.default_variables.items() : 63 63 mybuffer.append(" %s : %s" % (k, v)) 64 if self.environment_variables : 64 if self.environment_variables : 65 65 mybuffer.append("Environment variables :") 66 66 for (k, v) in self.environment_variables.items() : 67 67 mybuffer.append(" %s : %s" % (k, v)) 68 return "\n".join(mybuffer) 69 70 def logdebug(self, message) : 68 return "\n".join(mybuffer) 69 70 def logdebug(self, message) : 71 71 """Logs a debug message if needed.""" 72 72 if self.debug : 73 73 sys.stderr.write("%s\n" % message) 74 75 def cleanvars(self) : 74 75 def cleanvars(self) : 76 76 """Cleans the variables dictionnaries.""" 77 77 for dicname in ("default", "environment") : … … 80 80 if len(v) == 1 : 81 81 varsdic[k] = v[0] 82 82 83 83 def parse(self) : 84 84 """Parses a JL job.""" … … 95 95 or ((self.jlmarker == "@EJL") and (parts[1].upper() == "JI"))) : 96 96 # this is what we are interested in ! 97 try : 97 try : 98 98 (varname, value) = "".join(parts[2:]).split("=", 1) # TODO : parse multiple assignments on the same SET/JI statement 99 except : 99 except : 100 100 self.logdebug("Invalid JL SET statement [%s]" % repr(statement)) 101 else : 101 else : 102 102 # all still looks fine... 103 103 if parts[1].upper() == "DEFAULT" : 104 104 varsdic = self.default_variables 105 else : 106 varsdic = self.environment_variables 105 else : 106 varsdic = self.environment_variables 107 107 variable = varsdic.setdefault(varname.upper(), []) 108 108 variable.append(value) … … 121 121 self.parsed = 1 122 122 # self.logdebug("%s\n" % str(self)) 123 123 124 124 class EJLParser(PJLParser) : 125 125 """A parser for EJL (Epson Job Language) documents.""" 126 126 JL = "EJL" 127 128 def test() : 127 128 def test() : 129 129 """Test function.""" 130 130 if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : … … 135 135 infile = sys.stdin 136 136 mustclose = 0 137 else : 137 else : 138 138 if arg.endswith(".ejl") : 139 139 klass = EJLParser … … 142 142 try : 143 143 parser = klass(infile.read(), debug=1) 144 except PJLParserError, msg : 144 except PJLParserError, msg : 145 145 sys.stderr.write("ERROR: %s\n" % msg) 146 146 sys.stderr.flush() 147 if mustclose : 147 if mustclose : 148 148 infile.close() 149 print str(parser) 150 151 if __name__ == "__main__" : 149 print str(parser) 150 151 if __name__ == "__main__" : 152 152 test()