[412] | 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 | """This modules implements a page counter for plain text documents.""" |
---|
| 25 | |
---|
| 26 | import sys |
---|
| 27 | import os |
---|
| 28 | import mmap |
---|
| 29 | |
---|
| 30 | import pdlparser |
---|
| 31 | import version |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | class Parser(pdlparser.PDLParser) : |
---|
| 35 | """A parser for plain text documents.""" |
---|
| 36 | def isValid(self) : |
---|
| 37 | """Returns True if data is plain text, else False.""" |
---|
| 38 | return True |
---|
| 39 | |
---|
| 40 | def getJobSize(self) : |
---|
| 41 | """Counts pages in a plain text document.""" |
---|
| 42 | pagesize = 66 # TODO : Does this vary wrt the default page size ? |
---|
| 43 | # TODO : /etc/papersize and /etc/paper.config |
---|
| 44 | pagecount = 0 |
---|
| 45 | linecount = 0 |
---|
| 46 | for line in self.infile : |
---|
| 47 | linecount += 1 |
---|
| 48 | if (linecount > pagesize) \ |
---|
| 49 | or (line.find(chr(12)) != -1) : |
---|
| 50 | pagecount += 1 |
---|
| 51 | linecount = 0 |
---|
| 52 | return pagecount + 1 |
---|
| 53 | |
---|
| 54 | def test() : |
---|
| 55 | """Test function.""" |
---|
| 56 | if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) : |
---|
| 57 | sys.argv.append("-") |
---|
| 58 | totalsize = 0 |
---|
| 59 | for arg in sys.argv[1:] : |
---|
| 60 | if arg == "-" : |
---|
| 61 | infile = sys.stdin |
---|
| 62 | mustclose = 0 |
---|
| 63 | else : |
---|
| 64 | infile = open(arg, "rb") |
---|
| 65 | mustclose = 1 |
---|
| 66 | try : |
---|
| 67 | parser = Parser(infile, debug=1) |
---|
| 68 | totalsize += parser.getJobSize() |
---|
| 69 | except pdlparser.PDLParserError, msg : |
---|
| 70 | sys.stderr.write("ERROR: %s\n" % msg) |
---|
| 71 | sys.stderr.flush() |
---|
| 72 | if mustclose : |
---|
| 73 | infile.close() |
---|
| 74 | print "%s" % totalsize |
---|
| 75 | |
---|
| 76 | if __name__ == "__main__" : |
---|
| 77 | test() |
---|