Added scripts

This commit is contained in:
Luna 2024-03-15 13:39:55 +02:00
parent 9d924d93ad
commit a4f45e8bd6
3 changed files with 217 additions and 0 deletions

35
gen_raw.py Normal file
View File

@ -0,0 +1,35 @@
import json
from datetime import datetime
def generate_raw_text(trip_report):
raw_text += f"# Metadata\n"
raw_text += f"Chemical: {trip_report['chemical']}\n"
raw_text += f"Tester: {trip_report['tester']}\n"
raw_text += f"Dosage: {trip_report['dosage']}\n"
raw_text += f"Tolerance: {trip_report['tolerance']}\n"
raw_text += f"Cross-Tolerance: {trip_report['cross_tolerance']}\n\n"
raw_text += "# Report\n"
for entry in trip_report['entries']:
raw_text += f"{entry['timestamp']} ({entry['elapsed_time']}) {entry['notes']}\n"
raw_text += f"\nReport generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
raw_text += f"Submit date: {trip_report['submit_date']}\n"
return raw_text
def save_raw_text(raw_text, destination_path):
with open(destination_path, 'w') as txt_file:
txt_file.write(raw_text)
print(f"Raw text saved to {destination_path}.")
if __name__ == "__main__":
json_path = input("Enter path to JSON report: ")
destination_path = input("Enter path for destination TXT file: ")
with open(json_path, 'r') as json_file:
trip_report = json.load(json_file)
raw_text = generate_raw_text(trip_report)
save_raw_text(raw_text, destination_path)

135
gen_static.py Normal file
View File

@ -0,0 +1,135 @@
import json
from datetime import datetime
def generate_metadata_table(trip_report):
metadata_table = """
<table>
<thead>
<tr>
<th>Attribute</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chemical</td>
<td>{}</td>
</tr>
<tr>
<td>Tester</td>
<td>{}</td>
</tr>
<tr>
<td>Dosage</td>
<td>{}</td>
</tr>
<tr>
<td>Tolerance</td>
<td>{}</td>
</tr>
<tr>
<td>Cross-Tolerance</td>
<td>{}</td>
</tr>
</tbody>
</table>
""".format(trip_report['chemical'], trip_report['tester'], trip_report['dosage'], trip_report['tolerance'], trip_report['cross_tolerance'])
return metadata_table
def generate_trip_report_table(trip_report):
trip_report_table = """
<table>
<thead>
<tr>
<th>Timestamp</th>
<th>Elapsed Time</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
"""
for entry in trip_report['entries']:
trip_report_table += """
<tr>
<td>{}</td>
<td>{}</td>
<td>{}</td>
</tr>
""".format(entry['timestamp'], entry['elapsed_time'], entry['notes'])
trip_report_table += """
</tbody>
</table>
"""
return trip_report_table
def generate_html_report(trip_report):
metadata_table = generate_metadata_table(trip_report)
trip_report_table = generate_trip_report_table(trip_report)
submit_date = trip_report.get('submit_date', 'N/A')
generation_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
html_report = """
<html>
<head>
<style>
body {{
font-family: Arial, sans-serif;
color: #000;
background-color: #fff;
}}
table {{
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}}
th, td {{
border: 1px solid #000;
padding: 8px;
text-align: left;
}}
th {{
background-color: #000;
color: #fff;
}}
tr:nth-child(even) {{
background-color: #f2f2f2;
}}
</style>
</head>
<body>
<h3>Metadata</h3>
{}
<h3>Report</h3>
{}
<p>Report submitted at: {}</p>
<p>Static HTML generated at: {}</p>
</body>
</html>
""".format(metadata_table, trip_report_table, submit_date, generation_date)
return html_report
def save_html_report(html_report, destination_path):
with open(destination_path, 'w') as html_file:
html_file.write(html_report)
print("HTML report saved to {}.".format(destination_path))
if __name__ == "__main__":
json_path = input("Enter path to JSON report: ")
destination_path = input("Enter path for destination HTML file: ")
with open(json_path, 'r') as json_file:
trip_report = json.load(json_file)
html_report = generate_html_report(trip_report)
save_html_report(html_report, destination_path)

47
report.py Normal file
View File

@ -0,0 +1,47 @@
import json
from datetime import datetime
def generate_trip_report():
report = {}
report['name'] = input("Enter name of report: ")
report['chemical'] = input("Chemical: ")
report['tester'] = input("Tester: ")
report['dosage'] = input("Dosage (example: 5mg): ")
report['tolerance'] = input("Tolerance: ")
report['cross_tolerance'] = input("Cross-Tolerance: ")
report['entries'] = []
start_time = None # Variable to store the start time
while True:
entry = {}
current_time = datetime.now().strftime("%I:%M:%S %p")
if start_time is None:
start_time = datetime.now() # Set start time when submitting the first timestamp
elapsed_time = datetime.now() - start_time
formatted_elapsed_time = "{:02}:{:02}:{:02}".format(elapsed_time.seconds // 3600, (elapsed_time.seconds // 60) % 60, elapsed_time.seconds % 60)
entry['timestamp'] = current_time
entry['elapsed_time'] = formatted_elapsed_time
entry['notes'] = input(f"Notes (example: Took {report['dosage']} {report['chemical']}): ")
report['entries'].append(entry)
stop_input = input("Write 'save' to stop: ")
if stop_input.lower() == 'save':
report['submit_date'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
break
return report
def save_report_to_json(report):
with open(f"{report['name']}.json", 'w') as json_file:
json.dump(report, json_file, indent=4)
if __name__ == "__main__":
trip_report = generate_trip_report()
save_report_to_json(trip_report)
print(f"Trip report saved to {trip_report['name']}.json.")