On Mon, Apr 17, 2006 at 03:23:50PM +0200, Christian Boos wrote:
> obj = TracObject.factory(type, id)
I was thinking of something along these same lines after taking a look
at object.py last night. This would essentially do this very thing:
Add this interface to env.py:
class IResourceLocator(Interface):
""" An interface for locating Resources in the Trac environment. """
def get_resource_handles():
""" Return an iterable of ResourceHandle classes for all resources
this component is aware of. """
And this class:
class ResourceHandle(object):
""" A ResourceHandle object knows the type and ID of a Trac resource,
and
also how to instantiate the actual resource. """
type = None
def __init__(self, id):
self.id = id
def fetch(self):
""" Fetch the actual resource of the given type and ID. """
raise NotImplementedError
And finally this method to Environment:
def get_resource_handle(self, type, id):
""" Fetch a ResourceHandle object for resources with the given type
and ID. eg. get_resource_handle('wiki', 'WikiStart') """
for locator in self.resource_locators:
for handle in locator.get_resource_handles():
if handle.type == type:
return handle(id)
raise TracError("No resource handle found for resources of type %s"
% type)
eg.
>>> from trac.env import Environment
>>> e = Environment('../env/security')
h = e.get_resource_handle('ticket', 1)
>>> h
<trac.env.TicketResourceHandle object at 0xb774546c>
>>> h.type
ticket
>>> h.id
1
>>> h.fetch()
<6 <trac.ticket.model.Ticket object at 0xb77454cc>
The idea being, of course, that ResourceHandle objects are passed around
the access control system rather than the actual objects.
Still, all being said, I think this is probably a kludge, only really
useful for the security system. I'd prefer to simply use a simple tuple,
or the object itself if available.
> you get the appropriate instance (e.g. a WikiPage, a Ticket) in
> the unloaded state, which would be enough for permission related usage.
>
> As a straightforward extension of what I already did, a call to the factory
> with an non-registered type could simply result in an instance of TracObject
> itself (e.g. when doing TracObject.factory('config', 'plugins')).
>
> I can only encourage you to have a look at:
> http://projects.edgewall.com/trac/browser/sandbox/trac-xref/trac/object.py
> and, e.g. wiki/model.py or ticket/model.py .
Yeah I had a look at it before posting one of my other rants :) (the one
about resource facets).
> Not only would there be the advantages listed above, but the code could
> be simplified
> a lot by not having to have to give (env, req, (eventually db))
> arguments everywhere...
I agree, a consistent interface to all Trac objects would be very nice.
--
Evolution: Taking care of those too stupid to take care of themselves.
_______________________________________________
Trac-dev mailing list
[email protected]
http://lists.edgewall.com/mailman/listinfo/trac-dev