def __init__(self, dev=True, parent=None):
        super(RenameWidget, self).__init__(title='Rename', parent=parent)

        names_config = configs.get_config(
            config_name='tpRigToolkit-names',
            environment='development' if dev else 'production')
        naming_config = configs.get_config(
            config_name='tpRigToolkit-naming',
            environment='development' if dev else 'production')
        rename_widget = RenameToolset(names_config=names_config,
                                      naming_config=naming_config,
                                      parent=self)
        self.main_layout.addWidget(rename_widget)
Ejemplo n.º 2
0
def preffix_suffix_widget(client, naming_config=None, parent=None):
    if not naming_config:
        naming_config = configs.get_config(config_name='tpDcc-naming')

    model = PrefixSuffixWidgetModel(config=naming_config)
    controller = PrefixSuffixWidgetController(client=client, model=model)
    view = PrefixSuffixView(model=model, controller=controller, parent=parent)

    return view
Ejemplo n.º 3
0
    def __init__(self, *args, **kwargs):

        self._names_config = kwargs.get('names_config', None)
        self._naming_config = kwargs.get('naming_config', None)
        self._naming_lib = kwargs.get('naming_lib', None)
        self._dev = kwargs.get('dev', False)

        if not self._names_config:
            self._names_config = configs.get_config(
                config_name='tpDcc-naming',
                environment='development' if self._dev else 'production')

        if not self._naming_config:
            self._naming_config = configs.get_config(
                config_name='tpDcc-naming',
                environment='development' if self._dev else 'production')

        super(RenamerToolsetWidget, self).__init__(*args, **kwargs)
Ejemplo n.º 4
0
    def get_config(cls, config_id=None):
        """
        Returns tool config
        :return:
        """

        return configs.get_config(config_id or cls.ID,
                                  cls.PACKAGE,
                                  extra_data=cls.config_dict())
Ejemplo n.º 5
0
    def suffixes(self):
        if not self._config:
            return

        suffixes = self._config.get('suffixes', default=list())
        if not suffixes:
            naming_config = configs.get_config('tpDcc-naming')
            if naming_config:
                suffixes = naming_config.get('suffixes', default=dict())

        return suffixes
Ejemplo n.º 6
0
def init_lib(naming_file=None, dev=True, cache=True):
    global _NAME_LIB
    global _NAMING_FILE
    if _NAME_LIB and naming_file and _NAMING_FILE == naming_file and cache:
        return _NAME_LIB

    environment = 'development' if dev else 'production'
    config = configs.get_config('tpRigToolkit-names', environment=environment)
    _NAMING_FILE = naming_file or config.get_path()
    _NAME_LIB = namelib.NameLib(naming_file=_NAMING_FILE)

    return _NAME_LIB
Ejemplo n.º 7
0
    def show_plugin(self):
        super(RenamerPlugin, self).show_plugin()

        # TODO: This should be defined
        dev = False

        names_config = configs.get_config(
            config_name='tpRigToolkit-names',
            environment='development' if dev else 'production')
        naming_config = configs.get_config(
            config_name='tpRigToolkit-naming',
            environment='development' if dev else 'production')

        self._renamer_widget = renamer.RenamerToolsetWidget(
            names_config=names_config,
            naming_config=naming_config,
            parent=self)
        self._renamer_widget.initialize()
        self._renamer_widget.setSizePolicy(QSizePolicy.Expanding,
                                           QSizePolicy.Expanding)
        self._content_layout.addWidget(self._renamer_widget)
Ejemplo n.º 8
0
def init(import_libs=True):
    """
    Initializes tpRigToolkit module
    :param import_libs: bool, Whether to import deps libraries by default or not
    """

    logger = create_logger()

    if import_libs:
        dcc_loader.init()

    dcc_loader_module = core_dcc.get_dcc_loader_module('tpRigToolkit.dccs')
    if dcc_loader_module:
        dcc_loader_module.init()

    register_resources()

    # Register configuration files
    dev = strings.to_boolean(os.getenv('TPRIGTOOLKIT_DEV', 'False'))
    configs.register_package_configs(PACKAGE, os.path.dirname(tpRigToolkit.config.__file__))
    core_config = configs.get_config('tpRigToolkit-core', environment='development' if dev else 'production')
    if not core_config:
        logger.warning(
            'tpRigToolkit-core configuration file not found! '
            'Make sure that you have tpRigToolkit-config package installed!')
        return None

    libs_to_load = core_config.get('libs', list())
    tools_to_load = core_config.get('tools', list())

    with contexts.Timer('Libraries loaded', logger=logger):
        libs.LibsManager().register_package_libs(PACKAGE, libs_to_register=libs_to_load)

    with contexts.Timer('Tools loaded', logger=logger):
        tools.ToolsManager().register_package_tools(PACKAGE, tools_to_register=tools_to_load)

    tools_paths = tools.ToolsManager().paths(PACKAGE)
    with contexts.Timer('Toolsets loaded', logger=logger):
        qt_toolsets.ToolsetsManager().register_package_toolsets(
            PACKAGE, os.path.dirname(os.path.abspath(tpRigToolkit.toolsets.__file__)), tools_paths)

    with contexts.Timer('Menu created', logger=logger):
        main_menu = menus.create_main_menu(
            package_name=PACKAGE, force_creation=True, icon=resources.icon('tpRigToolkit'))
        if main_menu:
            hub_action = QAction(resources.icon('tpRigToolkit'), 'Hub', main_menu)
            main_menu.addAction(hub_action)
            hub_action.triggered.connect(run_hub)
        menus.create_menus(package_name=PACKAGE, dev=dev, force_main_menu_creation=False)
Ejemplo n.º 9
0
def init(dev=False):
    """
    Initializes module
    :param dev: bool, Whether tpDcc-core is initialized in dev mode or not
    """

    if dev:
        os.environ['TPDCC_DEV'] = str(dev)
    logger = create_logger(dev=dev)

    # Get DCC loader module
    dcc_loader_module = core_dcc.get_dcc_loader_module()
    logger.info('DCC loader module found: "{}"'.format(dcc_loader_module))
    if dcc_loader_module:
        dcc_loader_module.init_dcc()

    # After that, we initialize Qt library (we must do it after tpDcc one because tpDcc-libs-qt depends on tpDcc-core)
    # NOTE: DCC UI modules are automatically loaded by tpDcc-libs-qt
    qt_loader.init(dev=dev)

    configs.register_package_configs(PACKAGE,
                                     os.path.dirname(tpDcc.config.__file__))
    core_config = configs.get_config(
        'tpDcc-core', environment='development' if dev else 'production')
    if not core_config:
        logger.warning(
            'tpDcc-core configuration file not found! Make sure that you have tpDcc-config package installed!'
        )
        return None

    libs_to_load = core_config.get('libs', list())
    tools_to_load = core_config.get('tools', list())

    with contexts.Timer('Libraries loaded', logger=logger):
        libs.LibsManager().register_package_libs(PACKAGE,
                                                 libs_to_register=libs_to_load,
                                                 dev=dev)
        libs.LibsManager().load_registered_libs(PACKAGE)

    with contexts.Timer('Tools loaded', logger=logger):
        tools.ToolsManager().register_package_tools(
            PACKAGE, tools_to_register=tools_to_load, dev=dev)
        tools.ToolsManager().load_registered_tools(PACKAGE)

    with contexts.Timer('Toolsets loaded', logger=logger):
        toolsets.ToolsetsManager().register_path(
            PACKAGE, os.path.dirname(toolsets.__file__))
        toolsets.ToolsetsManager().load_registered_toolsets(
            PACKAGE, tools_to_load=tools_to_load)
Ejemplo n.º 10
0
def get_auto_suffixes(dev=True):
    """
    Returns dictionary containing suffixes that can be used to handle nomenclature
    :return:
    """

    environment = 'development' if dev else 'production'
    naming_config = configs.get_config(config_name='tpRigToolkit-naming',
                                       environment=environment)
    if not naming_config:
        return dict()

    auto_suffixes_dict = naming_config.get('auto_suffixes', default=dict())
    if not auto_suffixes_dict:
        return dict()

    return auto_suffixes_dict
Ejemplo n.º 11
0
    def __init__(self, config=None, names_config=None, naming_config=None):
        super(RenamerModel, self).__init__()

        self._config = config if config else tools.ToolsManager(
        ).get_tool_config('tpDcc-tools-renamer')
        self._names_config = names_config if names_config else configs.get_config(
            config_name='tpDcc-naming')
        self._naming_config = naming_config
        self._selection_type = 0
        self._hierarchy_check = False
        self._rename_shape = True
        self._filter_type = ''
        self._rules = list()
        self._active_rule = None
        self._tokens = list()
        self._unique_id_auto = True
        self._last_joint_end_auto = True
Ejemplo n.º 12
0
    def load_plugin(self,
                    pkg_name,
                    pkg_loaders,
                    environment,
                    root_pkg_name=None,
                    config_dict=None,
                    load=True):
        """
        Implements load_plugin function
        Registers a plugin instance to the manager
        :param pkg_name: str
        :param pkg_loaders: plugin instance to register
        :param environment:
        :param root_pkg_name:
        :param config_dict:
        :param load:
        :return: Plugin
        """

        from tpDcc.managers import configs

        if not pkg_loaders:
            return False

        package_loader = pkg_loaders[0] if isinstance(pkg_loaders,
                                                      (list,
                                                       tuple)) else pkg_loaders
        if not package_loader:
            return False

        if hasattr(package_loader, 'loader'):
            if not package_loader.loader:
                return False

        plugin_path = package_loader.filename if python.is_python2(
        ) else os.path.dirname(package_loader.loader.path)
        plugin_name = package_loader.fullname if python.is_python2(
        ) else package_loader.loader.name

        if not config_dict:
            config_dict = dict()

        config_dict.update({
            'join': os.path.join,
            'user': os.path.expanduser('~'),
            'filename': plugin_path,
            'fullname': plugin_name,
            'root': path_utils.clean_path(plugin_path)
        })

        if pkg_name not in self._plugins:
            self._plugins[pkg_name] = dict()

        libs_found = list()
        version_found = None
        init_fn = None
        mods_found = list()
        # packages_to_walk = [plugin_path] if python.is_python2() else [os.path.dirname(plugin_path)]
        for module_path in modules.iterate_modules(plugin_path,
                                                   skip_inits=False,
                                                   recursive=False):
            module_dot_path = modules.convert_to_dotted_path(module_path)
            try:
                mod = modules.import_module(module_dot_path)
                if not mod:
                    continue
            except Exception:
                continue
            if module_dot_path.endswith('__version__') and hasattr(
                    mod, 'get_version') and callable(mod.get_version):
                if version_found:
                    LOGGER.warning(
                        'Already found version: "{}" for "{}"'.format(
                            version_found, plugin_name))
                else:
                    version_found = getattr(mod, 'get_version')()

            if not init_fn and module_dot_path.endswith('loader') and hasattr(
                    mod, 'init') and callable(mod.init):
                init_fn = mod.init

            mod.LOADED = load
            mods_found.append(mod)

        for mod in mods_found:
            for cname, obj in inspect.getmembers(mod, inspect.isclass):
                for interface in self._interfaces:
                    if issubclass(obj, interface):
                        lib_config_dict = obj.config_dict(
                            file_name=plugin_path) or dict()
                        if not lib_config_dict:
                            continue
                        lib_id = lib_config_dict.get('id', None)
                        tool_config_name = lib_config_dict.get('name', None)
                        if not lib_id:
                            LOGGER.warning(
                                'Impossible to register library "{}" because its ID is not defined!'
                                .format(lib_id))
                            continue
                        if not tool_config_name:
                            LOGGER.warning(
                                'Impossible to register library "{}" because its name is not defined!'
                                .format(tool_config_name))
                            continue
                        if root_pkg_name and root_pkg_name in self._plugins and lib_id in self._plugins[
                                root_pkg_name]:
                            LOGGER.warning(
                                'Impossible to register library "{}" because its ID "{}" its already defined!'
                                .format(tool_config_name, lib_id))
                            continue

                        if not version_found:
                            version_found = '0.0.0'
                        obj.VERSION = version_found
                        obj.FILE_NAME = plugin_path
                        obj.FULL_NAME = plugin_name

                        libs_found.append((module_path, version_found, obj))
                        version_found = True
                        break

        if not libs_found:
            LOGGER.warning(
                'No libraries found in module "{}". Skipping ...'.format(
                    plugin_path))
            return False
        if len(libs_found) > 1:
            LOGGER.warning(
                'Multiple libraries found ({}) in module "{}". Loading first one. {} ...'
                .format(len(libs_found), plugin_path, libs_found[-1]))
            lib_found = libs_found[-1]
        else:
            lib_found = libs_found[0]
        lib_loader = modules.convert_to_dotted_path(lib_found[0])
        lib_loader = loader.find_loader(lib_loader)

        # Check if DCC specific implementation for plugin exists
        dcc_path = '{}.dccs.{}'.format(plugin_name, dcc.get_name())
        dcc_loader = None
        dcc_config = None
        try:
            dcc_loader = loader.find_loader(dcc_path)
        except ImportError:
            pass

        lib_config_dict = lib_found[2].config_dict(
            file_name=plugin_path) or dict()
        lib_id = lib_config_dict['id']
        _tool_name = lib_config_dict['name']

        tool_config_name = plugin_name.replace('.', '-')
        lib_config = configs.get_config(config_name=tool_config_name,
                                        package_name=pkg_name,
                                        root_package_name=root_pkg_name,
                                        environment=environment,
                                        config_dict=config_dict,
                                        extra_data=lib_config_dict)

        if dcc_loader:
            dcc_path = dcc_loader.fullname
            dcc_config = configs.get_config(config_name=dcc_path.replace(
                '.', '-'),
                                            package_name=pkg_name,
                                            environment=environment,
                                            config_dict=config_dict)
            if not dcc_config.get_path():
                dcc_config = None

        # Register resources
        def_resources_path = os.path.join(plugin_path, 'resources')
        # resources_path = plugin_config.data.get('resources_path', def_resources_path)
        resources_path = lib_config_dict.get('resources_path', None)
        if not resources_path or not os.path.isdir(resources_path):
            resources_path = def_resources_path
        if os.path.isdir(resources_path):
            resources.register_resource(resources_path, key='tools')
        else:
            pass
            # tp.logger.debug('No resources directory found for plugin "{}" ...'.format(_plugin_name))

        # Register DCC specific resources
        if dcc_loader and dcc_config:
            def_resources_path = os.path.join(dcc_loader.filename, 'resources')
            resources_path = dcc_config.data.get('resources_path',
                                                 def_resources_path)
            if not resources_path or not os.path.isdir(resources_path):
                resources_path = def_resources_path
            if os.path.isdir(resources_path):
                resources.register_resource(resources_path, key='plugins')
            else:
                pass
                # tp.logger.debug('No resources directory found for plugin "{}" ...'.format(_plugin_name))

        # Create lib loggers directory
        default_logger_dir = os.path.normpath(
            os.path.join(os.path.expanduser('~'), 'tpDcc', 'logs', 'libs'))
        default_logging_config = os.path.join(plugin_path, '__logging__.ini')
        logger_dir = lib_config_dict.get('logger_dir', default_logger_dir)
        if not os.path.isdir(logger_dir):
            os.makedirs(logger_dir)
        logging_file = lib_config_dict.get('logging_file',
                                           default_logging_config)

        lib_package = plugin_name
        lib_package_path = plugin_path
        dcc_package = None
        dcc_package_path = None
        if dcc_loader:
            dcc_package = dcc_loader.fullname if python.is_python2(
            ) else dcc_loader.loader.path
            dcc_package_path = dcc_loader.filename if python.is_python2(
            ) else dcc_loader.loader.name

        self._plugins[pkg_name][lib_id] = {
            'name': _tool_name,
            'package_name': pkg_name,
            'loader': package_loader,
            'config': lib_config,
            'config_dict': lib_config_dict,
            'plugin_loader': lib_loader,
            'plugin_package': lib_package,
            'plugin_package_path': lib_package_path,
            'version': lib_found[1] if lib_found[1] is not None else "0.0.0",
            'dcc_loader': dcc_loader,
            'dcc_package': dcc_package,
            'dcc_package_path': dcc_package_path,
            'dcc_config': dcc_config,
            'logging_file': logging_file,
            'plugin_instance': None
        }

        if init_fn:
            try:
                dev = True if environment == 'development' else False
                init_fn(dev=dev)
                LOGGER.info(
                    'Library "{}" registered and initialized successfully!'.
                    format(plugin_name))
            except Exception:
                LOGGER.warning(
                    'Library "{}" registered successfully but its initialization failed: {}'
                    .format(plugin_name, traceback.format_exc()))
        else:
            LOGGER.info(
                'Library "{}" registered successfully!'.format(plugin_name))

        return True
Ejemplo n.º 13
0
    def load_plugin(self,
                    pkg_name,
                    pkg_loaders,
                    environment,
                    root_pkg_name=None,
                    config_dict=None,
                    load=True):
        """
        Implements load_plugin function
        Registers a plugin instance to the manager
        :param pkg_name: str
        :param pkg_loaders: plugin instance to register
        :param environment:
        :param root_pkg_name:
        :param config_dict:
        :param load:
        :return: Plugin
        """

        if not pkg_loaders:
            return False

        package_loader = pkg_loaders[0] if isinstance(pkg_loaders,
                                                      (list,
                                                       tuple)) else pkg_loaders
        if not package_loader:
            return False

        if hasattr(package_loader, 'loader'):
            if not package_loader.loader:
                return False

        plugin_path = package_loader.filename if python.is_python2(
        ) else os.path.dirname(package_loader.loader.path)
        plugin_name = package_loader.fullname if python.is_python2(
        ) else package_loader.loader.name

        if not config_dict:
            config_dict = dict()

        local = os.getenv('APPDATA') or os.getenv('HOME')

        config_dict.update({
            'join': os.path.join,
            'user': os.path.expanduser('~'),
            'filename': plugin_path,
            'fullname': plugin_name,
            'root': path_utils.clean_path(plugin_path),
            'local': local,
            'home': local
        })

        if pkg_name not in self._plugins:
            self._plugins[pkg_name] = dict()

        tools_found = list()
        version_found = None
        packages_to_walk = [plugin_path] if python.is_python2() else [
            os.path.dirname(plugin_path)
        ]
        for sub_module in pkgutil.walk_packages(packages_to_walk):
            importer, sub_module_name, _ = sub_module
            qname = '{}.{}'.format(plugin_name, sub_module_name)
            try:
                mod = importer.find_module(sub_module_name).load_module(
                    sub_module_name)
            except Exception:
                # LOGGER.exception('Impossible to register plugin: "{}"'.format(plugin_path))
                continue

            if qname.endswith('__version__') and hasattr(mod, '__version__'):
                if version_found:
                    LOGGER.warning(
                        'Already found version: "{}" for "{}"'.format(
                            version_found, plugin_name))
                else:
                    version_found = getattr(mod, '__version__')

            mod.LOADED = load

            for cname, obj in inspect.getmembers(mod, inspect.isclass):
                for interface in self._interfaces:
                    if issubclass(obj, interface):
                        tool_config_dict = obj.config_dict(
                            file_name=plugin_path) or dict()
                        if not tool_config_dict:
                            continue
                        tool_id = tool_config_dict.get('id', None)
                        tool_config_name = tool_config_dict.get('name', None)
                        # tool_icon = tool_config_dict.get('icon', None)
                        if not tool_id:
                            LOGGER.warning(
                                'Impossible to register tool "{}" because its ID is not defined!'
                                .format(tool_id))
                            continue
                        if not tool_config_name:
                            LOGGER.warning(
                                'Impossible to register tool "{}" because its name is not defined!'
                                .format(tool_config_name))
                            continue
                        if root_pkg_name and root_pkg_name in self._plugins and tool_id in self._plugins[
                                root_pkg_name]:
                            LOGGER.warning(
                                'Impossible to register tool "{}" because its ID "{}" its already defined!'
                                .format(tool_config_name, tool_id))
                            continue

                        if not version_found:
                            version_found = '0.0.0'
                        obj.VERSION = version_found
                        obj.FILE_NAME = plugin_path
                        obj.FULL_NAME = plugin_name

                        tools_found.append((qname, version_found, obj))
                        version_found = True
                        break

        if not tools_found:
            LOGGER.warning(
                'No tools found in module "{}". Skipping ...'.format(
                    plugin_path))
            return False
        if len(tools_found) > 1:
            LOGGER.warning(
                'Multiple tools found ({}) in module "{}". Loading first one. {} ...'
                .format(len(tools_found), plugin_path, tools_found[-1]))
            tool_found = tools_found[-1]
        else:
            tool_found = tools_found[0]
        tool_loader = loader.find_loader(tool_found[0])

        # Check if DCC specific implementation for plugin exists
        dcc_path = '{}.dccs.{}'.format(plugin_name, dcc.get_name())
        dcc_loader = None
        dcc_config = None
        try:
            dcc_loader = loader.find_loader(dcc_path)
        except ImportError:
            pass

        tool_config_dict = tool_found[2].config_dict(
            file_name=plugin_path) or dict()
        tool_id = tool_config_dict['id']
        _tool_name = tool_config_dict['name']
        tool_icon = tool_config_dict['icon']

        tool_config_name = plugin_name.replace('.', '-')
        tool_config = configs.get_config(config_name=tool_config_name,
                                         package_name=pkg_name,
                                         root_package_name=root_pkg_name,
                                         environment=environment,
                                         config_dict=config_dict,
                                         extra_data=tool_config_dict)

        if dcc_loader:
            dcc_path = dcc_loader.fullname
            dcc_config = configs.get_config(config_name=dcc_path.replace(
                '.', '-'),
                                            package_name=pkg_name,
                                            environment=environment,
                                            config_dict=config_dict)
            if not dcc_config.get_path():
                dcc_config = None

        # Register resources
        def_resources_path = os.path.join(plugin_path, 'resources')
        # resources_path = plugin_config.data.get('resources_path', def_resources_path)
        resources_path = tool_config_dict.get('resources_path', None)
        if not resources_path or not os.path.isdir(resources_path):
            resources_path = def_resources_path
        if os.path.isdir(resources_path):
            resources.register_resource(resources_path, key='tools')
        else:
            pass
            # tp.logger.debug('No resources directory found for plugin "{}" ...'.format(_plugin_name))

        # Register DCC specific resources
        if dcc_loader and dcc_config:
            def_resources_path = os.path.join(dcc_loader.filename, 'resources')
            resources_path = dcc_config.data.get('resources_path',
                                                 def_resources_path)
            if not resources_path or not os.path.isdir(resources_path):
                resources_path = def_resources_path
            if os.path.isdir(resources_path):
                resources.register_resource(resources_path, key='plugins')
            else:
                pass
                # tp.logger.debug('No resources directory found for plugin "{}" ...'.format(_plugin_name))

        # Create tool loggers directory
        default_logger_dir = os.path.normpath(
            os.path.join(os.path.expanduser('~'), 'tpDcc', 'logs', 'tools'))
        default_logging_config = os.path.join(plugin_path, '__logging__.ini')
        logger_dir = tool_config_dict.get('logger_dir', default_logger_dir)
        if not os.path.isdir(logger_dir):
            os.makedirs(logger_dir)
        logging_file = tool_config_dict.get('logging_file',
                                            default_logging_config)

        tool_package = plugin_name
        tool_package_path = plugin_path
        dcc_package = None
        dcc_package_path = None
        if dcc_loader:
            dcc_package = dcc_loader.fullname if python.is_python2(
            ) else dcc_loader.loader.path
            dcc_package_path = dcc_loader.filename if python.is_python2(
            ) else dcc_loader.loader.name

        self._plugins[pkg_name][tool_id] = {
            'name': _tool_name,
            'icon': tool_icon,
            'package_name': pkg_name,
            'loader': package_loader,
            'config': tool_config,
            'config_dict': tool_config_dict,
            'plugin_loader': tool_loader,
            'plugin_package': tool_package,
            'plugin_package_path': tool_package_path,
            'version': tool_found[1] if tool_found[1] is not None else "0.0.0",
            'dcc_loader': dcc_loader,
            'dcc_package': dcc_package,
            'dcc_package_path': dcc_package_path,
            'dcc_config': dcc_config,
            'logging_file': logging_file,
            'plugin_instance': None
        }

        LOGGER.info('Tool "{}" registered successfully!'.format(plugin_name))

        return True