-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Materialize structured patterns on class slot usages and attributes
- Loading branch information
1 parent
3617c37
commit e1df199
Showing
6 changed files
with
115 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,3 +140,4 @@ Pipfile.lock | |
|
||
# No Pycharm | ||
.idea/ | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,64 @@ | ||
from functools import lru_cache | ||
import re | ||
from typing import Dict | ||
|
||
|
||
def generate_patterns(schema_view) -> Dict[str, str]: | ||
"""Generates a dictionary of slot patterns corresponding to | ||
the structured patterns in the settings. | ||
:param schema_view: SchemaView object with LinkML YAML | ||
already loaded | ||
:return generated_patterns: dictionary with the | ||
expanded structured patterns | ||
""" | ||
|
||
# fetch settings from schema_view | ||
settings_dict = schema_view.schema.settings | ||
|
||
# dictionary of key and string value of settings dict | ||
format_spec = {} | ||
|
||
for k, setting in settings_dict.items(): | ||
|
||
# create spec dictionary with keys that will replace | ||
# substrings in the structured pattern syntax | ||
format_spec[k] = setting.setting_value | ||
|
||
# dictionary with structured patterns in the key and | ||
# expanded, or materialized patterns as values | ||
generated_patterns = {} | ||
class PatternResolver(): | ||
|
||
# regular expression capturing the various use cases | ||
# for the optionally dot separated, curly braces bound, pattern syntax | ||
var_name = re.compile("{([a-z0-9_-]+([\.-_ ][a-z0-9]+)*)}", re.IGNORECASE) | ||
|
||
for _, slot_defn in schema_view.all_slots().items(): | ||
if slot_defn.structured_pattern: | ||
struct_pat = slot_defn.structured_pattern | ||
|
||
pattern = struct_pat.syntax | ||
|
||
# compute pattern from structured patterns | ||
# and format_spec dictionary | ||
|
||
|
||
# apply the regex to the pattern and look for matches | ||
matches = var_name.finditer(pattern) | ||
|
||
reversed = [] | ||
for item in matches: | ||
# Detect double set brackets | ||
match_string = None | ||
if ( | ||
item.start() > 0 | ||
and item.end() < len(pattern) | ||
and pattern[item.start() - 1] == "{" | ||
and pattern[item.end()] == "}" | ||
): | ||
match_string = item.group(1) | ||
|
||
elif item.group(1) in format_spec: | ||
match_string = str(format_spec[item.group(1)]) | ||
|
||
if match_string: | ||
reversed.insert( | ||
0, | ||
{ | ||
"string": match_string, | ||
"start": item.start(), | ||
"end": item.end(), | ||
}, | ||
) | ||
|
||
converted = pattern | ||
for item in reversed: | ||
converted = ( | ||
converted[: item["start"]] | ||
+ item["string"] | ||
+ converted[item["end"] :] | ||
def __init__(self, schema_view): | ||
# fetch settings from schema_view | ||
settings_dict = schema_view.schema.settings | ||
|
||
# dictionary of key and string value of settings dict | ||
self.format_spec = {} | ||
|
||
for k, setting in settings_dict.items(): | ||
|
||
# create spec dictionary with keys that will replace | ||
# substrings in the structured pattern syntax | ||
self.format_spec[k] = setting.setting_value | ||
|
||
@lru_cache | ||
def resolve(self, pattern: str) -> str: | ||
# apply the regex to the pattern and look for matches | ||
matches = self.var_name.finditer(pattern) | ||
|
||
reversed = [] | ||
for item in matches: | ||
# Detect double set brackets | ||
match_string = None | ||
if ( | ||
item.start() > 0 | ||
and item.end() < len(pattern) | ||
and pattern[item.start() - 1] == "{" | ||
and pattern[item.end()] == "}" | ||
): | ||
match_string = item.group(1) | ||
|
||
elif item.group(1) in self.format_spec: | ||
match_string = str(self.format_spec[item.group(1)]) | ||
|
||
if match_string: | ||
reversed.insert( | ||
0, | ||
{ | ||
"string": match_string, | ||
"start": item.start(), | ||
"end": item.end(), | ||
}, | ||
) | ||
|
||
generated_patterns[pattern] = converted | ||
converted = pattern | ||
for item in reversed: | ||
converted = ( | ||
converted[: item["start"]] | ||
+ item["string"] | ||
+ converted[item["end"] :] | ||
) | ||
|
||
return converted | ||
|
||
return generated_patterns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters