-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
196 lines (156 loc) · 5.5 KB
/
db.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import glob
import mmap
import re
import json
from os import path
from settings import *
from utils import isoformat_to_datetime
class DoesNotExist(Exception):
pass
class InvalidKey(Exception):
pass
class DBEntry(object):
def __init__(self, key, storage=STORAGE_DIR):
self.key = key
self.storage = storage
self._metadata = {}
self._text = ''
self.path = path.join(self.storage, self.key)
self.path_metadata = path.join(self.path, 'metadata.json')
self.path_pages = path.join(self.path, 'pages')
@property
def original_document_path(self):
document = path.join(self.path, 'original.pdf')
if not path.exists(document):
raise DoesNotExist()
return document
@property
def metadata(self):
if not self._metadata:
self._metadata = {
'tags': [],
'datetime_scan': None,
'datetime_document': None,
'original_name': '',
'title': ''
}
with open(self.path_metadata, 'r') as f:
self._metadata.update(json.load(f))
return self._metadata
@metadata.setter
def metadata(self, value):
if not self._metadata:
self.metadata
self._metadata = value
@property
def text(self):
if not self._text:
texts = []
for page in glob.glob(path.join(self.path, 'pages/*.txt')):
with open(page, 'r') as f:
texts.append(f.read())
self._text = '\n'.join(texts)
return self._text
@property
def datetime_scan(self):
isoformat = self.metadata.get('datetime_scan', None)
return isoformat_to_datetime(isoformat)
@property
def datetime_document(self):
isoformat = self.metadata.get('datetime_document', None)
return isoformat_to_datetime(isoformat)
@property
def datetime(self):
return (
self.datetime_document
if self.datetime_document else
self.datetime_scan
)
@property
def tags(self):
return self.metadata.get('tags', [])
@property
def color_tags(self):
tags = {}
for tag in self.tags:
tags[tag] = (
TAG_DEFINITIONS.get(tag, {}).get('color', TAG_DEFAULT_COLOR)
)
return tags
def write_metadata(self):
with open(self.path_metadata, 'w') as f:
f.write(json.dumps(self.metadata, indent=4, sort_keys=True))
def match_tags(self):
tags = []
for key, info in TAG_DEFINITIONS.items():
regex = None
matching_algorithm = info.get('matching_algorithm')
matching_term = info.get('match')
if matching_algorithm == MATCH_ANY:
words = matching_term.strip().split(' ')
regex = re.compile(r'(?i)({})'.format(
'|'.join([r'\b{}\b'.format(word) for word in words])
))
if matching_algorithm == MATCH_ALL:
words = matching_term.strip().split(' ')
regex = re.compile(r'(?i){}'.format(
''.join([r'(?=.*\b{}\b)'.format(word) for word in words])
), re.DOTALL)
if matching_algorithm == MATCH_LITERAL:
regex = re.compile(r'(?i)(\b{}\b)'.format(matching_term))
if matching_algorithm == MATCH_REGEX:
regex = re.compile(matching_term, re.DOTALL)
if regex and regex.search(self.text):
tags.append(key)
return tags
def retag(self):
tags = self.match_tags()
self.metadata.update({'tags': tags})
self.write_metadata()
def contains_text(self, text):
b_text = bytes(re.escape(text), 'utf-8')
for page in glob.iglob(path.join(self.path_pages, '*.txt')):
with open(page, 'rb', 0) as f:
with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as s:
if re.search(br'(?i)%b' % b_text, s):
return True
return False
def __str__(self):
return self.metadata.get('original_name')
def __repr__(self):
return self.__str__()
class DB(object):
def __init__(self, storage=STORAGE_DIR):
self.storage = storage
def _get_key_from_path(self, dpath):
result = re.search(
(
'^%s//?(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}_[a-z0-9]{40}).*$' %
re.escape(self.storage)
),
dpath
)
return result.group(1) if result else None
def all(self, sort=True):
entries = [
DBEntry(self._get_key_from_path(path))
for path in glob.glob(path.join(self.storage, '*'))
]
if sort:
return sorted(entries, reverse=True,
key=lambda e: (e.datetime is None, e.datetime))
return entries
def get(self, sha1):
if len(sha1) != 40:
raise InvalidKey()
result = glob.glob(path.join(self.storage, '*{}'.format(sha1)))
if len(result) == 0:
raise DoesNotExist()
return DBEntry(self._get_key_from_path(result[0]))
def filter(self, text='', tags=[]):
entries = self.all()
if tags:
entries = filter(lambda e: set(tags).issubset(e.tags), entries)
if text:
entries = filter(lambda e: e.contains_text(text), entries)
return entries