From 9d6c63b369a4bb77ab071546101272416dde3c1a Mon Sep 17 00:00:00 2001 From: Mihai Siia Date: Sun, 21 Apr 2024 19:44:29 -0700 Subject: [PATCH] add report generator script --- report_generator/example.json | 3 ++ .../report_generator/classes/Assignment.py | 12 +++++ report_generator/report_generator/main.py | 50 +++++++++++++++++++ .../report_generator/util/parse_json.py | 16 ++++++ report_generator/setup.py | 11 ++++ 5 files changed, 92 insertions(+) create mode 100644 report_generator/example.json create mode 100644 report_generator/report_generator/classes/Assignment.py create mode 100644 report_generator/report_generator/main.py create mode 100644 report_generator/report_generator/util/parse_json.py create mode 100644 report_generator/setup.py diff --git a/report_generator/example.json b/report_generator/example.json new file mode 100644 index 0000000..3d99184 --- /dev/null +++ b/report_generator/example.json @@ -0,0 +1,3 @@ +{ + "content": "\n Vivamus aliquam, enim in tincidunt varius, quam libero eleifend ante, ac elementum enim tortor quis est. Aliquam non ornare ligula. Cras eu faucibus ante. Nullam ut auctor est. Vestibulum et dui facilisis, fermentum elit ullamcorper, fringilla magna. Mauris metus mi, rhoncus vitae dui eu, tempor mattis quam. Suspendisse sed sapien vitae leo iaculis mattis. Integer bibendum venenatis ipsum. Donec sed elit sit amet turpis eleifend tincidunt. Etiam consequat lobortis lacus.\n\n Proin quis blandit enim, eget scelerisque magna. In porttitor aliquet magna ut mollis. Etiam eu tempus odio, eget varius augue. Morbi sit amet purus id orci elementum ultrices ut at nunc. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Fusce eget lacus a risus congue blandit nec sit amet enim. Sed vitae laoreet sapien, gravida semper massa. Etiam malesuada ante placerat, blandit nulla id, imperdiet lorem. Integer ut molestie est. In hac habitasse platea dictumst. In dapibus viverra odio in cursus. Aenean eu ex eu enim ultrices feugiat. Fusce consectetur, arcu at tempor mollis, nulla sem tincidunt risus, ut congue purus augue sit amet tellus. Donec tincidunt tellus ante, porta faucibus justo accumsan vel. Sed venenatis bibendum ipsum et dapibus. In scelerisque et magna et pretium.\n\n Quisque ac odio eget justo imperdiet ultricies. Mauris id diam vel purus mollis blandit quis ut odio. Etiam lacinia vestibulum tellus, at convallis augue viverra eu. Nulla pellentesque id urna vel pretium. In venenatis eros enim, vitae porttitor augue pulvinar non. Donec eros tortor, pharetra non libero in, suscipit dictum arcu. Ut ac tristique lacus. Nam non quam sed mi placerat imperdiet eu vitae nulla. Cras et ex sapien. Aliquam non turpis porttitor, posuere est vitae, porta tellus. Suspendisse at sapien sed magna ultrices euismod eget at risus. Pellentesque vulputate sem massa, vel tristique ligula commodo mattis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum sit amet convallis sapien, at mollis felis. Integer at sem felis. Sed orci ante, egestas nec posuere vitae, venenatis non lorem.\n\n Donec vel sem velit. Vivamus pellentesque bibendum justo, quis pharetra dui fringilla sed. Cras eu leo quis turpis finibus lobortis et a lectus. Maecenas iaculis facilisis dolor, ac porta ante convallis id. Ut sed lorem dignissim, gravida lacus et, consectetur turpis. Cras sed varius ante, nec faucibus ligula. Cras id elementum ligula. Etiam ac orci vitae mi dapibus maximus. Suspendisse a justo sed dolor mollis dignissim. Nunc elit massa, vulputate quis nulla vitae, finibus sodales justo. Vestibulum dignissim placerat nulla, lobortis rhoncus purus rutrum sed." +} \ No newline at end of file diff --git a/report_generator/report_generator/classes/Assignment.py b/report_generator/report_generator/classes/Assignment.py new file mode 100644 index 0000000..cbe2214 --- /dev/null +++ b/report_generator/report_generator/classes/Assignment.py @@ -0,0 +1,12 @@ + + +class Assignment(): + def __init__(self, + student_name='Test Student', + assignment_name='Test Assignment', + class_name='Test Class', + professor_name='Test Professor'): + self.student_name = student_name + self.assignment_name = assignment_name + self.class_name = class_name + self.professor_name = professor_name \ No newline at end of file diff --git a/report_generator/report_generator/main.py b/report_generator/report_generator/main.py new file mode 100644 index 0000000..ccfd56d --- /dev/null +++ b/report_generator/report_generator/main.py @@ -0,0 +1,50 @@ +''' +call with "python3 report_generator/main.py assemble_pdf path/to/json" +''' +from dotenv import load_dotenv +from pylatex import Document, Section, LineBreak, NewPage, PageStyle, Package +from pylatex.utils import bold, NoEscape + +import os +from os import join, dirname +import sys + +from util.parse_json import json_to_dict +# from classes.Assignment import Assignment + +def format_doc(): + + geometry_options = { + 'margin': '0.5in' + } + + doc = Document(geometry_options=geometry_options) + doc.change_document_style('empty') + return doc + +def assemble_pdf(data: dict): + print(data) + if (isinstance(data, dict) is False): + data = json_to_dict(data) + + # load path to pdflatex + dotenv_path = join(dirname(__file__), '.env') + load_dotenv(dotenv_path) + PDFLATEX_PATH = os.environ.get('PDFLATEX_PATH') + + doc = format_doc() + + keys = list(data.keys()) + with doc.create(Section(NoEscape(r'\centerline{' + keys[0] + '}'), numbering=False)): + for key in keys: + doc.append(data.get(key)) + + doc.append(NewPage()) + + doc.generate_pdf('out/out', clean_tex=False, compiler=PDFLATEX_PATH) + # doc = format_doc() + # doc.append(NewPage()) + # doc.generate_pdf('out/out', clean_tex=False, compiler='pdflatex', silent=False) + +if __name__ == '__main__': + globals()[sys.argv[1]](sys.argv[2]) \ No newline at end of file diff --git a/report_generator/report_generator/util/parse_json.py b/report_generator/report_generator/util/parse_json.py new file mode 100644 index 0000000..a127844 --- /dev/null +++ b/report_generator/report_generator/util/parse_json.py @@ -0,0 +1,16 @@ +import json +import sys + +def json_to_dict(x: str) -> dict : + data : dict + if (x[-5::] == '.json'): # checking if file extension is json + f = open(x, 'r') + data = json.load(f) + f.close() + else: + data = json.load(x) + + return data + +if __name__ == '__main__': + globals()[sys.argv[1]](sys.argv[2]) \ No newline at end of file diff --git a/report_generator/setup.py b/report_generator/setup.py new file mode 100644 index 0000000..da35d6a --- /dev/null +++ b/report_generator/setup.py @@ -0,0 +1,11 @@ +from setuptools import find_packages, setup + +setup( + name='report_generator', + packages=find_packages(include=['report_generator']), + version='0.0.1', + description='Generate pdfs from graded student assignments', + author='Mihai Siia', + install_requires=['pylatex==1.4.2', + 'python-dotenv==1.0.1'], +) \ No newline at end of file