示例#1
0
    def _get_module_names_from_folder(self, folder, prefix='mod_'):
        """List a given folder and find all possible module names.

        Module names:
        -------------
        Module names start with the "mod_" and don't end with .pyc or .pyo.

        Consequences:
        -------------
        Valid modules need to have an existing .py file or be folder-modules
        (don't name a folder module mod_foo.pyc :) ), even if they are
        actually loaded from the .pyc or .pyo in the end.
        This is done so that dangling .pyc/.pyo file from a module
        that was removed are not loaded by mistake.
        This situation shouldn't really happen if modRana is installed from a package,
        as all .pyc files are purged during package upgrade and regenerated.
        """
        if USING_QRC:
            # if we are running from qrc, we need to use the pyotherside function for enumerating
            # the modules stored in the qrc "bundle"
            import pyotherside
            module_names = filter(
                lambda x: x[0:len(prefix)] == prefix,
                pyotherside.qrc_list_dir(os.path.join("/", folder)))
        else:
            module_names = filter(lambda x: x[0:len(prefix)] == prefix,
                                  os.listdir(folder))

        # remove the extension
        module_names = map(lambda x: os.path.splitext(x)[0], module_names)
        # return a set of unique module names
        # * like this, two module names will not be returned if there are
        # both py and pyc files
        return set(module_names)
示例#2
0
文件: modrana.py 项目: ryfx/modrana
    def _getModuleNamesFromFolder(self, folder, prefix='mod_'):
        """list a given folder and find all possible module names
        Module names:
        Module names start with the "mod_" and don't end with .pyc or .pyo.
        Consequences:
        Valid modules need to have an existing .py file or be folder-modules
        (don't name a folder module mod_foo.pyc :) ), even if they are
        actually loaded from the .pyc or .pyo in the end.
        This is done so that dangling .pyc/.pyo file from a module
        that was removed are not loaded by mistake.
        This situation shouldn't really happen if modRana is installed from a package,
        as all .pyc files are purged during package upgrade and regenerated."""
        if USING_QRC:
            # if we are running from qrc, we need to use the pyotherside function for enumerating
            # the modules stored in the qrc "bundle"
            import pyotherside 
            moduleNames = filter(
                lambda x: x[0:len(prefix)] == prefix, pyotherside.qrc_list_dir(os.path.join("/", folder))
            )
        else:
            moduleNames = filter(
                lambda x: x[0:len(prefix)] == prefix, os.listdir(folder)
            )

        # remove the extension
        moduleNames = map(lambda x: os.path.splitext(x)[0], moduleNames)
        # return a set of unique module names
        # * like this, two module names will not be returned if there are
        # both py and pyc files
        return set(moduleNames)
示例#3
0
def walk(root):
    for entry in pyotherside.qrc_list_dir(root):
        name = os.path.join(root, entry)
        if pyotherside.qrc_is_dir(name):
            walk(name)
        else:
            print(name, '=', len(pyotherside.qrc_get_file_contents(name)), 'bytes')
示例#4
0
文件: utils.py 项目: omartijn/modrana
def internal_listdir(path):
    """Internal listdir function that works on both normal files and files
    bundled in qrc.

    :param str path: path to the folder to list
    :returns: folder contents
    :rtype: list of strings
    """
    if qrc.is_qrc:
        return pyotherside.qrc_list_dir(path)
    else:
        return os.listdir(path)
示例#5
0
文件: utils.py 项目: fferner/modrana
def internal_listdir(path):
    """Internal listdir function that works on both normal files and files
    bundled in qrc.

    :param str path: path to the folder to list
    :returns: folder contents
    :rtype: list of strings
    """
    if qrc.is_qrc:
        return pyotherside.qrc_list_dir(path)
    else:
        return os.listdir(path)
示例#6
0
def export_from_qrc(root, target):
    """Recursively export a given qrc subtree as given by root
       to the target folder.
    """
    #log.debug("exporting %s from qrc", root)
    try:
        file_counter = 0
        folder_counter = 0
        for entry in pyotherside.qrc_list_dir(root):
            name = os.path.join(root, entry)
            if pyotherside.qrc_is_dir(name):
                #log.debug('Creating directory: %s', name)
                os.makedirs(os.path.join(target, name))
                folder_counter += 1
                export_from_qrc(name, os.path.join(target, name))
            else:
                data = pyotherside.qrc_get_file_contents(name)
                #log.debug('Creating file: %s (%d bytes)', name, len(data))
                with open(name, "wb") as f:
                    f.write(data)
                file_counter += 1
        #log.debug("%d files and %d folders exported from %s", file_counter, folder_counter, root)
    except Exception:
        log.exception("qrc export from %s to %s failed", root, target)
示例#7
0
文件: qrc.py 项目: fferner/modrana
def export_from_qrc(root, target):
    """Recursively export a given qrc subtree as given by root
       to the target folder.
    """
    #log.debug("exporting %s from qrc", root)
    try:
        file_counter = 0
        folder_counter = 0
        for entry in pyotherside.qrc_list_dir(root):
            name = os.path.join(root, entry)
            if pyotherside.qrc_is_dir(name):
                #log.debug('Creating directory: %s', name)
                os.makedirs(os.path.join(target, name))
                folder_counter += 1
                export_from_qrc(name, os.path.join(target, name))
            else:
                data = pyotherside.qrc_get_file_contents(name)
                #log.debug('Creating file: %s (%d bytes)', name, len(data))
                with open(name, "wb") as f:
                    f.write(data)
                file_counter += 1
        #log.debug("%d files and %d folders exported from %s", file_counter, folder_counter, root)
    except Exception:
       log.exception("qrc export from %s to %s failed", root, target)
示例#8
0
    for entry in pyotherside.qrc_list_dir(root):
        name = os.path.join(root, entry)
        if pyotherside.qrc_is_dir(name):
            walk(name)
        else:
            print(name, '=', len(pyotherside.qrc_get_file_contents(name)), 'bytes')
walk('/')
print('='*30)
print(pyotherside.qrc_get_file_contents('qrc_example.py').decode('utf-8'))
print('='*30)

try:
    print('dir exists with number', pyotherside.qrc_is_dir(123))
except Exception as e:
    print('got exception (as expected):', e)

try:
    print('file exists with none', pyotherside.qrc_is_file(None))
except Exception as e:
    print('got exception (as expected):', e)

try:
    print('dir entries with invalid', pyotherside.qrc_list_dir('/nonexistent'))
except Exception as e:
    print('got exception (as expected):', e)

try:
    print('file contents with invalid', pyotherside.qrc_get_file_contents('/qrc_example.qml.nonexistent'))
except Exception as e:
    print('got exception (as expected):', e)