Skip to content
Snippets Groups Projects
Commit b0685276 authored by Nathan Daelman's avatar Nathan Daelman
Browse files

Extend further to libxc

parent 14fdec45
No related branches found
No related tags found
No related merge requests found
......@@ -2,33 +2,12 @@ from neomodel import config # type: ignore # pylint: disable=import-error,missi
from pathlib import Path
import resource
import sys
import yaml
from to_db import Neo4jOperation
from general_parser import get_neo4j_settings, get_libxc_settings
from maple_parser import maple_main
from libxc_c_parser import libxc_main, trampoline
def get_neo4j_settings() -> tuple[str, str, str, str]:
with open("config.yaml", "r", encoding="utf-8") as file:
yaml_config = yaml.safe_load(file)
return (
yaml_config["neo4j"]["user"],
yaml_config["neo4j"]["password"],
yaml_config["neo4j"]["host"],
yaml_config["neo4j"]["port"],
)
def get_libxc_settings() -> tuple[str, str, str]:
with open("config.yaml", "r", encoding="utf-8") as file:
yaml_config = yaml.safe_load(file)
return (
yaml_config["libxc"]["path"],
yaml_config["libxc"]["maple"],
yaml_config["libxc"]["src"],
)
def __main__():
sys.setrecursionlimit(1000)
resource.setrlimit(resource.RLIMIT_STACK, (2**25,-1))
......
import os
from typing import IO, Callable, Optional, Any
from neomodel import StructuredNode, RelationshipTo # type: ignore # pylint: disable=import-error,missing-module-docstring
import yaml
Triple = tuple[
tuple[type[StructuredNode], dict[str, Any]], # (ModelClass, Properties) for source node
tuple[type[StructuredNode], dict[str, Any]], # (ModelClass, Properties) for target node
type[RelationshipTo] # Relationship class
Optional[type[RelationshipTo]] # Relationship class
]
def get_neo4j_settings() -> tuple[str, str, str, str]:
with open("config.yaml", "r", encoding="utf-8") as file:
yaml_config = yaml.safe_load(file)
return (
yaml_config["neo4j"]["user"],
yaml_config["neo4j"]["password"],
yaml_config["neo4j"]["host"],
yaml_config["neo4j"]["port"],
)
def get_libxc_settings() -> tuple[str, str, str]:
with open("config.yaml", "r", encoding="utf-8") as file:
yaml_config = yaml.safe_load(file)
return (
yaml_config["libxc"]["path"],
yaml_config["libxc"]["maple"],
yaml_config["libxc"]["src"],
)
def read_next(
file: IO[str], previous_text: str, cleaner: Optional[Callable[[str], str]] = None
) -> Optional[tuple[IO[str], str, str]]:
......
from functools import partial
from neomodel import RelationshipTo # type: ignore # pylint: disable=import-error,missing-module-docstring
import re
from typing import IO, Callable, Union
from typing import IO, Any, Callable, Union
from general_parser import (
Triple,
flush_text,
......@@ -8,7 +9,7 @@ from general_parser import (
file_path_to_name,
read_next,
)
from to_db import FileNode
from to_db import FileNode, FunctionalNode
# matching functions
......@@ -16,11 +17,11 @@ def is_file_reference(line: str) -> bool:
return line.startswith("#include")
def is_description_start(line: str) -> bool:
def is_functional_start(line: str) -> bool:
return line.startswith("const xc_func_info_type")
def is_description_end(line: str) -> bool:
def is_functional_end(line: str) -> bool:
return line.endswith("};")
......@@ -41,7 +42,7 @@ def trampoline(func: Callable[..., Union[tuple, Callable]], *args, **kwargs):
# Adjusted process_file_reference to return a function or partial function
def process_file_reference(file: IO[str], text: str, posts: list[Triple]):
def process_file_reference(file: IO[str], text: str, posts: list[Triple]) -> partial:
"""Return to the main maple branch after processing a file reference"""
match = re.search(r'#include\s+"(.*)"', text)
if match:
......@@ -60,8 +61,41 @@ def process_file_reference(file: IO[str], text: str, posts: list[Triple]):
return partial(libxc_main, *flush_text(file, text, posts))
def filter_out_null(x: str) -> list[str]:
"""Filter out NULL values from a string"""
return [y for y in x.split(", ") if y != "NULL"]
def process_functional(file: IO[str], text: str, posts: list[Triple]) -> partial:
""""""
post = posts[-1]
functional_attributes = post[1][1]
if is_functional_end(text):
post[2] = FileNode.defines
return partial(libxc_main, *flush_text(file, text, posts))
if "name" not in functional_attributes:
match = re.search(r"\s+(XC_\w+),", text)
if match:
functional_attributes["name"] = match.group(1)
elif "citations" not in functional_attributes:
match = re.search(r"\s+{([\w+,&])}", text)
if match:
functional_attributes["citation"] = filter_out_null(match.group(1))
elif "properties" not in functional_attributes:
match = re.search(r"\s+{([\w+,])}", text)
if match:
functional_attributes["properties"] = match.group(1).split(", ")[3]
elif "initiliazer" not in functional_attributes:
match = re.search(r"\s+([\w+,])", text)
if match:
functional_attributes["initializer"] = filter_out_null(match.group(1))
return partial(process_functional, *flush_text(file, text, posts))
# Adjust libxc_main to work with trampoline
def libxc_main(file: IO[str], text: str, posts: list[Triple]):
def libxc_main(file: IO[str], text: str, posts: list[Triple]) -> partial:
"""Start on the main maple branch"""
next_line = read_next(file, text, clean_line)
if next_line is None:
......@@ -71,5 +105,14 @@ def libxc_main(file: IO[str], text: str, posts: list[Triple]):
if is_file_reference(incoming_text):
# Return a partial function for trampoline to call
return partial(process_file_reference, file, new_text, posts)
elif is_functional_start(incoming_text):
posts.append(
(
(FileNode, {"name": file_path_to_name(file.name)}),
(FunctionalNode, {}),
None,
)
)
return partial(process_functional, file, new_text, posts)
# Return a partial function for trampoline to continue
return partial(libxc_main, file, new_text, posts)
......@@ -44,9 +44,17 @@ class FileNode(StructuredNode):
name = StringProperty(unique_index=True, required=True)
references = RelationshipTo("FileNode", "references")
defines = RelationshipTo("FormulaNode", "defines")
cites = RelationshipTo("PublicationNode", "cites")
class FormulaNode(StructuredNode):
name = StringProperty(required=True)
parameters = ArrayProperty(StringProperty(), default=[])
function_refs = ArrayProperty(StringProperty(), default=[])
class FunctionalNode(StructuredNode):
name = StringProperty(required=True)
citations = ArrayProperty(StringProperty(), default=[])
properties = ArrayProperty(StringProperty(), default=[])
initializer = ArrayProperty(StringProperty(), default=[])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment