[pygccxml-commit] SF.net SVN: pygccxml: [1098] pyplusplus_dev
Brought to you by:
mbaas,
roman_yakovenko
|
From: <rom...@us...> - 2007-08-12 21:55:17
|
Revision: 1098
http://pygccxml.svn.sourceforge.net/pygccxml/?rev=1098&view=rev
Author: roman_yakovenko
Date: 2007-08-12 14:55:20 -0700 (Sun, 12 Aug 2007)
Log Message:
-----------
adding class which will save\load\provide access to the declarations, exposed by module builders, other than the current one
Modified Paths:
--------------
pyplusplus_dev/pyplusplus/utils/__init__.py
pyplusplus_dev/unittests/test_all.py
Added Paths:
-----------
pyplusplus_dev/unittests/exposed_decls_db_tester.py
Modified: pyplusplus_dev/pyplusplus/utils/__init__.py
===================================================================
--- pyplusplus_dev/pyplusplus/utils/__init__.py 2007-08-10 20:58:25 UTC (rev 1097)
+++ pyplusplus_dev/pyplusplus/utils/__init__.py 2007-08-12 21:55:20 UTC (rev 1098)
@@ -7,13 +7,12 @@
This module is a collection of unrelated algorithms, that works on code creators
tree.
"""
+import os
import math
from pygccxml import declarations
-from pyplusplus import code_creators
-
+from pyplusplus import code_creators
class missing_call_policies:
-
@staticmethod
def _selector( creator ):
if not isinstance( creator, code_creators.declaration_based_t ):
@@ -40,7 +39,6 @@
for creator in creators:
creator.parent.remove_creator( creator )
-
def split_sequence(seq, bucket_size):
#split sequence to buclets, where every will contain maximum bucket_size items
seq_len = len( seq )
@@ -53,4 +51,93 @@
to = min( ( i + 1) * bucket_size, seq_len )
buckets.append( seq[ from_ : to ] )
return buckets
-
\ No newline at end of file
+
+
+class exposed_decls_db_t( object ):
+ class row_creator_t( declarations.decl_visitor_t ):
+ def __init__( self, field_delimiter ):
+ self.__decl = None
+ self.__formatted = None
+ self.__field_delimiter = field_delimiter
+
+ def get_full_name(self):
+ return declarations.full_name( self.__decl )
+
+ def __call__( self, decl ):
+ if not isinstance( decl.parent, declarations.namespace_t ):
+ return None #we don't want to dump class internal declarations
+ self.__decl = decl
+ self.__formatted = None
+ try:
+ declarations.apply_visitor( self, decl )
+ except NotImplementedError:
+ pass
+ return self.__formatted
+
+ def visit_free_function( self ):
+ self.__formatted = '%s%s%s' % ( self.get_full_name()
+ , self.__field_delimiter
+ , self.__decl.function_type().decl_string )
+
+ def visit_class_declaration(self ):
+ self.__formatted = self.get_full_name()
+
+ def visit_class(self ):
+ self.__formatted = self.get_full_name()
+
+ def visit_enumeration(self ):
+ self.__formatted = self.get_full_name()
+
+ def visit_variable(self ):
+ self.__formatted = self.get_full_name()
+
+ def __init__( self, activated=False ):
+ self.__activated = activated
+ self.__exposed = {}
+ self.__row_creator = self.row_creator_t(field_delimiter='@')
+ self.__key_delimiter = '?'
+ self.__row_delimiter = os.linesep
+
+ def __create_key( self, decl ):
+ return decl.__class__.__name__
+
+ @property
+ def activated( self ):
+ return self.__activated
+
+ def expose( self, decl ):
+ if not self.__activated:
+ return None
+ row = self.__row_creator( decl )
+ if row is None:
+ return None
+ key = self.__create_key( decl )
+ if not self.__exposed.has_key( key ):
+ self.__exposed[ key ] = set()
+ self.__exposed[ key ].add( row )
+
+ def save( self, fpath ):
+ f = file( fpath, 'w+b' )
+ for key, items in self.__exposed.iteritems():
+ for item in items:
+ f.write( '%s%s%s%s' % ( key, self.__key_delimiter, item, self.__row_delimiter ) )
+ f.close()
+
+ def load( self, fpath ):
+ self.__exposed = {}
+ f = file( fpath, 'r+b' )
+ for line in f:
+ key, row = line.split( self.__key_delimiter)
+ if not self.__exposed.has_key( key ):
+ self.__exposed[ key ] = set()
+ self.__exposed[ key ].add( row[:len(self.__row_delimiter)] )
+
+ def is_exposed( self, decl ):
+ assert self.activated
+ key = self.__create_key( decl )
+ if not self.__exposed.has_key( key ):
+ return False
+ row = self.__row_creator( decl )
+ return row in self.__exposed[ key ]
+
+
Added: pyplusplus_dev/unittests/exposed_decls_db_tester.py
===================================================================
--- pyplusplus_dev/unittests/exposed_decls_db_tester.py (rev 0)
+++ pyplusplus_dev/unittests/exposed_decls_db_tester.py 2007-08-12 21:55:20 UTC (rev 1098)
@@ -0,0 +1,66 @@
+# Copyright 2004 Roman Yakovenko.
+# Distributed under the Boost Software License, Version 1.0. (See
+# accompanying file LICENSE_1_0.txt or copy at
+# http://www.boost.org/LICENSE_1_0.txt)
+
+import os
+import sys
+import unittest
+import autoconfig
+from pygccxml import parser
+from pygccxml import declarations
+from pyplusplus import code_creators
+from pyplusplus import module_creator
+from pyplusplus import module_builder
+from pyplusplus import utils as pypp_utils
+from pyplusplus import function_transformers as ft
+
+class tester_t(unittest.TestCase):
+ CODE = """
+ namespace ns{
+ enum AAA{ aaa=1 };
+ struct XXX{
+ struct yyy{};
+ enum bbb{ b=2 };
+ void fff();
+ };
+
+ int VVV;
+ void FFF( int );
+ }
+ """
+ def __init__(self, *args ):
+ unittest.TestCase.__init__(self, *args)
+
+ def test(self):
+ db = pypp_utils.exposed_decls_db_t(activated=True)
+ config = parser.config_t( gccxml_path=autoconfig.gccxml.executable )
+ global_ns = declarations.get_global_namespace( parser.parse_string( self.CODE, config ) )
+ ns = global_ns.namespace( 'ns' )
+ for d in ns.decls(recursive=True):
+ db.expose( d )
+
+ select_exposed = lambda decl: decl.name == decl.name.upper() \
+ and not isinstance( decl, declarations.member_calldef_t )
+
+ for x in ns.decls( select_exposed ):
+ self.failUnless( db.is_exposed( x ) == True )
+
+ db.save( os.path.join( autoconfig.build_dir, 'exposed.db.pypp' ) )
+
+ db2 = pypp_utils.exposed_decls_db_t(activated=True)
+ db2.load( os.path.join( autoconfig.build_dir, 'exposed.db.pypp' ) )
+ for x in ns.decls( select_exposed ):
+ self.failUnless( db.is_exposed( x ) == True )
+
+
+def create_suite():
+ suite = unittest.TestSuite()
+ suite.addTest( unittest.makeSuite(tester_t))
+ return suite
+
+def run_suite():
+ unittest.TextTestRunner(verbosity=2).run( create_suite() )
+
+if __name__ == "__main__":
+ run_suite()
Modified: pyplusplus_dev/unittests/test_all.py
===================================================================
--- pyplusplus_dev/unittests/test_all.py 2007-08-10 20:58:25 UTC (rev 1097)
+++ pyplusplus_dev/unittests/test_all.py 2007-08-12 21:55:20 UTC (rev 1098)
@@ -71,6 +71,7 @@
import throw_tester
import duplicate_aliases_tester
import non_overridable_tester
+import exposed_decls_db_tester
def create_suite(times):
testers = [
@@ -138,6 +139,7 @@
, throw_tester
, duplicate_aliases_tester
, non_overridable_tester
+ , exposed_decls_db_tester
]
main_suite = unittest.TestSuite()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|