summaryrefslogtreecommitdiffstats
path: root/xojgrep.py
blob: 9fb51634c86c7e96bb9b4f203f3c8ffeb08b002b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/python3
# vim: set fileencoding=utf-8 ts=4 sts=4 sw=4 tw=80 expandtab :

# Copyright (C) 2012 Florian Bruhin <xojgrep@the-compiler.org>

# xojgrep is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# xojgrep is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with xojgrep.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import argparse
import gzip
import lxml.etree
import pyPdf

def main():
    options = parseargs()


def istextfile(filename, blocksize=512):
    """ Uses heuristics to guess whether the given file is text or binary,
    by reading a single block of bytes from the file.
    If more than 30% of the chars in the block are non-text, or there are NUL
    ('\x00') bytes in the block, assume this is a binary file.

    Based on http://s.cmpl.cc/n
    """
    _text_characters = (
        b''.join(bytes((i,)) for i in range(32, 127)) + # Printable ASCII chars
        b'\n\r\t\f\b' # Control chars commonly used in text
    )
    with open(filename, 'rb') as f:
        block = f.read(blocksize)
    if b'\x00' in block:
        # Files with null bytes are binary
        return False
    elif not block:
        # An empty file is considered a valid text file
        return True
    # Use translate's 'deletechars' argument to efficiently remove all
    # occurrences of _text_characters from the block
    nontext = block.translate(None,  _text_characters)
    return float(len(nontext)) / len(block) <= 0.30


def getfilelist(path):
    filelist = []  
    for root, dirnames, filenames in os.walk(path):
        filelist.append(os.path.join(root, filename))
    return filelist


def parseargs():
    """ Parses the command-line arguments """
    parser = argparse.ArgumentParser(
        description='Grep xoj, text and pdf files from one tool.',
        epilog='FIXME',
    )
    parser.add_argument('file', nargs='+', default=None, help='File name')
    options = parser.parse_args()
    return options


class XOJFile:
    def __init__(self, filename):
        with gzip.open(filename) as f:
            # FIXME check for invalid files
            try:
                self.tree = lxml.etree.parse(f)
            except lxml.etree.XMLSyntaxError:
                self.tree = None
                raise # FIXME output error

    def getcontent(self):
        content = []
        # TODO check if that works with a xournal file without texts
        pagecount = len(self.tree.xpath('/xournal/page'))
        for pn in range(1, pagecount + 1):
            pagecontent = []
            texts = self.tree.xpath('/xournal/page[{}]//text'.format(pn))
            for text in texts:
                pagecontent.append(text.text)
            if pagecontent:
                content.append((pn, pagecontent))
        return content


class PDFFile:
    def __init__(self, filename):
        self.pdffile = open(filename, 'rb')
        self.doc = pyPdf.PdfFileReader(self.pdffile)

    def __del__(self):
        self.pdffile.close()

    def getcontent(self):
        pagecount = self.doc.numPages
        content = []
        for pn in range(1, pagecount + 1):
            page = self.doc.getPage(pn)
            text = page.extractText()
            if text:
                content.append((pn, [text]))
        return content


class TXTFile:
    def __init__(self, filename):
        self.txtfile = open(filename, 'r')

    def __del__(self):
        self.txtfile.close()

    def getcontent(self):
        content = []
        for (ln, line) in enumerate(self.txtfile, 1):
            content.append((ln, [line.rstrip('\n')]))
        return content


if __name__ == '__main__':
    sys.exit(main())