Exemple #1
0
    def loadInstance(self):
        """
        Loads the plugin from the proxy information that was created from the
        registry file.
        """
        if (self._loaded):
            return

        self._loaded = True
        module_path = self.modulePath()

        package = projex.packageFromPath(module_path)
        path = os.path.normpath(projex.packageRootPath(module_path))

        if (path in sys.path):
            sys.path.remove(path)

        sys.path.insert(0, path)

        try:
            __import__(package)

        except Exception, e:
            err = Plugin(self.name(), self.version())
            err.setError(e)
            err.setFilepath(module_path)

            self._instance = err

            self.setError(e)

            msg = "%s.plugin('%s') errored loading instance from %s"
            opts = (self.proxyClass().__name__, self.name(), module_path)
            logger.warning(msg % opts)
            logger.error(e)
Exemple #2
0
 def loadInstance( self ):
     """
     Loads the plugin from the proxy information that was created from the
     registry file.
     """
     if ( self._loaded ):
         return
     
     self._loaded = True
     module_path = self.modulePath()
     
     package = projex.packageFromPath(module_path)
     path    = os.path.normpath(projex.packageRootPath(module_path))
     
     if ( path in sys.path ):
         sys.path.remove(path)
     
     sys.path.insert(0, path)
     
     try:
         __import__(package)
     
     except Exception, e:
         err = Plugin(self.name(), self.version())
         err.setError(e)
         err.setFilepath(module_path)
         
         self._instance = err
         
         self.setError(e)
         
         msg = "%s.plugin('%s') errored loading instance from %s"
         opts = (self.proxyClass().__name__, self.name(), module_path)
         logger.warning(msg % opts)
         logger.error(e)
Exemple #3
0
def collect(basepath, exclude=None, processPlugins=True):
    """
    Collects all the packages associated with the inputted filepath.
    
    :param      module | <module>
    
    :return     ([<str> pkg, ..], [(<str> path, <str> relpath), ..] data)
    """
    if exclude is None:
        exclude = ['.py', '.pyc', '.pyo', '.css', '.exe']

    imports = []
    datas = []

    # walk the folder structure looking for all packages and data files
    basename = os.path.basename(basepath)
    basepath = os.path.abspath(basepath)
    baselen = len(basepath) - len(basename)

    plugfiles = []

    for root, folders, files in os.walk(basepath):
        if '.svn' in root or '.git' in root:
            continue

        # mark the plugins file for load
        plugdata = None
        if processPlugins and '__plugins__.py' in files:
            filename = os.path.join(root, '__plugins__.py')
            package = projex.packageFromPath(filename) + '.__plugins__'
            pkgpath = projex.packageRootPath(filename)

            if pkgpath not in sys.path:
                sys.path.insert(0, pkgpath)

            # import the plugins module
            __import__(package)
            pkg = sys.modules[package]

            recurse = getattr(pkg, '__recurse__', False)
            plugdata = {'recurse': recurse,
                        'packages': [],
                        'path': root}

            plugfiles.append(plugdata)

        # look for any recursion plugins
        else:
            for data in plugfiles:
                if data['recurse'] and root.startswith(data['path']):
                    plugdata = data
                    break

        if plugdata is not None:
            packages = plugdata['packages']

            # include package plugins
            for folder in folders:
                pkgpath = os.path.join(root, folder, '__init__.py')
                if os.path.exists(pkgpath):
                    packages.append(projex.packageFromPath(pkgpath))

        for file_ in files:
            module, ext = os.path.splitext(file_)

            # look for python modules
            if ext == '.py':
                package_path = projex.packageFromPath(os.path.join(root, file_))
                if not package_path:
                    continue

                if module != '__init__':
                    package_path += '.' + module

                imports.append(package_path)

                # test to see if this is a plugin file
                if plugdata is not None and module not in ('__init__',
                                                           '__plugins__'):
                    plugdata['packages'].append(package_path)

            # look for data
            elif ext not in exclude:
                src = os.path.join(root, file_)
                targ = os.path.join(root[baselen:])
                datas.append((src, targ))

    # save the plugin information
    for plugdata in plugfiles:
        fname = os.path.join(plugdata['path'], '__plugins__.py')
        packages = plugdata['packages']

        plugs = ',\n'.join(map(lambda x: "r'{0}'".format(x), packages))
        data = [
            '__recurse__ = {0}'.format(plugdata['recurse']),
            '__toc__ = [{0}]'.format(plugs)
        ]

        # write the data to the system
        f = open(fname, 'w')
        f.write('\n'.join(data))
        f.close()

    return imports, datas
Exemple #4
0
def collect(basepath, exclude=None, processPlugins=True):
    """
    Collects all the packages associated with the inputted filepath.
    
    :param      module | <module>
    
    :return     ([<str> pkg, ..], [(<str> path, <str> relpath), ..] data)
    """
    if exclude is None:
        exclude = ['.py', '.pyc', '.pyo', '.css', '.exe']

    imports = []
    datas = []

    # walk the folder structure looking for all packages and data files
    basename = os.path.basename(basepath)
    basepath = os.path.abspath(basepath)
    baselen = len(basepath) - len(basename)

    plugfiles = []

    for root, folders, files in os.walk(basepath):
        if '.svn' in root or '.git' in root:
            continue

        # mark the plugins file for load
        plugdata = None
        if processPlugins and '__plugins__.py' in files:
            filename = os.path.join(root, '__plugins__.py')
            package = projex.packageFromPath(filename) + '.__plugins__'
            pkgpath = projex.packageRootPath(filename)

            if pkgpath not in sys.path:
                sys.path.insert(0, pkgpath)

            # import the plugins module
            __import__(package)
            pkg = sys.modules[package]

            recurse = getattr(pkg, '__recurse__', False)
            plugdata = {'recurse': recurse, 'packages': [], 'path': root}

            plugfiles.append(plugdata)

        # look for any recursion plugins
        else:
            for data in plugfiles:
                if data['recurse'] and root.startswith(data['path']):
                    plugdata = data
                    break

        if plugdata is not None:
            packages = plugdata['packages']

            # include package plugins
            for folder in folders:
                pkgpath = os.path.join(root, folder, '__init__.py')
                if os.path.exists(pkgpath):
                    packages.append(projex.packageFromPath(pkgpath))

        for file_ in files:
            module, ext = os.path.splitext(file_)

            # look for python modules
            if ext == '.py':
                package_path = projex.packageFromPath(os.path.join(
                    root, file_))
                if not package_path:
                    continue

                if module != '__init__':
                    package_path += '.' + module

                imports.append(package_path)

                # test to see if this is a plugin file
                if plugdata is not None and module not in ('__init__',
                                                           '__plugins__'):
                    plugdata['packages'].append(package_path)

            # look for data
            elif ext not in exclude:
                src = os.path.join(root, file_)
                targ = os.path.join(root[baselen:])
                datas.append((src, targ))

    # save the plugin information
    for plugdata in plugfiles:
        fname = os.path.join(plugdata['path'], '__plugins__.py')
        packages = plugdata['packages']

        plugs = ',\n'.join(map(lambda x: "r'{0}'".format(x), packages))
        data = [
            '__recurse__ = {0}'.format(plugdata['recurse']),
            '__toc__ = [{0}]'.format(plugs)
        ]

        # write the data to the system
        f = open(fname, 'w')
        f.write('\n'.join(data))
        f.close()

    return imports, datas
Exemple #5
0
def generate(module,
             outputpath='./resources/docs/html',
             userpath='',
             config=None):
    """
    Generates documenation for the inputed filepath at the given output \
    location.  The system can also supply the root location for user \
    documentation that can be included in addition to the code by supplying \
    a root userdocs path.
    
    :param      module        | <module> || <str>
    :param      outputpath    | <str>
    :param      config        | <module> || None
    
    :return     <bool> success
    """
    from projex.docgen.document import Document

    if (type(module) == str):
        # extract from a specific filepath
        if (os.path.exists(module)):
            package = projex.packageFromPath(module)
            if (not package):
                msg = '%s is an invalid module.' % module
                raise errors.DocumentationError(msg)

            root_path = projex.packageRootPath(module)
            sys.path.insert(0, root_path)

        # otherwise, use it as the package name
        else:
            projex.requires(module)
            package = module.split('-')[0]

        try:
            __import__(package)
            module = sys.modules[package]

        except ImportError:
            msg = 'Could not import the %s module' % package
            raise errors.DocumentationError(msg)

        except KeyError:
            msg = 'Could not import the %s module' % package
            raise errors.DocumentationError(msg)

    # initialize the global environ
    if (config is None):
        config = default_config

    init_environ(module, config)

    logger.info('Generating module documentation %s...' % module.__name__)

    # load the base page and style information
    page = templates.template('page.html')

    # start generating the documents
    Document.cache.clear()

    # generate the module documentation
    try:
        ignore = config.IGNORE
    except AttributeError:
        ignore = None

    doc = generateModuleDocument(module, ignore=ignore)

    modpath = os.path.dirname(module.__file__)
    outpath = outputpath.replace('./', modpath + '/')

    # clear the docs path
    if (os.path.exists(outpath)):
        shutil.rmtree(outpath)

    if (not os.path.exists(outpath)):
        logger.info('Making doc path: ' + outpath)
        os.makedirs(outpath)

    # make the root api documentation
    api_path = outpath.rstrip('/') + '/api/'
    if not os.path.exists(api_path):
        logger.info('Making doc path:' + api_path)
        os.mkdir(api_path)

    # generate the api docs
    doc.export(api_path, page=page)

    # generate the all classes page
    generateClassDocs(outpath, page, module.__name__)
    generateModuleDocs(outpath, page, module.__name__)
    generateFunctionDocs(outpath, page, module.__name__)
    generateApiIndex(outpath, page)
    generateDocumentIndex(outpath, page)

    # create the user docs
    if (userpath):
        userdocs = os.path.abspath(userpath)
    elif (config != default_config):
        userdocs = os.path.dirname(config.__file__)
    else:
        userdocs = os.path.abspath('./docs')

    if (os.path.exists(userdocs)):
        targetuserdocs = outpath
        templ = templates.template('link_breadcrumbs.html')

        generateUserDocs(userdocs, targetuserdocs, page)

    # copy the syntaxhighlighter code
    targetpath = os.path.join(outpath, '_static')

    paths = []
    paths.append(resources.find('ext/prettify'))
    paths.append(templates.path('javascript'))
    paths.append(templates.path('css'))
    paths.append(templates.path('images'))
    paths.append(templates.path('img'))

    logger.info('Copying static resources...')

    # python26+
    try:
        ignore = shutil.ignore_patterns('.svn')
    except AttributeError:
        ignore = None

    for path in paths:
        if (not os.path.exists(path)):
            continue

        basename = os.path.basename(path)
        instpath = os.path.join(targetpath, basename)

        if (ignore is not None):
            shutil.copytree(path, instpath, ignore=ignore)
        else:
            shutil.copytree(path, instpath)

    # create a compressed xdk file
    logger.info('Creating XDK file...')
    zfilename = os.path.join(outpath, '../%s.xdk' % module.__name__)
    zfilename = os.path.abspath(zfilename)

    generateXdk(outpath, zfilename)
Exemple #6
0
    def loadPlugins(cls):
        """
        Initializes the plugins by loading modules from the inputed paths.
        """
        plugs = getattr(cls, '_%s__plugins' % cls.__name__, None)
        if plugs is not None:
            return

        plugs = {}
        setattr(cls, '_%s__plugins' % cls.__name__, plugs)
        typ = cls.pluginRegisterType()

        for path in cls.pluginPath():
            base_package = projex.packageFromPath(path)
            base_path = os.path.normpath(projex.packageRootPath(path))

            # make sure it is at the front of the path
            if base_path in sys.path:
                sys.path.remove(base_path)

            sys.path.insert(0, base_path)
            processed = ['__init__']

            # load support for registries
            if typ & Plugin.Type.RegistryFile:
                files = glob.glob(os.path.join(path, '*/register.xml'))
                for file in files:
                    name = os.path.normpath(file).split(os.path.sep)[-2]
                    processed.append(name)

                    try:
                        proxy = PluginProxy.fromFile(cls, file)
                        cls.register(proxy)

                    except Exception, e:
                        name = projex.text.pretty(name)
                        err = Plugin(name)
                        err.setError(e)
                        err.setFilepath(file)

                        cls.register(err)

                        # log the error
                        msg = "%s.plugin('%s') failed to load from %s."
                        logger.warning(msg % (cls.__name__, name, file))
                        logger.error(e)

            # load support for packages
            if typ & Plugin.Type.Package:
                files = glob.glob(os.path.join(path, '*/__init__.py'))
                for file in files:
                    name = os.path.normpath(file).split(os.path.sep)[-2]
                    if (name in processed):
                        continue

                    processed.append(name)
                    package = '.'.join([base_package, name]).strip('.')
                    if (not package):
                        continue

                    try:
                        __import__(package)

                    except Exception, e:
                        name = projex.text.pretty(name)
                        err = Plugin(name)
                        err.setError(e)
                        err.setFilepath(file)

                        cls.register(err)

                        # log the error
                        msg = "%s.plugin('%s') failed to load from %s."
                        logger.warning(msg % (cls.__name__, name, file))
                        logger.error(e)
Exemple #7
0
 def loadPlugins(cls):
     """
     Initializes the plugins by loading modules from the inputed paths.
     """
     plugs = getattr(cls, '_%s__plugins' % cls.__name__, None)
     if plugs is not None:
         return
     
     plugs = {}
     setattr(cls, '_%s__plugins' % cls.__name__, plugs)
     typ = cls.pluginRegisterType()
     
     for path in cls.pluginPath():
         base_package = projex.packageFromPath(path)
         base_path    = os.path.normpath(projex.packageRootPath(path))
         
         # make sure it is at the front of the path
         if base_path in sys.path:
             sys.path.remove(base_path)
         
         sys.path.insert(0, base_path)
         processed = ['__init__']
         
         # load support for registries
         if typ & Plugin.Type.RegistryFile:
             files = glob.glob(os.path.join(path, '*/register.xml'))
             for file in files:
                 name = os.path.normpath(file).split(os.path.sep)[-2]
                 processed.append(name)
                 
                 try:
                     proxy = PluginProxy.fromFile(cls, file)
                     cls.register(proxy)
                 
                 except Exception, e:
                     name = projex.text.pretty(name)
                     err  = Plugin(name)
                     err.setError(e)
                     err.setFilepath(file)
                     
                     cls.register(err)
                     
                     # log the error
                     msg = "%s.plugin('%s') failed to load from %s."
                     logger.warning(msg % (cls.__name__, name, file))
                     logger.error(e)
         
         # load support for packages
         if typ & Plugin.Type.Package:
             files = glob.glob(os.path.join(path, '*/__init__.py'))
             for file in files:
                 name = os.path.normpath(file).split(os.path.sep)[-2]
                 if ( name in processed ):
                     continue
                 
                 processed.append(name)
                 package = '.'.join([base_package, name]).strip('.')
                 if ( not package ):
                     continue
                 
                 try:
                     __import__(package)
                 
                 except Exception, e:
                     name = projex.text.pretty(name)
                     err  = Plugin(name)
                     err.setError(e)
                     err.setFilepath(file)
                     
                     cls.register(err)
                     
                     # log the error
                     msg = "%s.plugin('%s') failed to load from %s."
                     logger.warning(msg % (cls.__name__, name, file))
                     logger.error(e)
Exemple #8
0
def generate(  module,
               outputpath   = './resources/docs/html',
               userpath     = '',
               config       = None ):
    """
    Generates documenation for the inputed filepath at the given output \
    location.  The system can also supply the root location for user \
    documentation that can be included in addition to the code by supplying \
    a root userdocs path.
    
    :param      module        | <module> || <str>
    :param      outputpath    | <str>
    :param      config        | <module> || None
    
    :return     <bool> success
    """
    from projex.docgen.document import Document
    
    if ( type(module) == str ):
        # extract from a specific filepath
        if ( os.path.exists(module) ):
            package = projex.packageFromPath(module)
            if ( not package ):
                msg = '%s is an invalid module.' % module
                raise errors.DocumentationError(msg)
            
            root_path = projex.packageRootPath(module)
            sys.path.insert(0, root_path)
        
        # otherwise, use it as the package name
        else:
            projex.requires(module)
            package = module.split('-')[0]
        
        try:
            __import__(package)
            module = sys.modules[package]
        
        except ImportError:
            msg = 'Could not import the %s module' % package
            raise errors.DocumentationError(msg)
        
        except KeyError:
            msg = 'Could not import the %s module' % package
            raise errors.DocumentationError(msg)
    
    # initialize the global environ
    if ( config is None ):
        config = default_config
    
    init_environ(module, config)
    
    logger.info('Generating module documentation %s...' % module.__name__)
    
    # load the base page and style information
    page  = templates.template('page.html')
    
    # start generating the documents
    Document.cache.clear()
    
    # generate the module documentation
    try:
        ignore = config.IGNORE
    except AttributeError:
        ignore = None
    
    doc = generateModuleDocument(module, ignore = ignore)
    
    modpath = os.path.dirname(module.__file__)
    outpath = outputpath.replace( './', modpath + '/' )
    
    # clear the docs path
    if ( os.path.exists( outpath ) ):
        shutil.rmtree( outpath )
    
    if ( not os.path.exists( outpath ) ):
        logger.info( 'Making doc path: ' + outpath )
        os.makedirs(outpath)
    
    # make the root api documentation
    api_path = outpath.rstrip('/') + '/api/'
    if not os.path.exists(api_path):
        logger.info( 'Making doc path:' + api_path )
        os.mkdir(api_path)
    
    # generate the api docs
    doc.export(api_path, page = page)
    
    # generate the all classes page
    generateClassDocs( outpath, page, module.__name__ )
    generateModuleDocs( outpath, page, module.__name__)
    generateFunctionDocs( outpath, page, module.__name__)
    generateApiIndex( outpath, page )
    generateDocumentIndex( outpath, page)
    
    # create the user docs
    if ( userpath ):
        userdocs = os.path.abspath(userpath)
    elif ( config != default_config ):
        userdocs = os.path.dirname(config.__file__)
    else:
        userdocs = os.path.abspath('./docs')
    
    if ( os.path.exists(userdocs) ):
        targetuserdocs = outpath
        templ  = templates.template('link_breadcrumbs.html')
        
        generateUserDocs( userdocs, targetuserdocs, page )
    
    # copy the syntaxhighlighter code
    targetpath = os.path.join(outpath,'_static')
    
    paths = []
    paths.append(resources.find('ext/prettify'))
    paths.append(templates.path('javascript'))
    paths.append(templates.path('css'))
    paths.append(templates.path('images'))
    paths.append(templates.path('img'))
    
    logger.info('Copying static resources...')
    
    # python26+
    try:
        ignore = shutil.ignore_patterns('.svn')
    except AttributeError:
        ignore = None
        
    for path in paths:
        if ( not os.path.exists(path) ):
            continue
        
        basename = os.path.basename(path)
        instpath = os.path.join(targetpath, basename)
        
        if ( ignore is not None ):
            shutil.copytree( path, instpath, ignore = ignore )
        else:
            shutil.copytree(path, instpath)
    
    # create a compressed xdk file
    logger.info('Creating XDK file...')
    zfilename = os.path.join(outpath, '../%s.xdk' % module.__name__)
    zfilename = os.path.abspath(zfilename)
    
    generateXdk(outpath, zfilename)