1
0

esx: Refactor common code in the generator

Move common code from Property and Parameter into new Member class.

Rename the other base class to Type.
This commit is contained in:
Matthias Bolte 2011-05-01 11:29:40 +02:00
parent efd9265991
commit fbe3ab1a27

View File

@ -57,17 +57,11 @@ def aligned(left, right, length=59):
class Parameter: class Member:
def __init__(self, type, name, occurrence): def __init__(self, type, occurrence):
self.type = type self.type = type
self.occurrence = occurrence self.occurrence = occurrence
if ':' in name and name.startswith("_this"):
self.name, self.autobind_type = name.split(":")
else:
self.name = name
self.autobind_type = None
def is_enum(self): def is_enum(self):
return self.type in predefined_enums or self.type in enums_by_name return self.type in predefined_enums or self.type in enums_by_name
@ -81,6 +75,31 @@ class Parameter:
return self.type in enums_by_name or self.type in objects_by_name return self.type in enums_by_name or self.type in objects_by_name
def get_occurrence_comment(self):
if self.occurrence == OCCURRENCE__REQUIRED_ITEM:
return "/* required */"
elif self.occurrence == OCCURRENCE__REQUIRED_LIST:
return "/* required, list */"
elif self.occurrence == OCCURRENCE__OPTIONAL_ITEM:
return "/* optional */"
elif self.occurrence == OCCURRENCE__OPTIONAL_LIST:
return "/* optional, list */"
raise ValueError("unknown occurrence value '%s'" % self.occurrence)
class Parameter(Member):
def __init__(self, type, name, occurrence):
Member.__init__(self, type, occurrence)
if ':' in name and name.startswith("_this"):
self.name, self.autobind_type = name.split(":")
else:
self.name = name
self.autobind_type = None
def generate_parameter(self, is_last = False, is_header = True, offset = 0): def generate_parameter(self, is_last = False, is_header = True, offset = 0):
if self.occurrence == OCCURRENCE__IGNORED: if self.occurrence == OCCURRENCE__IGNORED:
raise ValueError("invalid function parameter occurrence value '%s'" % self.occurrence) raise ValueError("invalid function parameter occurrence value '%s'" % self.occurrence)
@ -131,7 +150,7 @@ class Parameter:
return " ESX_VI__METHOD__PARAMETER__SERIALIZE(%s, %s)\n" % (self.type, self.name) return " ESX_VI__METHOD__PARAMETER__SERIALIZE(%s, %s)\n" % (self.type, self.name)
def get_type_string(self, as_return_value = False): def get_type_string(self, as_return_value=False):
string = "" string = ""
if self.type == "String" and \ if self.type == "String" and \
@ -152,19 +171,6 @@ class Parameter:
return string return string
def get_occurrence_comment(self):
if self.occurrence == OCCURRENCE__REQUIRED_ITEM:
return "/* required */"
elif self.occurrence == OCCURRENCE__REQUIRED_LIST:
return "/* required, list */"
elif self.occurrence == OCCURRENCE__OPTIONAL_ITEM:
return "/* optional */"
elif self.occurrence == OCCURRENCE__OPTIONAL_LIST:
return "/* optional, list */"
raise ValueError("unknown occurrence value '%s'" % self.occurrence)
def get_occurrence_short_enum(self): def get_occurrence_short_enum(self):
if self.occurrence == OCCURRENCE__REQUIRED_ITEM: if self.occurrence == OCCURRENCE__REQUIRED_ITEM:
return "RequiredItem" return "RequiredItem"
@ -272,23 +278,11 @@ class Method:
class Property: class Property(Member):
def __init__(self, type, name, occurrence): def __init__(self, type, name, occurrence):
self.type = type Member.__init__(self, type, occurrence)
self.name = name self.name = name
self.occurrence = occurrence
def is_enum(self):
return self.type in predefined_enums or self.type in enums_by_name
def is_object(self):
return self.type in predefined_objects or self.type in objects_by_name
def is_type_generated(self):
return self.type in enums_by_name or self.type in objects_by_name
def generate_struct_member(self): def generate_struct_member(self):
@ -391,21 +385,8 @@ class Property:
return "esxVI_%s *" % self.type return "esxVI_%s *" % self.type
def get_occurrence_comment(self):
if self.occurrence == OCCURRENCE__REQUIRED_ITEM:
return "/* required */"
elif self.occurrence == OCCURRENCE__REQUIRED_LIST:
return "/* required, list */"
elif self.occurrence == OCCURRENCE__OPTIONAL_ITEM:
return "/* optional */"
elif self.occurrence == OCCURRENCE__OPTIONAL_LIST:
return "/* optional, list */"
raise ValueError("unknown occurrence value '%s'" % self.occurrence) class Type:
class Base:
def __init__(self, kind, name): def __init__(self, kind, name):
self.kind = kind self.kind = kind
self.name = name self.name = name
@ -435,7 +416,7 @@ class Base:
class Object(Base): class Object(Type):
FEATURE__DYNAMIC_CAST = (1 << 1) FEATURE__DYNAMIC_CAST = (1 << 1)
FEATURE__LIST = (1 << 2) FEATURE__LIST = (1 << 2)
FEATURE__DEEP_COPY = (1 << 3) FEATURE__DEEP_COPY = (1 << 3)
@ -444,7 +425,7 @@ class Object(Base):
FEATURE__DESERIALIZE = (1 << 6) FEATURE__DESERIALIZE = (1 << 6)
def __init__(self, name, extends, properties, features = 0, extended_by = None): def __init__(self, name, extends, properties, features = 0, extended_by = None):
Base.__init__(self, "struct", name) Type.__init__(self, "struct", name)
self.extends = extends self.extends = extends
self.features = features self.features = features
self.properties = properties self.properties = properties
@ -898,12 +879,12 @@ class Object(Base):
class ManagedObject(Base): class ManagedObject(Type):
FEATURE__LIST = (1 << 2) FEATURE__LIST = (1 << 2)
def __init__(self, name, extends, properties, features=0, extended_by=None): def __init__(self, name, extends, properties, features=0, extended_by=None):
Base.__init__(self, "struct", name) Type.__init__(self, "struct", name)
self.extends = extends self.extends = extends
self.features = features self.features = features
self.properties = properties self.properties = properties
@ -1198,14 +1179,14 @@ class ManagedObject(Base):
class Enum(Base): class Enum(Type):
FEATURE__ANY_TYPE = (1 << 1) FEATURE__ANY_TYPE = (1 << 1)
FEATURE__SERIALIZE = (1 << 2) FEATURE__SERIALIZE = (1 << 2)
FEATURE__DESERIALIZE = (1 << 3) FEATURE__DESERIALIZE = (1 << 3)
def __init__(self, name, values, features=0): def __init__(self, name, values, features=0):
Base.__init__(self, "enum", name) Type.__init__(self, "enum", name)
self.values = values self.values = values
self.features = features self.features = features