ComponentLookupError on plone.namedfile

I trying to load an image from a file to the image field of a content type.

my code looks like this:

def get_image(filename):
    path = os.path.join(os.path.dirname(__file__), 'tests')
    data = open(os.path.join(path, filename)).read()
    return NamedBlobImage(data, filename)

when I call the function I'm getting the following error:

Traceback (most recent call last):
  File "/home/hvelarde/MyProject/src/my.package/src/my/package/tests/test_setup.py", line 2, in <module>
    from my.package.config import DEPENDENCIES
  File "/home/hvelarde/MyProject/src/my.package/src/my/package/config.py", line 57, in <module>
    image=get_image(),
  File "/home/hvelarde/MyProject/src/my.package/src/my/package/utils.py", line 22, in get_image
    return NamedBlobImage(data, filename)
  File "/home/hvelarde/.buildout/eggs/plone.namedfile-2.0.5-py2.7.egg/plone/namedfile/file.py", line 423, in __init__
    super(NamedBlobImage, self).__init__(data, filename=filename)
  File "/home/hvelarde/.buildout/eggs/plone.namedfile-2.0.5-py2.7.egg/plone/namedfile/file.py", line 373, in __init__
    self._setData(data)
  File "/home/hvelarde/.buildout/eggs/plone.namedfile-2.0.5-py2.7.egg/plone/namedfile/file.py", line 430, in _setData
    super(NamedBlobImage, self)._setData(data)
  File "/home/hvelarde/.buildout/eggs/plone.namedfile-2.0.5-py2.7.egg/plone/namedfile/file.py", line 390, in _setData
    storable = getUtility(IStorage, name=dottedName)
  File "/home/hvelarde/.buildout/eggs/zope.component-3.9.5-py2.7.egg/zope/component/_api.py", line 169, in getUtility
    raise ComponentLookupError(interface, name)
ComponentLookupError: (<InterfaceClass plone.namedfile.interfaces.IStorage>, '__builtin__.str')

seems to me that the configure.zcml files of plone.namedfile is not being read.

the code is pretty similar to something I have used before. the only difference is I'm not using:

<includeDependencies package="." />

any idea of what is going on?

On 6/4/14, 2:03 PM, hvelarde wrote:

[hvelarde] hvelarde
http://mandrillapp.com/track/click.php?u=30171175&id=93756a38b9dd48e991797719ba72cda6&url=http%3A%2F%2Fcommunity.plone.org%2Fusers%2Fhvelarde&url_id=9b21963a955516639ca43302e5bc6d64b00edf52
June 4

I trying to load an image from a file to the image field of a content
type.

my code looks like this:

|def get_image(filename):
path = os.path.join(os.path.dirname(file), 'tests')
data = open(os.path.join(path, filename)).read()
return NamedBlobImage(data, filename)|

when I call the function I'm getting the following error:

|Traceback (most recent call last):
File "/home/hvelarde/MyProject/src/my.package/src/my/package/tests/test_setup.py", line 2, in
from my.package.config import DEPENDENCIES
File "/home/hvelarde/MyProject/src/my.package/src/my/package/config.py", line 57, in
image=get_image(),
File "/home/hvelarde/MyProject/src/my.package/src/my/package/utils.py", line 22, in get_image
return NamedBlobImage(data, filename)
File "/home/hvelarde/.buildout/eggs/plone.namedfile-2.0.5-py2.7.egg/plone/namedfile/file.py", line 423, in init
super(NamedBlobImage, self).init(data, filename=filename)
File "/home/hvelarde/.buildout/eggs/plone.namedfile-2.0.5-py2.7.egg/plone/namedfile/file.py", line 373, in init
self._setData(data)
File "/home/hvelarde/.buildout/eggs/plone.namedfile-2.0.5-py2.7.egg/plone/namedfile/file.py", line 430, in _setData
super(NamedBlobImage, self)._setData(data)
File "/home/hvelarde/.buildout/eggs/plone.namedfile-2.0.5-py2.7.egg/plone/namedfile/file.py", line 390, in _setData
storable = getUtility(IStorage, name=dottedName)
File "/home/hvelarde/.buildout/eggs/zope.component-3.9.5-py2.7.egg/zope/component/_api.py", line 169, in getUtility
raise ComponentLookupError(interface, name)
ComponentLookupError: (, 'builtin.str')|

seems to me that the configure.zcml files of plone.namedfile is not
being readed.

the code is pretty similar to something I have used before. the only
difference is I'm not using:

||

any idea of what is going on?


NamedBlobImage can't be instantiated until after zcml has loaded
(because it relies on these utilities), but you're trying to instantiate
one at import time. You'll have to find a way to defer that until after
configuration.

I don't know how you're using this image, but here's what I did in a
similar situation:

DEFAULT_AVATAR = None

later in the same module, in a content type class:

 def _get_portrait(self):
     global DEFAULT_AVATAR
     if DEFAULT_AVATAR is None:
         DEFAULT_AVATAR =

open(os.path.join(os.path.dirname(file), 'browser', 'static',
'default_avatar.png')).read()
return self.dict.get('portrait') or
NamedBlobImage(DEFAULT_AVATAR, 'image/png', u'default_avatar.png')
def _set_portrait(self, value):
self.dict['portrait'] = value
portrait = property(_get_portrait, _set_portrait)

This way there's still only one copy of the image in memory, but it
doesn't load until the system's fully configured.

David

Ugh, Discourse didn't do nice things to that email reply. Here's the code sample:

DEFAULT_AVATAR = None

# later in the same module, in a content type class:
def _get_portrait(self):
    global DEFAULT_AVATAR
    if DEFAULT_AVATAR is None:
        DEFAULT_AVATAR = open(os.path.join(os.path.dirname(__file__), 'browser', 'static', 'default_avatar.png')).read()
        return self.__dict__.get('portrait') or NamedBlobImage(DEFAULT_AVATAR, 'image/png', u'default_avatar.png')
def _set_portrait(self, value):
    self.__dict__['portrait'] = value
portrait = property(_get_portrait, _set_portrait)

probably this forum is not the best place to ask this kind of questions.

what about manually registering the utility in my code?

I ended up successfully using the helper method registerUtilities declared in the tests of plone.namedfile just to find out that in fact I did not need a NamedBlobImage instance, but only the image itself.

thanks!