Simon Belak <[EMAIL PROTECTED]> writes:

> An example would be most helpful.
> In general you can't get anything useful at declaration, but during 
> run-time, you can always access the class via self.

And how can I get 'self' inside the decorator?  Inside the class it is
easy...  

The skeleton of what I have is:

================================================================================
# decorador.py
def my_decorator(f, *args, **kw):
    try:
        f(*args, **kw)
    except (exceptions), error:
        ....

    more stuff here
    ...


# module.py
(some standar imports such as identity, TG, datetime, etc.)
import decorator # Michele Simionato's decorator module
from decorador import my_decorator

global_thing1 = value1
global_thing2 = value2

class Module(object):

     def __init__(self):
         ....

     @turbogears.expose(...)
     @identity.require(...)
     def function1(self, parameter1 = default1, parameter2 = default2):
         ...

     
     @turbogears.expose
     @turbogears.validate(...)
     @identity.require(...)
     @decorator.decorator(my_decorator)
     def function2(self, parameter1 = default1, parameter2 = default2,
                   tg_errors = None):

         if tg_errors:
             return self.function1(parameter2 = new_value)
         ...


     @turbogears.expose
     @turbogears.validate(...)
     @turbogears.error_handler(function1)
     @identity.require(...)
     @decorator.decorator(my_decorator)
     def function3(self, parameter1 = default1, parameter2 = default2):
         ...

================================================================================

I use tg_errors syntax in function2 because I couldn't get bind_args to work
:-( (this reduces one functionality and introduces a minor bug in my code, but
I want to get it finished before I waste more time trying to fix it).

If I stack decorators as in function3, what I want works.  If I use the syntax
in function2 it doesn't (and it is a problem in my code, but I can't solve it
without knowing the class or getting some parameters -- global_thing1 and
global_thing2 -- from the module).  

Putting my decorator right before @turbogears.expose() makes global_things
inacessible but would eliminate the minor bug if it worked.

>> Probably I'm doing something I shouldn't be doing, but this is my first
>> decorator -- and is saving me an average of 70+ LOC per module -- and I'm
>> using Michele Simionato's decorator.py to write my own decorators...
>> 
> If you are using it inside TG, turbogears.decorator (based on 
> Simionato's work) will integrate better.

Oops.  I forgot about it.  Is its syntax the same?  I'll give it a try. 

-- 
Jorge Godoy      <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to