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

Revision 418, 3.5 kB (checked in by jerome, 18 years ago)

Added plain text parser with support for both page counting and ink coverage.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
26import sys
27import os
28
29import pdlparser
30import version
31
32
33class Parser(pdlparser.PDLParser) :
34    """A parser for plain text documents."""
35    enscript = 'enscript --quiet --portrait --no-header --columns 1 --output - | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(fname)s" -'
36    a2ps = 'a2ps --borders 0 --quiet --portrait --no-header --columns 1 --output - | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r%(dpi)i -sOutputFile="%(fname)s" -'
37    def __init__(self, infile, debug=0, firstblock=None, lastblock=None) :
38        """Initialize the plain text parser."""
39        pdlparser.PDLParser.__init__(self, infile, debug, firstblock, lastblock)
40       
41        # Tries to detect is a plain text to PostScript command line tool is available
42        # and use the first one we find.
43        paths = os.environ.get("PATH", "/usr/local/bin:/usr/bin:/bin").split(os.pathsep)
44        for cmd in ("enscript", "a2ps") :
45            for path in paths :
46                if os.path.exists(os.path.join(path, cmd)) :
47                    self.totiffcommand = getattr(self, cmd)
48                    return
49       
50    def isValid(self) :   
51        """Returns True if data is plain text, else False.
52       
53           It's hard to detect a plain text file, so we just
54           read the first line, and if it doesn't end in CR or LF
55           we consider it's not plain text.
56        """   
57        line = self.infile.readline()
58        self.infile.seek(0)
59        if line.endswith("\n") or line.endswith("\r") :
60            self.logdebug("DEBUG: Input file seems to be in the plain text format.")
61            return True
62        else :   
63            return False
64           
65    def getJobSize(self) :
66        """Counts pages in a plain text document."""
67        pagesize = 66   # TODO : Does this vary wrt the default page size ?
68                        # TODO : /etc/papersize and /etc/paper.config
69        pagecount = 0
70        linecount = 0
71        for line in self.infile :
72            if line.endswith("\n") or line.endswith("\r") :
73                linecount += 1   
74                if (linecount > pagesize) \
75                   or (line.find("\f") != -1) :
76                    pagecount += 1
77                    linecount = 0
78            else :       
79                raise pdlparser.PDLParserError, "Unsupported file format. Please send the file to %s" % version.__authoremail__
80        return pagecount + 1    # NB : empty files are catched in isValid()
81       
82if __name__ == "__main__" :   
83    pdlparser.test(Parser)
Note: See TracBrowser for help on using the browser.