Code is now functional but untidy and in need of refactoring and commenting
This commit is contained in:
parent
e2ded85833
commit
a6a4c75577
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,6 +3,7 @@ auto_matched.txt
|
|||||||
organised_for_processing.txt
|
organised_for_processing.txt
|
||||||
validated.txt
|
validated.txt
|
||||||
output.txt
|
output.txt
|
||||||
|
output_file.txt
|
||||||
output
|
output
|
||||||
*.sh
|
*.sh
|
||||||
|
|
||||||
|
22
src/gitea.py
22
src/gitea.py
@ -1,5 +1,6 @@
|
|||||||
import requests
|
import requests
|
||||||
import base64
|
import base64
|
||||||
|
import json
|
||||||
from urllib.parse import urljoin, urlencode
|
from urllib.parse import urljoin, urlencode
|
||||||
|
|
||||||
class GiteaConnector:
|
class GiteaConnector:
|
||||||
@ -28,11 +29,9 @@ class GiteaConnector:
|
|||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
try:
|
try:
|
||||||
data = response.json() # Parse JSON response
|
data = response.json() # Parse JSON response
|
||||||
print(data)
|
|
||||||
if isinstance(data, list): # Confirm it's a list
|
if isinstance(data, list): # Confirm it's a list
|
||||||
# Extract 'title' from each item in the list
|
# Extract 'title' from each item in the list
|
||||||
existing_titles = [item['title'] for item in data if 'title' in item]
|
existing_titles = [item['title'] for item in data if 'title' in item]
|
||||||
print(existing_titles)
|
|
||||||
return existing_titles
|
return existing_titles
|
||||||
else:
|
else:
|
||||||
print("Unexpected response format: Expected a list.")
|
print("Unexpected response format: Expected a list.")
|
||||||
@ -82,16 +81,11 @@ class GiteaConnector:
|
|||||||
## Create new branch in the repo
|
## Create new branch in the repo
|
||||||
def create_branch(self, branch_name):
|
def create_branch(self, branch_name):
|
||||||
BASE_BRANCH = "main"
|
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:
|
create_branch_url = urljoin(self.repo_url, "branches")
|
||||||
base_sha = branch_response.json()['commit']['id']
|
|
||||||
|
|
||||||
create_branch_url = urljoin(self.repo_url, "git/refs")
|
|
||||||
branch_data = {
|
branch_data = {
|
||||||
"ref": f"refs/heads/{branch_name}",
|
"new_branch_name": branch_name,
|
||||||
"sha": base_sha,
|
"old_branch_name": BASE_BRANCH,
|
||||||
}
|
}
|
||||||
|
|
||||||
create_branch_response = requests.post(create_branch_url, headers=self.header, data=json.dumps(branch_data))
|
create_branch_response = requests.post(create_branch_url, headers=self.header, data=json.dumps(branch_data))
|
||||||
@ -102,16 +96,12 @@ class GiteaConnector:
|
|||||||
print(f"Failed to create branch: ", branch_name, create_branch_response.status_code, create_branch_response.text)
|
print(f"Failed to create branch: ", branch_name, create_branch_response.status_code, create_branch_response.text)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
else:
|
|
||||||
print("Failed to retreive branch SHA: ", branch_response.status_code, branch_response.text)
|
|
||||||
return False
|
|
||||||
|
|
||||||
## Create a new file in the repo
|
## Create a new file in the repo
|
||||||
def create_pis_file(self, branch, filename, file_content):
|
def create_pis_file(self, branch, filename, file_content):
|
||||||
file_path = f"pis/{filename}"
|
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 = {
|
file_data = {
|
||||||
"content": encoded_content,
|
"content": encoded_content,
|
||||||
|
17
src/main.py
17
src/main.py
@ -7,6 +7,8 @@ import json
|
|||||||
import parse_pdf
|
import parse_pdf
|
||||||
import train_detail
|
import train_detail
|
||||||
import validate
|
import validate
|
||||||
|
import pis_file
|
||||||
|
from gitea import GiteaConnector
|
||||||
|
|
||||||
# List all PDF files in the given directory
|
# List all PDF files in the given directory
|
||||||
def list_pdf_files(directory):
|
def list_pdf_files(directory):
|
||||||
@ -120,6 +122,18 @@ def main():
|
|||||||
|
|
||||||
validated = validate.check_and_validate_against_owlboard(auto_matched)
|
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)
|
# print(trains)
|
||||||
out = open("organised_for_processing.txt", "w")
|
out = open("organised_for_processing.txt", "w")
|
||||||
@ -131,6 +145,9 @@ def main():
|
|||||||
out = open("validated.txt", "w")
|
out = open("validated.txt", "w")
|
||||||
out.write(json.dumps(validated, indent=4, default=str))
|
out.write(json.dumps(validated, indent=4, default=str))
|
||||||
out.close()
|
out.close()
|
||||||
|
out = open("output_file.txt", "w")
|
||||||
|
out.write(output_file)
|
||||||
|
out.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
@ -1,3 +1,25 @@
|
|||||||
## Produces a PIS YAML file in the OwlBoard/data format
|
## Produces a PIS YAML file in the OwlBoard/data format
|
||||||
## and pushes it to a new branch for review
|
## 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
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user