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

Set up environment and tests

parents
No related branches found
No related tags found
No related merge requests found
File added
File added
neo4j:
user: 'neo4j'
password: 'password'
host: 'localhost'
port: 7687
libxc:
src: 'libxc/src/'
xc-info: 'libxc/bin/'
neo4j:
user: 'neo4j'
password: 'password'
host: 'localhost'
port: 7687
libxc:
src: '/home/nathan/Documents/libxc/src/'
xc-info: '/home/nathan/Documents/libxc/bin/'
from typing import Optional, Union
class MapleType:
pass
class Comment(MapleType):
def __init__(self, message: str):
self.message = message
class Formula(MapleType):
def __init__(self, value, filename):
self.value = value
self.filename = filename
class Mapper:
pass
class Includes(Mapper):
def __init__(self, value, filename):
self.value = value
self.filename = filename
def parse_comment_start(contents: list[str]) -> tuple[list[str], str]:
if contents[0].startswith("(*"):
return parse_comment_end(contents)
return contents, ""
def parse_comment_end(contents: list[str]) -> tuple[list[str], str]:
match = "*)"
comment_block, line_num = "", 0
for line in contents:
if line.endswith(match):
comment_block += line.strip("\n")
break
comment_block += line.strip("\n") + " "
line_num += 1
comment_block = comment_block.replace(match, "").replace("(*", "").strip()
return contents[line_num + 1 :], comment_block
def parse_comment(contents: list[str]) -> tuple[list[str], Optional[Comment]]:
trimmed_contents, comment_block = parse_comment_start(contents)
if comment_block:
return trimmed_contents, Comment(comment_block)
return trimmed_contents, None
def parse_file_reference(
contents: list[str], filename: str
) -> tuple[list[str], Optional[Includes]]:
matching_line = contents[0].rsplit("$include ", 1)
if len(matching_line) > 1:
return contents[1:], Includes(matching_line[1].strip('"'), filename)
return contents, None
def parse_formula_start(contents: list[str]) -> tuple[list[str], str]:
if ":=" in contents[0]:
return parse_formula_end(contents)
return contents, ""
def parse_formula_end(contents: list[str]) -> tuple[list[str], str]:
formula_block, line_num = "", 0
for line in contents:
if line.endswith(":"):
formula_block += line.strip("\n")
break
formula_block += line.strip("\n") + " "
line_num += 1
return contents[line_num + 1 :], formula_block
def parse_formula(
contents: list[str], filename: str
) -> tuple[list[str], Optional[Formula]]:
trimmed_contents, formula_block = parse_formula_start(contents)
if formula_block:
return trimmed_contents, Formula(formula_block, filename)
return trimmed_contents, None
def parse_file_contents(
contents: list[str], filename: str
) -> list[Union[MapleType, Mapper]]:
trimmed_contents: list[str] = contents
parsed_contents: list[Union[MapleType, Mapper]] = []
def loop_condition():
return len(trimmed_contents) > 0
while loop_condition():
parsed_num = len(parsed_contents)
trimmed_contents, comment = parse_comment(contents)
if comment is not None:
parsed_contents.append(comment)
if not loop_condition():
break
trimmed_contents, file_reference = parse_file_reference(
trimmed_contents, filename
)
if file_reference is not None:
parsed_contents.append(file_reference)
if not loop_condition():
break
trimmed_contents, formula = parse_formula(trimmed_contents, filename)
if formula is not None:
parsed_contents.append(formula)
if not loop_condition():
break
# in case no useful contents was found, shorten the line
# examples would include empty lines
if parsed_num == len(parsed_contents):
trimmed_contents.pop(0)
return parsed_contents
File added
File added
import pytest
import yaml
from maple_parser import *
# Read the YAML file
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
file.close()
libxc_src = config['libxc']['src']
@pytest.fixture
def gga_k_apbe():
with open(f"{libxc_src}/../maple/gga_exc/gga_k_apbe.mpl", "r") as file:
contents = [line.strip('\n') for line in file.readlines()]
file.close()
return contents
def test_comment(gga_k_apbe):
results = parse_comment([gga_k_apbe[8]])
assert results[1].message == "type: gga_exc"
def test_parse_reference(gga_k_apbe):
results = parse_file_reference([gga_k_apbe[16]], "gga_k_apbe.mpl")
assert results[1].value == "gga_x_pbe.mpl"
def test_parse_formula(gga_k_apbe):
results = parse_formula([gga_k_apbe[18]], "gga_k_apbe.mpl")
assert results[1].value == "f := (rs, z, xt, xs0, xs1) -> gga_kinetic(pbe_f, rs, z, xs0, xs1):"
def test_parse(gga_k_apbe):
results = parse_file_contents(gga_k_apbe, "gga_k_apbe.mpl")
assert len(results) == 5
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment