[PEAK] ZConfig and peak naming

Roché Compaan roche at upfrontsystems.co.za
Fri Jul 30 05:52:20 EDT 2004


Roché Compaan wrote:
> 
> 
> Roché Compaan wrote:
> 
>> * Phillip J. Eby <pje at telecommunity.com> [2004-07-29 23:20]:
>>
>>> Do something like this:
>>>
>>>    class Process(binding.Component):
>>>        fileURL = binding.Require("URL needed",        
>>> adaptTo=naming.IStreamFactory)
>>>        file = binding.Obtain(naming.Indirect('fileURL'))
>>>
>>> And make the 'attribute' for 'file' be 'fileURL' instead.
>>>
>>> 'naming.Indirect("some_name")' translates to, "use the object you 
>>> find at 'some_name' as a name to look up".
>>
>>
>>
>> Nice! Thanks a lot.
> 
> 
> That was premature joy :-( I get the following error:
> 
> protocols.adapters.AdaptationFailure: ("Can't adapt", 
> 'pkgfile:jack/notes.txt', <class 'peak.naming.interfaces.IStreamFactory'>)
> 
> So now I create a naming.URL instance from 'fileURL', and that adapts 
> fine to 'IStreamFactory':
> 
> class Process(binding.Component):
>     ...
>     fileURL = binding.Require("URL needed")
>     file = binding.Obtain(naming.Indirect('_fileURL'),
>         adaptTo=naming.IStreamFactory)
> 
>     def _fileURL(self):
>         from peak.naming.factories.openable import FileURL
>         return naming.toName(self.fileURL, FileURL.fromFilename)
>     _fileURL = binding.Make(_fileURL)
> 
> But if there is a sweet 2 liner that can do the job, I'd like to know 
> about it :-)
> 

I just tried a couple of shorter permutations:

class Process(...):
     ...
     fileURL = binding.Require("URL needed")
     file = binding.Obtain(
         naming.toName(naming.Indirect('fileURL'), FileURL.fromFilename),
         adaptTo=naming.IStreamFactory)

raises:
	peak.exceptions.InvalidName: <peak.naming.names.Indirect object at 
0x4070102c>

Then I tried:

    file = binding.Obtain(
        naming.Indirect('fileURL'),
        adaptTo=[naming.IStreamFactory])

protocols.adapters.AdaptationFailure: ("Can't adapt", 
<peak.naming.factories.openable.FileFactory object at 0x404f8bcc>, 
[<class 'peak.naming.interfaces.IStreamFactory'>])

Which is weird since, FileFactory's instances provide 
naming.IStreamFactory. Why does adaptation fail if the instance provides 
the correct interface?

Ok, so looking at the error above, naming.Indirect('fileURL') already 
resolves to a FileFactory.

Finally, just:

     file = binding.Obtain(naming.Indirect('fileURL'))

worked! If I print "process.file" I correctly have a FileFactory. After 
looking at naming.Indirect and binding.Obtain it made sense though.

-- 
Roché Compaan
Upfront Systems                 http://www.upfrontsystems.co.za



More information about the PEAK mailing list