[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) : |
---|
[414] | 37 | """Returns True if data is plain text, else False. |
---|
| 38 | |
---|
| 39 | It's hard to detect a plain text file, so we just |
---|
| 40 | read the first line, and if it doesn't end in CR or LF |
---|
| 41 | we consider it's not plain text. |
---|
| 42 | """ |
---|
| 43 | line = self.infile.readline() |
---|
| 44 | self.infile.seek(0) |
---|
| 45 | if line.endswith("\n") or line.endswith("\r") : |
---|
| 46 | self.logdebug("DEBUG: Input file seems to be in the plain text format.") |
---|
| 47 | return True |
---|
| 48 | else : |
---|
| 49 | return False |
---|
[412] | 50 | |
---|
| 51 | def getJobSize(self) : |
---|
| 52 | """Counts pages in a plain text document.""" |
---|
| 53 | pagesize = 66 # TODO : Does this vary wrt the default page size ? |
---|
| 54 | # TODO : /etc/papersize and /etc/paper.config |
---|
| 55 | pagecount = 0 |
---|
| 56 | linecount = 0 |
---|
| 57 | for line in self.infile : |
---|
[414] | 58 | if line.endswith("\n") or line.endswith("\r") : |
---|
| 59 | linecount += 1 |
---|
| 60 | if (linecount > pagesize) \ |
---|
| 61 | or (line.find("\f") != -1) : |
---|
| 62 | pagecount += 1 |
---|
| 63 | linecount = 0 |
---|
| 64 | else : |
---|
| 65 | raise pdlparser.PDLParserError, "Unsupported file format. Please send the file to %s" % version.__authoremail__ |
---|
| 66 | return pagecount + 1 # NB : empty files are catched in isValid() |
---|
[412] | 67 | |
---|
| 68 | if __name__ == "__main__" : |
---|
[414] | 69 | pdlparser.test(Parser) |
---|