-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
260 additions
and
45 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import re | ||
import yaml | ||
|
||
from typing import List | ||
from pathlib import Path | ||
from .rule import Rule | ||
|
||
class Loader: | ||
suffix = ['.yaml', '.yml'] | ||
|
||
def __init__(self, base_path: str): | ||
self.path = Path(base_path) | ||
self.included = [] | ||
self.gloabl_context = {} | ||
self.rules = [] | ||
|
||
def __load_yaml(self, path: Path) -> dict: | ||
with path.open() as f: | ||
return yaml.load(f, Loader=yaml.FullLoader) | ||
|
||
def __load_include(self, name: str) -> dict: | ||
path = Path(name).expanduser() | ||
if path.is_absolute(): | ||
# include /User/abc.yaml | ||
self.__load_file(path) | ||
return | ||
|
||
path = self.path.parent.joinpath(name) | ||
if path.is_file(): | ||
# include abc.yaml | ||
self.__load_file(path) | ||
return | ||
|
||
# include abc | ||
for suffix in self.suffix: | ||
new_path = path.with_suffix(suffix) | ||
print(new_path) | ||
if new_path.is_file(): | ||
self.__load_file(new_path) | ||
return | ||
|
||
raise Exception(f'include file not found: {name}') | ||
|
||
def __load_file(self, path: Path): | ||
if path in self.included: | ||
return | ||
self.included.append(path) | ||
data = self.__load_yaml(path) | ||
|
||
# check include | ||
if include := data.get('include', None): | ||
if isinstance(include, str): | ||
self.__load_include(include) | ||
if isinstance(include, list): | ||
for item in include: | ||
self.__load_include(item) | ||
|
||
# global context | ||
if context := data.get('context', None): | ||
for key, value in context.items(): | ||
if key in ["lines", "captures", "content"]: | ||
raise AttributeError(f'Invalid context key: {key}') | ||
self.gloabl_context[key] = re.compile(value) | ||
|
||
if patterns := data.get('patterns', None): | ||
for item in data['patterns']: | ||
self.rules.append(Rule(item)) | ||
|
||
def load(self) -> List[Rule]: | ||
self.__load_file(self.path) | ||
|
||
for rule in self.rules: | ||
rule.global_context = self.gloabl_context | ||
return self.rules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import pytest | ||
|
||
from flog.loader import * | ||
|
||
def str_to_rules(tmpdir, content): | ||
rule = tmpdir.join('test.yaml') | ||
rule.write(content) | ||
loader = Loader(str(rule)) | ||
return loader.load() | ||
|
||
def test_path(tmpdir): | ||
base = tmpdir.join('base.yaml') | ||
base.write(''' | ||
patterns: | ||
- name: Python version | ||
match: Python (3.\\d+\\.\\d+\\.\\d+) | ||
message: "Python version: {{0}}"''') | ||
|
||
rule = str_to_rules(tmpdir, 'include: base')[0] | ||
assert rule.name == 'Python version' | ||
|
||
rule = str_to_rules(tmpdir, 'include: base.yaml')[0] | ||
assert rule.name == 'Python version' | ||
|
||
rule = str_to_rules(tmpdir, 'include: base.yml')[0] | ||
assert rule.name == 'Python version' | ||
|
||
rule = str_to_rules(tmpdir, 'include: '+str(base))[0] | ||
assert rule.name == 'Python version' | ||
|
||
with pytest.raises(Exception) as exc_info: | ||
str_to_rules(tmpdir, 'include: notfound') | ||
assert str(exc_info.value) == 'include file not found: notfound' | ||
|
||
sub = tmpdir.mkdir('sub') | ||
rule = str_to_rules(sub, 'include: ../base')[0] | ||
assert rule.name == 'Python version' | ||
|
||
with pytest.raises(Exception) as exc_info: | ||
str_to_rules(sub, 'include: base') | ||
assert str(exc_info.value) == 'include file not found: base' | ||
|
||
def test_context(tmpdir): | ||
base = tmpdir.join('base.yaml') | ||
base.write(''' | ||
context: | ||
name: "version" | ||
patterns: | ||
- name: Python version | ||
match: Python (3.\\d+\\.\\d+\\.\\d+) | ||
message: "Python version: {{0}}"''') | ||
|
||
rule = str_to_rules(tmpdir, 'include: base')[0] | ||
assert rule.global_context['name'].pattern == 'version' | ||
|
||
rule = str_to_rules(tmpdir, '''\ | ||
include: base | ||
context: | ||
name: "new" | ||
''')[0] | ||
assert rule.global_context['name'].pattern == 'new' | ||
|
||
def test_recursive(tmpdir): | ||
base = tmpdir.join('base.yaml') | ||
base.write(''' | ||
context: | ||
name: "version" | ||
patterns: | ||
- name: Python version | ||
match: Python (3.\\d+\\.\\d+\\.\\d+) | ||
message: "Python version: {{0}}"''') | ||
|
||
base2 = tmpdir.join('base2.yaml') | ||
base2.write(''' | ||
include: base | ||
context: | ||
name: "version" | ||
patterns: | ||
- name: Build version | ||
match: Build (\\d+) | ||
message: "Buildd version: {{0}}"''') | ||
|
||
rules = str_to_rules(tmpdir, 'include: base2') | ||
assert rules[0].name == 'Python version' | ||
assert rules[1].name == 'Build version' | ||
|
||
def test_cycle(tmpdir): | ||
base = tmpdir.join('base.yaml') | ||
base.write(''' | ||
include: base2 | ||
context: | ||
name: "version" | ||
patterns: | ||
- name: Python version | ||
match: Python (3.\\d+\\.\\d+\\.\\d+) | ||
message: "Python version: {{0}}"''') | ||
|
||
base2 = tmpdir.join('base2.yaml') | ||
base2.write(''' | ||
include: base | ||
context: | ||
name: "version" | ||
patterns: | ||
- name: Build version | ||
match: Build (\\d+) | ||
message: "Buildd version: {{0}}"''') | ||
|
||
rules = str_to_rules(tmpdir, 'include: base2') | ||
assert len(rules) == 2 | ||
|
||
def test_context(tmpdir): | ||
base = tmpdir.join('base.yaml') | ||
base.write(''' | ||
context: | ||
name: "version" | ||
patterns: | ||
- name: Python version | ||
match: Python (3.\\d+\\.\\d+\\.\\d+) | ||
message: "Python version: {{0}}"''') | ||
|
||
rule = str_to_rules(tmpdir, 'include: base')[0] | ||
assert rule.global_context['name'].pattern == 'version' | ||
|
||
rule = str_to_rules(tmpdir, '''\ | ||
include: base | ||
context: | ||
name: "new" | ||
''')[0] | ||
assert rule.global_context['name'].pattern == 'new' | ||
|
||
def test_include_list(tmpdir): | ||
base = tmpdir.join('base.yaml') | ||
base.write(''' | ||
context: | ||
name: "version" | ||
patterns: | ||
- name: Python version | ||
match: Python (3.\\d+\\.\\d+\\.\\d+) | ||
message: "Python version: {{0}}"''') | ||
|
||
base2 = tmpdir.join('base2.yaml') | ||
base2.write(''' | ||
context: | ||
name: "version" | ||
patterns: | ||
- name: Build version | ||
match: Build (\\d+) | ||
message: "Buildd version: {{0}}"''') | ||
|
||
rules = str_to_rules(tmpdir, ''' | ||
include: | ||
- base | ||
- base2 | ||
''') | ||
assert rules[0].name == 'Python version' | ||
assert rules[1].name == 'Build version' |
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