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
|
# -*- coding: utf-8 -*-
"""Console script for msg_parser."""
import os.path
import sys
from argparse import Action
from argparse import ArgumentParser
from argparse import ArgumentTypeError
from argparse import FileType
from pprint import pprint
from msg_parser import MsOxMessage
class FullPaths(Action):
"""Expand user- and relative-paths"""
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, os.path.abspath(os.path.expanduser(values)))
def is_dir(dir_name):
"""Checks if a path is an actual directory"""
if not os.path.isdir(dir_name):
msg = "{0} is not a directory".format(dir_name)
raise ArgumentTypeError(msg)
else:
return dir_name
def create_parser(args):
parser = ArgumentParser(description="Microsoft Message Parser")
parser.add_argument(
"-i",
"--input",
dest="input_file",
required=True,
help="msg file path",
metavar="FILE",
type=FileType(),
)
parser.add_argument(
"-j",
"--json",
help="output parsed msg as json to console",
dest="json_output",
action="store_true",
)
parser.add_argument(
"-e",
"--eml",
help="provide email file path to save as eml file.",
dest="eml_file",
action=FullPaths,
type=is_dir,
)
return parser.parse_args(args)
def main():
args = create_parser(sys.argv[1:])
input_file = args.input_file
json_output = args.json_output
if json_output:
ms_msg = MsOxMessage(input_file)
pprint(ms_msg.get_message_as_json())
eml_file = args.eml_file
if eml_file:
ms_msg = MsOxMessage(input_file)
ms_msg.save_email_file(eml_file)
if __name__ == "__main__":
sys.exit(main())
|