0

I have an Interface like:

class IRepository(Interface):
    def __init__(path, **options):
        pass

I have implementations of this interface for both Git, and Mercurial. Now I want to write repository-factory that takes a string (the path) and returns an IRepository, by probing if it's a git or hg repository.

However, simply saying:

registerAdapter(repofactory, (str, unicode, ), IRepository)

does not work, cause neither str nor unicode support the IInterface interface.

For now, I'm going with:

registerAdapter(repofactory, (Interface, ), IRepository)

But I would like to know if there are interfaces out there that match only string objects and other Python built-in types.

manu
  • 3,544
  • 5
  • 28
  • 50

1 Answers1

0

No, string and unicode objects can't have interfaces. But for this use-case, I'd register named utilities instead and look up the utility by name, or list all utilities available:

from zope.component import getUtilitiesFor, getUtility

names = [name for name, utility in getUtilitiesFor(IRepository)]

gitrepo = getUtility(IRepository, name='git')
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Not exactly a solution, since the string is path in the fs and I would like to have an adapter that probes. – manu Jan 08 '13 at 22:17