Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip jupyterlab #89

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions server/__main__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import argparse
import logging

from .controller import odoo_server
from .constants import *
from server.controller import odoo_server
from server.constants import *

FORMAT = '(%(process)d) [%(levelname)s] %(message)s'
FORMAT = '%(asctime)s %(levelname)s: %(message)s'

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

if "alpha" in EXTENSION_VERSION:
logging.getLogger().setLevel(logging.DEBUG)
Expand Down
5 changes: 4 additions & 1 deletion server/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def init(ls, params):
),
]
))
# TODO: launch_thread here only in the case of jupyterlab
odoo_server.launch_thread(target=Odoo.initialize, args=(ls,))


@odoo_server.feature(SHUTDOWN)
@send_error_on_traceback
Expand Down Expand Up @@ -237,4 +240,4 @@ def on_change_config(config):
ConfigurationItem(
scope_uri='window',
section="Odoo")
]), callback=on_change_config)
]), callback=on_change_config)
65 changes: 64 additions & 1 deletion server/core/odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,57 @@
import time

import tracemalloc
import logging

log = logging.getLogger(__name__)


class Postgres():
def __init__(self, db='postgres', autocommit=True, user=None, password=None,
host=None, port=None, as_dict=True, app=None, timeout=None):
self.conn = None
self.cr = None

import psycopg2
import psycopg2.extras

connstring = "dbname=%s" % db
if host or port or user:
connstring += " host=%s" % (host or 'localhost')
if user:
connstring += " user=%s" % user
if port:
connstring += " port=%s" % port
if password:
connstring += " password=%s" % password
try:
self.conn = psycopg2.connect(connstring, application_name=app)
except TypeError:
# We still have to deal with old psycopg2 versions (eg: saas master)
self.conn = psycopg2.connect(connstring)
self.conn.autocommit = autocommit
if as_dict:
self.cr = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
else:
# kept as it's slightly better for performance
self.cr = self.conn.cursor()
if timeout:
self.cr.execute("SET statement_timeout TO %s", (timeout, ))

def __enter__(self):
return self.cr

def __exit__(self, exc_type, exc_value, exc_traceback):
self.close()

def __del__(self):
self.close()

def close(self):
if self.cr:
self.cr.close()
if self.conn:
self.conn.close()


class Odoo():
Expand Down Expand Up @@ -93,7 +144,7 @@ def acquire_read(self, timeout=-1):
return
OdooLanguageServer.access_mode.set("read")
yield Odoo.get() == self # to be sure Odoo.instance is still bound to the same instance

self.thread_access_condition.release()
OdooLanguageServer.access_mode.set("none")

Expand Down Expand Up @@ -150,6 +201,15 @@ def initialize(ls:LanguageServer = None):
Odoo.instance.grammar = parso.load_grammar()
Odoo.instance.start_build_time = time.time()
Odoo.instance.odooPath = odooConfig.odooPath
if hasattr(odooConfig, 'database') and os.environ.get('PGDATABASE'):
Odoo.instance.database = os.environ.get('PGDATABASE')
else:
Odoo.instance.database = False
if Odoo.instance.database:
with Postgres(Odoo.instance.database) as cr:
cr.execute("SELECT name FROM ir_module_module WHERE state = 'installed';")
Odoo.instance.installed_modules = [mod[0] for mod in cr.fetchall()]

if os.name == "nt":
Odoo.instance.odooPath = Odoo.instance.odooPath[0].capitalize() + Odoo.instance.odooPath[1:]
Odoo.instance.load_builtins(ls)
Expand Down Expand Up @@ -372,6 +432,9 @@ def build_modules(self, ls):
dirs = os.listdir(path)
for dir in dirs:
if os.path.isdir(os.path.join(path, dir)):
if Odoo.instance.database and dir not in Odoo.instance.installed_modules:
log.info('Module %s not installed, skipped', dir)
continue
PythonArchBuilder(ls, addonsSymbol, os.path.join(path, dir)).load_arch(require_module=True)
if self.stop_init:
break
Expand Down
2 changes: 1 addition & 1 deletion server/odoo_language_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ def set(instance):
OdooLanguageServer.instance.set(instance)

odoo_server = OdooLanguageServer()
OdooLanguageServer.set(odoo_server)
OdooLanguageServer.set(odoo_server)
16 changes: 16 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import setuptools


setuptools.setup(
version='0.2.2',
name='odoo-language-server',
long_description_content_type='text/markdown',
packages=setuptools.find_packages(),
include_package_data=True,
install_requires=[
'lsprotocol',
'pygls',
'psycopg2',
],
entry_points={'console_scripts': ['odoo-ls = server.__main__:main']},
)