27
27
log = logging .getLogger (__name__ )
28
28
29
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 ()
76
+
30
77
31
78
class Odoo ():
32
79
@@ -97,7 +144,7 @@ def acquire_read(self, timeout=-1):
97
144
return
98
145
OdooLanguageServer .access_mode .set ("read" )
99
146
yield Odoo .get () == self # to be sure Odoo.instance is still bound to the same instance
100
-
147
+
101
148
self .thread_access_condition .release ()
102
149
OdooLanguageServer .access_mode .set ("none" )
103
150
@@ -134,16 +181,12 @@ def initialize(ls:LanguageServer = None):
134
181
135
182
try :
136
183
Odoo .instance = Odoo ()
137
- log .debug ('===========CONFIG ls %s' , ls )
138
184
odooConfig = ls .lsp .send_request ("Odoo/getConfiguration" ).result ()
139
- log .debug ('===========odooCONFIG???? %s' , odooConfig )
140
185
config = ls .get_configuration (WorkspaceConfigurationParams (items = [
141
186
ConfigurationItem (
142
187
scope_uri = 'window' ,
143
188
section = "Odoo" )
144
189
])).result ()
145
- log .debug ('===========CONFIG???? %s' , config )
146
-
147
190
Odoo .instance .refreshMode = config [0 ]["autoRefresh" ]
148
191
Odoo .instance .autoSaveDelay = config [0 ]["autoRefreshDelay" ]
149
192
ls .file_change_event_queue .set_delay (Odoo .instance .autoSaveDelay )
@@ -158,6 +201,15 @@ def initialize(ls:LanguageServer = None):
158
201
Odoo .instance .grammar = parso .load_grammar ()
159
202
Odoo .instance .start_build_time = time .time ()
160
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
+
161
213
if os .name == "nt" :
162
214
Odoo .instance .odooPath = Odoo .instance .odooPath [0 ].capitalize () + Odoo .instance .odooPath [1 :]
163
215
Odoo .instance .load_builtins (ls )
@@ -380,6 +432,9 @@ def build_modules(self, ls):
380
432
dirs = os .listdir (path )
381
433
for dir in dirs :
382
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
383
438
PythonArchBuilder (ls , addonsSymbol , os .path .join (path , dir )).load_arch (require_module = True )
384
439
if self .stop_init :
385
440
break
0 commit comments