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

Commit

Permalink
adds support for loading preplate models via python package
Browse files Browse the repository at this point in the history
  • Loading branch information
BradMclain committed Oct 30, 2018
1 parent 5758451 commit 5c2eac9
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 44 deletions.
2 changes: 1 addition & 1 deletion prestans/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@

__all__ = ['http', 'types', 'rest', 'parser', 'serializer', 'deserializer', 'provider', 'ext', 'exception']

__version_info__ = (2, 3, 3)
__version_info__ = (2, 4, 0)
__version__ = '.'.join(str(v) for v in __version_info__)
18 changes: 12 additions & 6 deletions prestans/devel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ def _add_generate_sub_commands(self):
"-m",
"--model",
required=True,
dest="model_path",
help="path to models description file"
dest="models_definition",
help="path to models definition file or package"
)


gen_parser.add_argument(
"-o",
"--output",
Expand Down Expand Up @@ -149,15 +148,22 @@ def _dispatch_gen(self):
if not os.path.isdir(self._args.output):
raise exception.Base("%s is not a writeable directory" % self._args.output)

if not os.path.isfile(self._args.model_path):
raise exception.Base("failed to read model file %s" % self._args.model_path)
if not os.path.isfile(self._args.models_definition):
if not self.check_package_exists(self._args.models_definition):
raise exception.Base("failed to locate package or models definitions file at: %s" % self._args.models_definition)

from prestans.devel.gen import Preplate
preplate = Preplate(
template_type=self._args.template,
model_file=self._args.model_path,
models_definition=self._args.models_definition,
namespace=self._args.namespace,
filter_namespace=self._args.filter_namespace,
output_directory=self._args.output)

preplate.run()

@classmethod
def check_package_exists(cls, package):
import importlib
package_spec = importlib.util.find_spec(name=package)
return package_spec is not None
38 changes: 22 additions & 16 deletions prestans/devel/gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,27 @@

class Inspector(object):

def __init__(self, model_file):
self._model_file = model_file
def __init__(self, models_definition):
self._models_definition = models_definition

self._is_file = os.path.isfile(self._models_definition)

def inspect(self):

blueprints = list()

try:
sys.path.append(os.path.dirname(self._model_file))
model_python_file = os.path.split(self._model_file)[1:][0]
namespace = model_python_file.split(".")[0]
models = __import__(namespace, globals(), locals(), [])
except ImportError as import_error:
print ("\nFailed to process %s, %s" % (self._model_file, import_error))
sys.exit(1)
if self._is_file:
try:
sys.path.append(os.path.dirname(self._models_definition))
model_python_file = os.path.split(self._models_definition)[1:][0]
namespace = model_python_file.split(".")[0]
models = __import__(namespace, globals(), locals(), [])
except ImportError as import_error:
print ("\nFailed to process %s, %s" % (self._models_definition, import_error))
sys.exit(1)
else:
import importlib
models = importlib.import_module(name=self._models_definition)

for name, type_instance in iter(models.__dict__.items()):
if name.startswith('__') or inspect.ismethod(type_instance) or not inspect.isclass(type_instance) or\
Expand All @@ -70,10 +76,10 @@ def inspect(self):

class Preplate(object):

def __init__(self, template_type, model_file, namespace, filter_namespace, output_directory):
def __init__(self, template_type, models_definition, namespace, filter_namespace, output_directory):

self._template_type = template_type
self._model_file = model_file
self._models_definition = models_definition
self._namespace = namespace
self._filter_namespace = filter_namespace
self._output_directory = output_directory
Expand All @@ -91,8 +97,8 @@ def template_type(self):
return self._template_type

@property
def model_file(self):
return self._model_file
def models_definition(self):
return self._models_definition

@property
def namespace(self):
Expand All @@ -117,7 +123,7 @@ def run(self):
from prestans.devel.gen.closure import Model
template = Model(
template_engine=self._template_engine,
model_file=self._model_file,
models_definition=self.models_definition,
namespace=self._namespace,
filter_namespace=self._filter_namespace,
output_directory=self._output_directory
Expand All @@ -126,7 +132,7 @@ def run(self):
from prestans.devel.gen.closure import Filter
template = Filter(
template_engine=self._template_engine,
model_file=self._model_file,
models_definition=self.models_definition,
namespace=self._namespace,
output_directory=self._output_directory
)
Expand Down
16 changes: 8 additions & 8 deletions prestans/devel/gen/closure.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,9 @@ def element_template_is_model(self):

class Base(object):

def __init__(self, template_engine, model_file, namespace, output_directory):
def __init__(self, template_engine, models_definition, namespace, output_directory):
self._template_engine = template_engine
self._model_file = model_file
self._models_definition = models_definition
self._namespace = namespace
self._output_directory = output_directory
self._dependencies = list()
Expand Down Expand Up @@ -455,15 +455,15 @@ def attribute_string(self):

class Model(Base):

def __init__(self, template_engine, model_file, namespace, filter_namespace, output_directory):
def __init__(self, template_engine, models_definition, namespace, filter_namespace, output_directory):

Base.__init__(self, template_engine, model_file, namespace, output_directory)
Base.__init__(self, template_engine, models_definition, namespace, output_directory)
self._filter_namespace = filter_namespace
self._template = self._template_engine.get_template("closure/model/model.jinja")

def run(self):

inspector = prestans.devel.gen.Inspector(model_file=self._model_file)
inspector = prestans.devel.gen.Inspector(models_definition=self._models_definition)
blueprints = inspector.inspect()

for model_blueprint in blueprints:
Expand Down Expand Up @@ -507,14 +507,14 @@ def run(self):

class Filter(Base):

def __init__(self, template_engine, model_file, namespace, output_directory):
def __init__(self, template_engine, models_definition, namespace, output_directory):

Base.__init__(self, template_engine, model_file, namespace, output_directory)
Base.__init__(self, template_engine, models_definition, namespace, output_directory)
self._template = self._template_engine.get_template("closure/filter/filter.jinja")

def run(self):

inspector = prestans.devel.gen.Inspector(model_file=self._model_file)
inspector = prestans.devel.gen.Inspector(models_definition=self._models_definition)
blueprints = inspector.inspect()

for model_blueprint in blueprints:
Expand Down
10 changes: 5 additions & 5 deletions tests/devel/gen/closure/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def test_init(self):

base = Base(
template_engine=None,
model_file="models.py",
models_definition="models.py",
namespace="namespace.models",
output_directory="namespace/models"
)
self.assertIsNone(base._template_engine)
self.assertEqual(base._model_file, "models.py")
self.assertEqual(base._models_definition, "models.py")
self.assertEqual(base._namespace, "namespace.models")
self.assertEqual(base._output_directory, "namespace/models")
self.assertEqual(base._dependencies, [])
Expand All @@ -25,7 +25,7 @@ def test_init(self):
def test_add_filter_dependency(self):
base = Base(
template_engine=None,
model_file="models.py",
models_definition="models.py",
namespace="namespace.filters",
output_directory="namespace/filters"
)
Expand Down Expand Up @@ -58,7 +58,7 @@ class Log(types.Model):
def test_add_model_dependency(self):
base = Base(
template_engine=None,
model_file="models.py",
models_definition="models.py",
namespace="namespace.models",
output_directory="namespace/models"
)
Expand Down Expand Up @@ -103,7 +103,7 @@ class User(types.Model):
def test_add_attribute_string(self):
base = Base(
template_engine=None,
model_file="models.py",
models_definition="models.py",
namespace="namespace.models",
output_directory="namespace/models"
)
Expand Down
15 changes: 7 additions & 8 deletions tests/devel/gen/test_preplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@ class PreplateTest(unittest.TestCase):
def test_init(self):
preplate = Preplate(
template_type="closure.model",
model_file="project/models.py",
models_definition="project/models.py",
namespace="project.namespace.models",
filter_namespace="project.namespace.filters",
output_directory="project/namespace/models"
)
self.assertEqual(preplate.template_type, "closure.model")
self.assertEqual(preplate.model_file, "project/models.py")
self.assertEqual(preplate.models_definition, "project/models.py")
self.assertEqual(preplate.namespace, "project.namespace.models")
self.assertEqual(preplate.filter_namespace, "project.namespace.filters")
self.assertEqual(preplate.output_directory, "project/namespace/models")

from jinja2 import Environment
self.assertTrue(isinstance(preplate.template_engine, Environment))


def test_run_unknown_template(self):
preplate = Preplate(
template_type="closure.unknown",
model_file="models.py",
models_definition="models.py",
namespace="namespace.models",
filter_namespace="project.namespace.filters",
output_directory="namespace/models"
Expand All @@ -39,15 +38,15 @@ def test_run_unknown_template(self):
def test_run_closure_model(self, model_init, model_run):
preplate = Preplate(
template_type="closure.model",
model_file="models.py",
models_definition="models.py",
namespace="namespace.models",
filter_namespace="namespace.filters",
output_directory="namespace/models"
)
self.assertEqual(preplate.run(), 0)
model_init.assert_called_with(
template_engine=preplate.template_engine,
model_file=preplate.model_file,
models_definition=preplate.models_definition,
namespace=preplate.namespace,
filter_namespace=preplate.filter_namespace,
output_directory=preplate.output_directory
Expand All @@ -59,15 +58,15 @@ def test_run_closure_model(self, model_init, model_run):
def test_run_closure_filter(self, filter_init, filter_run):
preplate = Preplate(
template_type="closure.filter",
model_file="models.py",
models_definition="models.py",
namespace="namespace.filter",
filter_namespace="namespace.filters",
output_directory="namespace/filter"
)
self.assertEqual(preplate.run(), 0)
filter_init.assert_called_with(
template_engine=preplate.template_engine,
model_file=preplate.model_file,
models_definition=preplate.models_definition,
namespace=preplate.namespace,
output_directory=preplate.output_directory
)
Expand Down

0 comments on commit 5c2eac9

Please sign in to comment.