/
subcommands.py
272 lines (216 loc) · 7.5 KB
/
subcommands.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# pylint: disable=invalid-name,broad-except
"""
The functions in this module are entry points for ratom sub-commands, e.g. `ratom entities ...`
"""
import json
import logging
from datetime import datetime
from operator import itemgetter
from pathlib import Path
from typing import Optional
import enlighten
from packaging.version import parse
from sqlalchemy import func
from libratom.cli.utils import MockContext, install_spacy_model, list_spacy_models
from libratom.lib.core import (
export_messages_from_file,
get_set_of_files,
get_spacy_models,
load_spacy_model,
)
from libratom.lib.database import db_init, db_session
from libratom.lib.entities import extract_entities
from libratom.lib.report import generate_report, scan_files, store_configuration_in_db
from libratom.models import FileReport
logger = logging.getLogger(__name__)
OUTPUT_FILENAME_TEMPLATE = "{}_{}_{}.sqlite3"
def entities(
out: Path,
spacy_model_name: str,
jobs: Optional[int],
src: Path,
include_message_contents: bool = False,
progress: bool = False,
) -> int:
"""
Click sub command function called by `ratom entities`
"""
# Make or fake our progress bar context objects
if progress:
progress_bars = enlighten.get_manager()
progress_bar_context = progress_bars.counter
else:
progress_bar_context = MockContext
# Resolve output file based on src parameter
if out.is_dir():
out = out / OUTPUT_FILENAME_TEMPLATE.format(
src.name,
"entities",
datetime.now()
.isoformat(timespec="seconds")
.translate(str.maketrans({"-": "", ":": ""})),
)
# Make DB file's parents if needed
out.parent.mkdir(parents=True, exist_ok=True)
# DB setup
Session = db_init(out)
# Get set of PST files from the source
files = get_set_of_files(src)
if not files:
logger.info(f"No PST file found in {src}")
# Compute and store file information
with progress_bar_context(
total=len(files),
desc="Initial file scan",
unit="files",
color="green",
leave=False,
) as file_bar, db_session(Session) as session:
status = scan_files(
files, session, jobs=jobs, progress_callback=file_bar.update
)
if status == 1:
logger.warning("Aborting")
return status
# Get spaCy model
logger.info(f"Loading spaCy model: {spacy_model_name}")
spacy_model, spacy_model_version = load_spacy_model(spacy_model_name)
if not spacy_model:
return 1
# Try to see if we're using a stale model version
try:
latest_version = get_spacy_models()[spacy_model_name][0]
if parse(latest_version) > parse(spacy_model_version):
logger.info(
f"Model {spacy_model_name} {spacy_model_version} will be used, but {latest_version} is available"
)
except Exception as exc:
logger.debug(exc, exc_info=True)
# Get messages and extract entities
with db_session(Session) as session:
# Record configuration info
store_configuration_in_db(
session, str(src), jobs, spacy_model_name, spacy_model_version
)
# Get total message count
msg_count = session.query(func.sum(FileReport.msg_count)).scalar()
# Get list of good files
good_files = [
Path(file.path)
for file in session.query(FileReport).filter(FileReport.error.is_(None))
]
with progress_bar_context(
total=msg_count, desc="Processing messages", unit="msg", color="blue"
) as processing_msg_bar, progress_bar_context(
total=msg_count,
desc="Generating message reports",
unit="msg",
color="green",
) as reporting_msg_bar:
status = extract_entities(
files=good_files,
session=session,
spacy_model=spacy_model,
include_message_contents=include_message_contents,
jobs=jobs,
processing_progress_callback=processing_msg_bar.update,
reporting_progress_callback=reporting_msg_bar.update,
)
logger.info("All done")
return status
def report(
out: Path,
jobs: Optional[int],
src: Path,
include_message_contents: bool = False,
progress: bool = False,
) -> int:
"""
Click sub command function called by `ratom report`
"""
# Make or fake our progress bar context objects
if progress:
progress_bars = enlighten.get_manager()
progress_bar_context = progress_bars.counter
else:
progress_bar_context = MockContext
# Resolve output file based on src parameter
if out.is_dir():
out = out / OUTPUT_FILENAME_TEMPLATE.format(
src.name,
"report",
datetime.now()
.isoformat(timespec="seconds")
.translate(str.maketrans({"-": "", ":": ""})),
)
# Make DB file's parents if needed
out.parent.mkdir(parents=True, exist_ok=True)
# DB setup
Session = db_init(out)
# Get set of PST files from the source
files = get_set_of_files(src)
if not files:
logger.info(f"No PST file found in {src}")
# Compute and store file information
with progress_bar_context(
total=len(files),
desc="Initial file scan",
unit="files",
color="green",
leave=False,
) as file_bar, db_session(Session) as session:
status = scan_files(
files, session, jobs=jobs, progress_callback=file_bar.update
)
if status == 1:
logger.warning("Aborting")
return status
# Get messages and extract entities
with db_session(Session) as session:
# Record configuration info
store_configuration_in_db(session, str(src), jobs)
# Get total message count
msg_count = session.query(func.sum(FileReport.msg_count)).scalar()
# Get list of good files
good_files = [
Path(file.path)
for file in session.query(FileReport).filter(FileReport.error.is_(None))
]
with progress_bar_context(
total=msg_count, desc="Processing messages", unit="msg", color="green"
) as msg_bar:
status = generate_report(
files=good_files,
session=session,
include_message_contents=include_message_contents,
progress_callback=msg_bar.update,
)
logger.info("All done")
return status
def model(_list: bool, install: str, upgrade: str, version: str) -> int:
"""
Click sub command function called by `ratom model`
"""
if install:
return install_spacy_model(install, version, False)
if upgrade:
return install_spacy_model(upgrade, None, True)
# List by default
return list_spacy_models()
def emldump(out: Path, location: Path, src: Path) -> int:
"""
Generate .eml files from pst/mbox files.
"""
# Extract inputs from json file
with src.open() as f:
input_elements = json.load(f)
# Process each file / id_list
for input_element in input_elements:
filename, id_list = itemgetter("filename", "id_list")(input_element)
try:
export_messages_from_file(location / filename, id_list, out)
except Exception as exc:
logger.warning(
f"Skipping {location / filename}, reason: {exc}", exc_info=True,
)
return 0