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 page counter for plain text documents.""" |
---|
24 | |
---|
25 | import sys |
---|
26 | import os |
---|
27 | |
---|
28 | import pdlparser |
---|
29 | import version |
---|
30 | |
---|
31 | |
---|
32 | class Parser(pdlparser.PDLParser) : |
---|
33 | """A parser for plain text documents.""" |
---|
34 | totiffcommands = [ 'enscript --quiet --portrait --no-header --columns 1 --output - | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(fname)s" -', |
---|
35 | 'a2ps --borders 0 --quiet --portrait --no-header --columns 1 --output - | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(fname)s" -', |
---|
36 | ] |
---|
37 | def isValid(self) : |
---|
38 | """Returns True if data is plain text, else False. |
---|
39 | |
---|
40 | It's hard to detect a plain text file, so we just |
---|
41 | read the first line, and if it doesn't end in CR or LF |
---|
42 | we consider it's not plain text. |
---|
43 | """ |
---|
44 | line = self.infile.readline() |
---|
45 | self.infile.seek(0) |
---|
46 | if line.endswith("\n") or line.endswith("\r") : |
---|
47 | self.logdebug("DEBUG: Input file seems to be in the plain text format.") |
---|
48 | return True |
---|
49 | else : |
---|
50 | return False |
---|
51 | |
---|
52 | def getJobSize(self) : |
---|
53 | """Counts pages in a plain text document.""" |
---|
54 | pagesize = 66 # TODO : Does this vary wrt the default page size ? |
---|
55 | # TODO : /etc/papersize and /etc/paper.config |
---|
56 | pagecount = 0 |
---|
57 | linecount = 0 |
---|
58 | for line in self.infile : |
---|
59 | if line.endswith("\n") or line.endswith("\r") : |
---|
60 | linecount += 1 |
---|
61 | if (linecount > pagesize) : |
---|
62 | pagecount += 1 |
---|
63 | linecount = 0 |
---|
64 | else : |
---|
65 | cnt = line.count("\f") |
---|
66 | if cnt : |
---|
67 | pagecount += cnt |
---|
68 | linecount = 0 |
---|
69 | else : |
---|
70 | raise pdlparser.PDLParserError, "Unsupported file format. Please send the file to %s" % version.__authoremail__ |
---|
71 | return pagecount + 1 # NB : empty files are catched in isValid() |
---|
72 | |
---|
73 | if __name__ == "__main__" : |
---|
74 | pdlparser.test(Parser) |
---|