11from typing import Tuple , List , final
22from PIL import Image , ImageDraw , ImageFont
3- from sys import exit , argv
43import os
54import tempfile
65import shutil
@@ -12,20 +11,20 @@ class SheetWriter:
1211 """
1312 work_dir = tempfile .mkdtemp ('neosent' )
1413
15- def __init__ (self , dimension : Tuple [int , int ] = (1280 , 720 ), bg_color : Tuple [float , ...] = (0 , 0 , 0 ), font : Tuple [str ] = 'OpenSansEmoji.ttf' ):
14+ def __init__ (self , dimension : Tuple [int , int ], bg_color : Tuple [int , int , int ], fg_color : Tuple [int , int , int ],
15+ font : str , font_size : int , filename : str ):
1616 self .dimension = dimension
1717 self .bg_color = bg_color
18- self .font = ImageFont .truetype (font , size = 60 )
18+ self .fg_color = fg_color
19+ self .font = ImageFont .truetype (font , size = font_size )
20+ self .filename = filename
1921
20- if len (argv ) == 1 :
21- print ("Usage: neosent FILE" )
2222 try :
23- self .file = self .parse_file (argv [ 1 ] )
23+ self .file = self .parse_file (self . filename )
2424 except IndexError :
25- exit ( 1 )
25+ return 1
2626 except FileNotFoundError :
27- print (f'No such file or directory: { argv [1 ]} ' )
28- exit (2 )
27+ return 2
2928
3029 @staticmethod
3130 def parse_file (file ) -> List :
@@ -53,8 +52,7 @@ def parse_file(file) -> List:
5352
5453 return list_of_pages
5554
56- @staticmethod
57- def convert_to_pdf ():
55+ def convert_to_pdf (self ):
5856 """
5957 Convert png files to a single pdf file
6058 """
@@ -71,7 +69,7 @@ def convert_to_pdf():
7169 # images are listed in decreased order in /tmp
7270 imgs .reverse ()
7371 # strip away file extension from presentation file
74- img .save (f'{ argv [ 1 ] } .pdf' , save_all = True , append_images = imgs [1 :])
72+ img .save (f'{ self . filename } .pdf' , save_all = True , append_images = imgs [1 :])
7573
7674 def _draw_text_page (self , name : str , text : str ):
7775 img = Image .new ('RGB' , self .dimension , self .bg_color )
@@ -98,7 +96,7 @@ def wrap_text(text: str, width: int):
9896
9997 text = wrap_text (text , 22 )
10098 page .text ((self .dimension [0 ]/ 2 , (self .dimension [1 ])/ 2.2 ), text ,
101- font = self .font , anchor = 'mm' , align = 'left' , )
99+ font = self .font , fill = self . fg_color , anchor = 'mm' , align = 'left' , )
102100 img .save (name )
103101
104102 def _draw_image_page (self , name : str , img ):
@@ -121,7 +119,7 @@ def _draw_image_page(self, name: str, img):
121119 img .save (name )
122120 except FileNotFoundError :
123121 print (f"Error: { img } was not found." )
124- exit ( 1 )
122+ return 2
125123
126124 # class end point
127125 def create_sheet (self ):
@@ -153,26 +151,28 @@ def create_sheet(self):
153151 # get user pwd
154152 main_dir = os .getcwd ()
155153 # move required files to tmp folder
156- shutil .copy (f'./{ argv [ 1 ] } ' , self .work_dir )
154+ shutil .copy (f'./{ self . filename } ' , self .work_dir )
157155 os .chdir (self .work_dir )
158156 self .convert_to_pdf ()
159157
160158 # get generated pdf file's name and move the final pdf to user's dir
161- pdf_file = f'{ argv [ 1 ] } .pdf'
162- final_pdf_file = f'{ argv [ 1 ] } ' .split ('.' )
159+ pdf_file = f'{ self . filename } .pdf'
160+ final_pdf_file = f'{ self . filename } ' .split ('.' )
163161 if len (final_pdf_file ) != 1 :
164162 final_pdf_file = '' .join (final_pdf_file [:- 1 ])
165163 else :
166164 final_pdf_file = '' .join (final_pdf_file )
167165 final_pdf_file = f'{ final_pdf_file } .pdf'
168166
169- # copies and removes instead of shutil.move
167+ # copies and removes instead of shutil.move
170168 # with shutil.move it would error out if folders are in different file systems
171169 try :
172- shutil .copy (f'{ self .work_dir } /{ pdf_file } ' , f'{ main_dir } /{ final_pdf_file } ' )
170+ shutil .copy (f'{ self .work_dir } /{ pdf_file } ' ,
171+ f'{ main_dir } /{ final_pdf_file } ' )
173172 except :
174173 os .remove (f'{ main_dir } /{ final_pdf_file } ' )
175- shutil .copy (f'{ self .work_dir } /{ pdf_file } ' , f'{ main_dir } /{ final_pdf_file } ' )
174+ shutil .copy (f'{ self .work_dir } /{ pdf_file } ' ,
175+ f'{ main_dir } /{ final_pdf_file } ' )
176176 os .remove (f'{ self .work_dir } /{ pdf_file } ' )
177177 # cleanup tmp folder
178178 shutil .rmtree (self .work_dir )
0 commit comments