Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #398 from simphony/revert-script-utils-import-in-v…
Browse files Browse the repository at this point in the history
…alidation-master

Revert relocation of validation support code to the module itself
  • Loading branch information
stefanoborini committed Mar 10, 2017
2 parents cdde241 + b8d63e2 commit db393e6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 4 deletions.
45 changes: 43 additions & 2 deletions scripts/validation.template
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
# This file is copied and renamed in simphony/cuds/meta/ to support the
# meta classes in performing validation
import warnings

import re
import numpy

from scripts.utils import to_camel_case, without_cuba_prefix

def to_camel_case(text, special={'cuds': 'CUDS'}):
""" Convert text to CamelCase (for class name)

Parameters
----------
text : str
The text to be converted

special : dict
If any substring of text (lower case) matches a key of `special`,
the substring is replaced by the value

Returns
-------
result : str
"""

def replace_func(matched):
# word should be lower case already
word = matched.group(0).strip("_")
if word in special:
# Handle special case
return special[word]
else:
# Capitalise the first character
return word[0].upper()+word[1:]

return re.sub(r'(_?[a-zA-Z]+)', replace_func, text.lower())


def without_cuba_prefix(string):
"""Removes the CUBA. prefix to the string if there."""
if is_cuba_key(string):
return string[5:]

return string


def is_cuba_key(value):
"""True if value is a qualified cuba key"""
return isinstance(value, (str, unicode)) and value.startswith("CUBA.")


def check_valid_shape(value, shape, cuba_key):
Expand Down
45 changes: 43 additions & 2 deletions simphony/cuds/meta/validation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
# This file is copied and renamed in simphony/cuds/meta/ to support the
# meta classes in performing validation
import warnings

import re
import numpy

from scripts.utils import to_camel_case, without_cuba_prefix

def to_camel_case(text, special={'cuds': 'CUDS'}):
""" Convert text to CamelCase (for class name)
Parameters
----------
text : str
The text to be converted
special : dict
If any substring of text (lower case) matches a key of `special`,
the substring is replaced by the value
Returns
-------
result : str
"""

def replace_func(matched):
# word should be lower case already
word = matched.group(0).strip("_")
if word in special:
# Handle special case
return special[word]
else:
# Capitalise the first character
return word[0].upper() + word[1:]

return re.sub(r'(_?[a-zA-Z]+)', replace_func, text.lower())


def without_cuba_prefix(string):
"""Removes the CUBA. prefix to the string if there."""
if is_cuba_key(string):
return string[5:]

return string


def is_cuba_key(value):
"""True if value is a qualified cuba key"""
return isinstance(value, (str, unicode)) and value.startswith("CUBA.")


def check_valid_shape(value, shape, cuba_key):
Expand Down

0 comments on commit db393e6

Please sign in to comment.