Skip to content

Commit

Permalink
Merge pull request #1 from Matthijsy/feature/add-enum-types
Browse files Browse the repository at this point in the history
Add enum types for writing reflected entities
  • Loading branch information
eblis authored Oct 9, 2024
2 parents ecfe516 + 8100525 commit 6cd7cd7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
7 changes: 7 additions & 0 deletions odata/reflect-templates/enum_entity.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%page args="name, entity"/>\
<% short_name = name.split(".")[-1] %>\
${short_name} = Enum("${short_name}", {\
%for prop in entity:
"${prop.name}" : "${prop.value}",\
%endfor
})
9 changes: 8 additions & 1 deletion odata/reflect-templates/main.mako
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ import datetime
import uuid
import decimal

from enum import Enum

from odata.entity import EntityBase
from odata.property import StringProperty, IntegerProperty, NavigationProperty, DatetimeProperty, DecimalProperty, FloatProperty, BooleanProperty, UUIDProperty

from odata.enumtype import EnumType, EnumTypeProperty

class ReflectionBase(EntityBase):
pass

# ************ Start enum type definitions ************
%for type_name in enum_types:
<%include file="enum_entity.mako" args="name=type_name, entity=enum_types[type_name]"/>
%endfor
# ************ End enum type definitions ************

# ************ Start type definitions ************
%for type_name in types:
Expand Down
3 changes: 3 additions & 0 deletions odata/reflect-templates/property.mako
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
% if property.is_computed_value:
, is_computed_value=True\
% endif
% if hasattr(property, 'enum_class'):
, enum_class=${property.enum_class.__name__}\
% endif
)
10 changes: 8 additions & 2 deletions odata/reflector.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import rich
from io import StringIO
from pathlib import Path
from enum import EnumMeta

from mako.lookup import TemplateLookup
from mako.runtime import Context
Expand All @@ -92,7 +93,8 @@
"DecimalProperty": "decimal.Decimal",
"FloatProperty": "float",
"BooleanProperty": "bool",
"UUIDProperty": "uuid.UUID"
"UUIDProperty": "uuid.UUID",
"EnumTypeProperty": "str"
}


Expand All @@ -109,10 +111,14 @@ def write_reflected_types(self):
lookup = TemplateLookup(directories=[str(template_folder)], output_encoding="utf-8", preprocessor=[lambda x: x.replace("\r\n", "\n")])
template = lookup.get_template("main.mako")

types = {k: v for k, v in self.types.items() if not isinstance(v, EnumMeta)}
enum_types = {k: v for k, v in self.types.items() if isinstance(v, EnumMeta)}

buffer = StringIO()
context = Context(buffer,
entities=self.entities,
types=self.types,
types=types,
enum_types=enum_types,
type_translations=type_translations,
package=self.package,
metadata_url=self.metadata_url)
Expand Down

0 comments on commit 6cd7cd7

Please sign in to comment.