Author: dmeyer
Date: Wed Nov 29 17:22:42 2006
New Revision: 2118

Added:
   trunk/base/src/distribution/build_py.py
   trunk/base/src/distribution/xmlconfig.py
Modified:
   trunk/base/src/distribution/core.py

Log:
add config xml to python converter

Added: trunk/base/src/distribution/build_py.py
==============================================================================
--- (empty file)
+++ trunk/base/src/distribution/build_py.py     Wed Nov 29 17:22:42 2006
@@ -0,0 +1,43 @@
+import os
+import glob
+import types
+import stat
+
+import distutils.command.build_py
+
+import xmlconfig
+
+class build_py(distutils.command.build_py.build_py):
+
+    kaa_compiler = {
+        'cxml': xmlconfig.convert
+    }
+
+    def kaa_compile_extras(self, module, module_file, package):
+        if type(package) is types.StringType:
+            package = package.split('.')
+        elif type(package) not in (types.ListType, types.TupleType):
+            raise TypeError, \
+                  "'package' must be a string (dot-separated), list, or tuple"
+        ttype = os.path.splitext(module_file)[1][1:]
+        outfile = self.get_module_outfile(self.build_lib, package, module)
+        tmpfile = outfile[:-2] + ttype
+        if os.path.isfile(outfile) and \
+               os.stat(module_file)[stat.ST_MTIME] < 
os.stat(outfile)[stat.ST_MTIME]:
+            # template up-to-date
+            return
+        self.copy_file(module_file, tmpfile, preserve_mode=0)
+        print 'convert %s -> %s' % (tmpfile, tmpfile[:-len(ttype)] + 'py')
+        self.kaa_compiler[ttype](tmpfile, tmpfile[:-len(ttype)] + 'py')
+        os.unlink(tmpfile)
+
+        
+    def build_packages (self):
+        distutils.command.build_py.build_py.build_packages(self)
+        for package in self.packages:
+            package_dir = self.get_package_dir(package)
+            for ext in self.kaa_compiler.keys():
+                for f in glob.glob(os.path.join(package_dir, "*." + ext)):
+                    module_file = os.path.abspath(f)
+                    module = os.path.splitext(os.path.basename(f))[0]
+                    self.kaa_compile_extras(module, module_file, package)

Modified: trunk/base/src/distribution/core.py
==============================================================================
--- trunk/base/src/distribution/core.py (original)
+++ trunk/base/src/distribution/core.py Wed Nov 29 17:22:42 2006
@@ -37,8 +37,9 @@
 import tempfile
 import distutils.core
 
-# version checking
+# internal imports
 from version import Version
+from build_py import build_py
 
 __all__ = ['compile', 'check_library', 'get_library', 'setup', 'ConfigFile',
            'Extension', 'Library']
@@ -475,6 +476,7 @@
     # add extra commands
     if not 'cmdclass' in kwargs:
         kwargs['cmdclass'] = {}
+    kwargs['cmdclass']['build_py'] = build_py
     kwargs['cmdclass']['ebuild'] = GentooEbuild
     
     # run the distutils.setup function

Added: trunk/base/src/distribution/xmlconfig.py
==============================================================================
--- (empty file)
+++ trunk/base/src/distribution/xmlconfig.py    Wed Nov 29 17:22:42 2006
@@ -0,0 +1,155 @@
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------------
+# xmlconfig.py - xml config to python converter
+# -----------------------------------------------------------------------------
+# $Id$
+#
+# -----------------------------------------------------------------------------
+# Copyright (C) 2006 Dirk Meyer, Jason Tackaberry
+#
+# First Edition: Dirk Meyer <[EMAIL PROTECTED]>
+# Maintainer:    Dirk Meyer <[EMAIL PROTECTED]>
+#
+# Please see the file AUTHORS for a complete list of authors.
+#
+# This library is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version
+# 2.1 as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+#
+# -----------------------------------------------------------------------------
+
+__all__ = [ 'convert' ]
+
+# python imports
+import sys
+import pprint
+
+
+def get_value(value, type):
+    if value is None:
+        return eval('%s()' % type)
+    if type is not None:
+        return type(value)
+    if value.lower() == 'true':
+        return True
+    if value.lower() == 'false':
+        return False
+    if value.isdigit():
+        return int(value)
+    return str(value)
+
+
+def format_content(node):
+    s = node.get_rawcontent().replace('\t', '        ')
+    spaces = ''
+    while s:
+        if s[0] == ' ':
+            spaces += ' '
+            s = s[1:]
+            continue
+        if s[0] == '\n':
+            spaces = ''
+            s = s[1:]
+            continue
+        break
+    return s.replace('\n' + spaces, '\n').strip()
+
+    
+def parse_basic(node, fd, type, deep):
+    fd.write('%s(' % type)
+    first = True
+    if node.getattr('name'):
+        fd.write('name=\'%s\'' % node.getattr('name'))
+        first = False
+    for child in node:
+        if child.name != 'desc':
+            continue
+        if not first:
+            fd.write(', ')
+        first = False
+        desc = format_content(child)
+        if desc.find('\n') > 0:
+            desc = deep + desc.replace('\n', '\n' + deep)
+            fd.write('desc=\'\'\'\n%s\n%s\'\'\'' % (desc, deep))
+        else:
+            fd.write('desc=\'%s\'' % desc)
+    return first
+    
+
+def parse_var(node, fd, deep):
+    first = parse_basic(node, fd, 'Var', deep)
+    default = node.getattr('default')
+    deftype = node.getattr('type')
+    if default or deftype:
+        if not first:
+            fd.write(', ')
+        first = False
+        default = get_value(default, deftype)
+        fd.write('default=%s' % pprint.pformat(default).strip())
+        
+    for child in node:
+        if child.name not in ('values'):
+            continue
+        if not first:
+            fd.write(', ')
+        first = False
+        values = []
+        for value in child.children:
+            values.append(get_value(value.content, value.getattr('type')))
+        fd.write('type=%s' % pprint.pformat(tuple(values)).strip())
+        break
+    fd.write(')')
+
+    
+def parse_group(node, fd, deep='', type='Group'):
+    first = parse_basic(node, fd, type, deep)
+    for child in node:
+        if child.name not in ('schema',):
+            continue
+        if not first:
+            fd.write(', ')
+        first = False
+        if child.name == 'schema':
+            deep = deep + '  '
+            fd.write('schema=[\n\n' + deep)
+            for schema in child.children:
+                if schema.name == 'group':
+                    parse_group(schema, fd, deep)
+                    fd.write(',\n\n' + deep)
+                if schema.name == 'var':
+                    parse_var(schema, fd, deep)
+                    fd.write(',\n\n' + deep)
+            deep = deep[:-2]
+            fd.write(']\n' + deep)
+    fd.write(')')
+
+
+def convert(xml, python):
+    # import here and not before or kaa.base install itself
+    # will fail because there is no kaa.base at that time.
+    import kaa.xml
+
+    tree = kaa.xml.Document(xml, 'config')
+    # out = sys.__stdout__
+    out = open(python, 'w')
+    
+    out.write('# auto generated file based on %s\n\n' % xml)
+    out.write('from kaa.config import Var, Group, Dict, List, Config\n\n')
+    out.write('config = ')
+
+    parse_group(tree, out, type='Config')
+    out.write('\n\n')
+    for child in tree:
+        if child.name != 'code':
+            continue
+        out.write(format_content(child) + '\n\n')

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to