__init__.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. """ OpenERP core library."""
  4. #----------------------------------------------------------
  5. # odoo must be a namespace package for odoo.addons to become one too
  6. # https://packaging.python.org/guides/packaging-namespace-packages/
  7. #----------------------------------------------------------
  8. import pkgutil
  9. import os.path
  10. __path__ = [
  11. os.path.abspath(path)
  12. for path in pkgutil.extend_path(__path__, __name__)
  13. ]
  14. import sys
  15. assert sys.version_info > (3, 7), "Outdated python version detected, Odoo requires Python >= 3.7 to run."
  16. #----------------------------------------------------------
  17. # Running mode flags (gevent, prefork)
  18. #----------------------------------------------------------
  19. # Is the server running with gevent.
  20. evented = False
  21. if len(sys.argv) > 1 and sys.argv[1] == 'gevent':
  22. sys.argv.remove('gevent')
  23. import gevent.monkey
  24. import psycopg2
  25. from gevent.socket import wait_read, wait_write
  26. gevent.monkey.patch_all()
  27. def gevent_wait_callback(conn, timeout=None):
  28. """A wait callback useful to allow gevent to work with Psycopg."""
  29. # Copyright (C) 2010-2012 Daniele Varrazzo <daniele.varrazzo@gmail.com>
  30. # This function is borrowed from psycogreen module which is licensed
  31. # under the BSD license (see in odoo/debian/copyright)
  32. while 1:
  33. state = conn.poll()
  34. if state == psycopg2.extensions.POLL_OK:
  35. break
  36. elif state == psycopg2.extensions.POLL_READ:
  37. wait_read(conn.fileno(), timeout=timeout)
  38. elif state == psycopg2.extensions.POLL_WRITE:
  39. wait_write(conn.fileno(), timeout=timeout)
  40. else:
  41. raise psycopg2.OperationalError(
  42. "Bad result from poll: %r" % state)
  43. psycopg2.extensions.set_wait_callback(gevent_wait_callback)
  44. evented = True
  45. # Is the server running in prefork mode (e.g. behind Gunicorn).
  46. # If this is True, the processes have to communicate some events,
  47. # e.g. database update or cache invalidation. Each process has also
  48. # its own copy of the data structure and we don't need to care about
  49. # locks between threads.
  50. multi_process = False
  51. #----------------------------------------------------------
  52. # libc UTC hack
  53. #----------------------------------------------------------
  54. # Make sure the OpenERP server runs in UTC.
  55. import os
  56. os.environ['TZ'] = 'UTC' # Set the timezone
  57. import time
  58. if hasattr(time, 'tzset'):
  59. time.tzset()
  60. #----------------------------------------------------------
  61. # PyPDF2 hack
  62. # ensure that zlib does not throw error -5 when decompressing
  63. # because some pdf won't fit into allocated memory
  64. # https://docs.python.org/3/library/zlib.html#zlib.decompressobj
  65. # ----------------------------------------------------------
  66. import PyPDF2
  67. try:
  68. import zlib
  69. def _decompress(data):
  70. zobj = zlib.decompressobj()
  71. return zobj.decompress(data)
  72. PyPDF2.filters.decompress = _decompress
  73. except ImportError:
  74. pass # no fix required
  75. #----------------------------------------------------------
  76. # Shortcuts
  77. #----------------------------------------------------------
  78. # The hard-coded super-user id (a.k.a. administrator, or root user).
  79. SUPERUSER_ID = 1
  80. def registry(database_name=None):
  81. """
  82. Return the model registry for the given database, or the database mentioned
  83. on the current thread. If the registry does not exist yet, it is created on
  84. the fly.
  85. """
  86. if database_name is None:
  87. import threading
  88. database_name = threading.current_thread().dbname
  89. return modules.registry.Registry(database_name)
  90. #----------------------------------------------------------
  91. # Imports
  92. #----------------------------------------------------------
  93. from . import upgrade # this namespace must be imported first
  94. from . import addons
  95. from . import conf
  96. from . import loglevels
  97. from . import modules
  98. from . import netsvc
  99. from . import osv
  100. from . import release
  101. from . import service
  102. from . import sql_db
  103. from . import tools
  104. #----------------------------------------------------------
  105. # Model classes, fields, api decorators, and translations
  106. #----------------------------------------------------------
  107. from . import models
  108. from . import fields
  109. from . import api
  110. from odoo.tools.translate import _, _lt
  111. from odoo.fields import Command
  112. #----------------------------------------------------------
  113. # Other imports, which may require stuff from above
  114. #----------------------------------------------------------
  115. from . import cli
  116. from . import http