Skip to content

Commit

Permalink
Bordel
Browse files Browse the repository at this point in the history
  • Loading branch information
yves-chevallier committed Oct 14, 2024
1 parent f9764c5 commit 113f626
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 382 deletions.
30 changes: 30 additions & 0 deletions baygon/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
A Validated schema is modifed into a BaygonConfig in which:
- Points a computed from `weights` and `points` fields.
- Filters are converted into a Filters object.
- Tests are converted into a Tests object.
- Elements such as `executable`, `env`, `tty`, `timeout` are propagated to the tests.
- exectuable string is converted into Executable object.
"""

from .schema import SchemaConfig
from .filters import Filters
from .matchers import MatcherFactory
from .executables import Executable
from .score import assign_points


class BaygonConfig:
def __init__(self, data):
self.data = SchemaConfig(data)

assign_points(self.data)

def _traverse(self, data):
if "executable" in data:
data["executable"] = Executable(data["executable"])
if "filters" in data:
data["filters"] = Filters(data["filters"])
if "tests" in data:
self._traverse(data["tests"])
20 changes: 13 additions & 7 deletions baygon/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from abc import ABC, abstractmethod
from collections.abc import Sequence
from functools import lru_cache

from .helpers import parse_pcre_flags
from .error import InvalidFilterError
from .kernel import RestrictedEvaluator

Expand Down Expand Up @@ -106,13 +106,13 @@ class FilterReplace(Filter):
'world world'
"""

def __init__(self, pattern: str, replace: str):
def __init__(self, search: str, replace: str):
super().__init__()
self.pattern = pattern
self.search = search
self.replace = replace

def apply(self, value: str) -> str:
return value.replace(self.pattern, self.replace)
return value.replace(self.search, self.replace)


class FilterRegex(Filter):
Expand All @@ -122,11 +122,11 @@ class FilterRegex(Filter):
'h-ll- w-rld'
"""

def __init__(self, pattern: str, replace: str, flags=[]):
def __init__(self, pattern: str, replace: str, flags=""):
super().__init__()
self.pattern = pattern
self.replace = replace
self.regex = re.compile(pattern, flags)
self.regex = re.compile(pattern, parse_pcre_flags(flags))

def apply(self, value: str) -> str:
return self.regex.sub(self.replace, value)
Expand Down Expand Up @@ -182,7 +182,13 @@ def _parse_filter(self, filters):
args = [args]
instances.append(FilterFactory(name, *args))
return instances

if isinstance(filters, list):
instances = []
for f in filters:
if not isinstance(f, Filter):
raise InvalidFilterError(f"Invalid type for filter: {type(f)}")
instances.append(f)
return instances
raise InvalidFilterError(f"Invalid type for filters: {type(filters)}")

def __getitem__(self, index):
Expand Down
Loading

0 comments on commit 113f626

Please sign in to comment.