Skip to content

Commit

Permalink
web handler for importing CREs and standards
Browse files Browse the repository at this point in the history
  • Loading branch information
northdpole committed Aug 3, 2024
1 parent c01b90a commit 67e16d4
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 121 deletions.
14 changes: 9 additions & 5 deletions application/cmd/cre_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def register_node(node: defs.Node, collection: db.Node_collection) -> db.Node:
register_node(node=link.document, collection=collection)

elif type(link.document).__name__ == defs.CRE.__name__:
# dbcre = register_cre(link.document, collection) # CREs are idempotent
# dbcre,_ = register_cre(link.document, collection) # CREs are idempotent
c = collection.get_CREs(name=link.document.name)[0]
dbcre = db.dbCREfromCRE(c)
collection.add_link(dbcre, linked_node, type=link.ltype)
Expand All @@ -123,14 +123,19 @@ def register_node(node: defs.Node, collection: db.Node_collection) -> db.Node:
return linked_node


def register_cre(cre: defs.CRE, collection: db.Node_collection) -> db.CRE:
def register_cre(cre: defs.CRE, collection: db.Node_collection) -> Tuple[db.CRE, bool]:
existing = False
if collection.get_CREs(name=cre.id):
existing = True

dbcre: db.CRE = collection.add_cre(cre)
for link in cre.links:
if type(link.document) == defs.CRE:
logger.info(f"{link.document.id} {link.ltype} {cre.id}")
lower_cre, _ = register_cre(link.document, collection)
collection.add_internal_link(
higher=dbcre,
lower=register_cre(link.document, collection),
lower=lower_cre,
type=link.ltype,
)
else:
Expand All @@ -139,7 +144,7 @@ def register_cre(cre: defs.CRE, collection: db.Node_collection) -> db.CRE:
node=register_node(node=link.document, collection=collection),
type=link.ltype,
)
return dbcre
return dbcre, existing


def parse_file(
Expand Down Expand Up @@ -316,7 +321,6 @@ def parse_standards_from_spreadsheeet(
documents = spreadsheet_parsers.parse_export_format(cre_file)
register_cre(documents, collection)
pass

elif any(key.startswith("CRE hierarchy") for key in cre_file[0].keys()):
conn = redis.connect()
collection = collection.with_graph()
Expand Down
109 changes: 53 additions & 56 deletions application/defs/cre_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@ class ExportFormat: # TODO: this can likely be replaced with a method that iter
# <doctype>:<name>:<varname>
separator = "|"
section = "name"
# subsection = "subsection"
subsection = "subsection"
hyperlink = "hyperlink"
# link_type = "link_type"
# name = "name"
link_type = "link_type"
name = "name"
id = "id"
description = "description"
# cre_link = "Linked_CRE_"
cre_link = "Linked_CRE_"
cre = "CRE"
tooltype = "ToolType"
# sectionID = "SectionID"

def __str__(self):
return str(self.value)
sectionID = "SectionID"

@staticmethod
def get_doctype(header: str) -> Optional["Credoctypes"]:
Expand All @@ -41,143 +38,143 @@ def get_doctype(header: str) -> Optional["Credoctypes"]:
def node_name_key(sname: str) -> str:
"""returns :<sname>: used mostly for matching"""
return "%s%s%s" % (
ExportFormat.separator.value,
ExportFormat.separator,
sname,
ExportFormat.separator.value,
ExportFormat.separator,
)

@staticmethod
def tooltype_key(sname: str, doctype: "Credoctypes") -> str:
"returns <doctype>:<name>:tooltype"
return "%s%s%s%s%s" % (
doctype.value,
ExportFormat.separator.value,
doctype,
ExportFormat.separator,
sname,
ExportFormat.separator.value,
ExportFormat.tooltype.value,
ExportFormat.separator,
ExportFormat.tooltype,
)

@staticmethod
def sectionID_key(sname: str, doctype: "Credoctypes") -> str:
"returns <doctype>:<name>:sectionID"
return "%s%s%s%s%s" % (
doctype.value,
ExportFormat.separator.value,
doctype,
ExportFormat.separator,
sname,
ExportFormat.separator.value,
ExportFormat.sectionID.value,
ExportFormat.separator,
ExportFormat.sectionID,
)

@staticmethod
def description_key(sname: str, doctype: "Credoctypes") -> str:
"returns <doctype>:<name>:description"
return "%s%s%s%s%s" % (
doctype.value,
ExportFormat.separator.value,
doctype,
ExportFormat.separator,
sname,
ExportFormat.separator.value,
ExportFormat.description.value,
ExportFormat.separator,
ExportFormat.description,
)

@staticmethod
def section_key(sname: str, doctype: "Credoctypes") -> str:
"returns <doctype>:<name>:section"
return "%s%s%s%s%s" % (
doctype.value,
ExportFormat.separator.value,
doctype,
ExportFormat.separator,
sname,
ExportFormat.separator.value,
ExportFormat.section.value,
ExportFormat.separator,
ExportFormat.section,
)

@staticmethod
def subsection_key(sname: str, doctype: "Credoctypes") -> str:
"returns <doctype>:<sname>:subsection"
return "%s%s%s%s%s" % (
doctype.value,
ExportFormat.separator.value,
doctype,
ExportFormat.separator,
sname,
ExportFormat.separator.value,
ExportFormat.subsection.value,
ExportFormat.separator,
ExportFormat.subsection,
)

@staticmethod
def hyperlink_key(sname: str, doctype: "Credoctypes") -> str:
"returns <sname>:hyperlink"
return "%s%s%s%s%s" % (
doctype.value,
ExportFormat.separator.value,
doctype,
ExportFormat.separator,
sname,
ExportFormat.separator.value,
ExportFormat.hyperlink.value,
ExportFormat.separator,
ExportFormat.hyperlink,
)

@staticmethod
def link_type_key(sname: str, doctype: "Credoctypes") -> str:
"returns <sname>:link_type"
return "%s%s%s%s%s" % (
doctype.value,
ExportFormat.separator.value,
doctype,
ExportFormat.separator,
sname,
ExportFormat.separator.value,
ExportFormat.link_type.value,
ExportFormat.separator,
ExportFormat.link_type,
)

@staticmethod
def linked_cre_id_key(name: str) -> str:
"returns Linked_CRE_<name>:id"
return "%s%s%s%s" % (
ExportFormat.cre_link.value,
ExportFormat.cre_link,
name,
ExportFormat.separator.value,
ExportFormat.id.value,
ExportFormat.separator,
ExportFormat.id,
)

@staticmethod
def linked_cre_name_key(name: str) -> str:
"returns Linked_CRE_<name>:name"
return "%s%s%s%s" % (
ExportFormat.cre_link.value,
ExportFormat.cre_link,
name,
ExportFormat.separator.value,
ExportFormat.name.value,
ExportFormat.separator,
ExportFormat.name,
)

@staticmethod
def linked_cre_link_type_key(name: str) -> str:
"returns Linked_CRE_<name>:link_type"
return "%s%s%s%s" % (
ExportFormat.cre_link.value,
ExportFormat.cre_link,
name,
ExportFormat.separator.value,
ExportFormat.link_type.value,
ExportFormat.separator,
ExportFormat.link_type,
)

@staticmethod
def cre_id_key() -> str:
"returns CRE:id"
return "%s%s%s" % (
ExportFormat.cre.value,
ExportFormat.separator.value,
ExportFormat.id.value,
ExportFormat.cre,
ExportFormat.separator,
ExportFormat.id,
)

@staticmethod
def cre_name_key() -> str:
"returns CRE:name"
return "%s%s%s" % (
ExportFormat.cre.value,
ExportFormat.separator.value,
ExportFormat.name.value,
ExportFormat.cre,
ExportFormat.separator,
ExportFormat.name,
)

@staticmethod
def cre_description_key() -> str:
"returns CRE:description"
return "%s%s%s" % (
ExportFormat.cre.value,
ExportFormat.separator.value,
ExportFormat.description.value,
ExportFormat.cre,
ExportFormat.separator,
ExportFormat.description,
)


Expand Down
6 changes: 4 additions & 2 deletions application/tests/cre_main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ def test_register_cre(self) -> None:
tags=["CREt1", "CREt2"],
metadata={"tags": ["CREl1", "CREl2"]},
)
self.assertEqual(main.register_cre(cre, self.collection).name, cre.name)
self.assertEqual(main.register_cre(cre, self.collection).external_id, cre.id)
c, _ = main.register_cre(cre, self.collection)
self.assertEqual(c.name, cre.name)

self.assertEqual(c.external_id, cre.id)
self.assertEqual(
len(self.collection.session.query(db.CRE).all()), 1
) # 1 cre in the db
Expand Down
Loading

0 comments on commit 67e16d4

Please sign in to comment.