Skip to content

Commit 3dc74f6

Browse files
committed
[WIP] add jupyterlab
1 parent d5735a2 commit 3dc74f6

File tree

5 files changed

+89
-7
lines changed

5 files changed

+89
-7
lines changed

server/__main__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import argparse
22
import logging
33

4-
from .controller import odoo_server
5-
from .constants import *
4+
from server.controller import odoo_server
5+
from server.constants import *
66

7-
FORMAT = '(%(process)d) [%(levelname)s] %(message)s'
7+
FORMAT = '%(asctime)s %(levelname)s: %(message)s'
88

99
def add_arguments(parser):
1010
parser.description = "simple odoo server example"
@@ -39,7 +39,7 @@ def main():
3939
parser = argparse.ArgumentParser()
4040
add_arguments(parser)
4141
args = parser.parse_args()
42-
logging.basicConfig(format=FORMAT, filename=args.log, level=logging.WARNING, filemode="w")
42+
logging.basicConfig(format=FORMAT, datefmt='%Y-%m-%d %I:%M:%S', filename=args.log, level=logging.DEBUG, filemode="w")
4343

4444
if "alpha" in EXTENSION_VERSION:
4545
logging.getLogger().setLevel(logging.DEBUG)

server/controller.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def init(ls, params):
4747
),
4848
]
4949
))
50+
# TODO: launch_thread here only in the case of jupyterlab
51+
odoo_server.launch_thread(target=Odoo.initialize, args=(ls,))
52+
5053

5154
@odoo_server.feature(SHUTDOWN)
5255
@send_error_on_traceback
@@ -237,4 +240,4 @@ def on_change_config(config):
237240
ConfigurationItem(
238241
scope_uri='window',
239242
section="Odoo")
240-
]), callback=on_change_config)
243+
]), callback=on_change_config)

server/core/odoo.py

+64-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,57 @@
2222
import time
2323

2424
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()
2576

2677

2778
class Odoo():
@@ -93,7 +144,7 @@ def acquire_read(self, timeout=-1):
93144
return
94145
OdooLanguageServer.access_mode.set("read")
95146
yield Odoo.get() == self # to be sure Odoo.instance is still bound to the same instance
96-
147+
97148
self.thread_access_condition.release()
98149
OdooLanguageServer.access_mode.set("none")
99150

@@ -150,6 +201,15 @@ def initialize(ls:LanguageServer = None):
150201
Odoo.instance.grammar = parso.load_grammar()
151202
Odoo.instance.start_build_time = time.time()
152203
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+
153213
if os.name == "nt":
154214
Odoo.instance.odooPath = Odoo.instance.odooPath[0].capitalize() + Odoo.instance.odooPath[1:]
155215
Odoo.instance.load_builtins(ls)
@@ -372,6 +432,9 @@ def build_modules(self, ls):
372432
dirs = os.listdir(path)
373433
for dir in dirs:
374434
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('Module %s not installed, skipped', dir)
437+
continue
375438
PythonArchBuilder(ls, addonsSymbol, os.path.join(path, dir)).load_arch(require_module=True)
376439
if self.stop_init:
377440
break

server/odoo_language_server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ def set(instance):
5050
OdooLanguageServer.instance.set(instance)
5151

5252
odoo_server = OdooLanguageServer()
53-
OdooLanguageServer.set(odoo_server)
53+
OdooLanguageServer.set(odoo_server)

setup.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import setuptools
2+
3+
4+
setuptools.setup(
5+
version='0.2.2',
6+
name='odoo-language-server',
7+
long_description_content_type='text/markdown',
8+
packages=setuptools.find_packages(),
9+
include_package_data=True,
10+
install_requires=[
11+
'lsprotocol',
12+
'pygls',
13+
'psycopg2',
14+
],
15+
entry_points={'console_scripts': ['odoo-ls = server.__main__:main']},
16+
)

0 commit comments

Comments
 (0)