Discussion:
[Erp5-dev] Monkey Patching problem
Łukasz Nowak
2007-09-04 13:36:12 UTC
Permalink
Hello,

I'd like to monkey patch Products.ERP5.Document.Resource file.

I've got my own product (Ventis), its contents are:

Ventis/
Ventis/__init__.py:
import ZopePatch
Ventis/ZopePatch.py:
from zLOG import LOG, INFO
LOG('Ventis Patching',INFO,'Resouce Patch')
from Products.Ventis.patches import ResourcePatch
Ventis/patches/
Ventis/patches/__init__.py (empty)
Ventis/patches/ResourcePatch.py:
from Products.ERP5.Document.Resource import Resource
def shufla(self):
print 'eeek eeeek'
Resource.shufla = shufla

I'm 100% sure that my product is analysed - adding logs into
Ventis/ZopePatch.py and Ventis/patches/ResourcePatch.py shows them
while starting zope.

But when I set breakpoint in Products/ERP5/Document/Resource.py, catch
it and try to invoke shufla(), I've got AttributeError.

What do I do wrong? I've peeked some data from ERP5Type monkeypatching
and all *seems* to be correct.

Thanks for any tips.

Regards,
Luke

PS. Same problem, while trying to monkeypatch
Products.ERP5.Document.Order...
--
?ukasz Nowak R&D Ventis http://www.ventis.com.pl/
tel: +48 32 768 16 85 fax: +48 32 392 10 61
``Use the Source, Luke...''
Jérome Perrin
2007-09-04 13:51:09 UTC
Permalink
Yes, monkey patching _is_ a problem :)
Post by Łukasz Nowak
Hello,
I'd like to monkey patch Products.ERP5.Document.Resource file.
Ventis/
import ZopePatch
from zLOG import LOG, INFO
LOG('Ventis Patching',INFO,'Resouce Patch')
from Products.Ventis.patches import ResourcePatch
Ventis/patches/
Ventis/patches/__init__.py (empty)
from Products.ERP5.Document.Resource import Resource
When ERP5Type is initialized, it loads all documents from ERP5-like
products by affecting them a dynamic path, everything is under ERP5Type
product for forward compatibility (otherwise renaming a product or
moving a document from one product to another is a nightmare).
Resource will be loaded as Products.ERP5Type.Document.Resource.Resource
but this is only after ERP5Type is initialized.

J?rome
Łukasz Nowak
2007-09-04 14:06:31 UTC
Permalink
Hello,

On 2007-09-04, 15:51:09
Post by Jérome Perrin
Yes, monkey patching _is_ a problem :)
Post by Łukasz Nowak
Hello,
I'd like to monkey patch Products.ERP5.Document.Resource file.
Ventis/
import ZopePatch
from zLOG import LOG, INFO
LOG('Ventis Patching',INFO,'Resouce Patch')
from Products.Ventis.patches import ResourcePatch
Ventis/patches/
Ventis/patches/__init__.py (empty)
from Products.ERP5.Document.Resource import Resource
When ERP5Type is initialized, it loads all documents from ERP5-like
products by affecting them a dynamic path, everything is under
ERP5Type product for forward compatibility (otherwise renaming a
product or moving a document from one product to another is a
nightmare). Resource will be loaded as
Products.ERP5Type.Document.Resource.Resource but this is only after
ERP5Type is initialized.
So... shall I do from Products.ERP5Type.Document.Resource import
Resource? It is not working (ImportError: No module named Resource). So
clue is in "but this is only after ERP5Type is initialized." - how
wait/hook into it? Any code snippet to achieve it?

Thanks,
Luke
--
?ukasz Nowak R&D Ventis http://www.ventis.com.pl/
tel: +48 32 768 16 85 fax: +48 32 392 10 61
``Use the Source, Luke...''
Romain Courteaud
2007-09-04 14:41:21 UTC
Permalink
Post by Łukasz Nowak
So... shall I do from Products.ERP5Type.Document.Resource import
Resource? It is not working (ImportError: No module named Resource). So
clue is in "but this is only after ERP5Type is initialized." - how
wait/hook into it? Any code snippet to achieve it?
So, in this case, you have to use portal_classes to surcharge the
Document like this:

from Products.ERP5Type.Document.Resource import Resource as ERP5Resource
class Resource(ERP5Resource):
def foo(...)


Regards,
Romain
Łukasz Nowak
2007-09-04 15:55:28 UTC
Permalink
Hello,

On 2007-09-04, 16:41:21
Post by Romain Courteaud
Post by Łukasz Nowak
So... shall I do from Products.ERP5Type.Document.Resource import
Resource? It is not working (ImportError: No module named
Resource). So clue is in "but this is only after ERP5Type is
initialized." - how wait/hook into it? Any code snippet to achieve
it?
So, in this case, you have to use portal_classes to surcharge the
from Products.ERP5Type.Document.Resource import Resource as
def foo(...)
Ha! Got it! Thanks. Wouldn't be nice to put that information on wiki,
with nice explanations and so on - by experienced developer? I know,
that one shall not monkey patch ERP5, but sometimes it is only solution
(for now we were using something like patch-o-matic in Ventis, to patch
source code).

Regards,
Luke
--
?ukasz Nowak R&D Ventis http://www.ventis.com.pl/
tel: +48 32 768 16 85 fax: +48 32 392 10 61
``Use the Source, Luke...''
Pelletier Vincent
2007-09-04 14:45:56 UTC
Permalink
how wait/hook into it? Any code snippet to achieve it?
My opinion is that ERP5Type loading of document types should be a bit smarter
to avoid loading the same document type multiple times (list loaded document
types, list loadable document types, mix'n'match) or to develop a more
complete priority handling for document classes, allowing - in your case - to
create your own version of Resource document class which inherits explicitely
from ERP5 one but get loaded instead of it.

I have no idea of how long it can take nor of the right way to do it at the
moment...
--
Vincent Pelletier
Loading...