root / pkpgcounter / trunk / pkpgpdls / plain.py @ 3558

Revision 3502, 2.7 kB (checked in by jerome, 15 years ago)

Doesn't check anymore if the current line ends with an EOL marker. From
what I understand from reading Python's documentation, this can only
occur on the very last line of the file anyway. Fixes #44.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# -*- coding: utf-8 -*-
2#
3# pkpgcounter : a generic Page Description Language parser
4#
5# (c) 2003-2009 Jerome Alet <alet@librelogiciel.com>
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
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.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19# $Id$
20#
21
22"""This modules implements a page counter for plain text documents."""
23
24import pdlparser
25import version
26
27class Parser(pdlparser.PDLParser) :
28    """A parser for plain text documents."""
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" -',
31                     ]
32    required = [ "a2ps | enscript", "gs" ]
33    openmode = "rU"
34    format = "plain text"
35    def isValid(self) :
36        """Returns True if data is plain text, else False.
37
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.
41        """
42        lines = self.firstblock.split("\r\n")
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 :
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            linecount += 1
60            if (linecount > pagesize) :
61                pagecount += 1
62                linecount = 0
63            else :
64                cnt = line.count("\f")
65                if cnt :
66                    pagecount += cnt
67                    linecount = 0
68
69        return pagecount + 1    # NB : empty files are catched in isValid()
Note: See TracBrowser for help on using the browser.