mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 23:04:48 +08:00
Make sure serialization works for projects
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import errno
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from jedi._compatibility import FileNotFoundError, PermissionError, IsADirectoryError
|
from jedi._compatibility import FileNotFoundError, PermissionError, IsADirectoryError
|
||||||
@@ -32,9 +33,13 @@ def _force_unicode_list(lst):
|
|||||||
class Project(object):
|
class Project(object):
|
||||||
_environment = None
|
_environment = None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _get_config_folder_path(base_path):
|
||||||
|
return os.path.join(base_path, _CONFIG_FOLDER)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_json_path(base_path):
|
def _get_json_path(base_path):
|
||||||
return os.path.join(base_path, _CONFIG_FOLDER, 'project.json')
|
return os.path.join(Project._get_config_folder_path(base_path), 'project.json')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls, path):
|
def load(cls, path):
|
||||||
@@ -45,9 +50,7 @@ class Project(object):
|
|||||||
version, data = json.load(f)
|
version, data = json.load(f)
|
||||||
|
|
||||||
if version == 1:
|
if version == 1:
|
||||||
self = cls.__new__()
|
return cls(**data)
|
||||||
self.__dict__.update(data)
|
|
||||||
return self
|
|
||||||
else:
|
else:
|
||||||
raise WrongVersion(
|
raise WrongVersion(
|
||||||
"The Jedi version of this project seems newer than what we can handle."
|
"The Jedi version of this project seems newer than what we can handle."
|
||||||
@@ -70,7 +73,7 @@ class Project(object):
|
|||||||
being properly configured on the ``sys.path``.
|
being properly configured on the ``sys.path``.
|
||||||
"""
|
"""
|
||||||
def py2_comp(path, python_path=None, python_version=None, sys_path=None,
|
def py2_comp(path, python_path=None, python_version=None, sys_path=None,
|
||||||
added_sys_path=True, smart_sys_path=True):
|
added_sys_path=(), smart_sys_path=True):
|
||||||
if python_version is not None and python_path is not None:
|
if python_version is not None and python_path is not None:
|
||||||
raise ValueError('You cannot use both python_version and python_path')
|
raise ValueError('You cannot use both python_version and python_path')
|
||||||
self._path = os.path.abspath(path)
|
self._path = os.path.abspath(path)
|
||||||
@@ -80,7 +83,7 @@ class Project(object):
|
|||||||
self._sys_path = sys_path
|
self._sys_path = sys_path
|
||||||
self._smart_sys_path = smart_sys_path
|
self._smart_sys_path = smart_sys_path
|
||||||
self._django = False
|
self._django = False
|
||||||
self.added_sys_path = []
|
self.added_sys_path = list(added_sys_path)
|
||||||
"""The sys path that is going to be added at the end of the """
|
"""The sys path that is going to be added at the end of the """
|
||||||
|
|
||||||
py2_comp(path, **kwargs)
|
py2_comp(path, **kwargs)
|
||||||
@@ -145,9 +148,16 @@ class Project(object):
|
|||||||
def save(self):
|
def save(self):
|
||||||
data = dict(self.__dict__)
|
data = dict(self.__dict__)
|
||||||
data.pop('_environment', None)
|
data.pop('_environment', None)
|
||||||
|
data.pop('_django', None) # TODO make django setting public?
|
||||||
data = {k.lstrip('_'): v for k, v in data.items()}
|
data = {k.lstrip('_'): v for k, v in data.items()}
|
||||||
|
|
||||||
with open(self._get_json_path(self._path), 'wb') as f:
|
# TODO when dropping Python 2 use pathlib.Path.mkdir(parents=True, exist_ok=True)
|
||||||
|
try:
|
||||||
|
os.makedirs(self._get_config_folder_path(self._path))
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno != errno.EEXIST:
|
||||||
|
raise
|
||||||
|
with open(self._get_json_path(self._path), 'w') as f:
|
||||||
return json.dump((_SERIALIZER_VERSION, data), f)
|
return json.dump((_SERIALIZER_VERSION, data), f)
|
||||||
|
|
||||||
def get_environment(self):
|
def get_environment(self):
|
||||||
|
|||||||
@@ -30,3 +30,11 @@ def test_added_sys_path(inference_state):
|
|||||||
p = '/some_random_path'
|
p = '/some_random_path'
|
||||||
project.added_sys_path = [p]
|
project.added_sys_path = [p]
|
||||||
assert p in project._get_base_sys_path(inference_state)
|
assert p in project._get_base_sys_path(inference_state)
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_save_project(tmpdir):
|
||||||
|
project = Project(tmpdir.strpath, added_sys_path=['/foo'])
|
||||||
|
project.save()
|
||||||
|
|
||||||
|
loaded = Project.load(tmpdir.strpath)
|
||||||
|
assert loaded.added_sys_path == ['/foo']
|
||||||
|
|||||||
Reference in New Issue
Block a user