|
22 | 22 | import time
|
23 | 23 |
|
24 | 24 | import tracemalloc
|
| 25 | +import logging |
| 26 | + |
| 27 | +log = logging.getLogger(__name__) |
| 28 | + |
| 29 | + |
| 30 | +class Postgres(): |
| 31 | + def __init__(self, db='postgres', autocommit=True, user=None, password=None, |
| 32 | + host=None, port=None, as_dict=True, app=None, timeout=None): |
| 33 | + self.conn = None |
| 34 | + self.cr = None |
| 35 | + |
| 36 | + import psycopg2 |
| 37 | + import psycopg2.extras |
| 38 | + |
| 39 | + connstring = "dbname=%s" % db |
| 40 | + if host or port or user: |
| 41 | + connstring += " host=%s" % (host or 'localhost') |
| 42 | + if user: |
| 43 | + connstring += " user=%s" % user |
| 44 | + if port: |
| 45 | + connstring += " port=%s" % port |
| 46 | + if password: |
| 47 | + connstring += " password=%s" % password |
| 48 | + try: |
| 49 | + self.conn = psycopg2.connect(connstring, application_name=app) |
| 50 | + except TypeError: |
| 51 | + # We still have to deal with old psycopg2 versions (eg: saas master) |
| 52 | + self.conn = psycopg2.connect(connstring) |
| 53 | + self.conn.autocommit = autocommit |
| 54 | + if as_dict: |
| 55 | + self.cr = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor) |
| 56 | + else: |
| 57 | + # kept as it's slightly better for performance |
| 58 | + self.cr = self.conn.cursor() |
| 59 | + if timeout: |
| 60 | + self.cr.execute("SET statement_timeout TO %s", (timeout, )) |
| 61 | + |
| 62 | + def __enter__(self): |
| 63 | + return self.cr |
| 64 | + |
| 65 | + def __exit__(self, exc_type, exc_value, exc_traceback): |
| 66 | + self.close() |
| 67 | + |
| 68 | + def __del__(self): |
| 69 | + self.close() |
| 70 | + |
| 71 | + def close(self): |
| 72 | + if self.cr: |
| 73 | + self.cr.close() |
| 74 | + if self.conn: |
| 75 | + self.conn.close() |
25 | 76 |
|
26 | 77 |
|
27 | 78 | class Odoo():
|
@@ -93,7 +144,7 @@ def acquire_read(self, timeout=-1):
|
93 | 144 | return
|
94 | 145 | OdooLanguageServer.access_mode.set("read")
|
95 | 146 | yield Odoo.get() == self # to be sure Odoo.instance is still bound to the same instance
|
96 |
| - |
| 147 | + |
97 | 148 | self.thread_access_condition.release()
|
98 | 149 | OdooLanguageServer.access_mode.set("none")
|
99 | 150 |
|
@@ -150,6 +201,15 @@ def initialize(ls:LanguageServer = None):
|
150 | 201 | Odoo.instance.grammar = parso.load_grammar()
|
151 | 202 | Odoo.instance.start_build_time = time.time()
|
152 | 203 | Odoo.instance.odooPath = odooConfig.odooPath
|
| 204 | + if hasattr(odooConfig, 'database') and os.environ.get('PGDATABASE'): |
| 205 | + Odoo.instance.database = os.environ.get('PGDATABASE') |
| 206 | + else: |
| 207 | + Odoo.instance.database = False |
| 208 | + if Odoo.instance.database: |
| 209 | + with Postgres(Odoo.instance.database) as cr: |
| 210 | + cr.execute("SELECT name FROM ir_module_module WHERE state = 'installed';") |
| 211 | + Odoo.instance.installed_modules = [mod[0] for mod in cr.fetchall()] |
| 212 | + |
153 | 213 | if os.name == "nt":
|
154 | 214 | Odoo.instance.odooPath = Odoo.instance.odooPath[0].capitalize() + Odoo.instance.odooPath[1:]
|
155 | 215 | Odoo.instance.load_builtins(ls)
|
@@ -372,6 +432,9 @@ def build_modules(self, ls):
|
372 | 432 | dirs = os.listdir(path)
|
373 | 433 | for dir in dirs:
|
374 | 434 | if os.path.isdir(os.path.join(path, dir)):
|
| 435 | + if Odoo.instance.database and dir not in Odoo.instance.installed_modules: |
| 436 | + log.info('----skipped %s', dir) |
| 437 | + continue |
375 | 438 | PythonArchBuilder(ls, addonsSymbol, os.path.join(path, dir)).load_arch(require_module=True)
|
376 | 439 | if self.stop_init:
|
377 | 440 | break
|
|
0 commit comments