Skip to content

Commit

Permalink
added customization flags
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphGL committed Apr 14, 2022
1 parent ed76972 commit 3de6f94
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
30 changes: 25 additions & 5 deletions neosent/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
from neosent.sheetwriter import *
from sys import argv, exit
from sys import exit
import pkg_resources
import click

@click.group(invoke_without_command=True)
@click.option('--set-res', 'res', default=(3840, 2160), show_default=True, type=(int, int),
metavar='<height width>', help="the sheet's height and width") # Needs to be converted into (1280x720)
@click.option('--set-font', 'font', default="data/OpenSansEmoji.ttf",
metavar='<font_path>', help='the font used')
@click.option('--set-bg', 'bg', default=(8, 8, 8), type=(int, int, int),
metavar='<R G B>', help='set the background color') # Needs to be converted to (0, 0, 0) before being consumed
@click.option('--set-fg', 'fg', default=(248, 248, 242), type=(int, int, int),
metavar='<R G B>', help='set the foreground color') # Idem
@click.option('--set-font-size', 'font_size', default=240, show_default=True, type=int,
metavar='<size>', help='set the font size')
@click.argument('filename', type=click.Path(exists=True))
def cli(res, font, bg, fg, font_size, filename):
font_path = ''
if font == 'data/OpenSansEmoji.ttf':
font_path = pkg_resources.resource_filename(__name__, font)
else:
font_path = font

def main():
font_path = pkg_resources.resource_filename(__name__, 'data/OpenSansEmoji.ttf')
sheet = SheetWriter((1280, 720), (0, 0, 0), '/usr/share/fonts/gnu-free/FreeSans.ttf')
sheet = SheetWriter(res, bg, fg, font_path, font_size, filename)
sheet.create_sheet()
print(res, font, bg, fg, filename)
exit(0)


if __name__ == '__main__':
main()
cli()
40 changes: 20 additions & 20 deletions neosent/sheetwriter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Tuple, List, final
from PIL import Image, ImageDraw, ImageFont
from sys import exit, argv
import os
import tempfile
import shutil
Expand All @@ -12,20 +11,20 @@ class SheetWriter:
"""
work_dir = tempfile.mkdtemp('neosent')

def __init__(self, dimension: Tuple[int, int] = (1280, 720), bg_color: Tuple[float, ...] = (0, 0, 0), font: Tuple[str] = 'OpenSansEmoji.ttf'):
def __init__(self, dimension: Tuple[int, int], bg_color: Tuple[int, int, int], fg_color: Tuple[int, int, int],
font: str, font_size: int, filename: str):
self.dimension = dimension
self.bg_color = bg_color
self.font = ImageFont.truetype(font, size=60)
self.fg_color = fg_color
self.font = ImageFont.truetype(font, size=font_size)
self.filename = filename

if len(argv) == 1:
print("Usage: neosent FILE")
try:
self.file = self.parse_file(argv[1])
self.file = self.parse_file(self.filename)
except IndexError:
exit(1)
return 1
except FileNotFoundError:
print(f'No such file or directory: {argv[1]}')
exit(2)
return 2

@staticmethod
def parse_file(file) -> List:
Expand Down Expand Up @@ -53,8 +52,7 @@ def parse_file(file) -> List:

return list_of_pages

@staticmethod
def convert_to_pdf():
def convert_to_pdf(self):
"""
Convert png files to a single pdf file
"""
Expand All @@ -71,7 +69,7 @@ def convert_to_pdf():
# images are listed in decreased order in /tmp
imgs.reverse()
# strip away file extension from presentation file
img.save(f'{argv[1]}.pdf', save_all=True, append_images=imgs[1:])
img.save(f'{self.filename}.pdf', save_all=True, append_images=imgs[1:])

def _draw_text_page(self, name: str, text: str):
img = Image.new('RGB', self.dimension, self.bg_color)
Expand All @@ -98,7 +96,7 @@ def wrap_text(text: str, width: int):

text = wrap_text(text, 22)
page.text((self.dimension[0]/2, (self.dimension[1])/2.2), text,
font=self.font, anchor='mm', align='left', )
font=self.font, fill=self.fg_color, anchor='mm', align='left', )
img.save(name)

def _draw_image_page(self, name: str, img):
Expand All @@ -121,7 +119,7 @@ def _draw_image_page(self, name: str, img):
img.save(name)
except FileNotFoundError:
print(f"Error: {img} was not found.")
exit(1)
return 2

# class end point
def create_sheet(self):
Expand Down Expand Up @@ -153,26 +151,28 @@ def create_sheet(self):
# get user pwd
main_dir = os.getcwd()
# move required files to tmp folder
shutil.copy(f'./{argv[1]}', self.work_dir)
shutil.copy(f'./{self.filename}', self.work_dir)
os.chdir(self.work_dir)
self.convert_to_pdf()

# get generated pdf file's name and move the final pdf to user's dir
pdf_file = f'{argv[1]}.pdf'
final_pdf_file = f'{argv[1]}'.split('.')
pdf_file = f'{self.filename}.pdf'
final_pdf_file = f'{self.filename}'.split('.')
if len(final_pdf_file) != 1:
final_pdf_file = ''.join(final_pdf_file[:-1])
else:
final_pdf_file = ''.join(final_pdf_file)
final_pdf_file = f'{final_pdf_file}.pdf'

# copies and removes instead of shutil.move
# copies and removes instead of shutil.move
# with shutil.move it would error out if folders are in different file systems
try:
shutil.copy(f'{self.work_dir}/{pdf_file}', f'{main_dir}/{final_pdf_file}')
shutil.copy(f'{self.work_dir}/{pdf_file}',
f'{main_dir}/{final_pdf_file}')
except:
os.remove(f'{main_dir}/{final_pdf_file}')
shutil.copy(f'{self.work_dir}/{pdf_file}', f'{main_dir}/{final_pdf_file}')
shutil.copy(f'{self.work_dir}/{pdf_file}',
f'{main_dir}/{final_pdf_file}')
os.remove(f'{self.work_dir}/{pdf_file}')
# cleanup tmp folder
shutil.rmtree(self.work_dir)
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ def read(fname):

setup(
name='NeoSent',
version='0.1',
version='0.8',
url='https://github.com/RaphGL/NeoSent',
author='RaphGL',
author_email='[email protected]',
description='Suckful Sent',
long_description=read('README.md'),
packages=find_namespace_packages(),
install_requires=['pillow'],
install_requires=['pillow', 'click'],
classifiers=[
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
],
entry_points={
'console_scripts': [
'neosent = neosent.main:main',
'neosent = neosent.main:cli',
],
},
include_package_data=True,
Expand Down

0 comments on commit 3de6f94

Please sign in to comment.