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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #
  2. # This file is part of Lodel 2 (https://github.com/OpenEdition)
  3. #
  4. # Copyright (C) 2015-2017 Cléo UMS-3287
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import pymongo
  20. from pymongo import MongoClient
  21. from lodel.context import LodelContext
  22. LodelContext.expose_modules(globals(), {
  23. 'lodel.settings.settings': [('Settings', 'settings')],
  24. 'lodel.logger': 'logger'})
  25. common_collections = {
  26. 'object': 'objects',
  27. 'relation': 'relation'
  28. }
  29. LODEL_SORT_OPERATORS_MAP = {
  30. 'ASC': pymongo.ASCENDING,
  31. 'DESC': pymongo.DESCENDING
  32. }
  33. MONGODB_SORT_OPERATORS_MAP = {
  34. 'ASC': 1,
  35. 'DESC': -1
  36. }
  37. MANDATORY_CONNECTION_ARGS = ('host', 'port', 'login', 'password', 'dbname')
  38. class MongoDbConnectionError(Exception):
  39. pass
  40. ##@brief Forge a mongodb uri connection string
  41. #@param host str : hostname
  42. #@param port int|str : port number
  43. #@param username str
  44. #@param password str
  45. #@param db_name str : the db to authenticate on (mongo as auth per db)
  46. #@param ro bool : if True open a read_only connection
  47. #@return a connection string
  48. #@see https://docs.mongodb.com/v2.4/reference/connection-string/#connection-string-options
  49. #@todo escape arguments
  50. def connection_string(host, port, username, password, db_name = None, ro = None):
  51. ret = 'mongodb://'
  52. if username != None:
  53. ret += username
  54. if password != None:
  55. ret += ':'+password
  56. ret+='@'
  57. elif password != None:
  58. raise RuntimeError("Password given but no username given...")
  59. host = 'localhost' if host is None else host
  60. ret += host
  61. if port is not None:
  62. ret += ':'+str(port)
  63. if db_name is not None:
  64. ret += '/'+db_name
  65. else:
  66. logger.warning("No database indicated. Huge chance for authentication \
  67. to fails")
  68. if ro:
  69. ret += '?readOnly='+str(bool(ro))
  70. return ret
  71. ##@brief Return an instanciated MongoClient from a connstring
  72. #@param connstring str : as returned by connection_string() method
  73. #@return A MongoClient instance
  74. def connect(connstring):
  75. return MongoClient(connstring)
  76. ## @brief Returns a collection name given a EmClass
  77. # @param class_object EmClass
  78. # @return str
  79. def object_collection_name(class_object):
  80. return class_object.__name__
  81. def collection_name(class_name):
  82. return class_name
  83. ## @brief Determine a collection field name given a lodel2 fieldname
  84. # @note For the moment this method only return the argument but EVERYWHERE
  85. # in the datasource we should use this method to gather proper fieldnames
  86. # @param fieldname str : A lodel2 fieldname
  87. # @return A string representing a well formated mongodb fieldname
  88. # @see mongo_filednames
  89. def mongo_fieldname(fieldname):
  90. return fieldname
  91. ## @brief Same as mongo_fieldname but for list of fields
  92. #
  93. # A small utility function
  94. # @param fieldnames iterable : contains str only
  95. # @return a list of converted fildnames (str)
  96. # @see mongo_fieldname
  97. def mongo_fieldnames(fieldnames):
  98. return [mongo_fieldname(fname) for fname in fieldnames]
  99. ## @brief Returns a list of orting options
  100. # @param query_filters_order list
  101. # @return list
  102. def parse_query_order(query_filters_order):
  103. return [(field, LODEL_SORT_OPERATORS_MAP[direction])
  104. for field, direction in query_filters_order]