Source code for pycante

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# pycante.py

# "THE WISKEY-WARE LICENSE":
# <jbc.develop@gmail.com> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a WISKEY in return Juan BC


#===============================================================================
# DOC
#===============================================================================

"""The hotest way to deal with PyQt

Allows a unique way to deal with QtDesigner *.ui* files.

**Example:**

.. code-block:: python

        from PyQt4 import QtGui
        import pycante

        class MyWidget(pycante.E("/path/to/file.ui"), AnotherClass);
            pass

        class MyAnotherWidget(pycante.E(QtGui.QFrame), AnotherClass):
            pass

        w0 = MyWidget()
        w1 = MyAnotherWidget()

BTW, *picante* in spanish means *spicy*.

"""

#===============================================================================
# META
#===============================================================================

__version__ = "1.0"
__license__ = "WISKEY License"
__author__ = "JBC"
__email__ = "jbc dot develop at gmail dot com"
__url__ = "https://bitbucket.org/leliel12/pycante"
__date__ = "2011-08-24"


#===============================================================================
# IMPORTS
#===============================================================================

import os, sys, inspect

from PyQt4 import QtGui
from PyQt4 import uic


#===============================================================================
# AutoSetupClass
#===============================================================================

class _AutoSetup(object):
    """This class is used for auto execute 'setupUi' method if exist in a
    widget

    """

    def __init__(self, *args, **kwargs):
        super(_AutoSetup, self).__init__(*args, **kwargs)
        if hasattr(self, "setupUi") and hasattr(self.setupUi, "__call__"):
            self.setupUi(self)


#===============================================================================
# FUNCTIONS
#===============================================================================

[docs]def E(ui_or_widget): """Resolve a qt widget class from ui file path or Widget class **Params** :ui_or_widget: for inerith visual stile (can be a designer ui file) **Return** New base class **Example:** .. code-block:: python from PyQt4 import QtGui import pycante class MyWidget(pycante.E("my/ui/file.ui")): pass class AnotherWidget(pycante.E(QtGui.QFrame)): pass """ ui_path = None bases = None if inspect.isclass(ui_or_widget) and issubclass(ui_or_widget, QtGui.QWidget): bases = [ui_or_widget] else: ui_path = ui_or_widget bases = list(uic.loadUiType(ui_path)) name = bases[0].__name__ bases = tuple([_AutoSetup] + bases) cls_locals = {"__ui_file__": ui_path} return type(name, bases, cls_locals)
[docs]def EDir(path): """Creates a binding for resolve *.ui* files in to a given ``path``. If you use a widget class the path is ignored. **Params:** :path: A path to a firectory where all the ui files lives. **Return:** Bind to a given path. **Example:** .. code-block:: python from PyQt4 import QtGui import pycante UI = pycante.EDir("path/to/where/all/my/uis/files/live") class MyWidget(UI("file.ui")); pass class MyAnotherWidget(UI(QtGui.QFrame)): pass w0 = MyWidget() w1 = MyAnotherWidget() """ assert isinstance(path, basestring) def ED(ui_or_widget): if isinstance(ui_or_widget, basestring): return E(os.path.join(path, ui_or_widget)) return E(ui_or_widget) return ED #============================================================================== # MAIN #===============================================================================
if __name__ == "__main__": print(__doc__)