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

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

Changed copyright years.

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