__all__ = ('FactoryBuild', )

from glob import glob
from distutils.cmd import Command
import fnmatch
import os
import kivy
import types

ignore_list = (
    'kivy.lib',
    'kivy.input.providers',
    'kivy.input.postproc',
    'kivy.modules',
    'kivy.tools',
    'kivy.parser',
)

class FactoryBuild(Command):
    description = 'Build the factory relation file (for factory.py)'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        print '--------------------------------------------'
        print 'Building factory relation file'
        print '--------------------------------------------'

        # ensure we don't have any thing like doc running
        symbols = []
        root_dir = os.path.dirname(kivy.__file__)
        for root, dirnames, filenames in os.walk(root_dir):
            if not root.startswith(root_dir):
                raise Exception('Directory should start with the kivy'
                                'directory')
            root = 'kivy' + root[len(root_dir):].replace(os.path.sep, '.')
            for filename in fnmatch.filter(filenames, '*.[ps][yo]'):
                module = '%s.%s' % (root, filename[:-3])

                # check ignore list first
                ignore = False
                for ignore in ignore_list:
                    if module.startswith(ignore):
                        ignore = True
                        break
                if ignore is True:
                    #print '<<< ignored (ignore list)'
                    continue

                # special case, core providers
                if root.startswith('kivy.core.'):
                    if not root.endswith('__init__.py'):
                        #print '<<< ignored (not a __init__.py)'
                        continue

                print '>>>', module, '::',

                try:
                    m = __import__(name=module, fromlist='.')
                except Exception, e:
                    print
                    print 'ERROR:', e
                    continue
                if not hasattr(m, '__all__'):
                    print
                    continue
                for symbol in getattr(m, '__all__'):
                    if symbol.startswith('_'):
                        continue
                    attr = getattr(m, symbol)
                    if type(attr) not in (
                            types.TypeType,
                            types.ClassType,
                        ):
                        continue
                    symbols.append((symbol, module))
                    print symbol,
                print

        print
        print '--------------------------------------------'
        print 'Found %d symbols, generating file' % len(symbols)
        print '--------------------------------------------'

        filename = os.path.join(root_dir, 'factory_registers.py')
        with open(filename, 'w') as fd:
            fd.write('# Auto-generated file by setup.py build_factory\n')
            fd.write('\n')
            fd.write('from kivy.factory import Factory\n')
            fd.write('\n')
            for x in symbols:
                fd.write("Factory.register('%s', module='%s')\n" % x)

        print 'File written at', filename

@@ -673,32 +673,212 @@
 -------------'%0A%0A
+        root_dir = os.path.dirname(kivy.__file__)%0A        filename = os.path.join(root_dir, 'factory_registers.py')%0A        with open(filename, 'w') as fd:%0A            fd.close()%0A%0A
         # ensure
@@ -944,58 +944,8 @@
  %5B%5D%0A
-        root_dir = os.path.dirname(kivy.__file__)%0A
     

fix the factory registers file generation (prevent to call the command twice to make it work by creating an empty file at the start.)