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

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

Changed coding statement to please this fucking Emacs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[3410]1# -*- coding: utf-8 -*-
[412]2#
3# pkpgcounter : a generic Page Description Language parser
4#
[564]5# (c) 2003, 2004, 2005, 2006, 2007, 2008 Jerome Alet <alet@librelogiciel.com>
[463]6# This program is free software: you can redistribute it and/or modify
[412]7# it under the terms of the GNU General Public License as published by
[463]8# the Free Software Foundation, either version 3 of the License, or
[412]9# (at your option) any later version.
[463]10#
[412]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
[463]17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
[412]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."""
[492]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" -',
[428]31                     ] 
[527]32    required = [ "a2ps | enscript", "gs" ]
[491]33    openmode = "rU"                 
[555]34    format = "plain text"
[412]35    def isValid(self) :   
[414]36        """Returns True if data is plain text, else False.
37       
[494]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.
[414]41        """   
[522]42        lines = self.firstblock.split("\r\n")
[494]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 :
[414]48            return True
49        else :   
50            return False
[412]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 :
[491]59            if line.endswith("\n") :
[414]60                linecount += 1   
[422]61                if (linecount > pagesize) :
[414]62                    pagecount += 1
63                    linecount = 0
[422]64                else :   
65                    cnt = line.count("\f")
66                    if cnt :
67                        pagecount += cnt
68                        linecount = 0
[414]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.