Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
only allow valid python identifiers as resource names
Browse files Browse the repository at this point in the history
See #436
  • Loading branch information
mzuehlke committed Sep 28, 2017
1 parent 3663ded commit 85213b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions cate/core/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ def delete_resource(self, res_name: str):
del self._resource_cache[res_name]

def rename_resource(self, res_name: str, new_res_name: str) -> None:
Workspace._validate_res_name(new_res_name)
with self._lock:
res_step = self.workflow.find_node(res_name)
if res_step is None:
Expand Down Expand Up @@ -533,6 +534,7 @@ def set_resource(self,
default_res_pattern = conf.get_default_res_pattern()
res_pattern = op.op_meta_info.header.get('res_pattern', default_res_pattern)
res_name = self._new_resource_name(res_pattern)
Workspace._validate_res_name(res_name)

new_step = OpStep(op, node_id=res_name)

Expand Down Expand Up @@ -656,6 +658,15 @@ def _assert_open(self):
def _new_resource_name(self, res_pattern):
return new_indexed_name({step.id for step in self.workflow.steps}, res_pattern)

@staticmethod
def _validate_res_name(res_name: str):
if not res_name.isidentifier():
raise WorkspaceError(
"Resource name '%s' is not valid. "
"The name must only contain the uppercase and lowercase letters A through Z, the underscore _ and, "
"except for the first character, the digits 0 through 9." % res_name)



# noinspection PyArgumentList
class WorkspaceError(Exception):
Expand Down
19 changes: 18 additions & 1 deletion test/core/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import xarray as xr

from cate.core.workflow import Workflow, OpStep
from cate.core.workspace import Workspace, mk_op_arg, mk_op_args, mk_op_kwargs
from cate.core.workspace import Workspace, WorkspaceError, mk_op_arg, mk_op_args, mk_op_kwargs
from cate.util import UNDEFINED
from cate.util.opmetainf import OpMetaInfo

Expand Down Expand Up @@ -304,6 +304,23 @@ def set_resource_and_execute():
expected_res_names = {'res_%s' % (i + 1) for i in range(num_res)}
self.assertEqual(actual_res_names, expected_res_names)

def test_validate_res_name(self):
Workspace._validate_res_name("a")
Workspace._validate_res_name("A")
Workspace._validate_res_name("abc_42")
Workspace._validate_res_name("abc42")
Workspace._validate_res_name("_abc42")
# with self.assertRaises(WorkspaceError):
# Workspace._validate_res_name("0")
with self.assertRaises(WorkspaceError):
Workspace._validate_res_name("a-b")
with self.assertRaises(WorkspaceError):
Workspace._validate_res_name("a+b")
with self.assertRaises(WorkspaceError):
Workspace._validate_res_name("a.b")
with self.assertRaises(WorkspaceError):
Workspace._validate_res_name("file://path")

def test_example(self):
expected_json_text = """{
"schema_version": 1,
Expand Down

0 comments on commit 85213b6

Please sign in to comment.