From a6a4c75577dfd5e6c788b2980f542f8d566f1106 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 2 Nov 2024 21:11:22 +0000 Subject: [PATCH] Code is now functional but untidy and in need of refactoring and commenting --- .gitignore | 1 + src/gitea.py | 36 +++++++++++++----------------------- src/main.py | 17 +++++++++++++++++ src/pis_file.py | 22 ++++++++++++++++++++++ 4 files changed, 53 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 2734678..c61d75f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ auto_matched.txt organised_for_processing.txt validated.txt output.txt +output_file.txt output *.sh diff --git a/src/gitea.py b/src/gitea.py index a0d4fff..d29cd36 100644 --- a/src/gitea.py +++ b/src/gitea.py @@ -1,5 +1,6 @@ import requests import base64 +import json from urllib.parse import urljoin, urlencode class GiteaConnector: @@ -28,11 +29,9 @@ class GiteaConnector: if response.status_code == 200: try: data = response.json() # Parse JSON response - print(data) if isinstance(data, list): # Confirm it's a list # Extract 'title' from each item in the list existing_titles = [item['title'] for item in data if 'title' in item] - print(existing_titles) return existing_titles else: print("Unexpected response format: Expected a list.") @@ -82,36 +81,27 @@ class GiteaConnector: ## Create new branch in the repo def create_branch(self, branch_name): BASE_BRANCH = "main" - base_branch_url = urljoin(self.repo_url, f"branch/{BASE_BRANCH}") - branch_response = requests.get(base_branch_url, headers=self.header) - if branch_response.status_code == 200: - base_sha = branch_response.json()['commit']['id'] - - create_branch_url = urljoin(self.repo_url, "git/refs") - branch_data = { - "ref": f"refs/heads/{branch_name}", - "sha": base_sha, - } - - create_branch_response = requests.post(create_branch_url, headers=self.header, data=json.dumps(branch_data)) - if create_branch_response.status_code == 201: - print(f"Branch {branch_name} created successfully") - return True - else: - print(f"Failed to create branch: ", branch_name, create_branch_response.status_code, create_branch_response.text) - return False + create_branch_url = urljoin(self.repo_url, "branches") + branch_data = { + "new_branch_name": branch_name, + "old_branch_name": BASE_BRANCH, + } + create_branch_response = requests.post(create_branch_url, headers=self.header, data=json.dumps(branch_data)) + if create_branch_response.status_code == 201: + print(f"Branch {branch_name} created successfully") + return True else: - print("Failed to retreive branch SHA: ", branch_response.status_code, branch_response.text) + print(f"Failed to create branch: ", branch_name, create_branch_response.status_code, create_branch_response.text) return False ## Create a new file in the repo def create_pis_file(self, branch, filename, file_content): file_path = f"pis/{filename}" - encoded_content = base64.encode(file_content.encode('utf-8')) + encoded_content = base64.b64encode(file_content.encode('utf-8')).decode('utf-8') - create_file_url = url_multijoin(self.repo_url, f'contents/{file_path}') + create_file_url = urljoin(self.repo_url, f'contents/{file_path}.yaml') file_data = { "content": encoded_content, diff --git a/src/main.py b/src/main.py index bf8ca1d..f455e1a 100644 --- a/src/main.py +++ b/src/main.py @@ -7,6 +7,8 @@ import json import parse_pdf import train_detail import validate +import pis_file +from gitea import GiteaConnector # List all PDF files in the given directory def list_pdf_files(directory): @@ -120,6 +122,18 @@ def main(): validated = validate.check_and_validate_against_owlboard(auto_matched) + output_file = pis_file.create_new_pis_file(validated) + + ## Create new branch and post output_file + gitea_client = GiteaConnector("https://git.fjla.uk/api/v1/repos/owlboard/data/", "fred.boniface", os.getenv("GITEA_PASS")) + BRANCH_NAME = f"auto-dgp2-{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}" + branch = gitea_client.create_branch(BRANCH_NAME) + if not branch: + print("Branch creation failed. Unable to continue") + os.exit() + gitea_client.create_pis_file(BRANCH_NAME, BRANCH_NAME, output_file) + + # print(trains) out = open("organised_for_processing.txt", "w") @@ -131,6 +145,9 @@ def main(): out = open("validated.txt", "w") out.write(json.dumps(validated, indent=4, default=str)) out.close() + out = open("output_file.txt", "w") + out.write(output_file) + out.close() if __name__ == "__main__": main() \ No newline at end of file diff --git a/src/pis_file.py b/src/pis_file.py index c7b00a5..f99fe39 100644 --- a/src/pis_file.py +++ b/src/pis_file.py @@ -1,3 +1,25 @@ ## Produces a PIS YAML file in the OwlBoard/data format ## and pushes it to a new branch for review +def create_new_pis_file(input): + file_content = "# File produced by DGP2\n\nFor Review before merging\nXXXX\n\n" + for train in input: + file_content += f"""\n +- code: {train.get('diagram_pis_code', 'N/A')} + # diagramDate: {train.get('diagram_date', 'N/A')} + # diagramHeadcode: {train.get('train_headcode', 'N/A')} + # diagram_time1: {train.get('diagram_time1', 'N/A')} + # diagram_time2: {train.get('diagram_time2', 'N/A')} +""" + for entry in train.get('timetable_entries', []): + file_content += f""" + stops: {",".join(stop.lower() for stop in entry.get('stops', []))} + # stpIndicator: {entry.get('stpIndicator', 'N/A')} + # trainUid: {entry.get('trainUid', 'N/A')} + # scheduleStart: {entry.get('scheduleStart', 'N/A')} + # scheduleEnd: {entry.get('scheduleEnd', 'N/A')} + # daysRun: {",".join(day.upper() for day in entry.get('daysRun', []))} + # trainStartTime: {entry.get('trainStartTime', 'N/A')} +""" + return file_content +