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

Revision 564, 3.0 kB (checked in by jerome, 17 years ago)

Changed copyright years.
Removed unnecessary shebang lines.
Changed default encoding to UTF-8 from ISO-8859-15 (only
ascii is used anyway).

  • 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, 2004, 2005, 2006, 2007, 2008 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            if line.endswith("\n") :
60                linecount += 1   
61                if (linecount > pagesize) :
62                    pagecount += 1
63                    linecount = 0
64                else :   
65                    cnt = line.count("\f")
66                    if cnt :
67                        pagecount += cnt
68                        linecount = 0
69            else :       
70                raise pdlparser.PDLParserError, "Unsupported file format. Please send the file to %s" % version.__authoremail__
71        return pagecount + 1    # NB : empty files are catched in isValid()
Note: See TracBrowser for help on using the browser.