Skip to content

Commit

Permalink
wip jupyterlab
Browse files Browse the repository at this point in the history
  • Loading branch information
sts-odoo committed Dec 4, 2023
1 parent 22c4dc3 commit 25c9d32
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 7 deletions.
4 changes: 2 additions & 2 deletions server/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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.DEBUG, 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
65 changes: 60 additions & 5 deletions server/core/odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,53 @@
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 @@ -97,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 @@ -134,16 +181,12 @@ def initialize(ls:LanguageServer = None):

try:
Odoo.instance = Odoo()
log.debug('===========CONFIG ls %s', ls)
odooConfig = ls.lsp.send_request("Odoo/getConfiguration").result()
log.debug('===========odooCONFIG???? %s', odooConfig)
config = ls.get_configuration(WorkspaceConfigurationParams(items=[
ConfigurationItem(
scope_uri='window',
section="Odoo")
])).result()
log.debug('===========CONFIG???? %s', config)

Odoo.instance.refreshMode = config[0]["autoRefresh"]
Odoo.instance.autoSaveDelay = config[0]["autoRefreshDelay"]
ls.file_change_event_queue.set_delay(Odoo.instance.autoSaveDelay)
Expand All @@ -158,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 @@ -380,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('----skipped %s', dir)
continue
PythonArchBuilder(ls, addonsSymbol, os.path.join(path, dir)).load_arch(require_module=True)
if self.stop_init:
break
Expand Down
7 changes: 7 additions & 0 deletions server/features/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from lsprotocol.types import (CompletionItemKind, CompletionList, CompletionItemKind, CompletionItem,
CompletionItemLabelDetails, MarkupContent, MarkupKind)

import logging

log = logging.getLogger(__name__)

class AutoCompleteFeature:

@staticmethod
Expand Down Expand Up @@ -78,6 +82,9 @@ def build_model_completion_list(models, module):

@staticmethod
def autocomplete(path, content, line, char):
log.info('=========%s', Odoo)
log.info('=========%s', Odoo.get())
log.info('=========%s', Odoo.get().grammar)
parsoTree = Odoo.get().grammar.parse(content, error_recovery=True, cache = False)
element = parsoTree.get_leaf_for_position((line, char-1), include_prefixes=True)
#Test assignement
Expand Down
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']},
)

0 comments on commit 25c9d32

Please sign in to comment.