Changeset 189

Show
Ignore:
Timestamp:
21.06.2008 12:39:59 (7 months ago)
Author:
poillubo
Message:

* some modifications for the config generator:

+ added functions to know if a fun/meth arg is a **args or **kwargs
+ added a special case in get_default_arg when there are no default args
+ added the find_default_buttons functions (same use as find_default_plugins, ...)
+ added an overriding constructor in some buttons
+ changed a useless PlopButton? into Button in the default config

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/conf/config-example.py

    r170 r189  
    2424from pycawm import (Client, ClientUnderPointer, MoveUnderPointer, PycaWM, 
    2525                    ScreenshotUnderPointer) 
    26 from pycawm.button import (CloseButton, MiniIconButton, ShadeButton, 
    27                            MaximizeButton, IconifyButton, PlopButton
     26from pycawm.button import (Button, CloseButton, MiniIconButton, ShadeButton, 
     27                           MaximizeButton, IconifyButton
    2828from pycawm.hookmanager import add_post_hook, add_pre_hook 
    2929from pycawm.menu import (Menu, FocusesMenu, IconifiedClientsMenu, 
     
    215215                dict(x=500, y=0, 
    216216                     buttons=[ 
    217     [PlopButton('plop.png', 
     217    [Button('plop.png', 
    218218                action_1=lambda self, x, y: ApplicationMenu(pika, x=x, y=y+16)), 
    219      PlopButton('plop.png', 
     219     Button('plop.png', 
    220220                action_1=lambda self, x, y: ConfigMenu(pika, x=x, y=y+16)), 
    221      PlopButton('plop.png', 
     221     Button('plop.png', 
    222222                action_1=lambda self, x, y: ConfigMenu(pika, x=x, y=y+16))], 
    223     [PlopButton('plop.png', 
     223    [Button('plop.png', 
    224224                action_1=lambda self, x, y: ConfigMenu(pika, x=x, y=y+16))], 
    225     [PlopButton('plop.png', 
     225    [Button('plop.png', 
    226226                action_1=lambda self, x, y: ApplicationMenu(pika, x=x, y=y+16)), 
    227      PlopButton('plop.png', 
     227     Button('plop.png', 
    228228                action_1=lambda self, x, y: ConfigMenu(pika, x=x, y=y+16))]])) 
    229229 
  • trunk/pycawm/button.py

    r151 r189  
    129129 
    130130class PlopButton(Button): 
     131    def __init__(self, image): 
     132        Button.__init__(self, image) 
     133 
    131134    def action_2(self): 
    132135        print 'plop!' 
     
    136139 
    137140class CloseButton(Button): 
     141    def __init__(self, image): 
     142        Button.__init__(self, image) 
     143 
    138144    def action_1(self): 
    139145        self.container.close() 
    140146 
    141147class MiniIconButton(Button): 
    142     def __init__(self, *args, **kwargs): 
    143         Button.__init__(self, *args, **kwargs
     148    def __init__(self, image): 
     149        Button.__init__(self, image
    144150        self.images['miniicon'] = Client.get_mini_icon 
    145151 
     
    150156 
    151157class IconifyButton(Button): 
     158    def __init__(self, image): 
     159        Button.__init__(self, image) 
     160 
    152161    def action_1(self): 
    153162        self.container.iconify() 
  • trunk/pycawm/misc.py

    r184 r189  
    2525from types import ClassType, TypeType, MethodType, FunctionType 
    2626 
     27import button 
    2728import focuses 
    2829import placements 
     
    9394 
    9495    defaults = fun.func_defaults 
     96    if defaults is None: 
     97        return None 
     98 
    9599    indice = list(co.co_varnames).index(name) 
    96100    offset = co.co_argcount - len(defaults) 
     
    99103        return defaults[indice - offset] 
    100104    return None 
     105 
     106def is_arg_args(fun, name): 
     107    """Is the NAME argument for the method/function FUN a **args?""" 
     108    fun = get_func_object(fun) 
     109    if fun is None: 
     110        return False 
     111 
     112    co = fun.func_code 
     113    # no *args for this code object? 
     114    if not co.co_flags & 4: 
     115        return False 
     116 
     117    if name not in co.co_varnames: 
     118        return False 
     119 
     120    indice = list(co.co_varnames).index(name) 
     121    return indice == co.co_argcount 
     122 
     123def is_arg_kwargs(fun, name): 
     124    """Is the NAME argument for the method/function FUN a **kwargs?""" 
     125    fun = get_func_object(fun) 
     126    if fun is None: 
     127        return False 
     128 
     129    co = fun.func_code 
     130    # no **kwargs for this code object? 
     131    if not co.co_flags & 8: 
     132        return False 
     133 
     134    if name not in co.co_varnames: 
     135        return False 
     136 
     137    indice = list(co.co_varnames).index(name) 
     138    return indice == co.co_argcount + 1 
    101139 
    102140def is_class_valid(klass, base): 
     
    132170    return [plugin for plugin in gen_plugs 
    133171            if is_plugin_valid(plugin)] 
     172 
     173def is_button_valid(button_): 
     174    try: 
     175        # do not use is_strict_subclass here, Button is a usable button too 
     176        return (issubclass(button_, button.Button) and 
     177                not button_.__name__.startswith('_')) 
     178    except TypeError: 
     179        return False 
     180 
     181def find_default_buttons(): 
     182    """Find all the button classes in the "button" module.""" 
     183    gen_buttons = (button.__dict__.get(button_name) 
     184                   for button_name in dir(button)) 
     185    return [button_ for button_ in gen_buttons 
     186            if is_button_valid(button_)]