Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/python3 support #40

Merged
merged 7 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ pdfdir = @pdfdir@
prefix = @prefix@
program_transform_name = @program_transform_name@
psdir = @psdir@
runstatedir = @runstatedir@
sbindir = @sbindir@
sharedstatedir = @sharedstatedir@
srcdir = @srcdir@
Expand Down
1 change: 1 addition & 0 deletions backend/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pdfdir = @pdfdir@
prefix = @prefix@
program_transform_name = @program_transform_name@
psdir = @psdir@
runstatedir = @runstatedir@
sbindir = @sbindir@
sharedstatedir = @sharedstatedir@
srcdir = @srcdir@
Expand Down
5 changes: 3 additions & 2 deletions backend/lib/gwv/json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import math

from gwv import schema
import six


class JsonFormat(object):
Expand Down Expand Up @@ -115,8 +116,8 @@ def struct_as_json(struct, json_format):
rv['properties'] = fields_as_json(struct.field_list, json_format)

required_list = []
for (name, d) in rv['properties'].iteritems():
if d.has_key('isRequired'):
for (name, d) in six.iteritems(rv['properties']):
if 'isRequired' in d:
if d['isRequired']:
required_list.append(name)
del d['isRequired']
Expand Down
3 changes: 2 additions & 1 deletion backend/lib/gwv/template_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from gwv.templates import js
from gwv.templates import md
from gwv.templates import objc
import six

# Mapping from file extension to a codegen template constructor.
codegen_template_mapping = collections.OrderedDict([
Expand Down Expand Up @@ -147,7 +148,7 @@ def get_codegen_template(self, template_filename):
class should match for unknown file types, so this should never happen.
"""

for (pattern, template_class) in codegen_template_mapping.iteritems():
for (pattern, template_class) in six.iteritems(codegen_template_mapping):
if re.match(pattern, template_filename):
return template_class(template_filename)

Expand Down
10 changes: 8 additions & 2 deletions backend/lib/gwv/templates/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

from gwv import schema
from gwv.templates import base
from six.moves import map
from six.moves import range


class CodegenTemplate(base.CodegenTemplate):
Expand Down Expand Up @@ -212,7 +214,7 @@ def resource_id_number(resource_id_str):
"""Returns the resource id number."""

_, hex_number = resource_id_str.split('_')
return long(hex_number, 16)
return int(hex_number, 16)


def resource_id_bytes(resource_id_str):
Expand All @@ -223,7 +225,11 @@ def resource_id_bytes(resource_id_str):
packed = struct.pack('>HQ',
getattr(schema.Field.ResourceType, type_name).value,
number)
return map(ord, packed)
if isinstance(packed, str):
rv = [ord(x) for x in packed]
else:
rv = [x for x in packed]
return rv


def list_to_bitfield(table):
Expand Down
1 change: 1 addition & 0 deletions backend/lib/gwv/templates/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from gwv import schema
from gwv.templates import c
from six.moves import map


class CodegenTemplate(c.CodegenTemplate):
Expand Down
3 changes: 2 additions & 1 deletion backend/lib/gwv/templates/objc.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from gwv import exception
from gwv import schema
from gwv.templates import base
import six

CLASS_PREFIX_NLK = 'NLK'
CLASS_PREFIX_GPB = 'GPB'
Expand Down Expand Up @@ -100,7 +101,7 @@ def flatten(objects):
flat_objects = []
for obj in objects:
if (isinstance(obj, collections.Iterable) and
not isinstance(obj, (str, unicode))):
not isinstance(obj, (str, six.text_type))):
flat_objects.extend(flatten(obj))
elif obj is not None:
flat_objects.append(obj)
Expand Down
1 change: 1 addition & 0 deletions backend/lib/gwv/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import unittest
from gwv import schema
from gwv import visitor
from six.moves import map

class ValidationWarning(Exception):
"""Exception indicating that schema failed validation."""
Expand Down
23 changes: 12 additions & 11 deletions backend/lib/nwv/nwv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ def _order_messages_by_dependency(messages, namespace):
remaining_messages = collections.OrderedDict(
(x.full_name, (x, _get_dependencies_in_namespace(x, namespace)))
for x in messages)
while remaining_messages:
already_processed = set()
while len(remaining_messages) != len(already_processed):
for nested_msg_desc, deps in remaining_messages.values():
if nested_msg_desc.full_name in already_processed:
continue
for dep in deps:
if dep in remaining_messages:
if dep not in already_processed and dep in remaining_messages:
break
else:
del remaining_messages[nested_msg_desc.full_name]
already_processed.add(nested_msg_desc.full_name)
yield nested_msg_desc


Expand Down Expand Up @@ -470,7 +473,7 @@ def link_typespace(self, typespace):
typespace.version_map = schema.VersionMap(options.version_map)

for nested_msg_desc in _order_messages_by_dependency(
typespace.desc.messages.values(), typespace.full_name):
list(typespace.desc.messages.values()), typespace.full_name):
if nested_msg_desc.is_map_entry:
continue # ignore map entries
nested_msg = self.get_obj(nested_msg_desc.full_name)
Expand Down Expand Up @@ -557,7 +560,7 @@ def link_trait(self, trait):
trait.state_list.append(self.get_obj(field_desc.full_name))

for nested_msg_desc in _order_messages_by_dependency(
trait.desc.messages.values(), trait.full_name):
list(trait.desc.messages.values()), trait.full_name):
if nested_msg_desc.is_map_entry:
continue # ignore map entries
nested_msg = self.get_obj(nested_msg_desc.full_name)
Expand Down Expand Up @@ -711,11 +714,9 @@ def link_enum(self, enum):
for value in enum.desc.values.values():
description = self.parse_comments(value)
pair_opts = value.options.Extensions[wdl_options_pb2.enumvalue]

value.full_name.replace(
inflection.underscore(enum.base_name).decode('utf-8').upper() + '_',
inflection.underscore(enum.base_name).upper() + '_',
'')

enum_pair = schema.EnumPair(value.full_name, value.number, description)
enum_pair.source_file = enum.source_file

Expand Down Expand Up @@ -1004,7 +1005,7 @@ def add_proto_to_pool(self, desc):
'TRAIT': options.Extensions[wdl_options_pb2.trait].id,
'EVENT': options.Extensions[wdl_options_pb2.event].id,
'COMMAND': options.Extensions[wdl_options_pb2.command].id
}.get(typ, _unique_number.next())
}.get(typ, next(_unique_number))
elif isinstance(desc, proto_pool.FieldDesc):
parent_typ = wdl_options_pb2.MessageType.Name(
desc.parent.options.Extensions[wdl_options_pb2.message_type])
Expand All @@ -1022,10 +1023,10 @@ def add_proto_to_pool(self, desc):
typ_cls = schema.ConstantGroup
else:
typ_cls = schema.Enum
obj_id = _unique_number.next()
obj_id = next(_unique_number)
elif isinstance(desc, proto_pool.FileDesc):
typ_cls = schema.File
obj_id = _unique_number.next()
obj_id = next(_unique_number)

obj = typ_cls(desc.full_name, obj_id, comments)
obj.desc = desc
Expand Down
10 changes: 5 additions & 5 deletions backend/lib/nwv/proto_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __getattr__(self, attr):
return getattr(self._base_, attr)

def __dir__(self):
return self.__dict__.keys() + dir(self._base_)
return list(self.__dict__.keys()) + dir(self._base_)

# Just spit out the base as string
def __str__(self):
Expand Down Expand Up @@ -342,16 +342,16 @@ def get_field(self, field_name):
return self._fields.get(normalize_type(field_name))

def get_files(self):
return self._files.values()
return list(self._files.values())

def get_messages(self):
return self._messages.values()
return list(self._messages.values())

def get_enums(self):
return self._enums.values()
return list(self._enums.values())

def get_fields(self):
return self._fields.values()
return list(self._fields.values())


def normalize_type(type_name):
Expand Down
1 change: 1 addition & 0 deletions backend/lib/nwv/validators/min_version_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

"""Ensures min_version never exceeds parent version."""

from __future__ import absolute_import
from gwv import schema
from gwv import validator

Expand Down
2 changes: 1 addition & 1 deletion backend/lib/nwv/validators/name_suffix_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# TODO(robbarnes) Resource, Event, Response (Event), UpdateParameters
)

_ALL_SUFFIXES = frozenset(itertools.chain(*_SUFFIX_MAP.values()))
_ALL_SUFFIXES = frozenset(itertools.chain(*list(_SUFFIX_MAP.values())))

# These violate the rules, but are allowed since they can't be changed anymore.
_LEGACY_WHITELIST = frozenset([
Expand Down
2 changes: 1 addition & 1 deletion backend/lib/nwv/validators/number_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def visit_Field(self, field):
return

fixed_encoding_width = self.fpn(field.min_value, field.max_value,
field.precision)
field.precision) or 0
if field.fixed_width < fixed_encoding_width:
self.add_failure('Fixed width not large enough for given '
'min, max and precision.')
Expand Down
1 change: 1 addition & 0 deletions backend/lib/nwv/validators/test_min_version_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

"""Test for MinVersionValidator validator."""

from __future__ import absolute_import
import unittest
from gwv import schema
from gwv import validator
Expand Down
12 changes: 9 additions & 3 deletions backend/lib/nwv/wdl_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,23 @@ def codegen(request, response):
out_file.name = filename
# The newline was added in the legacy template_set file writer,
# so it's included here to preserve compatibility.
out_file.content = content.encode('utf-8') + '\n'
out_file.content = content.encode('utf-8') + '\n'.encode('utf-8')


if __name__ == '__main__':
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARN)

std_in = sys.stdin.read()
if sys.version_info.major == 2:
std_in = sys.stdin.read()
else:
std_in = open(0, "rb").read()

request_pb2 = plugin_pb2.CodeGeneratorRequest.FromString(std_in)
response_pb2 = plugin_pb2.CodeGeneratorResponse()

codegen(request_pb2, response_pb2)

sys.stdout.write(response_pb2.SerializeToString())
if sys.version_info.major == 2:
sys.stdout.write(response_pb2.SerializeToString())
else:
open(1,"wb").write(response_pb2.SerializeToString())
1 change: 1 addition & 0 deletions codegen/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ pdfdir = @pdfdir@
prefix = @prefix@
program_transform_name = @program_transform_name@
psdir = @psdir@
runstatedir = @runstatedir@
sbindir = @sbindir@
sharedstatedir = @sharedstatedir@
srcdir = @srcdir@
Expand Down
1 change: 1 addition & 0 deletions codegen/weave-device-cpp/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pdfdir = @pdfdir@
prefix = @prefix@
program_transform_name = @program_transform_name@
psdir = @psdir@
runstatedir = @runstatedir@
sbindir = @sbindir@
sharedstatedir = @sharedstatedir@
srcdir = @srcdir@
Expand Down
2 changes: 1 addition & 1 deletion codegen/weave-device-cpp/templates/common.macros
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ enum {{ enum.base_name }} {
chain(get_struct_dependencies(obj)|reject('common')|map(attribute='parent'))|
chain(get_struct_dependencies(obj)|select('common'))|
reject('false')|unique|reject('standard')|reject('vendor')|
reject('equalto',obj)|sort -%}
reject('equalto',obj)|sort(attribute='full_name') -%}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unrelated to the Python 3 upgrade. Is it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is VERY much related. In Python2, comparison between unrelated objects would yield a stable result, in Python3, in the absence of a comparison operator, this throws an error. In this case, the objects sorted were derived from a common ancestor, but defined no comparison. When we were comparing Typespace and Trait, the comparison would throw an error. I would note that the new code parallels the structure we've seen in other templates for codegen that have not been opensourced yet.


#include <{{ c_header_file(dep) }}>
{% endfor %}
Expand Down
Loading