No Description
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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. LODEL_SORT_OPERATORS_MAP = {
  10. 'ASC': pymongo.ASCENDING,
  11. 'DESC': pymongo.DESCENDING
  12. }
  13. MONGODB_SORT_OPERATORS_MAP = {
  14. 'ASC': 1,
  15. 'DESC': -1
  16. }
  17. MANDATORY_CONNECTION_ARGS = ('host', 'port', 'login', 'password', 'dbname')
  18. class MongoDbConnectionError(Exception):
  19. pass
  20. def connection_string(host, port, username, password):
  21. ret = 'mongodb://'
  22. if username != None:
  23. ret += username
  24. if password != None:
  25. ret += ':'+password
  26. ret+='@'
  27. elif password != None:
  28. raise RuntimeError("Password given but no username given...")
  29. host = 'localhost' if host is None else host
  30. ret += host
  31. if port is not None:
  32. ret += ':'+str(port)
  33. return ret
  34. ##@brief Return an instanciated MongoClient
  35. #@param host str : hostname or ip
  36. #@param port int : port
  37. #@param username str | None: username
  38. #@param password str|None : password
  39. def connection(host, port, username, password):
  40. conn_str = connection_string(host, port, username, password)
  41. return MongoClient(conn_str)
  42. ##@brief Return a database cursor
  43. #@param host str : hostname or ip
  44. #@param port int : port
  45. #@param db_name str : database name
  46. #@param username str | None: username
  47. #@param password str|None : password
  48. def connect(host, port, db_name, username, password):
  49. conn = connection(host, port, username, password)
  50. database = conn[db_name]
  51. return database
  52. ## @brief Returns a collection name given a EmClass
  53. # @param class_object EmClass
  54. # @return str
  55. def object_collection_name(class_object):
  56. return class_object.__name__
  57. def collection_name(class_name):
  58. return class_name
  59. ## @brief Determine a collection field name given a lodel2 fieldname
  60. # @note For the moment this method only return the argument but EVERYWHERE
  61. # in the datasource we should use this method to gather proper fieldnames
  62. # @param fieldname str : A lodel2 fieldname
  63. # @return A string representing a well formated mongodb fieldname
  64. # @see mongo_filednames
  65. def mongo_fieldname(fieldname):
  66. return fieldname
  67. ## @brief Same as mongo_fieldname but for list of fields
  68. #
  69. # A small utility function
  70. # @param fieldnames iterable : contains str only
  71. # @return a list of converted fildnames (str)
  72. # @see mongo_fieldname
  73. def mongo_fieldnames(fieldnames):
  74. return [mongo_fieldname(fname) for fname in fieldnames]
  75. ## @brief Returns a list of orting options
  76. # @param query_filters_order list
  77. # @return list
  78. def parse_query_order(query_filters_order):
  79. return [(field, LODEL_SORT_OPERATORS_MAP[direction])
  80. for field, direction in query_filters_order]