2727log = logging .getLogger (__name__ )
2828
2929
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+
3077
3178class Odoo ():
3279
@@ -97,7 +144,7 @@ def acquire_read(self, timeout=-1):
97144 return
98145 OdooLanguageServer .access_mode .set ("read" )
99146 yield Odoo .get () == self # to be sure Odoo.instance is still bound to the same instance
100-
147+
101148 self .thread_access_condition .release ()
102149 OdooLanguageServer .access_mode .set ("none" )
103150
@@ -134,16 +181,12 @@ def initialize(ls:LanguageServer = None):
134181
135182 try :
136183 Odoo .instance = Odoo ()
137- log .debug ('===========CONFIG ls %s' , ls )
138184 odooConfig = ls .lsp .send_request ("Odoo/getConfiguration" ).result ()
139- log .debug ('===========odooCONFIG???? %s' , odooConfig )
140185 config = ls .get_configuration (WorkspaceConfigurationParams (items = [
141186 ConfigurationItem (
142187 scope_uri = 'window' ,
143188 section = "Odoo" )
144189 ])).result ()
145- log .debug ('===========CONFIG???? %s' , config )
146-
147190 Odoo .instance .refreshMode = config [0 ]["autoRefresh" ]
148191 Odoo .instance .autoSaveDelay = config [0 ]["autoRefreshDelay" ]
149192 ls .file_change_event_queue .set_delay (Odoo .instance .autoSaveDelay )
@@ -158,6 +201,15 @@ def initialize(ls:LanguageServer = None):
158201 Odoo .instance .grammar = parso .load_grammar ()
159202 Odoo .instance .start_build_time = time .time ()
160203 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+
161213 if os .name == "nt" :
162214 Odoo .instance .odooPath = Odoo .instance .odooPath [0 ].capitalize () + Odoo .instance .odooPath [1 :]
163215 Odoo .instance .load_builtins (ls )
@@ -380,6 +432,9 @@ def build_modules(self, ls):
380432 dirs = os .listdir (path )
381433 for dir in dirs :
382434 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
383438 PythonArchBuilder (ls , addonsSymbol , os .path .join (path , dir )).load_arch (require_module = True )
384439 if self .stop_init :
385440 break
0 commit comments