Compare commits

...

2 Commits

5 changed files with 105 additions and 30 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@ auto_matched.txt
organised_for_processing.txt
validated.txt
output.txt
output_file.txt
output
*.sh

View File

@ -1,6 +1,7 @@
import requests
import base64
from urllib.parse import urljoin
import json
from urllib.parse import urljoin, urlencode
class GiteaConnector:
def __init__(self, repo_url, user, key):
@ -14,22 +15,52 @@ class GiteaConnector:
"Content-Type": "application/json",
}
## Get open issues in repo
def get_open_issue_titles(self):
url_suffix = "issues"
url = urljoin(self.repo_url, url_suffix)
queries = {
"state": "open",
"type": "issues",
}
full_url = f"{url}?{urlencode(queries)}"
response = requests.get(full_url, headers=self.header)
if response.status_code == 200:
try:
data = response.json() # Parse JSON response
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]
return existing_titles
else:
print("Unexpected response format: Expected a list.")
except ValueError:
print("Error: Response is not valid JSON.")
else:
print("Error fetching existing issues")
return []
## Create an issue in the repo
def create_issue(self,title,content):
url_suffix = "issues"
url = urljoin(self.repo_url, url_suffix)
existing_issue_titles = self.get_open_issue_titles()
if title in existing_issue_titles:
print("Issue regarding this code exists already")
return True
issue_data = {
"title": title,
"body": content,
"assignees": ["fred.boniface"],
"labels": [261],
"labels": [261,269],
}
print(f"URL: {url}")
response = requests.post(url, headers=self.header, json=issue_data)
if response.status_code == 201:
print("Succesfully creates issue: ", response.json())
print("Succesfully created issue")
return True
else:
print("Failed to create issue: ", response.status_code, response.text)
@ -50,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,

View File

@ -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()

25
src/pis_file.py Normal file
View File

@ -0,0 +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

View File

@ -58,14 +58,24 @@ def check_and_validate_against_owlboard(train_entries):
elif len(train_entry['timetable_entries']) == 1:
issue_title = f"PIS Error | Code: {train_entry['diagram_pis_code']}"
issue_content = f"""
PIS Code {train_entry['diagram_pis_code']}.
Diagram dated {train_entry['diagram_date']} has been parsed and produced a mismatch with the OwlBoard database.
PIS Code {train_entry['diagram_pis_code']}.
Diagram dated {train_entry['diagram_date']} has been parsed and produced a mismatch with the OwlBoard database.
Diagram Stops: {",".join(train_entry['timetable_entries'][0]["stops"])}
Diagram Stops: {",".join(train_entry['timetable_entries'][0]["stops"])}
Database Stops: {",".join(database_stops)}
Database Stops: {",".join(database_stops)}
This requires a manual check of the PIS file to identify mistakes or changes.
The train which was identified in the database is:
stpIndicator: {train_entry['timetable_entries'][0]['stpIndicator']}
operator: {train_entry['timetable_entries'][0]['operator']}
trainUID: {train_entry['timetable_entries'][0]['trainUid']}
scheduleStart: {train_entry['timetable_entries'][0]['scheduleStart']}
scheduleEnd: {train_entry['timetable_entries'][0]['scheduleEnd']}
daysRun: {",".join(train_entry['timetable_entries'][0]['daysRun'])}
This requires a manual check of the PIS file to identify mistakes or changes.
Make sure that you validate that the service runs on the correct day.
If the stpIndicator is 'O' or 'N', it is possible that the train schedule is newer than the parsed diagram.
"""
print(issue_content)
gitea_client.create_issue(issue_title, issue_content)