Skip to content

Commit 1764517

Browse files
author
Sven Siegmund
committed
rework cli
1 parent c862e10 commit 1764517

File tree

16 files changed

+183
-546
lines changed

16 files changed

+183
-546
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ __pycache__/
33
*.py[cod]
44
*$py.class
55

6+
# PowerShell garbage
7+
Out-Null
8+
69
# Vim files
710
*~
811
*.swp

src/rtfparse/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#!/usr/bin/env python
22

33

4-
from rtfparse.cli import main
5-
64
# Towncrier needs version
75
from rtfparse.__about__ import __version__
6+
from rtfparse.cli import main
87

98
if __name__ == "__main__":
109
main()

src/rtfparse/__main__.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/rtfparse/cli.py

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
#!/usr/bin/env python
2+
# PYTHON_ARGCOMPLETE_OK
23

4+
import io
35
import logging
46
import logging.config
7+
from argparse import ArgumentParser, Namespace
58
from pathlib import Path
9+
10+
import argcomplete
11+
import compressed_rtf as cr
12+
import extract_msg as em
613
from provide_dir import provide_dir
14+
715
from rtfparse import logging_conf
16+
from rtfparse.__about__ import __version__
17+
from rtfparse.parser import Rtf_Parser
18+
from rtfparse.renderers import de_encapsulate_html
819

920

1021
def setup_logger(directory: Path) -> logging.Logger:
@@ -30,21 +41,24 @@ def setup_logger(directory: Path) -> logging.Logger:
3041
logger = setup_logger(Path.home() / "rtfparse")
3142

3243

33-
def argument_parser() -> argparse.ArgumentParser:
44+
def argument_parser() -> ArgumentParser:
3445
"""
3546
Creates an argument parser for command line arguments
3647
"""
37-
parser = argparse.ArgumentParser(description="RTF parser")
48+
parser = ArgumentParser(description="RTF parser", prog="rtfparse")
3849
parser.add_argument(
39-
"-v", "--version", action="store_true", help="print out rtfparse version and exit"
50+
"-v",
51+
"--version",
52+
action="version",
53+
version=" ".join(("%(prog)s", __version__)),
54+
help="print out rtfparse version and exit",
4055
)
41-
parser.add_argument("--autoconfig", action="store_true", help="Configure rtfparse automatically")
4256
parser.add_argument(
43-
"-f", "--file", action="store", metavar="PATH", type=Path, help="path to the rtf file"
57+
"-r", "--rtf-file", action="store", metavar="PATH", type=Path, help="path to the rtf file"
4458
)
4559
parser.add_argument(
4660
"-m",
47-
"--msg",
61+
"--msg-file",
4862
action="store",
4963
metavar="PATH",
5064
type=Path,
@@ -56,18 +70,55 @@ def argument_parser() -> argparse.ArgumentParser:
5670
parser.add_argument(
5771
"-i", "--embed-img", action="store_true", help="Embed images from email to HTML"
5872
)
73+
parser.add_argument(
74+
"-o", "--output-file", metavar="PATH", type=Path, help="path to the desired output file"
75+
)
76+
parser.add_argument(
77+
"-a",
78+
"--attachments-dir",
79+
metavar="PATH",
80+
type=Path,
81+
help="path to directory where to save email attachments",
82+
)
5983
return parser
6084

6185

62-
def run(cli_args: argparse.Namespace) -> None:
63-
logger.info("Program runs")
86+
def de_encapsulate(rp: Rtf_Parser, target_file: Path) -> None:
87+
renderer = de_encapsulate_html.De_encapsulate_HTML()
88+
with open(target_file, mode="w", encoding="utf-8") as htmlfile:
89+
logger.info(f"Rendering the encapsulated HTML")
90+
renderer.render(rp.parsed, htmlfile)
91+
logger.info(f"Encapsulated HTML rendered")
92+
93+
94+
def run(cli_args: Namespace) -> None:
95+
if cli_args.rtf_file and cli_args.rtf_file.exists():
96+
with open(cli_args.rtf_file, mode="rb") as rtf_file:
97+
rp = Rtf_Parser(rtf_file=rtf_file)
98+
rp.parse_file()
99+
elif cli_args.msg_file:
100+
msg = em.openMsg(f"{cli_args.msg_file}")
101+
if cli_args.attachments_dir:
102+
for attachment in msg.attachments:
103+
with open(
104+
cli_args.attachments_dir / f"{attachment.longFilename}", mode="wb"
105+
) as att_file:
106+
att_file.write(attachment.data)
107+
decompressed_rtf = cr.decompress(msg.compressedRtf)
108+
with open(cli_args.msg_file.with_suffix(".rtf"), mode="wb") as email_rtf:
109+
email_rtf.write(decompressed_rtf)
110+
with io.BytesIO(decompressed_rtf) as rtf_file:
111+
rp = Rtf_Parser(rtf_file=rtf_file)
112+
rp.parse_file()
113+
if cli_args.de_encapsulate_html and cli_args.output_file:
114+
de_encapsulate(rp, cli_args.output_file.with_suffix(".html"))
64115

65116

66-
def main(version) -> None:
117+
def main() -> None:
67118
"""
68119
Entry point for any component start from the commmand line
69120
"""
70-
logger.debug(f"{utils.program_name} started")
121+
logger.debug(f"rtfparse started")
71122
parser = argument_parser()
72123
argcomplete.autocomplete(parser)
73124
cli_args = parser.parse_args()
@@ -76,4 +127,4 @@ def main(version) -> None:
76127
run(cli_args)
77128
except Exception as err:
78129
logger.exception(f"Uncaught exception {repr(err)} occurred.")
79-
logger.debug(f"{utils.program_name} ended")
130+
logger.debug(f"rtfparse ended")

src/rtfparse/config_loader.py

Lines changed: 0 additions & 220 deletions
This file was deleted.

0 commit comments

Comments
 (0)