[Engine-devel] ovirtSDK: ObjectsFactory would bring ability to customize objects

Greetings. I'm an automation tester in charge of testing SDK, my problem is that I cannot influence the objects that are created without so called "monkey patching" (google that and you will see what it means) the ovirtsdk.infrastructure.brokers.*. I made some comments about this #782891, but I was not so clear there, so I hope this will be better: In [12]: api.datacenters.list()[0] Out[12]: <ovirtsdk.infrastructure.brokers.DataCenter at 0x1a62890> You see that this returns an object that was declared somewhere in SDK. We have AFAIK no good way to say which object should be created. It would be good for us to be able to tell SDK: " My dear SDK, every time you are asked, please don't create and give me ovirtsdk.infrastructure.brokers.DataCenter, but something very similar: our.tests.infrastructure.brokers.DataCenter " This object would be a subclass SDK object. Than we would not have make another layer which would be converting between objects from ovirtsdk and objects we need, which will contain our own code (__eq__, __in__, str, ...). I hope this would be good for other SDK users as well. I think this could be achieved by having a overridable mapping: entitites_map = { 'DataCenters': ovirtsdk.api.DataCenters, # Use the default. 'DataCenters': our.tests.infrastructure.brokers.DataCenter, 'Clusters': our.tests.infrastructure.brokers.Clusters, 'Cluster': our.tests.infrastructure.brokers.Cluster } SDK would create the objects somehow like this: new_cluster = entities_map['Cluster'](param1="sdff", param2="dsfs", ...) Or even better if there was a Factory: class DefaultEntitiesFactory(object): def cluster(self, param1=None, param2=None, datacenter=...): cluster = ovirtsdk.infrastructure.brokers.Cluster(param1, param2, ...) ... return cluster def datacenter(self, param1=None, param2=None) dc = ovirtsdk.infrastructure.brokers.Datacenter(param1, param2, ...) ... return dc and there was, somewhere in the sdk: ovirtsdk..somemodule.ENTITIES_FACTORY = DefaultEntitiesFactory We could than switch the factory to something which fits better: ovirtsdk..somemodule.ENTITIES_FACTORY = MyBetterEntitiesFactory whereas the MyBetterEntitiesFactory would be a subclass: class MyBetterEntitiesFactory(DefaultEntitiesFactory): def cluster(self, param1=None, param2=None, datacenter=...): cluster = our.tests.infrastructure.brokers.Cluster(param1, param2, ...) do_some_fancy_cool_stuff(self, cluster) return cluster pass I hope I made myself clear this time. Jarda

On 02/14/2012 02:57 PM, Jaroslav Henner wrote:
Greetings.
I'm an automation tester in charge of testing SDK, my problem is that I cannot influence the objects that are created without so called "monkey patching" (google that and you will see what it means) the ovirtsdk.infrastructure.brokers.*. I made some comments about this #782891, but I was not so clear there, so I hope this will be better:
In [12]: api.datacenters.list()[0] Out[12]: <ovirtsdk.infrastructure.brokers.DataCenter at 0x1a62890>
You see that this returns an object that was declared somewhere in SDK. We have AFAIK no good way to say which object should be created.
It would be good for us to be able to tell SDK: " My dear SDK, every time you are asked, please don't create and give me ovirtsdk.infrastructure.brokers.DataCenter, but something very similar: our.tests.infrastructure.brokers.DataCenter "
I'm not sure i understand - you want the SDK to return an object from a class it does not know (i.e., the SDK to return an object from your class)? how will this look like if the SDK was in another language (say java)?

On Tue 14 Feb 2012 03:58:05 PM CET, Itamar Heim wrote:
On 02/14/2012 02:57 PM, Jaroslav Henner wrote:
Greetings.
I'm an automation tester in charge of testing SDK, my problem is that I cannot influence the objects that are created without so called "monkey patching" (google that and you will see what it means) the ovirtsdk.infrastructure.brokers.*. I made some comments about this #782891, but I was not so clear there, so I hope this will be better:
In [12]: api.datacenters.list()[0] Out[12]: <ovirtsdk.infrastructure.brokers.DataCenter at 0x1a62890>
You see that this returns an object that was declared somewhere in SDK. We have AFAIK no good way to say which object should be created.
It would be good for us to be able to tell SDK: " My dear SDK, every time you are asked, please don't create and give me ovirtsdk.infrastructure.brokers.DataCenter, but something very similar: our.tests.infrastructure.brokers.DataCenter "
I'm not sure i understand - you want the SDK to return an object from a class it does not know (i.e., the SDK to return an object from your class)? how will this look like if the SDK was in another language (say java)?
There would be some interface/abstract/concrete class DefaultFactory. This will define some common interface. There would be some setter inside SDK which would contain the instance of that DefaultFactory. SDK user would be able to set it to his own instance of (descendant) of DefaultFactory. From this point on, the objects created by SDK would be from the newly set factory -- customized. [http://en.wikipedia.org/wiki/Abstract_factory_pattern]

On 02/14/2012 06:01 PM, Jaroslav Henner wrote:
On Tue 14 Feb 2012 03:58:05 PM CET, Itamar Heim wrote:
On 02/14/2012 02:57 PM, Jaroslav Henner wrote:
Greetings.
I'm an automation tester in charge of testing SDK, my problem is that I cannot influence the objects that are created without so called "monkey patching" (google that and you will see what it means) the ovirtsdk.infrastructure.brokers.*. I made some comments about this #782891, but I was not so clear there, so I hope this will be better:
In [12]: api.datacenters.list()[0] Out[12]: <ovirtsdk.infrastructure.brokers.DataCenter at 0x1a62890>
You see that this returns an object that was declared somewhere in SDK. We have AFAIK no good way to say which object should be created.
It would be good for us to be able to tell SDK: " My dear SDK, every time you are asked, please don't create and give me ovirtsdk.infrastructure.brokers.DataCenter, but something very similar: our.tests.infrastructure.brokers.DataCenter "
I'm not sure i understand - you want the SDK to return an object from a class it does not know (i.e., the SDK to return an object from your class)? how will this look like if the SDK was in another language (say java)?
There would be some interface/abstract/concrete class DefaultFactory. This will define some common interface. There would be some setter inside SDK which would contain the instance of that DefaultFactory. SDK user would be able to set it to his own instance of (descendant) of DefaultFactory. From this point on, the objects created by SDK would be from the newly set factory -- customized. [http://en.wikipedia.org/wiki/Abstract_factory_pattern]
while i understand the benefit this will give you in extending the classes, It doesn't sounds to me like the classical sdk, which just gives you the API at OO class level?
participants (2)
-
Itamar Heim
-
Jaroslav Henner