root / pkpgcounter / trunk / pkpgpdls / ooo.py @ 252

Revision 252, 2.9 kB (checked in by jerome, 19 years ago)

Added a PJL parsing module to extract SET and DEFAULT statements.
Improved general readability.
Fixed some minor problems thanks to pychecker.

  • 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 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
24import sys
25import zipfile
26
27import pdlparser
28
29class Parser(pdlparser.PDLParser) :
30    """A parser for OpenOffice.org documents."""
31    def isValid(self) :       
32        """Returns 1 if data is DVI, else 0."""
33        if self.firstblock[:2] == "PK" :
34            try :
35                self.archive = zipfile.ZipFile(self.infile)
36                self.contentxml = self.archive.read("content.xml")
37                self.metaxml = self.archive.read("meta.xml")
38            except :   
39                return 0
40            else :
41                self.logdebug("DEBUG: Input file is in the OpenOffice.org format.")
42                return 1
43        else :   
44            return 0
45           
46    def getJobSize(self) :
47        """Counts pages in an OpenOffice.org document.
48       
49           Algorithm by Jerome Alet.
50        """
51        pagecount = 0
52        try :
53            # First try with Text documents
54            index = self.metaxml.index("meta:page-count=")
55            pagecount = int(self.metaxml[index:].split('"')[1])
56        except :
57            # Now try with Impress documents
58            pagecount = self.contentxml.count("<draw:page ")
59            if not pagecount :
60                # Probably a Spreadsheet document
61                raise pdlparser.PDLParserError, "OpenOffice.org's spreadsheet documents are not yet supported."
62        return pagecount
63       
64def test() :       
65    """Test function."""
66    if (len(sys.argv) < 2) or ((not sys.stdin.isatty()) and ("-" not in sys.argv[1:])) :
67        sys.argv.append("-")
68    totalsize = 0   
69    for arg in sys.argv[1:] :
70        if arg == "-" :
71            infile = sys.stdin
72            mustclose = 0
73        else :   
74            infile = open(arg, "rb")
75            mustclose = 1
76        try :
77            parser = Parser(infile, debug=1)
78            totalsize += parser.getJobSize()
79        except pdlparser.PDLParserError, msg :   
80            sys.stderr.write("ERROR: %s\n" % msg)
81            sys.stderr.flush()
82        if mustclose :   
83            infile.close()
84    print "%s" % totalsize
85   
86if __name__ == "__main__" :   
87    test()
Note: See TracBrowser for help on using the browser.