Add missing license header to structsgen.pw

git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@685 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
pull/1/head
aramis_acg 2010-04-15 21:50:12 +00:00
parent 1bcd29d704
commit 28141f5880
1 changed files with 261 additions and 216 deletions

View File

@ -1,216 +1,261 @@
import os #!/usr/bin/env python
import re # -*- Coding: UTF-8 -*-
#==[regexps]================================================= # ---------------------------------------------------------------------------
# Open Asset Import Library (ASSIMP)
# Clean desc # ---------------------------------------------------------------------------
REdefine = re.compile(r'' #
r'(?P<desc>)' # /** *desc */ # Copyright (c) 2006-2010, ASSIMP Development Team
r'#\s*define\s(?P<name>[^(\n]+?)\s(?P<code>.+)$' # #define name value #
, re.MULTILINE) # All rights reserved.
#
# Get structs # Redistribution and use of this software in source and binary forms,
REstructs = re.compile(r'' # with or without modification, are permitted provided that the following
#r'//\s?[\-]*\s(?P<desc>.*?)\*/\s' # /** *desc */ # conditions are met:
#r'//\s?[\-]*(?P<desc>.*?)\*/(?:.*?)' # garbage #
r'//\s?[\-]*\s(?P<desc>.*?)\*/\W*?' # /** *desc */ # * Redistributions of source code must retain the above
r'struct\s(?:ASSIMP_API\s)?(?P<name>[a-z][a-z0-9_]\w+\b)' # struct name # copyright notice, this list of conditions and the
r'[^{]*?\{' # { # following disclaimer.
r'(?P<code>.*?)' # code #
r'\}\s*(PACK_STRUCT)?;' # }; # * Redistributions in binary form must reproduce the above
, re.IGNORECASE + re.DOTALL + re.MULTILINE) # copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other
# Clean desc # materials provided with the distribution.
REdesc = re.compile(r'' #
r'^\s*?([*]|/\*\*)(?P<line>.*?)' # * line # * Neither the name of the ASSIMP team, nor the names of its
, re.IGNORECASE + re.DOTALL + re.MULTILINE) # contributors may be used to endorse or promote products
# derived from this software without specific prior
# Remove #ifdef __cplusplus # written permission of the ASSIMP Development Team.
RErmifdef = re.compile(r'' #
r'#ifdef __cplusplus' # #ifdef __cplusplus # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
r'(?P<code>.*)' # code # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
r'#endif(\s*//\s*!?\s*__cplusplus)*' # #endif # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
, re.IGNORECASE + re.DOTALL) # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# Replace comments # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
RErpcom = re.compile(r'' # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
r'[ ]*(/\*\*\s|\*/|\B\*\s|//!)' # /** # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
r'(?P<line>.*?)' # * line # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
, re.IGNORECASE + re.DOTALL) # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# Restructure # ---------------------------------------------------------------------------
def GetType(type, prefix='c_'):
t = type """Update PyAssimp's data structures to keep up with the
while t.endswith('*'): C/C++ headers."""
t = t[:-1]
types = {'unsigned int':'uint', 'unsigned char':'ubyte', 'size_t':'uint',} import os
if t in types: import re
t = types[t]
t = prefix + t #==[regexps]=================================================
while type.endswith('*'):
t = "POINTER(" + t + ")" # Clean desc
type = type[:-1] REdefine = re.compile(r''
return t r'(?P<desc>)' # /** *desc */
r'#\s*define\s(?P<name>[^(\n]+?)\s(?P<code>.+)$' # #define name value
def restructure( match ): , re.MULTILINE)
type = match.group("type")
if match.group("struct") == "": # Get structs
type = GetType(type) REstructs = re.compile(r''
elif match.group("struct") == "C_ENUM ": #r'//\s?[\-]*\s(?P<desc>.*?)\*/\s' # /** *desc */
type = "c_uint" #r'//\s?[\-]*(?P<desc>.*?)\*/(?:.*?)' # garbage
else: r'//\s?[\-]*\s(?P<desc>.*?)\*/\W*?' # /** *desc */
type = GetType(type[2:], '') r'struct\s(?:ASSIMP_API\s)?(?P<name>[a-z][a-z0-9_]\w+\b)' # struct name
if match.group("index"): r'[^{]*?\{' # {
type = type + "*" + match.group("index") r'(?P<code>.*?)' # code
r'\}\s*(PACK_STRUCT)?;' # };
result = "" , re.IGNORECASE + re.DOTALL + re.MULTILINE)
for name in match.group("name").split(','):
result += "(\"" + name.strip() + "\", "+ type + ")," # Clean desc
REdesc = re.compile(r''
return result r'^\s*?([*]|/\*\*)(?P<line>.*?)' # * line
, re.IGNORECASE + re.DOTALL + re.MULTILINE)
RErestruc = re.compile(r''
r'(?P<struct>C_STRUCT\s|C_ENUM\s|)' # [C_STRUCT] # Remove #ifdef __cplusplus
r'(?P<type>\w+\s?\w+?[*]*)\s' # type RErmifdef = re.compile(r''
#r'(?P<name>\w+)' # name r'#ifdef __cplusplus' # #ifdef __cplusplus
r'(?P<name>\w+|[a-z0-9_, ]+)' # name r'(?P<code>.*)' # code
r'(:?\[(?P<index>\w+)\])?;' # []; (optional) r'#endif(\s*//\s*!?\s*__cplusplus)*' # #endif
, re.DOTALL) , re.IGNORECASE + re.DOTALL)
#==[template]================================================
template = """ # Replace comments
class $NAME$(Structure): RErpcom = re.compile(r''
\"\"\" r'[ ]*(/\*\*\s|\*/|\B\*\s|//!)' # /**
$DESCRIPTION$ r'(?P<line>.*?)' # * line
\"\"\" , re.IGNORECASE + re.DOTALL)
$DEFINES$
_fields_ = [ # Restructure
$FIELDS$ def GetType(type, prefix='c_'):
] t = type
""" while t.endswith('*'):
t = t[:-1]
templateSR = """ types = {'unsigned int':'uint', 'unsigned char':'ubyte', 'size_t':'uint',}
class $NAME$(Structure): if t in types:
\"\"\" t = types[t]
$DESCRIPTION$ t = prefix + t
\"\"\" while type.endswith('*'):
$DEFINES$ t = "POINTER(" + t + ")"
type = type[:-1]
$NAME$._fields_ = [ return t
$FIELDS$
] def restructure( match ):
""" type = match.group("type")
#============================================================ if match.group("struct") == "":
def Structify(fileName): type = GetType(type)
file = open(fileName, 'r') elif match.group("struct") == "C_ENUM ":
text = file.read() type = "c_uint"
result = [] else:
type = GetType(type[2:], '')
# Get defines. if match.group("index"):
defs = REdefine.findall(text) type = type + "*" + match.group("index")
# Create defines
defines = "\n" result = ""
for define in defs: for name in match.group("name").split(','):
# Clean desc result += "(\"" + name.strip() + "\", "+ type + "),"
desc = REdesc.sub('', define[0])
# Replace comments return result
desc = RErpcom.sub('#\g<line>', desc)
defines += desc RErestruc = re.compile(r''
defines += " "*4 + define[1] + " = " + define[2] + "\n" r'(?P<struct>C_STRUCT\s|C_ENUM\s|)' # [C_STRUCT]
r'(?P<type>\w+\s?\w+?[*]*)\s' # type
# Get structs #r'(?P<name>\w+)' # name
rs = REstructs.finditer(text) r'(?P<name>\w+|[a-z0-9_, ]+)' # name
r'(:?\[(?P<index>\w+)\])?;' # []; (optional)
fileName = os.path.basename(fileName) , re.DOTALL)
print fileName #==[template]================================================
for r in rs: template = """
name = r.group('name')[2:] class $NAME$(Structure):
desc = r.group('desc') \"\"\"
$DESCRIPTION$
# Skip some structs \"\"\"
if name == "FileIO" or name == "File" or name == "locateFromAssimpHeap": $DEFINES$
continue _fields_ = [
$FIELDS$
text = r.group('code') ]
"""
# Clean desc
desc = REdesc.sub('', desc) templateSR = """
class $NAME$(Structure):
desc = "See '"+ fileName +"' for details." #TODO \"\"\"
$DESCRIPTION$
# Remove #ifdef __cplusplus \"\"\"
text = RErmifdef.sub('', text) $DEFINES$
# Whether the struct contains more than just POD $NAME$._fields_ = [
primitive = text.find('C_STRUCT') == -1 $FIELDS$
]
# Restructure """
text = RErestruc.sub(restructure, text) #============================================================
def Structify(fileName):
# Replace comments file = open(fileName, 'r')
text = RErpcom.sub('#\g<line>', text) text = file.read()
result = []
# Whether it's selfreferencing: ex. struct Node { Node* parent; };
selfreferencing = text.find('POINTER('+name+')') != -1 # Get defines.
defs = REdefine.findall(text)
complex = name == "Scene" # Create defines
defines = "\n"
# Create description for define in defs:
description = "" # Clean desc
for line in desc.split('\n'): desc = REdesc.sub('', define[0])
description += " "*4 + line.strip() + "\n" # Replace comments
description = description.rstrip() desc = RErpcom.sub('#\g<line>', desc)
defines += desc
# Create fields defines += " "*4 + define[1] + " = " + define[2] + "\n"
fields = ""
for line in text.split('\n'): # Get structs
fields += " "*12 + line.strip() + "\n" rs = REstructs.finditer(text)
fields = fields.strip()
fileName = os.path.basename(fileName)
if selfreferencing: print fileName
templ = templateSR for r in rs:
else: name = r.group('name')[2:]
templ = template desc = r.group('desc')
# Put it all together # Skip some structs
text = templ.replace('$NAME$', name) if name == "FileIO" or name == "File" or name == "locateFromAssimpHeap":
text = text.replace('$DESCRIPTION$', description) continue
text = text.replace('$FIELDS$', fields)
text = r.group('code')
if ((name.lower() == fileName.split('.')[0][2:].lower()) and (name != 'Material')) or name == "String":
text = text.replace('$DEFINES$', defines) # Clean desc
else: desc = REdesc.sub('', desc)
text = text.replace('$DEFINES$', '')
desc = "See '"+ fileName +"' for details." #TODO
result.append((primitive, selfreferencing, complex, text))
# Remove #ifdef __cplusplus
return result text = RErmifdef.sub('', text)
text = "#-*- coding: UTF-8 -*-\n\n" # Whether the struct contains more than just POD
text += "from ctypes import POINTER, c_int, c_uint, c_char, c_float, Structure, c_char_p, c_double, c_ubyte\n\n" primitive = text.find('C_STRUCT') == -1
structs1 = "" # Restructure
structs2 = "" text = RErestruc.sub(restructure, text)
structs3 = ""
structs4 = "" # Replace comments
text = RErpcom.sub('#\g<line>', text)
path = '../include/'
files = os.listdir (path) # Whether it's selfreferencing: ex. struct Node { Node* parent; };
#files = ["aiScene.h", "aiTypes.h"] selfreferencing = text.find('POINTER('+name+')') != -1
for fileName in files:
if fileName.endswith('.h'): complex = name == "Scene"
for struct in Structify(os.path.join(path, fileName)):
primitive, sr, complex, struct = struct # Create description
if primitive: description = ""
structs1 += struct for line in desc.split('\n'):
elif sr: description += " "*4 + line.strip() + "\n"
structs2 += struct description = description.rstrip()
elif complex:
structs4 += struct # Create fields
else: fields = ""
structs3 += struct for line in text.split('\n'):
fields += " "*12 + line.strip() + "\n"
text += structs1 + structs2 + structs3 + structs4 fields = fields.strip()
file = open('structs.txt', 'w') if selfreferencing:
file.write(text) templ = templateSR
file.close() else:
templ = template
# Put it all together
text = templ.replace('$NAME$', name)
text = text.replace('$DESCRIPTION$', description)
text = text.replace('$FIELDS$', fields)
if ((name.lower() == fileName.split('.')[0][2:].lower()) and (name != 'Material')) or name == "String":
text = text.replace('$DEFINES$', defines)
else:
text = text.replace('$DEFINES$', '')
result.append((primitive, selfreferencing, complex, text))
return result
text = "#-*- coding: UTF-8 -*-\n\n"
text += "from ctypes import POINTER, c_int, c_uint, c_char, c_float, Structure, c_char_p, c_double, c_ubyte\n\n"
structs1 = ""
structs2 = ""
structs3 = ""
structs4 = ""
path = '../include/'
files = os.listdir (path)
#files = ["aiScene.h", "aiTypes.h"]
for fileName in files:
if fileName.endswith('.h'):
for struct in Structify(os.path.join(path, fileName)):
primitive, sr, complex, struct = struct
if primitive:
structs1 += struct
elif sr:
structs2 += struct
elif complex:
structs4 += struct
else:
structs3 += struct
text += structs1 + structs2 + structs3 + structs4
file = open('structs.txt', 'w')
file.write(text)
file.close()