11#!/usr/bin/env python
2+ # PYTHON_ARGCOMPLETE_OK
23
4+ import io
35import logging
46import logging .config
7+ from argparse import ArgumentParser , Namespace
58from pathlib import Path
9+
10+ import argcomplete
11+ import compressed_rtf as cr
12+ import extract_msg as em
613from provide_dir import provide_dir
14+
715from 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
1021def setup_logger (directory : Path ) -> logging .Logger :
@@ -30,21 +41,24 @@ def setup_logger(directory: Path) -> logging.Logger:
3041logger = 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" )
0 commit comments