root / pkpgcounter / trunk / pkpgpdls / dvi.py @ 356

Revision 356, 3.2 kB (checked in by jerome, 18 years ago)

Added missing docstring, thanks to pylint

  • Property svn:keywords set to Author Id Date 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 DVI documents."""
25
26import sys
27import os
28import mmap
29from struct import unpack
30
31import pdlparser
32
33class Parser(pdlparser.PDLParser) :
34    """A parser for DVI documents."""
35    def isValid(self) :       
36        """Returns 1 if data is DVI, else 0."""
37        try :
38            if (ord(self.firstblock[0]) == 0xf7) and (ord(self.lastblock[-1]) == 0xdf) :
39                self.logdebug("DEBUG: Input file is in the DVI format.")
40                return 1
41            else :   
42                return 0
43        except IndexError :         
44            return 0
45           
46    def getJobSize(self) :
47        """Counts pages in a DVI document.
48       
49           Algorithm by Jerome Alet.
50           
51           The documentation used for this was :
52         
53           http://www.math.umd.edu/~asnowden/comp-cont/dvi.html
54        """
55        infileno = self.infile.fileno()
56        minfile = mmap.mmap(infileno, os.fstat(infileno)[6], prot=mmap.PROT_READ, flags=mmap.MAP_SHARED)
57        pagecount = 0
58        pos = -1
59        eofchar = chr(0xdf)
60        postchar = chr(0xf8)
61        try :
62            while minfile[pos] == eofchar :
63                pos -= 1
64            idbyte = minfile[pos]   
65            if idbyte != minfile[1] :
66                raise IndexError, "Invalid DVI file."
67            pos = unpack(">I", minfile[pos - 4:pos])[0]
68            if minfile[pos] != postchar :
69                raise IndexError, "Invalid DVI file."
70            pagecount = unpack(">H", minfile[pos + 27: pos + 29])[0]
71        except IndexError : # EOF ?
72            pass
73        minfile.close() # reached EOF
74        return pagecount
75       
76def test() :       
77    """Test function."""
78    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
79        sys.argv.append("-")
80    totalsize = 0   
81    for arg in sys.argv[1:] :
82        if arg == "-" :
83            infile = sys.stdin
84            mustclose = 0
85        else :   
86            infile = open(arg, "rb")
87            mustclose = 1
88        try :
89            parser = Parser(infile, debug=1)
90            totalsize += parser.getJobSize()
91        except pdlparser.PDLParserError, msg :   
92            sys.stderr.write("ERROR: %s\n" % msg)
93            sys.stderr.flush()
94        if mustclose :   
95            infile.close()
96    print "%s" % totalsize
97   
98if __name__ == "__main__" :   
99    test()
Note: See TracBrowser for help on using the browser.