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

Revision 493, 3.0 kB (checked in by jerome, 16 years ago)

Re-optimize disk access by not reopening and re-reading first and last block
more than once.

  • 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, 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
25import pdlparser
26import version
27
28class Parser(pdlparser.PDLParser) :
29    """A parser for plain text documents."""
30    totiffcommands = [ 'enscript --quiet --portrait --no-header --columns 1 --output - "%(infname)s" | gs -sDEVICE=tiff24nc -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r"%(dpi)i" -sOutputFile="%(outfname)s" -',
31                       '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" -',
32                     ] 
33    openmode = "rU"                 
34    def isValid(self) :   
35        """Returns True if data is plain text, else False.
36       
37           It's hard to detect a plain text file, so we just
38           read the first line, and if it doesn't end in CR or LF
39           we consider it's not plain text.
40           
41           TODO : use first and last block's content instead of readline().
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
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 :
58            if line.endswith("\n") :
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            else :       
69                raise pdlparser.PDLParserError, "Unsupported file format. Please send the file to %s" % version.__authoremail__
70        return pagecount + 1    # NB : empty files are catched in isValid()
Note: See TracBrowser for help on using the browser.