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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. import pymongo
  3. from pymongo import MongoClient
  4. from lodel.settings.settings import Settings as settings
  5. from lodel import logger
  6. common_collections = {
  7. 'object': 'objects',
  8. 'relation': 'relation'
  9. }
  10. LODEL_SORT_OPERATORS_MAP = {
  11. 'ASC': pymongo.ASCENDING,
  12. 'DESC': pymongo.DESCENDING
  13. }
  14. MONGODB_SORT_OPERATORS_MAP = {
  15. 'ASC': 1,
  16. 'DESC': -1
  17. }
  18. MANDATORY_CONNECTION_ARGS = ('host', 'port', 'login', 'password', 'dbname')
  19. class MongoDbConnectionError(Exception):
  20. pass
  21. ##@brief Forge a mongodb uri connection string
  22. #@param host str : hostname
  23. #@param port int|str : port number
  24. #@param username str
  25. #@param password str
  26. #@param db_name str : the db to authenticate on (mongo as auth per db)
  27. #@param ro bool : if True open a read_only connection
  28. #@return a connection string
  29. #@see https://docs.mongodb.com/v2.4/reference/connection-string/#connection-string-options
  30. #@todo escape arguments
  31. def connection_string(host, port, username, password, db_name = None, ro = None):
  32. ret = 'mongodb://'
  33. if username != None:
  34. ret += username
  35. if password != None:
  36. ret += ':'+password
  37. ret+='@'
  38. elif password != None:
  39. raise RuntimeError("Password given but no username given...")
  40. host = 'localhost' if host is None else host
  41. ret += host
  42. if port is not None:
  43. ret += ':'+str(port)
  44. if db_name is not None:
  45. ret += '/'+db_name
  46. else:
  47. logger.warning("No database indicated. Huge chance for authentication \
  48. to fails")
  49. if ro:
  50. ret += '?readOnly='+str(bool(ro))
  51. return ret
  52. ##@brief Return an instanciated MongoClient from a connstring
  53. #@param connstring str : as returned by connection_string() method
  54. #@return A MongoClient instance
  55. def connect(connstring):
  56. return MongoClient(connstring)
  57. ## @brief Returns a collection name given a EmClass
  58. # @param class_object EmClass
  59. # @return str
  60. def object_collection_name(class_object):
  61. return class_object.__name__
  62. def collection_name(class_name):
  63. return class_name
  64. ## @brief Determine a collection field name given a lodel2 fieldname
  65. # @note For the moment this method only return the argument but EVERYWHERE
  66. # in the datasource we should use this method to gather proper fieldnames
  67. # @param fieldname str : A lodel2 fieldname
  68. # @return A string representing a well formated mongodb fieldname
  69. # @see mongo_filednames
  70. def mongo_fieldname(fieldname):
  71. return fieldname
  72. ## @brief Same as mongo_fieldname but for list of fields
  73. #
  74. # A small utility function
  75. # @param fieldnames iterable : contains str only
  76. # @return a list of converted fildnames (str)
  77. # @see mongo_fieldname
  78. def mongo_fieldnames(fieldnames):
  79. return [mongo_fieldname(fname) for fname in fieldnames]
  80. ## @brief Returns a list of orting options
  81. # @param query_filters_order list
  82. # @return list
  83. def parse_query_order(query_filters_order):
  84. return [(field, LODEL_SORT_OPERATORS_MAP[direction])
  85. for field, direction in query_filters_order]