Skip to content

Commit

Permalink
Fix #44 and add support for comments starting with '#'
Browse files Browse the repository at this point in the history
  • Loading branch information
minomicetto committed Jul 10, 2024
1 parent 3c33f7d commit 079c013
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
19 changes: 13 additions & 6 deletions PyOphidia/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,16 +905,23 @@ def wisvalid(self, workflow):
return False
w = None

# Remove comment blocks
checked_workflow = re.sub(re.compile(r"/\*.*?\*/|//.*?\n", re.DOTALL), "\n", workflow)

if isinstance(checked_workflow, str):
if isinstance(workflow, str):
try:
# Remove comment blocks
checked_workflow = re.sub(r"(?m)^ *#.*\n?", "", workflow)
pattern = r"(\".*?\"|\'.*?\')|(/\*.*?\*/|//[^\r\n]*$)"
regex = re.compile(pattern, re.MULTILINE|re.DOTALL)
def _replacer(match):
if match.group(2) is not None:
return ""
else:
return match.group(1)
checked_workflow = regex.sub(_replacer, checked_workflow)
w = json.loads(checked_workflow)
except ValueError:
return False, "Workflow is not a valid JSON"
elif isinstance(checked_workflow, dict):
w = checked_workflow
elif isinstance(workflow, dict):
w = workflow
else:
return False, "Workflow is not a valid dictionary"
if "name" not in w or not w["name"]:
Expand Down
54 changes: 34 additions & 20 deletions PyOphidia/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,39 @@ def start_experiment(data):
experiment = start_experiment(data)
return experiment

def validate(self):
"""
Check the workflow experiment definition validity
Returns
-------
True in case of valid workflow, False otherwise
Example
-------
e1 = Experiment(name="Experiment 1", author="sample author",
abstract="sample abstract")
t1 = e1.newTask(operator="oph_reduce", arguments={'operation': 'avg'},
dependencies={})
e1.validate()
"""
try:
from client import Client
except ImportError:
from .client import Client

client = Client(
local_mode=True,
)

experiment_validity = client.wisvalid(
json.dumps(self.workflow_to_json())
)
if experiment_validity[1] == "experiment is valid":
return True
else:
return False

def check(self, filename="sample.dot", visual=True):
"""
Check the workflow experiment definition validity and display the
Expand Down Expand Up @@ -521,26 +554,7 @@ def _find_subgraphs(tasks):
cluster_counter += 1
return subgraphs_list

def _check_experiment_validity():

try:
from client import Client
except ImportError:
from .client import Client

client = Client(
local_mode=True,
)

experiment_validity = client.wisvalid(
json.dumps(self.workflow_to_json())
)
if experiment_validity[1] == "experiment is valid":
return True
else:
return False

experiment_validity = _check_experiment_validity()
experiment_validity = self.validate()
self.__param_check(
[
{"name": "filename", "value": filename, "type": str},
Expand Down

0 comments on commit 079c013

Please sign in to comment.