[3410] | 1 | # -*- coding: utf-8 -*- |
---|
[412] | 2 | # |
---|
| 3 | # pkpgcounter : a generic Page Description Language parser |
---|
| 4 | # |
---|
[3578] | 5 | # (c) 2003-2019 Jerome Alet <alet@librelogiciel.com> |
---|
[463] | 6 | # This program is free software: you can redistribute it and/or modify |
---|
[412] | 7 | # it under the terms of the GNU General Public License as published by |
---|
[463] | 8 | # the Free Software Foundation, either version 3 of the License, or |
---|
[412] | 9 | # (at your option) any later version. |
---|
[3436] | 10 | # |
---|
[412] | 11 | # This program is distributed in the hope that it will be useful, |
---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | # GNU General Public License for more details. |
---|
[3436] | 15 | # |
---|
[412] | 16 | # You should have received a copy of the GNU General Public License |
---|
[463] | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
[412] | 18 | # |
---|
| 19 | # $Id$ |
---|
| 20 | # |
---|
| 21 | |
---|
| 22 | """This modules implements a page counter for plain text documents.""" |
---|
| 23 | |
---|
| 24 | import pdlparser |
---|
| 25 | import version |
---|
| 26 | |
---|
| 27 | class Parser(pdlparser.PDLParser) : |
---|
| 28 | """A parser for plain text documents.""" |
---|
[492] | 29 | totiffcommands = [ 'enscript --quiet --portrait --no-header --columns 1 --output - "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r"%(dpi)i" -sOutputFile="%(outfname)s" -', |
---|
| 30 | 'a2ps --borders 0 --quiet --portrait --no-header --columns 1 --output - "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r"%(dpi)i" -sOutputFile="%(outfname)s" -', |
---|
[3436] | 31 | ] |
---|
[527] | 32 | required = [ "a2ps | enscript", "gs" ] |
---|
[3436] | 33 | openmode = "rU" |
---|
[555] | 34 | format = "plain text" |
---|
[3436] | 35 | def isValid(self) : |
---|
[414] | 36 | """Returns True if data is plain text, else False. |
---|
[3436] | 37 | |
---|
[494] | 38 | It's hard to detect a plain text file, so we just try to |
---|
| 39 | extract lines from the first block (sufficiently large). |
---|
| 40 | If it's impossible to find one we consider it's not plain text. |
---|
[3436] | 41 | """ |
---|
[522] | 42 | lines = self.firstblock.split("\r\n") |
---|
[494] | 43 | if len(lines) == 1 : |
---|
| 44 | lines = lines[0].split("\r") |
---|
| 45 | if len(lines) == 1 : |
---|
| 46 | lines = lines[0].split("\n") |
---|
| 47 | if len(lines) > 1 : |
---|
[414] | 48 | return True |
---|
[3436] | 49 | else : |
---|
[414] | 50 | return False |
---|
[3436] | 51 | |
---|
[412] | 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 : |
---|
[3502] | 59 | linecount += 1 |
---|
| 60 | if (linecount > pagesize) : |
---|
| 61 | pagecount += 1 |
---|
| 62 | linecount = 0 |
---|
[3436] | 63 | else : |
---|
[3502] | 64 | cnt = line.count("\f") |
---|
| 65 | if cnt : |
---|
| 66 | pagecount += cnt |
---|
| 67 | linecount = 0 |
---|
| 68 | |
---|
[414] | 69 | return pagecount + 1 # NB : empty files are catched in isValid() |
---|