-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved installing services, added cleancontract, added string utils…
…, added processes
- Loading branch information
1 parent
d67cc6e
commit 7dc3f78
Showing
9 changed files
with
186 additions
and
1 deletion.
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
Empty file.
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,8 @@ | ||
from abc import ABC | ||
|
||
from fractal.core.process.process_scope import ProcessScope | ||
|
||
|
||
class Action(ABC): | ||
def execute(self, scope: ProcessScope) -> ProcessScope: | ||
pass |
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,66 @@ | ||
from dataclasses import asdict | ||
|
||
from fractal.core.process.action import Action | ||
from fractal.core.process.process_scope import ProcessScope | ||
from fractal.core.specifications.generic.specification import Specification | ||
|
||
|
||
class SetValueAction(Action): | ||
def __init__(self, **kwargs): | ||
self.kwargs = kwargs | ||
|
||
def execute(self, scope: ProcessScope) -> ProcessScope: | ||
return scope.update(ProcessScope(self.kwargs)) | ||
|
||
|
||
class IncreaseValueAction(Action): | ||
def __init__(self, name, value): | ||
self.name = name | ||
self.value = value | ||
|
||
def execute(self, scope: ProcessScope) -> ProcessScope: | ||
scope[self.name] += self.value | ||
return scope | ||
|
||
|
||
class PrintAction(Action): | ||
def __init__(self, text): | ||
self.text = text | ||
|
||
def execute(self, scope: ProcessScope) -> ProcessScope: | ||
print(self.text) | ||
return scope | ||
|
||
|
||
class PrintValueAction(Action): | ||
def __init__(self, name): | ||
self.name = name | ||
|
||
def execute(self, scope: ProcessScope) -> ProcessScope: | ||
print(scope[self.name]) | ||
return scope | ||
|
||
|
||
class AddEntityAction(Action): | ||
def __init__(self, repository_key: str = "repository", **entity_defaults): | ||
self.repository_key = repository_key | ||
self.entity_defaults = entity_defaults | ||
|
||
def execute(self, scope: ProcessScope) -> ProcessScope: | ||
entity_class = scope[self.repository_key].entity | ||
data = self.entity_defaults | ||
if hasattr(scope, "contract"): | ||
data.update(scope["contract"]) | ||
entity = entity_class(**data) | ||
scope["entity"] = scope[self.repository_key].add(entity) | ||
return scope | ||
|
||
|
||
class FetchEntityAction(Action): | ||
def __init__(self, specification: Specification, repository_key: str = "repository"): | ||
self.specification = specification | ||
self.repository_key = repository_key | ||
|
||
def execute(self, scope: ProcessScope) -> ProcessScope: | ||
scope["entity"] = scope[self.repository_key].find_one(self.specification) | ||
return scope |
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,43 @@ | ||
from typing import Iterable, Optional | ||
|
||
from fractal.core.process.action import Action | ||
from fractal.core.process.process import Process | ||
from fractal.core.process.process_scope import ProcessScope | ||
from fractal.core.specifications.generic.specification import Specification | ||
|
||
|
||
class IfElseAction(Action): | ||
def __init__(self, specification: Specification, process_true: Process, process_false: Optional[Process] = None): | ||
self.specification = specification | ||
self.process_true = process_true | ||
self.process_false = process_false | ||
|
||
def execute(self, scope: ProcessScope) -> ProcessScope: | ||
if self.specification.is_satisfied_by(scope): | ||
scope.update(self.process_true.run(scope)) | ||
elif self.process_false: | ||
scope.update(self.process_false.run(scope)) | ||
return scope | ||
|
||
|
||
class WhileAction(Action): | ||
def __init__(self, specification: Specification, process: Process): | ||
self.specification = specification | ||
self.process = process | ||
|
||
def execute(self, scope: ProcessScope) -> ProcessScope: | ||
while self.specification.is_satisfied_by(scope): | ||
scope.update(self.process.run(scope)) | ||
return scope | ||
|
||
|
||
class ForEachAction(Action): | ||
def __init__(self, iterable: Iterable, process: Process): | ||
self.iterable = iterable | ||
self.process = process | ||
|
||
def execute(self, scope: ProcessScope) -> ProcessScope: | ||
for item in self.iterable: | ||
scope["item"] = item | ||
scope.update(self.process.run(scope)) | ||
return scope |
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,16 @@ | ||
from typing import Optional, List | ||
|
||
from fractal.core.process.action import Action | ||
from fractal.core.process.process_scope import ProcessScope | ||
|
||
|
||
class Process: | ||
def __init__(self, actions: List[Action]): | ||
self.actions = actions | ||
|
||
def run(self, scope: Optional[ProcessScope] = None) -> ProcessScope: | ||
if not scope: | ||
scope = ProcessScope() | ||
for action in self.actions: | ||
scope.update(action.execute(scope)) | ||
return scope |
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,25 @@ | ||
from typing import Optional, Dict | ||
|
||
|
||
class ProcessScope: | ||
def __init__(self, data: Optional[Dict] = None): | ||
self.data = data or {} | ||
|
||
def __getattr__(self, item): | ||
return self.data.get(item) | ||
|
||
def __getitem__(self, item): | ||
return self.data.get(item) | ||
|
||
def __setitem__(self, key, value): | ||
self.data[key] = value | ||
|
||
def __repr__(self): | ||
return self.data.__repr__() | ||
|
||
def update(self, scope: 'ProcessScope') -> 'ProcessScope': | ||
self.data.update(scope.data) | ||
return self | ||
|
||
def get(self, key, default=None): | ||
return self.data.get(key, default) |
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,10 @@ | ||
import re | ||
|
||
|
||
def camel_to_snake(name): | ||
name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) | ||
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower() | ||
|
||
|
||
def snake_to_camel(name): | ||
return ''.join(word.title() for word in name.split('_')) |