List Info

Thread: Initializing a Tool with config variables?




Initializing a Tool with config variables?
country flaguser name
United States
2007-03-09 10:58:54
Hey all,

I'm trying to initialize a Tool object with config
variables, but I'm
not sure how. Imagine a tool like:

========= Begin Code =============
class Buffet(Tool):
  def __init__(self, customizable_arg):
    self.o =
Create_Object_We_Only_Want_To_Create_Once(customizable_arg)
    self._point = "before_finalize"
    self.callable = do_something()

  def do_something(self): self.o.pull_trigger()
========= End Code ==============

All of the tools in _cptools.py are instantiated immediately
after
definition; how would I attach this Tool to a page handler
at run
time?

Thanks
Bill Mill
bill.mill at gmail.com


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Initializing a Tool with config variables?
country flaguser name
United States
2007-03-09 12:33:37
Bill Mill wrote:
> I'm trying to initialize a Tool object with config
variables,
> but I'm not sure how. Imagine a tool like:
> 
> ========= Begin Code =============
> class Buffet(Tool):
>   def __init__(self, customizable_arg):
>     self.o =
>
Create_Object_We_Only_Want_To_Create_Once(customizable_arg)
>     self._point = "before_finalize"
>     self.callable = do_something()
> 
>   def do_something(self): self.o.pull_trigger()
> ========= End Code ==============
> 
> All of the tools in _cptools.py are instantiated
immediately after
> definition; how would I attach this Tool to a page
handler at run
> time?

If I understand you correctly, I would just accept the
config arg inside
do_something and use an internal "object created"
flag. The sessions
module does something similar inside sessions.init:

    if not hasattr(cherrypy, "session"):
        cherrypy.session =
cherrypy._ThreadLocalProxy('session')
        if hasattr(sess, "setup"):
            sess.setup()

...so that sess.setup() is only run once, on the first
request. Yours
might look something like this:

class Buffet(Tool):
    
    def __init__(self):
        self.o = None
        self._point = "before_finalize"

    def callable(self, customizable_arg):
        if self.o is None:
            self.o = Create_Once(customizable_arg)
        self.o.pull_trigger()

It's up to you to communicate to users of your Tool that
"customizable
arg" should not be specified per-path. ;)


Robert Brewer
System Architect
Amor Ministries
fumanchuamor.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Initializing a Tool with config variables?
country flaguser name
United States
2007-03-09 14:20:59
> If I understand you correctly, I would just accept the
config arg inside
> do_something and use an internal "object
created" flag. The sessions
> module does something similar inside sessions.init:
>
>     if not hasattr(cherrypy, "session"):
>         cherrypy.session =
cherrypy._ThreadLocalProxy('session')
>         if hasattr(sess, "setup"):
>             sess.setup()

This is indeed exactly what I am trying to do.

>
> ...so that sess.setup() is only run once, on the first
request. Yours
> might look something like this:
>
> class Buffet(Tool):
>
>     def __init__(self):
>         self.o = None
>         self._point = "before_finalize"
>
>     def callable(self, customizable_arg):
>         if self.o is None:
>             self.o = Create_Once(customizable_arg)
>         self.o.pull_trigger()
>

Hmmm... I guess I thought there was going to be a better way
to do
this. This solution just strikes me as functional but messy.
Shouldn't
instantiation code go inside the __init__?

In the interim, I've thrown together some code. Is there any
reason
not to do this:

class Buffet(Tool):
  def __init__(self, customizable_arg):
    self.o = Create_Once(customizable_arg)
    self._point = "before_finalize"
    self.callable = do_something()

  def do_something(self): self.o.pull_trigger()

class Root(object):
  def __init__(self):
    cherrypy.tools.buffet = Buffet('cheetah')
    cherrypy.config.update({'tools.buffet.on': True})

quickstart(Root(), ...)

-Bill Mill
bill.mill at gmail.com


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )