Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions src/phoebus_guibuilder/git_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@


class GitYaml:
def __init__(self, dom: str):
def __init__(self, dom: str, prefix: str):
self.pattern: str = "^(.*)-(.*)-(.*)"
self.dom: str = dom
self.git_ref: str = (
f"https://api.github.com/repos/epics-containers/{dom}-services/git/refs"
)
self.prefix: re.Match[str] | None = re.match(self.pattern, prefix)
assert self.prefix is not None, "Empty Prefix"

def re_group(self, component: str) -> str | None:
match: re.Match[str] | None = re.match(self.pattern, component)

if match:
return match.group(1)

Expand All @@ -30,20 +31,19 @@ def get_json_from_url(self, url: str) -> dict | None:
return err.response.json()

def get_yaml(self, url) -> str | None:
data: dict | None = self.get_json_from_url(url)
if data is not None:
base64_content = data["content"]
decoded_content = base64.b64decode(base64_content).decode("utf-8")
config_json: dict | None = self.get_json_from_url(url)
assert config_json is not None, "Could not fetch config json tree"

return decoded_content
ioc_yaml_url = self.fetch_matches(config_json, "iocyaml")
if ioc_yaml_url is not None:
data: dict | None = self.get_json_from_url(ioc_yaml_url)
if data is not None:
base64_content = data["content"]
decoded_content = base64.b64decode(base64_content).decode("utf-8")

def fetch_matches(
self,
tree: dict,
task: str,
prefix: str = "",
option: str = "",
) -> str | None:
return decoded_content

def fetch_matches(self, tree: dict, task: str) -> str | None:
if task == "ref":
matching_refs = [
item
Expand All @@ -58,19 +58,21 @@ def fetch_matches(
for item in tree
if isinstance(item, dict) and item.get("path") == "services"
]
print(f"DEBUG1:{matching_url}")
return matching_url[0]["url"]

elif task == "subfolder":
tree = tree["tree"]
matching_folders = [
item
for item in tree
if isinstance(item, dict)
and self.re_group(str(item.get("path"))) == prefix.lower()
]
if matching_folders:
return matching_folders[0]["url"]
if self.prefix:
print(f"DEBUG1:{self.prefix.group(1).lower()}")
matching_folders = [
item
for item in tree
if isinstance(item, dict)
and self.re_group(str(item.get("path")))
== self.prefix.group(1).lower()
]
if matching_folders:
return matching_folders[0]["url"]

elif task == "config":
tree = tree["tree"]
Expand All @@ -79,23 +81,26 @@ def fetch_matches(
for item in tree
if isinstance(item, dict) and item.get("path") == "config"
]
print(f"DEBUG2:{config[0]['url']}")
return config[0]["url"]

else:
answer = [
elif task == "iocyaml":
tree = tree["tree"]
ioc_url = [
item
for item in tree
if isinstance(item, dict) and item.get(option) == task
if isinstance(item, dict) and item.get("path") == "ioc.yaml"
]
return answer[0]["url"]
print(f"DEBUG3:{ioc_url[0]['url']}")
return ioc_url[0]["url"]

def fetch_ioc_yaml(
self,
):
commit_hashes: dict | None = self.get_json_from_url(self.git_ref)

assert commit_hashes is not None, "Could not pull commit hashes"
if commit_hashes["message"]:
if len(commit_hashes) < 3:
print(f"{commit_hashes['message']}")
return

main_hash = self.fetch_matches(commit_hashes, "ref")
Expand Down
38 changes: 20 additions & 18 deletions src/phoebus_guibuilder/guibuilder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import io

import yaml

from phoebus_guibuilder.datatypes import Beamline, Component, Entry
Expand Down Expand Up @@ -50,7 +52,7 @@ def find_services_extract_ioc(

for component in self.components:
print(component.P)
ioc_yaml = GitYaml(self.beamline.dom).fetch_ioc_yaml()
ioc_yaml = GitYaml(self.beamline.dom, prefix=component.P).fetch_ioc_yaml()
if ioc_yaml is not None:
self.extract_valid_entities(
ioc_yaml,
Expand All @@ -67,24 +69,24 @@ def extract_valid_entities(self, ioc_yaml: str, component: Component):
entities: list[dict[str, str]] = []
component_match = f"{component.P}:{component.R}"

with open(ioc_yaml) as ioc:
conf = yaml.safe_load(ioc)
entities = conf["entities"]
print(entities)
for entity in entities:
if (
"P" in entity.keys() and entity["P"] == component_match
): # the suffix could be M, could be R
self.valid_entities.append(
Entry(
type=entity["type"],
DESC=None,
P=entity["P"],
M=None,
R=None,
)
ioc = io.StringIO(ioc_yaml)
conf = yaml.safe_load(ioc)
entities = conf["entities"]
print(entities)
for entity in entities:
if (
"P" in entity.keys() and entity["P"] == component_match
): # the suffix could be M, could be R
self.valid_entities.append(
Entry(
type=entity["type"],
DESC=None,
P=entity["P"],
M=None,
R=None,
)
print(self.valid_entities)
)
print(self.valid_entities)

def gui_map(self, entrys: list[Entry]):
"""
Expand Down
Loading