Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ target/
# PyCharm IDE stuff
.idea
simphony/version.py
simphony-metadata
*-venv
venv
17 changes: 8 additions & 9 deletions scripts/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import click
import yaml

from . import validation

# May be 'simphony.meta', we can make this as a command-line attribute
PATH_TO_CLASSES = ''

Expand All @@ -30,9 +28,6 @@
# FIXME: These are excluded because they are not defined CUBA keys
EXCLUDE_SUPPORTED_PARAMETERS = ('definition', 'models', 'variables', 'data',)

# validation.py for validation codes.
VALIDATION_PY_PATH = os.path.splitext(validation.__file__)[0]+'.py'

# keywords that are excludes from DataContainers
CUBA_DATA_CONTAINER_EXCLUDE = ['Id', 'Position']

Expand Down Expand Up @@ -955,15 +950,19 @@ def meta_class(yaml_file, out_path, overwrite):
# Create validation.py
validation_path = os.path.join(temp_dir, 'validation.py')

from . import validation
# validation.py for validation codes.
validation_py_path = os.path.splitext(validation.__file__)[0]+'.py'

with open(validation_path, 'wb') as dst_file, \
open(VALIDATION_PY_PATH, 'rb') as src_file:
open(validation_py_path, 'rb') as src_file:

# Replace import path for KEYWORDS
def read_lines(src_file):
while True:
line = src_file.next()
yield re.sub(r'.+import KEYWORDS',
IMPORT_PATHS['KEYWORDS'], line)
yield re.sub(r'(\s*).+import KEYWORDS',
"\\1"+IMPORT_PATHS['KEYWORDS'], line)

# Copy the rest of the file
print(*read_lines(src_file), file=dst_file, sep='')
Expand Down Expand Up @@ -1068,7 +1067,7 @@ def keywords(cuba_input, cuds_input, output):
content['type'] = "None"
content['name'] = to_camel_case(keyword)
content['key'] = keyword
content['shape'] = None
content['shape'] = [1]
lines.extend(template.format(**content))
lines.append('}\n')

Expand Down
2 changes: 1 addition & 1 deletion scripts/tests/test_meta_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def test_Basis(self):

def test_Origin(self):
origin = meta_class.Origin()
arr = origin.point == numpy.array([0, 0, 0])
arr = origin.position == numpy.array([0, 0, 0])
self.assertTrue(arr.all())

def test_Box(self):
Expand Down
5 changes: 3 additions & 2 deletions scripts/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

import numpy

from simphony.core.keywords import KEYWORDS


def to_camel_case(text, special={'cuds': 'CUDS'}):
""" Convert text to CamelCase (for class name)
Expand Down Expand Up @@ -132,6 +130,7 @@ def validate_cuba_keyword(value, key):
the value is not an instance of that class
'''
from . import api
from simphony.core.keywords import KEYWORDS

# Sanitising, although generated code already did
key = key.replace('CUBA.', '')
Expand Down Expand Up @@ -206,6 +205,8 @@ def cast_data_type(value, key):
TypeError
If casting would be unsafe
'''
from simphony.core.keywords import KEYWORDS

keyword_name = key.upper()

if keyword_name in KEYWORDS:
Expand Down
98 changes: 98 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import os
import textwrap
from subprocess import check_call

from setuptools import setup, find_packages
from distutils.cmd import Command
from distutils.command.build import build
from distutils.command.install import install

# Read description
with open('README.rst', 'r') as readme:
Expand All @@ -10,6 +15,94 @@
VERSION = '0.3.1.dev0'


class BuildMeta(Command):
user_options = []

def initialize_options(self):
self.simphony_metadata_path = None

def finalize_options(self):
if self.simphony_metadata_path is None:
self.simphony_metadata_path = os.path.join(
os.getcwd(),
"simphony-metadata/")
try:
if not os.path.exists(self.simphony_metadata_path):
check_call([
"git",
"clone",
"https://github.com/simphony/simphony-metadata/"])
except OSError:
print (textwrap.dedent("""
Failed to run git. Make sure it is installed in your
environment"""))

def run(self):
metadata_yml = os.path.join(
self.simphony_metadata_path,
"yaml_files",
"simphony_metadata.yml")
cuba_yml = os.path.join(
self.simphony_metadata_path,
"yaml_files",
"cuba.yml")

if not (os.path.exists(cuba_yml) and os.path.exists(metadata_yml)):
print (textwrap.dedent("""
Cannot open simphony-metadata YAML files.
Please specify an appropriate path to the simphony-metadata
git repository in setup.cfg:

[build]
simphony_metadata_path=path/to/simphony-metadata-repo/
"""))
raise

with open(metadata_yml, 'rb') as simphony_metadata:
from scripts.generate import meta_class
meta_class.callback(simphony_metadata, "simphony/cuds/meta/", True)

with open(metadata_yml, 'rb') as simphony_metadata, \
open(cuba_yml, 'rb') as cuba, \
open("simphony/core/keywords.py", "wb") as keywords_out:

from scripts.generate import keywords
keywords.callback(cuba, simphony_metadata, keywords_out)

with open(metadata_yml, 'rb') as simphony_metadata, \
open(cuba_yml, 'rb') as cuba, \
open("simphony/core/cuba.py", "wb") as cuba_out:

from scripts.generate import cuba_enum
cuba_enum.callback(cuba, simphony_metadata, cuba_out)

cmd_args = ["yapf", "--style", "pep8", "--in-place"]
try:
check_call(cmd_args + ["simphony/core/keywords.py"])
check_call(cmd_args + ["simphony/core/keywords.py"])
check_call(cmd_args + ["--recursive", "simphony/cuds/meta/"])
except OSError:
print (textwrap.dedent("""
Failed to run yapf. Make sure it is installed in your
python environment, by running

pip install yapf
"""))
raise


class CustomBuild(build):
sub_commands = build.sub_commands + [
('build_meta', None)
]


class CustomInstall(install):
sub_commands = install.sub_commands + [
('build_meta', None)
]


def write_version_py(filename=None):
if filename is None:
filename = os.path.join(
Expand Down Expand Up @@ -39,6 +132,11 @@ def write_version_py(filename=None):
'H5IO': ["tables>=3.1.1"],
'CUBAGen': ["click >= 3.3", "pyyaml >= 3.11"]},
packages=find_packages(),
cmdclass={
'build_meta': BuildMeta,
'build': CustomBuild,
'install': CustomInstall,
},
entry_points={
'console_scripts': [
('simphony-meta-generate = '
Expand Down
Loading