Hi, all! I would like to define a class inside a python script (so I can catalog it and use catalog to store info, as the tutorial about catalog everything suggests). So I've done: ======== class Autore: " classe per il catalogo " def set_autore(self, autore=''): """ setta l'autore """ self.autore=autore def set_variante(self, variante=''): """ setta l'autore """ self.variante = variante def variante(self): """ ritorna la variante """ return self.variante def set_qualificazione(self, qualificazione=''): """ setta la qualificazione """ self.qualificazione = qualificazione def qualificazione(self): """ ritorna la qualificazione """ return self.qualificazione un_autore = Autore() un_autore.set_autore('prova') un_autore.set_variante('una variante') un_autore.set_qualificazione('una qualificazione') print un_autore.variante() print un_autore.qualificazione() return printed ====== but zope complains about I can call the methods of this class: *Error Type: Unauthorized* *Error Value: You are not allowed to access set_autore in this context So, what can I do? How can I create virtual objects to be cataloged? *
On Wednesday 05 March 2008 10:21:32 Yuri wrote:
Hi, all!
I would like to define a class inside a python script (so I can catalog it and use catalog to store info, as the tutorial about catalog everything suggests).
The default Script(Python) objects dont support class definition, because of security constraints. One easy option would be to just use some other object, eg. a "File" and use its properties. Another option (with more initial effort, but easier to maintain later on) is to implement a custom Product; have a look at http://www.zope.org/Documentation/Books/ZDG/current on how to do this. hth peter.
So I've done:
======== class Autore: " classe per il catalogo "
def set_autore(self, autore=''): """ setta l'autore """ self.autore=autore
def set_variante(self, variante=''): """ setta l'autore """ self.variante = variante
def variante(self): """ ritorna la variante """ return self.variante
def set_qualificazione(self, qualificazione=''): """ setta la qualificazione """ self.qualificazione = qualificazione
def qualificazione(self): """ ritorna la qualificazione """ return self.qualificazione
un_autore = Autore() un_autore.set_autore('prova') un_autore.set_variante('una variante') un_autore.set_qualificazione('una qualificazione')
print un_autore.variante() print un_autore.qualificazione()
return printed
======
but zope complains about I can call the methods of this class:
*Error Type: Unauthorized* *Error Value: You are not allowed to access set_autore in this context
So, what can I do? How can I create virtual objects to be cataloged? * _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Yuri wrote:
Hi, all!
I would like to define a class inside a python script (so I can catalog it and use catalog to store info, as the tutorial about catalog everything suggests).
Its not possible in Python Scripts. You would need to write either an external method or a small product. Regards Tino
Tino Wildenhain wrote:
Yuri wrote:
Hi, all!
I would like to define a class inside a python script (so I can catalog it and use catalog to store info, as the tutorial about catalog everything suggests).
Its not possible in Python Scripts. You would need to write either an external method or a small product.
An external method can load only a function, not a class, so really I need to write a product just only for a class?
Regards Tino
(Wed, Mar 05, 2008 at 10:43:33AM +0100) Yuri wrote/schrieb/egrapse:
An external method can load only a function, not a class, so really I need to write a product just only for a class?
A simple product can be had with as little as 80 lines of code: http://papakiteliatziar.gr/BetaBoring Since doing anything properly in Zope leads to writing filesystem based products anyway, why bother with ZMI python scripts for anything but the most basic "script" use? Regards, Sascha
Yuri wrote at 2008-3-5 10:43 +0100:
... An external method can load only a function, not a class, so really I need to write a product just only for a class?
An external method can define classes (but you can not store their instances persistently) and it can import classes from arbitrary Python modules and packages (their instances can be stored persistently). When untrusted code should access the instances, do not forget to add security declarations and activate them. You find details in the Zope Developper Guide (it is old but still describes Zope 2 quite well). Note that instances that should get indexed usually need to support "getPhysicalPath" and they usually need to be retrievable under the path returned by their "getPhysicalPath". This may not be fully easy to achieve. -- Dieter
Tino Wildenhain wrote:
Yuri wrote:
Hi, all!
I would like to define a class inside a python script (so I can catalog it and use catalog to store info, as the tutorial about catalog everything suggests).
Its not possible in Python Scripts. You would need to write either an external method or a small product.
http://www.faqs.org/docs/ZopeBook/ScriptingZope.html You also cannot easily hand instances of your own classes over to DTML or scripts. The issue here is that your instances won't have Zope security information. You can define and use your own classes and instances to your heart's delight, just don't expect Zope to use them directly. Limit yourself to returning simple Python structures like strings, dictionaries and lists or Zope objects. So developing a product, is the only option?
Hi Yuri, Yuri wrote:
Tino Wildenhain wrote:
Yuri wrote:
Hi, all!
I would like to define a class inside a python script (so I can catalog it and use catalog to store info, as the tutorial about catalog everything suggests).
Its not possible in Python Scripts. You would need to write either an external method or a small product.
http://www.faqs.org/docs/ZopeBook/ScriptingZope.html
You also cannot easily hand instances of your own classes over to DTML or scripts. The issue here is that your instances won't have Zope security information. You can define and use your own classes and instances to your heart's delight, just don't expect Zope to use them directly. Limit yourself to returning simple Python structures like strings, dictionaries and lists or Zope objects.
So developing a product, is the only option?
Well actually its not so hard as it seems. And products are more easy to handle then external methods (initializing is more clear). All you need to create a folder with a good name for your project in the instance or common Products folder (depending on your zope.conf ) then copy ZOPE_ROOT/lib/python/Products/PythonScripts/module_access_example.py to YourFanceProduct/__init__.py then you edit this file to your likings based on both the documentation (yes I know this is a bit sparse) and the hints given in that file. Not you can define your own classes and functions in this file as well. For better structure you are encouraged to create actual module files in that directory instead of writing everything into __init__.py once your create more then just a toolset. Regards Tino
--On 5. März 2008 10:21:32 +0100 Yuri <yurj@alfa.it> wrote:
Hi, all!
I would like to define a class inside a python script (so I can catalog it and use catalog to store info, as the tutorial about catalog everything suggests).
This is basically bad-style. Define your class somewhere in your product code and if necessary make it available through related APIs of the AccessControl module. -aj
Andreas Jung wrote:
--On 5. März 2008 10:21:32 +0100 Yuri <yurj@alfa.it> wrote:
Hi, all!
I would like to define a class inside a python script (so I can catalog it and use catalog to store info, as the tutorial about catalog everything suggests).
This is basically bad-style. Define your class somewhere in your product code and if necessary make it available through related APIs of the AccessControl module.
The class has not to be made available, just a "bridge" to catalog it. For example, ZSQL method has a class file, so it is not a problem nor a "bad style" to define it somewhere and use it internally. what I need to do is my_catalog.catalog_object(an_object, 'path') So I can index values and search for them, without having objects. The idea is taken from here: http://zope.org/Members/rbickers/cataloganything
Andreas Jung wrote:
--On 5. März 2008 10:21:32 +0100 Yuri <yurj@alfa.it> wrote:
Hi, all!
I would like to define a class inside a python script (so I can catalog it and use catalog to store info, as the tutorial about catalog everything suggests).
This is basically bad-style. Define your class somewhere in your product code and if necessary make it available through related APIs of the AccessControl module.
-aj
I've found this: ==== If you have a KeywordIndex for the attribute 'foo', then you need to arrange that the reult objects in your recordset have sequences for the column / atrribute 'foo'. E.g., for a query like: select bar.id as 'bar, bar.name as 'name', foo.name as 'foo from bar, foo where foo.bar_id = bar.id you need to transform the result set: bars = {} for row in rows: bar_id= row['bar'] bar = bars.get(bar_id) if bar is None: bar = bars[bar_id[ = {'bar': bar_id, 'name': row['name'], 'foo': [], } bar['foo'].append(row['foo']) *Now* you can index the records you have transformed: for bar in bars.values(): catalog.indexObject(compute_uid(bar), make_record(bar)) === what are compute_uid(bar) and make_record(bar)? btw, indexes "calls" the object I pass to indexObject or catalog_object, so I need an object with methods or a dictionary is enough? I'm testing it, but it is not... ==== def ret_autore(self): "ritorna un autore" return 'prova' un_autore = {} un_autore['aut']='prova' #the index is called autore un_autore.autore = ret_autore context.catalogo_autori.catalog_object(un_autore, un_autore['aut']) ==== returns: *Error Type: TypeError* *Error Value: object has read-only attributes*
participants (6)
-
Andreas Jung -
Dieter Maurer -
Peter Sabaini -
Sascha Welter -
Tino Wildenhain -
Yuri