설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

leobject.py 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. #-*- coding: utf-8 -*-
  2. import importlib
  3. import warnings
  4. import copy
  5. from lodel.context import LodelContext
  6. LodelContext.expose_modules(globals(), {
  7. 'lodel.logger': 'logger',
  8. 'lodel.settings': 'Settings',
  9. 'lodel.settings.utils': 'SettingsError',
  10. 'lodel.leapi.query': ['LeInsertQuery', 'LeUpdateQuery', 'LeDeleteQuery',
  11. 'LeGetQuery'],
  12. 'lodel.leapi.exceptions': ['LeApiError', 'LeApiErrors',
  13. 'LeApiDataCheckError', 'LeApiDataCheckErrors', 'LeApiQueryError',
  14. 'LeApiQueryErrors'],
  15. 'lodel.plugin.exceptions': ['PluginError', 'PluginTypeError',
  16. 'LodelScriptError', 'DatasourcePluginError'],
  17. 'lodel.plugin.hooks': ['LodelHook'],
  18. 'lodel.plugin': ['Plugin', 'DatasourcePlugin'],
  19. 'lodel.leapi.datahandlers.base_classes': ['DatasConstructor', 'Reference']})
  20. ##@brief Stores the name of the field present in each LeObject that indicates
  21. #the name of LeObject subclass represented by this object
  22. CLASS_ID_FIELDNAME = "classname"
  23. ##@brief Wrapper class for LeObject getter & setter
  24. #
  25. # This class intend to provide easy & friendly access to LeObject fields values
  26. # without name collision problems
  27. # @note Wrapped methods are : LeObject.data() & LeObject.set_data()
  28. class LeObjectValues(object):
  29. ##@brief Construct a new LeObjectValues
  30. # @param set_callback method : The LeObject.set_datas() method of corresponding LeObject class
  31. # @param get_callback method : The LeObject.get_datas() method of corresponding LeObject class
  32. def __init__(self, fieldnames_callback, set_callback, get_callback):
  33. self._setter = set_callback
  34. self._getter = get_callback
  35. ##@brief Provide read access to datas values
  36. # @note Read access should be provided for all fields
  37. # @param fname str : Field name
  38. def __getattribute__(self, fname):
  39. getter = super().__getattribute__('_getter')
  40. return getter(fname)
  41. ##@brief Provide write access to datas values
  42. # @note Write acces shouldn't be provided for internal or immutable fields
  43. # @param fname str : Field name
  44. # @param fval * : the field value
  45. def __setattribute__(self, fname, fval):
  46. setter = super().__getattribute__('_setter')
  47. return setter(fname, fval)
  48. class LeObject(object):
  49. ##@brief boolean that tells if an object is abtract or not
  50. _abstract = None
  51. ##@brief A dict that stores DataHandler instances indexed by field name
  52. _fields = None
  53. ##@brief A tuple of fieldname (or a uniq fieldname) representing uid
  54. _uid = None
  55. ##@brief Read only datasource ( see @ref lodel2_datasources )
  56. _ro_datasource = None
  57. ##@brief Read & write datasource ( see @ref lodel2_datasources )
  58. _rw_datasource = None
  59. ##@brief Store the list of child classes
  60. _child_classes = None
  61. ##@brief Name of the datasource plugin
  62. _datasource_name = None
  63. def __new__(cls, **kwargs):
  64. self = object.__new__(cls)
  65. ##@brief A dict that stores fieldvalues indexed by fieldname
  66. self.__datas = { fname:None for fname in self._fields }
  67. ##@brief Store a list of initianilized fields when instanciation not complete else store True
  68. self.__initialized = list()
  69. ##@brief Datas accessor. Instance of @ref LeObjectValues
  70. self.d = LeObjectValues(self.fieldnames, self.set_data, self.data)
  71. for fieldname, fieldval in kwargs.items():
  72. self.__datas[fieldname] = fieldval
  73. self.__initialized.append(fieldname)
  74. self.__is_initialized = False
  75. self.__set_initialized()
  76. return self
  77. ##@brief Construct an object representing an Editorial component
  78. # @note Can be considered as EmClass instance
  79. def __init__(self, **kwargs):
  80. if self._abstract:
  81. raise NotImplementedError("%s is abstract, you cannot instanciate it." % self.__class__.__name__ )
  82. # Checks that uid is given
  83. for uid_name in self._uid:
  84. if uid_name not in kwargs:
  85. raise LeApiError("Cannot instanciate a LeObject without it's identifier")
  86. self.__datas[uid_name] = kwargs[uid_name]
  87. del(kwargs[uid_name])
  88. self.__initialized.append(uid_name)
  89. # Processing given fields
  90. allowed_fieldnames = self.fieldnames(include_ro = False)
  91. err_list = dict()
  92. for fieldname, fieldval in kwargs.items():
  93. if fieldname not in allowed_fieldnames:
  94. if fieldname in self._fields:
  95. err_list[fieldname] = LeApiError(
  96. "Value given but the field is internal")
  97. else:
  98. err_list[fieldname] = LeApiError(
  99. "Unknown fieldname : '%s'" % fieldname)
  100. else:
  101. self.__datas[fieldname] = fieldval
  102. self.__initialized.append(fieldname)
  103. if len(err_list) > 0:
  104. raise LeApiErrors(msg = "Unable to __init__ %s" % self.__class__,
  105. exceptions = err_list)
  106. self.__set_initialized()
  107. #-----------------------------------#
  108. # Fields datas handling methods #
  109. #-----------------------------------#
  110. ##@brief Property method True if LeObject is initialized else False
  111. @property
  112. def initialized(self):
  113. return self.__is_initialized
  114. ##@return The uid field name
  115. @classmethod
  116. def uid_fieldname(cls):
  117. return cls._uid
  118. ##@brief Return a list of fieldnames
  119. # @param include_ro bool : if True include read only field names
  120. # @return a list of str
  121. @classmethod
  122. def fieldnames(cls, include_ro = False):
  123. if not include_ro:
  124. return [ fname for fname in cls._fields if not cls._fields[fname].is_internal() ]
  125. else:
  126. return list(cls._fields.keys())
  127. @classmethod
  128. def name2objname(cls, name):
  129. return name.title()
  130. ##@brief Return the datahandler asssociated with a LeObject field
  131. # @param fieldname str : The fieldname
  132. # @return A data handler instance
  133. #@todo update class of exception raised
  134. @classmethod
  135. def data_handler(cls, fieldname):
  136. if not fieldname in cls._fields:
  137. raise NameError("No field named '%s' in %s" % (fieldname, cls.__name__))
  138. return cls._fields[fieldname]
  139. ##@brief Getter for references datahandlers
  140. #@param with_backref bool : if true return only references with back_references
  141. #@return <code>{'fieldname': datahandler, ...}</code>
  142. @classmethod
  143. def reference_handlers(cls, with_backref = True):
  144. return { fname: fdh
  145. for fname, fdh in cls.fields(True).items()
  146. if issubclass(fdh.__class__, Reference) and \
  147. (not with_backref or fdh.back_reference is not None)}
  148. ##@brief Return a LeObject child class from a name
  149. # @warning This method has to be called from dynamically generated LeObjects
  150. # @param leobject_name str : LeObject name
  151. # @return A LeObject child class
  152. # @throw NameError if invalid name given
  153. @classmethod
  154. def name2class(cls, leobject_name):
  155. if cls.__module__ == 'lodel.leapi.leobject':
  156. raise NotImplementedError("Abstract method")
  157. mod = importlib.import_module(cls.__module__)
  158. try:
  159. return getattr(mod, leobject_name)
  160. except (AttributeError, TypeError) :
  161. raise LeApiError("No LeObject named '%s'" % leobject_name)
  162. @classmethod
  163. def is_abstract(cls):
  164. return cls._abstract
  165. ##@brief Field data handler getter
  166. #@param fieldname str : The field name
  167. #@return A datahandler instance
  168. #@throw NameError if the field doesn't exist
  169. @classmethod
  170. def field(cls, fieldname):
  171. try:
  172. return cls._fields[fieldname]
  173. except KeyError:
  174. raise NameError("No field named '%s' in %s" % ( fieldname,
  175. cls.__name__))
  176. ##@return A dict with fieldname as key and datahandler as instance
  177. @classmethod
  178. def fields(cls, include_ro = False):
  179. if include_ro:
  180. return copy.copy(cls._fields)
  181. else:
  182. return {fname:cls._fields[fname] for fname in cls._fields if not cls._fields[fname].is_internal()}
  183. ##@brief Return the list of parents classes
  184. #
  185. #@note the first item of the list is the current class, the second is it's
  186. #parent etc...
  187. #@param cls
  188. #@warning multiple inheritance broken by this method
  189. #@return a list of LeObject child classes
  190. #@todo multiple parent capabilities implementation
  191. @classmethod
  192. def hierarch(cls):
  193. res = [cls]
  194. cur = cls
  195. while True:
  196. cur = cur.__bases__[0] # Multiple inheritance broken HERE
  197. if cur in (LeObject, object):
  198. break
  199. else:
  200. res.append(cur)
  201. return res
  202. ##@brief Return a tuple a child classes
  203. #@return a tuple of child classes
  204. @classmethod
  205. def child_classes(cls):
  206. return copy.copy(cls._child_classes)
  207. ##@brief Return the parent class that is the "source" of uid
  208. #
  209. #The method goal is to return the parent class that defines UID.
  210. #@return a LeObject child class or false if no UID defined
  211. @classmethod
  212. def uid_source(cls):
  213. if cls._uid is None or len(cls._uid) == 0:
  214. return False
  215. hierarch = cls.hierarch()
  216. prev = hierarch[0]
  217. uid_handlers = set( cls._fields[name] for name in cls._uid )
  218. for pcls in cls.hierarch()[1:]:
  219. puid_handlers = set(cls._fields[name] for name in pcls._uid)
  220. if set(pcls._uid) != set(prev._uid) \
  221. or puid_handlers != uid_handlers:
  222. break
  223. prev = pcls
  224. return prev
  225. ##@brief Initialise both datasources (ro and rw)
  226. #
  227. #This method is used once at dyncode load to replace the datasource string
  228. #by a datasource instance to avoid doing this operation for each query
  229. #@see LeObject::_init_datasource()
  230. @classmethod
  231. def _init_datasources(cls):
  232. if isinstance(cls._datasource_name, str):
  233. rw_ds = ro_ds = cls._datasource_name
  234. else:
  235. ro_ds, rw_ds = cls._datasource_name
  236. #Read only datasource initialisation
  237. cls._ro_datasource = DatasourcePlugin.init_datasource(ro_ds, True)
  238. if cls._ro_datasource is None:
  239. log_msg = "No read only datasource set for LeObject %s"
  240. log_msg %= cls.__name__
  241. logger.debug(log_msg)
  242. else:
  243. log_msg = "Read only datasource '%s' initialized for LeObject %s"
  244. log_msg %= (ro_ds, cls.__name__)
  245. logger.debug(log_msg)
  246. #Read write datasource initialisation
  247. cls._rw_datasource = DatasourcePlugin.init_datasource(rw_ds, False)
  248. if cls._ro_datasource is None:
  249. log_msg = "No read/write datasource set for LeObject %s"
  250. log_msg %= cls.__name__
  251. logger.debug(log_msg)
  252. else:
  253. log_msg = "Read/write datasource '%s' initialized for LeObject %s"
  254. log_msg %= (ro_ds, cls.__name__)
  255. logger.debug(log_msg)
  256. ##@brief Return the uid of the current LeObject instance
  257. #@return the uid value
  258. #@warning Broke multiple uid capabilities
  259. def uid(self):
  260. return self.data(self._uid[0])
  261. ##@brief Read only access to all datas
  262. # @note for fancy data accessor use @ref LeObject.g attribute @ref LeObjectValues instance
  263. # @param name str : field name
  264. # @return the Value
  265. # @throw RuntimeError if the field is not initialized yet
  266. # @throw NameError if name is not an existing field name
  267. def data(self, field_name):
  268. if field_name not in self._fields.keys():
  269. raise NameError("No such field in %s : %s" % (self.__class__.__name__, field_name))
  270. if not self.initialized and field_name not in self.__initialized:
  271. raise RuntimeError("The field %s is not initialized yet (and have no value)" % field_name)
  272. return self.__datas[field_name]
  273. ##@brief Read only access to all datas
  274. #@return a dict representing datas of current instance
  275. def datas(self, internal = False):
  276. return {fname:self.data(fname) for fname in self.fieldnames(internal)}
  277. ##@brief Datas setter
  278. # @note for fancy data accessor use @ref LeObject.g attribute @ref LeObjectValues instance
  279. # @param fname str : field name
  280. # @param fval * : field value
  281. # @return the value that is really set
  282. # @throw NameError if fname is not valid
  283. # @throw AttributeError if the field is not writtable
  284. def set_data(self, fname, fval):
  285. if fname not in self.fieldnames(include_ro = False):
  286. if fname not in self._fields.keys():
  287. raise NameError("No such field in %s : %s" % (self.__class__.__name__, fname))
  288. else:
  289. raise AttributeError("The field %s is read only" % fname)
  290. self.__datas[fname] = fval
  291. if not self.initialized and fname not in self.__initialized:
  292. # Add field to initialized fields list
  293. self.__initialized.append(fname)
  294. self.__set_initialized()
  295. if self.initialized:
  296. # Running full value check
  297. ret = self.__check_modified_values()
  298. if ret is None:
  299. return self.__datas[fname]
  300. else:
  301. raise LeApiErrors("Data check error", ret)
  302. else:
  303. # Doing value check on modified field
  304. # We skip full validation here because the LeObject is not fully initialized yet
  305. val, err = self._fields[fname].check_data_value(fval)
  306. if isinstance(err, Exception):
  307. #Revert change to be in valid state
  308. del(self.__datas[fname])
  309. del(self.__initialized[-1])
  310. raise LeApiErrors("Data check error", {fname:err})
  311. else:
  312. self.__datas[fname] = val
  313. ##@brief Update the __initialized attribute according to LeObject internal state
  314. #
  315. # Check the list of initialized fields and set __initialized to True if all fields initialized
  316. def __set_initialized(self):
  317. if isinstance(self.__initialized, list):
  318. expected_fields = self.fieldnames(include_ro = False) + self._uid
  319. if set(expected_fields) == set(self.__initialized):
  320. self.__is_initialized = True
  321. ##@brief Designed to be called when datas are modified
  322. #
  323. # Make different checks on the LeObject given it's state (fully initialized or not)
  324. # @return None if checks succeded else return an exception list
  325. def __check_modified_values(self):
  326. err_list = dict()
  327. if self.__initialized is True:
  328. # Data value check
  329. for fname in self.fieldnames(include_ro = False):
  330. val, err = self._fields[fname].check_data_value(self.__datas[fname])
  331. if err is not None:
  332. err_list[fname] = err
  333. else:
  334. self.__datas[fname] = val
  335. # Data construction
  336. if len(err_list) == 0:
  337. for fname in self.fieldnames(include_ro = True):
  338. try:
  339. field = self._fields[fname]
  340. self.__datas[fname] = field.construct_data( self,
  341. fname,
  342. self.__datas,
  343. self.__datas[fname]
  344. )
  345. except Exception as exp:
  346. err_list[fname] = exp
  347. # Datas consistency check
  348. if len(err_list) == 0:
  349. for fname in self.fieldnames(include_ro = True):
  350. field = self._fields[fname]
  351. ret = field.check_data_consistency(self, fname, self.__datas)
  352. if isinstance(ret, Exception):
  353. err_list[fname] = ret
  354. else:
  355. # Data value check for initialized datas
  356. for fname in self.__initialized:
  357. val, err = self._fields[fname].check_data_value(self.__datas[fname])
  358. if err is not None:
  359. err_list[fname] = err
  360. else:
  361. self.__datas[fname] = val
  362. return err_list if len(err_list) > 0 else None
  363. #--------------------#
  364. # Other methods #
  365. #--------------------#
  366. ##@brief Temporary method to set private fields attribute at dynamic code generation
  367. #
  368. # This method is used in the generated dynamic code to set the _fields attribute
  369. # at the end of the dyncode parse
  370. # @warning This method is deleted once the dynamic code loaded
  371. # @param field_list list : list of EmField instance
  372. # @param cls
  373. @classmethod
  374. def _set__fields(cls, field_list):
  375. cls._fields = field_list
  376. ## @brief Check that datas are valid for this type
  377. # @param datas dict : key == field name value are field values
  378. # @param complete bool : if True expect that datas provide values for all non internal fields
  379. # @param allow_internal bool : if True don't raise an error if a field is internal
  380. # @param cls
  381. # @return Checked datas
  382. # @throw LeApiDataCheckError if errors reported during check
  383. @classmethod
  384. def check_datas_value(cls, datas, complete = False, allow_internal = True):
  385. err_l = dict() #Error storing
  386. correct = set() #valid fields name
  387. mandatory = set() #mandatory fields name
  388. for fname, datahandler in cls._fields.items():
  389. if allow_internal or not datahandler.is_internal():
  390. correct.add(fname)
  391. if complete and not hasattr(datahandler, 'default'):
  392. mandatory.add(fname)
  393. provided = set(datas.keys())
  394. # searching for unknow fields
  395. for u_f in provided - correct:
  396. #Here we can check if the field is invalid or rejected because
  397. # it is internel
  398. err_l[u_f] = AttributeError("Unknown or unauthorized field '%s'" % u_f)
  399. # searching for missing mandatory fieldsa
  400. for missing in mandatory - provided:
  401. err_l[missing] = AttributeError("The data for field '%s' is missing" % missing)
  402. #Checks datas
  403. checked_datas = dict()
  404. for name, value in [ (name, value) for name, value in datas.items() if name in correct ]:
  405. dh = cls._fields[name]
  406. res = dh.check_data_value(value)
  407. checked_datas[name], err = res
  408. if err:
  409. err_l[name] = err
  410. if len(err_l) > 0:
  411. raise LeApiDataCheckErrors("Error while checking datas", err_l)
  412. return checked_datas
  413. ##@brief Check and prepare datas
  414. #
  415. # @warning when complete = False we are not able to make construct_datas() and _check_data_consistency()
  416. #
  417. # @param datas dict : {fieldname : fieldvalue, ...}
  418. # @param complete bool : If True you MUST give all the datas
  419. # @param allow_internal : Wether or not interal fields are expected in datas
  420. # @param cls
  421. # @return Datas ready for use
  422. # @todo: complete is very unsafe, find a way to get rid of it
  423. @classmethod
  424. def prepare_datas(cls, datas, complete=False, allow_internal=True):
  425. if not complete:
  426. warnings.warn("\nActual implementation can make broken datas \
  427. construction and consitency when datas are not complete\n")
  428. ret_datas = cls.check_datas_value(datas, complete, allow_internal)
  429. if isinstance(ret_datas, Exception):
  430. raise ret_datas
  431. if complete:
  432. ret_datas = cls._construct_datas(ret_datas)
  433. cls._check_datas_consistency(ret_datas)
  434. return ret_datas
  435. ## @brief Construct datas values
  436. #
  437. # @param cls
  438. # @param datas dict : Datas that have been returned by LeCrud.check_datas_value() methods
  439. # @return A new dict of datas
  440. # @todo IMPLEMENTATION
  441. @classmethod
  442. def _construct_datas(cls, datas):
  443. constructor = DatasConstructor(cls, datas, cls._fields)
  444. ret = {
  445. fname:constructor[fname]
  446. for fname, ftype in cls._fields.items()
  447. if not ftype.is_internal() or ftype.internal != 'autosql'
  448. }
  449. return ret
  450. ## @brief Check datas consistency
  451. # @warning assert that datas is complete
  452. # @param cls
  453. # @param datas dict : Datas that have been returned by LeCrud._construct_datas() method
  454. # @throw LeApiDataCheckError if fails
  455. @classmethod
  456. def _check_datas_consistency(cls, datas):
  457. err_l = []
  458. err_l = dict()
  459. for fname, dh in cls._fields.items():
  460. ret = dh.check_data_consistency(cls, fname, datas)
  461. if isinstance(ret, Exception):
  462. err_l[fname] = ret
  463. if len(err_l) > 0:
  464. raise LeApiDataCheckError("Datas consistency checks fails", err_l)
  465. ## @brief Check datas consistency
  466. # @warning assert that datas is complete
  467. # @param cls
  468. # @param datas dict : Datas that have been returned by LeCrud.prepare_datas() method
  469. @classmethod
  470. def make_consistency(cls, datas, type_query = 'insert'):
  471. for fname, dh in cls._fields.items():
  472. ret = dh.make_consistency(fname, datas, type_query)
  473. ## @brief Add a new instance of LeObject
  474. # @return a new uid en case of success, False otherwise
  475. @classmethod
  476. def insert(cls, datas):
  477. query = LeInsertQuery(cls)
  478. return query.execute(datas)
  479. ## @brief Update an instance of LeObject
  480. #
  481. #@param datas : list of new datas
  482. def update(self, datas = None):
  483. datas = self.datas(internal=False) if datas is None else datas
  484. uids = self._uid
  485. query_filter = list()
  486. for uid in uids:
  487. query_filter.append((uid, '=', self.data(uid)))
  488. try:
  489. query = LeUpdateQuery(self.__class__, query_filter)
  490. except Exception as err:
  491. raise err
  492. try:
  493. result = query.execute(datas)
  494. except Exception as err:
  495. raise err
  496. return result
  497. ## @brief Delete an instance of LeObject
  498. #
  499. #@return 1 if the objet has been deleted
  500. def delete(self):
  501. uids = self._uid
  502. query_filter = list()
  503. for uid in uids:
  504. query_filter.append((uid, '=', self.data(uid)))
  505. query = LeDeleteQuery(self.__class__, query_filter)
  506. result = query.execute()
  507. return result
  508. ## @brief Delete instances of LeObject
  509. #@param uids a list: lists of (fieldname, fieldvalue), with fieldname in cls._uids
  510. #@returns the
  511. @classmethod
  512. def delete_bundle(cls, query_filters):
  513. deleted = 0
  514. try:
  515. query = LeDeleteQuery(cls, query_filters)
  516. except Exception as err:
  517. raise err
  518. try:
  519. result = query.execute()
  520. except Exception as err:
  521. raise err
  522. if not result is None:
  523. deleted += result
  524. return deleted
  525. ## @brief Get instances of LeObject
  526. #
  527. #@param target_class LeObject : class of object the query is about
  528. #@param query_filters dict : (filters, relational filters), with filters is a list of tuples : (FIELD, OPERATOR, VALUE) )
  529. #@param field_list list|None : list of string representing fields see
  530. #@ref leobject_filters
  531. #@param order list : A list of field names or tuple (FIELDNAME,[ASC | DESC])
  532. #@param group list : A list of field names or tuple (FIELDNAME,[ASC | DESC])
  533. #@param limit int : The maximum number of returned results
  534. #@param offset int : offset
  535. #@param Inst
  536. #@return a list of items (lists of (fieldname, fieldvalue))
  537. @classmethod
  538. def get(cls, query_filters, field_list=None, order=None, group=None, limit=None, offset=0):
  539. if field_list is not None:
  540. for uid in [ uidname
  541. for uidname in cls.uid_fieldname()
  542. if uidname not in field_list ]:
  543. field_list.append(uid)
  544. if CLASS_ID_FIELDNAME not in field_list:
  545. field_list.append(CLASS_ID_FIELDNAME)
  546. try:
  547. query = LeGetQuery(
  548. cls, query_filters = query_filters, field_list = field_list,
  549. order = order, group = group, limit = limit, offset = offset)
  550. except ValueError as err:
  551. raise err
  552. try:
  553. result = query.execute()
  554. except Exception as err:
  555. raise err
  556. objects = list()
  557. for res in result:
  558. res_cls = cls.name2class(res[CLASS_ID_FIELDNAME])
  559. inst = res_cls.__new__(res_cls,**res)
  560. objects.append(inst)
  561. return objects
  562. ##@brief Retrieve an object given an UID
  563. #@todo broken multiple UID
  564. @classmethod
  565. def get_from_uid(cls, uid):
  566. uidname = cls.uid_fieldname()[0] #Brokes composed UID
  567. res = cls.get([(uidname,'=', uid)])
  568. if len(res) > 1:
  569. raise LodelFatalError("Get from uid returned more than one \
  570. object ! For class %s with uid value = %s" % (cls, uid))
  571. elif len(res) == 0:
  572. return None
  573. return res[0]