Nessuna descrizione
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.

utils.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. import pymongo
  3. from pymongo import MongoClient
  4. from lodel.settings.settings import Settings as settings
  5. common_collections = {
  6. 'object': 'objects',
  7. 'relation': 'relation'
  8. }
  9. collection_prefix = {
  10. 'relation': 'rel_',
  11. 'object': 'class_'
  12. }
  13. LODEL_SORT_OPERATORS_MAP = {
  14. 'ASC': pymongo.ASCENDING,
  15. 'DESC': pymongo.DESCENDING
  16. }
  17. MONGODB_SORT_OPERATORS_MAP = {
  18. 'ASC': 1,
  19. 'DESC': -1
  20. }
  21. MANDATORY_CONNECTION_ARGS = ('host', 'port', 'login', 'password', 'dbname')
  22. class MongoDbConnectionError(Exception):
  23. pass
  24. ## @brief gets the settings given a connection name
  25. # @param connection_name str
  26. # @return dict
  27. # @todo Use the settings module to store the connections parameters
  28. def get_connection_args(connnection_name='default'):
  29. return {'host': 'localhost', 'port': 28015, 'login': 'lodel_admin', 'password': 'lapwd', 'dbname': 'lodel'}
  30. def connection_string(host, port, username, password):
  31. ret = 'mongodb://'
  32. if username != None:
  33. ret += username
  34. if password != None:
  35. ret += ':'+password
  36. ret+='@'
  37. elif password != None:
  38. raise RuntimeError("Password given but no username given...")
  39. host = 'localhost' if host is None else host
  40. ret += host
  41. if port != None:
  42. ret += ':'+str(port)
  43. return ret
  44. ##@brief Return an instanciated MongoClient
  45. #@param host str : hostname or ip
  46. #@param port int : port
  47. #@param username str | None: username
  48. #@param password str|None : password
  49. def connection(host, port, username, password):
  50. conn_str = connection_string(host, port, username, password)
  51. return MongoClient(conn_str)
  52. ##@brief Return a database cursor
  53. #@param host str : hostname or ip
  54. #@param port int : port
  55. #@param db_name str : database name
  56. #@param username str | None: username
  57. #@param password str|None : password
  58. def connect(host, port, db_name, username, password):
  59. conn = connection(host, port, username, password)
  60. database = conn[db_name]
  61. return database
  62. ## @brief Returns a collection name given a EmClass
  63. # @param class_object EmClass
  64. # @return str
  65. def object_collection_name(class_object):
  66. if not class_object.pure_abstract:
  67. class_parent = class_object.parents[0].uid
  68. collection_name = ("%s%s" % (collection_prefix['object'], class_parent)).lower()
  69. else:
  70. collection_name = ("%s%s" % (collection_prefix['object'], class_object.name)).lower()
  71. return collection_name
  72. ## @brief Determine a collection field name given a lodel2 fieldname
  73. # @note For the moment this method only return the argument but EVERYWHERE
  74. # in the datasource we should use this method to gather proper fieldnames
  75. # @param fieldname str : A lodel2 fieldname
  76. # @return A string representing a well formated mongodb fieldname
  77. # @see mongo_filednames
  78. def mongo_fieldname(fieldname):
  79. return fieldname
  80. ## @brief Same as mongo_fieldname but for list of fields
  81. #
  82. # A small utility function
  83. # @param fieldnames iterable : contains str only
  84. # @return a list of converted fildnames (str)
  85. # @see mongo_fieldname
  86. def mongo_fieldnames(fieldnames):
  87. return [mongo_fieldname(fname) for fname in fieldnames]
  88. ## @brief Returns a list of orting options
  89. # @param query_filters_order list
  90. # @return list
  91. def parse_query_order(query_filters_order):
  92. return [(field, LODEL_SORT_OPERATORS_MAP[direction])
  93. for field, direction in query_filters_order]