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

Commit

Permalink
Merge pull request #160 from anomaly/issue156-python3-compatibility
Browse files Browse the repository at this point in the history
Adds python3 compatibility
  • Loading branch information
BradMclain authored Feb 26, 2018
2 parents 172c821 + 5a0a8cd commit 067f3af
Show file tree
Hide file tree
Showing 106 changed files with 10,451 additions and 4,645 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = tests/*
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
test:
python setup.py test

.PHONY: tests
tests:
tox

.PHONY: coverage
coverage:
py.test --cov-report term-missing:skip-covered --cov-config .coveragerc --cov=prestans tests

.PHONY: dist
dist:
python setup.py sdist bdist_wheel
Expand All @@ -10,6 +18,7 @@ dist:
release:
python setup.py sdist bdist_wheel upload

.PHONY: clean
clean:
python setup.py clean
if [ -a .eggs ]; then rm -rf .eggs; fi;
Expand Down
1 change: 1 addition & 0 deletions prestans/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
from __future__ import unicode_literals

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

Expand Down
21 changes: 10 additions & 11 deletions prestans/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import prestans.exception


class Base(object):

def loads(self, input_string):
Expand All @@ -46,14 +47,15 @@ def content_type(self):
class JSON(Base):

def loads(self, input_string):

import json
parsed_json = None

if isinstance(input_string, bytes):
input_string = input_string.decode()

try:
parsed_json = json.loads(input_string)
except Exception, exp:
raise prestans.exception.DeSerializationFailedError('JSON')
except Exception as exp:
raise prestans.exception.DeSerializationFailedError("JSON: %s" % exp)

return parsed_json

Expand All @@ -64,17 +66,14 @@ def content_type(self):
class XMLPlist(Base):

def loads(self, input_string):

import plistlib
unserialized_plist = None

try:
unserialized_plist = plistlib.readPlistFromString(input_string)
except Exception, exp:
raise prestans.exception.DeSerializationFailedError("XML/Plist")
plist_object = plistlib.readPlistFromString(input_string)
except Exception as exp:
raise prestans.exception.DeSerializationFailedError("XMLPlist: %s" % exp)

return unserialized_plist
return plist_object

def content_type(self):
return 'application/xml'

4 changes: 2 additions & 2 deletions prestans/devel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import argparse
import os

from prestans import exception
from prestans.devel import exception
from prestans import __version__


Expand Down Expand Up @@ -71,7 +71,7 @@ def parse(self):

def _add_generate_sub_commands(self):
"""
Subcommands for generating models for usage by clients.
Sub commands for generating models for usage by clients.
Currently supports Google Closure.
"""

Expand Down
21 changes: 13 additions & 8 deletions prestans/devel/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#


class Base(Exception):

def __init__(self, message, error_code=2):
self._error_code = error_code
self._message = message
def __init__(self, message, error_code=2):
self._error_code = error_code
self._message = message

@property
def error_code(self):
return self._error_code

@property
def error_code(self):
return self._error_code
@property
def message(self):
return self._message

def __str__(self):
return self._message
def __str__(self):
return self._message
57 changes: 47 additions & 10 deletions prestans/devel/gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
import inspect
import jinja2
import os
import sys

Expand All @@ -56,17 +55,16 @@ def inspect(self):
print ("\nFailed to process %s, %s" % (self._model_file, import_error))
sys.exit(1)

for name, type_instance in models.__dict__.iteritems():
for name, type_instance in iter(models.__dict__.items()):
if name.startswith('__') or inspect.ismethod(type_instance) or not inspect.isclass(type_instance) or\
not issubclass(type_instance, prestans.types.Model):
continue

blueprint = type_instance().blueprint()
for field_name, field_blueprint in blueprint['fields'].iteritems():
for field_name, field_blueprint in iter(blueprint['fields'].items()):
field_blueprint['map_name'] = type_instance().attribute_rewrite_map()[field_name]
blueprints.append(blueprint)


return blueprints


Expand All @@ -79,20 +77,59 @@ def __init__(self, template_type, model_file, namespace, filter_namespace, outpu
self._namespace = namespace
self._filter_namespace = filter_namespace
self._output_directory = output_directory
loader = jinja2.PackageLoader('prestans', 'devel/gen/templates')
self._template_engine = jinja2.Environment(trim_blocks=True, loader=loader)

from jinja2 import Environment
from jinja2 import PackageLoader

loader = PackageLoader('prestans', 'devel/gen/templates')
self._template_engine = Environment(trim_blocks=True, loader=loader)
self._template_engine.globals["isinstance"] = isinstance
self._template_engine.globals["list"] = list

@property
def template_type(self):
return self._template_type

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

@property
def namespace(self):
return self._namespace

@property
def filter_namespace(self):
return self._filter_namespace

@property
def output_directory(self):
return self._output_directory

@property
def template_engine(self):
return self._template_engine

def run(self):

template = None
if self._template_type == "closure.model":
template = prestans.devel.gen.closure.Model(template_engine=self._template_engine,
model_file=self._model_file, namespace=self._namespace, filter_namespace=self._filter_namespace, output_directory=self._output_directory)
from prestans.devel.gen.closure import Model
template = Model(
template_engine=self._template_engine,
model_file=self._model_file,
namespace=self._namespace,
filter_namespace=self._filter_namespace,
output_directory=self._output_directory
)
elif self._template_type == "closure.filter":
template = prestans.devel.gen.closure.Filter(template_engine=self._template_engine,
model_file=self._model_file, namespace=self._namespace, output_directory=self._output_directory)
from prestans.devel.gen.closure import Filter
template = Filter(
template_engine=self._template_engine,
model_file=self._model_file,
namespace=self._namespace,
output_directory=self._output_directory
)

if template is None:
return 1
Expand Down
75 changes: 38 additions & 37 deletions prestans/devel/gen/closure.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def udl_to_cc(text, ignore_first=False):

class BasicTypeElementTemplate(object):

def __init__(self, blueprint_type, blueprint):
def __init__(self, blueprint):

self._blueprint_type = blueprint_type
self._blueprint_type = blueprint["type"]
self._required = None
self._default = None
self._minimum = None
Expand All @@ -60,32 +60,34 @@ def __init__(self, blueprint_type, blueprint):
self._format = None
self._trim = None

constraints = blueprint["constraints"]

if self._blueprint_type == "string":
self._required = blueprint['required']
self._min_length = blueprint['min_length']
self._max_length = blueprint['max_length']
self._default = blueprint['default']
self._choices = blueprint['choices']
self._format = blueprint['format']
self._trim = blueprint['trim']
self._required = constraints['required']
self._min_length = constraints['min_length']
self._max_length = constraints['max_length']
self._default = constraints['default']
self._choices = constraints['choices']
self._format = constraints['format']
self._trim = constraints['trim']
self._client_class_name = "String"
elif self._blueprint_type == 'integer':
self._required = blueprint['required']
self._default = blueprint['default']
self._minimum = blueprint['minimum']
self._maximum = blueprint['maximum']
self._choices = blueprint['choices']
self._required = constraints['required']
self._default = constraints['default']
self._minimum = constraints['minimum']
self._maximum = constraints['maximum']
self._choices = constraints['choices']
self._client_class_name = "Integer"
elif self._blueprint_type == 'float':
self._required = blueprint['required']
self._default = blueprint['default']
self._minimum = blueprint['minimum']
self._maximum = blueprint['maximum']
self._choices = blueprint['choices']
self._required = constraints['required']
self._default = constraints['default']
self._minimum = constraints['minimum']
self._maximum = constraints['maximum']
self._choices = constraints['choices']
self._client_class_name = "Float"
elif self._blueprint_type == 'boolean':
self._required = blueprint['required']
self._default = blueprint['default']
self._required = constraints['required']
self._default = constraints['default']
self._client_class_name = "Boolean"

if self._required is None:
Expand Down Expand Up @@ -125,7 +127,7 @@ def default(self):
if self._default is None:
return "null"
elif type(self._default) == str:
return "\"%s\"" % (self._default)
return "\"%s\"" % self._default
elif type(self._default) == bool:
if self._default:
return "true"
Expand Down Expand Up @@ -195,6 +197,8 @@ def __init__(self, name, blueprint):
self._trim = None

self._blueprint_type = blueprint['type']

# todo: move or make this cleaner
self._map_name = blueprint['map_name']

# basic types
Expand Down Expand Up @@ -254,17 +258,13 @@ def __init__(self, name, blueprint):
self._max_length = blueprint['constraints']['max_length']
self._client_class_name = "Array"

element_template = blueprint['constraints']['element_template']

if element_template['type'] == 'model':
element_template_blueprint = blueprint['constraints']['element_template']
if element_template_blueprint['type'] == 'model':
self._element_template_is_model = True
self._element_template = element_template['constraints']['model_template']
self._element_template = element_template_blueprint['constraints']['model_template']
else:
self._element_template_is_model = False
self._element_template = BasicTypeElementTemplate(
blueprint_type=element_template['type'],
blueprint=element_template['constraints']
)
self._element_template = BasicTypeElementTemplate(blueprint=element_template_blueprint)

@property
def name(self):
Expand Down Expand Up @@ -325,11 +325,11 @@ def allowed_mime_types(self):
@property
def default(self):
# dates are checked first otherwise string will catch them
if self._default == prestans.types.DateTime.CONSTANT.NOW:
if self._default == prestans.types.DateTime.NOW:
return "prestans.types.DateTime.NOW"
elif self._default == prestans.types.Date.CONSTANT.TODAY:
elif self._default == prestans.types.Date.TODAY:
return "prestans.types.Date.TODAY"
elif self._default == prestans.types.Time.CONSTANT.NOW:
elif self._default == prestans.types.Time.NOW:
return "prestans.types.Time.NOW"
elif self._default is None:
return "null"
Expand Down Expand Up @@ -412,6 +412,7 @@ def __init__(self, template_engine, model_file, namespace, output_directory):
self._dependencies = list()
self._attribute_string = ""

# todo: move this to Filter class
def add_filter_dependency(self, attribute):

dependency = None
Expand All @@ -423,9 +424,9 @@ def add_filter_dependency(self, attribute):
if dependency is not None and dependency not in self._dependencies:
self._dependencies.append(dependency)

# todo: move this to Model class
def add_model_dependency(self, attribute):

dependency = None
if attribute.blueprint_type == 'array' and attribute.element_template_is_model:
dependency = "%s.%s" % (self._namespace, attribute.element_template)
elif attribute.blueprint_type == 'array':
Expand All @@ -438,7 +439,7 @@ def add_model_dependency(self, attribute):
if dependency is not None and dependency not in self._dependencies:
self._dependencies.append(dependency)

# used in filters
# todo: move this to Filter class
def add_attribute_string(self, attribute):
if attribute.blueprint_type == 'model':
self._attribute_string += "this.%s_.anyFieldsEnabled() || " % attribute.ccif
Expand Down Expand Up @@ -472,7 +473,7 @@ def run(self):
self._dependencies = list()
self._attribute_string = ""

for field_name, field_blueprint in model_blueprint['fields'].iteritems():
for field_name, field_blueprint in iter(model_blueprint['fields'].items()):

attribute = AttributeMetaData(name=field_name, blueprint=field_blueprint)
attributes.append(attribute)
Expand Down Expand Up @@ -523,7 +524,7 @@ def run(self):
self._dependencies = list()
self._attribute_string = ""

for field_name, field_blueprint in model_blueprint['fields'].iteritems():
for field_name, field_blueprint in iter(model_blueprint['fields'].items()):

attribute = AttributeMetaData(name=field_name, blueprint=field_blueprint)
attributes.append(attribute)
Expand Down
Loading

0 comments on commit 067f3af

Please sign in to comment.