| OLD | NEW |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 # pylint: disable=W0232, E0203, W0201 | 40 # pylint: disable=W0232, E0203, W0201 |
| 41 | 41 |
| 42 import abc | 42 import abc |
| 43 import json | 43 import json |
| 44 import re | 44 import re |
| 45 | 45 |
| 46 | 46 |
| 47 # Base classes | 47 # Base classes |
| 48 | 48 |
| 49 | 49 |
| 50 class BaseIdl: | 50 class BaseIdl(object): |
| 51 """Abstract base class, used for JSON serialization.""" | 51 """Abstract base class, used for JSON serialization.""" |
| 52 __metaclass__ = abc.ABCMeta | 52 __metaclass__ = abc.ABCMeta |
| 53 | 53 |
| 54 @abc.abstractmethod | 54 @abc.abstractmethod |
| 55 def json_serializable(self): | 55 def json_serializable(self): |
| 56 """Returns a JSON serializable form of the object. | 56 """Returns a JSON serializable form of the object. |
| 57 | 57 |
| 58 This should be a dictionary, with keys scoped names of the form | 58 This should be a dictionary, with keys scoped names of the form |
| 59 Class::key, where the scope is the class name. | 59 Class::key, where the scope is the class name. |
| 60 This is so we produce identical output to the Perl code, which uses | 60 This is so we produce identical output to the Perl code, which uses |
| 61 the Perl module JSON.pm, which uses this format. | 61 the Perl module JSON.pm, which uses this format. |
| 62 """ | 62 """ |
| 63 pass | 63 pass |
| 64 | 64 |
| 65 | 65 |
| 66 class TypedObject: | 66 class TypedObject(object): |
| 67 """Object with a type, such as an Attribute or Operation (return value). | 67 """Object with a type, such as an Attribute or Operation (return value). |
| 68 | 68 |
| 69 The type can be an actual type, or can be a typedef, which must be resolved | 69 The type can be an actual type, or can be a typedef, which must be resolved |
| 70 before passing data to the code generator. | 70 before passing data to the code generator. |
| 71 """ | 71 """ |
| 72 __metaclass__ = abc.ABCMeta | 72 __metaclass__ = abc.ABCMeta |
| 73 idl_type = None | 73 idl_type = None |
| 74 extended_attributes = None | 74 extended_attributes = None |
| 75 | 75 |
| 76 def resolve_typedefs(self, typedefs): | 76 def resolve_typedefs(self, typedefs): |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 | 322 |
| 323 def resolve_typedefs(idl_type, typedefs): | 323 def resolve_typedefs(idl_type, typedefs): |
| 324 """Return an IDL type (as string) with typedefs resolved.""" | 324 """Return an IDL type (as string) with typedefs resolved.""" |
| 325 # Converts a string representation to and from an IdlType object to handle | 325 # Converts a string representation to and from an IdlType object to handle |
| 326 # parsing of composite types (arrays and sequences) and encapsulate typedef | 326 # parsing of composite types (arrays and sequences) and encapsulate typedef |
| 327 # resolution, e.g., GLint[] -> unsigned long[] requires parsing the '[]'. | 327 # resolution, e.g., GLint[] -> unsigned long[] requires parsing the '[]'. |
| 328 # Use fluent interface to avoid auxiliary variable. | 328 # Use fluent interface to avoid auxiliary variable. |
| 329 return str(IdlType.from_string(idl_type).resolve_typedefs(typedefs)) | 329 return str(IdlType.from_string(idl_type).resolve_typedefs(typedefs)) |
| 330 | 330 |
| 331 | 331 |
| 332 class IdlType: | 332 class IdlType(object): |
| 333 # FIXME: replace type strings with these objects, | 333 # FIXME: replace type strings with these objects, |
| 334 # so don't need to parse everywhere types are used. | 334 # so don't need to parse everywhere types are used. |
| 335 # Types are stored internally as strings, not objects, | 335 # Types are stored internally as strings, not objects, |
| 336 # e.g., as 'sequence<Foo>' or 'Foo[]', | 336 # e.g., as 'sequence<Foo>' or 'Foo[]', |
| 337 # hence need to parse the string whenever a type is used. | 337 # hence need to parse the string whenever a type is used. |
| 338 # FIXME: incorporate Nullable, Variadic, etc. | 338 # FIXME: incorporate Nullable, Variadic, etc. |
| 339 # FIXME: properly should nest types | 339 # FIXME: properly should nest types |
| 340 # Formally types are nested, e.g., short?[] vs. short[]?, | 340 # Formally types are nested, e.g., short?[] vs. short[]?, |
| 341 # but in practice these complex types aren't used and can treat | 341 # but in practice these complex types aren't used and can treat |
| 342 # as orthogonal properties. | 342 # as orthogonal properties. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 | 432 |
| 433 | 433 |
| 434 # JSON export | 434 # JSON export |
| 435 | 435 |
| 436 | 436 |
| 437 class IdlEncoder(json.JSONEncoder): | 437 class IdlEncoder(json.JSONEncoder): |
| 438 def default(self, obj): | 438 def default(self, obj): |
| 439 if isinstance(obj, BaseIdl): | 439 if isinstance(obj, BaseIdl): |
| 440 return obj.json_serializable() | 440 return obj.json_serializable() |
| 441 return json.JSONEncoder.default(self, obj) | 441 return json.JSONEncoder.default(self, obj) |
| OLD | NEW |