Skip to content

Commit 9f5001b

Browse files
committed
simplify handling env vars and python path
1 parent 5e1f993 commit 9f5001b

File tree

7 files changed

+55
-141
lines changed

7 files changed

+55
-141
lines changed

robotcode/language_server/robotframework/diagnostics/imports_manager.py

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import ast
44
import asyncio
55
import os
6-
import sys
7-
import threading
86
import weakref
97
from abc import ABC, abstractmethod
108
from collections import OrderedDict
@@ -473,42 +471,16 @@ def __init__(self, parent_protocol: RobotLanguageServerProtocol, folder: Uri, co
473471
self._command_line_variables: Optional[List[VariableDefinition]] = None
474472
self._command_line_variables_lock = Lock()
475473

476-
self._python_path: Optional[List[str]] = None
477-
self._python_path_lock = threading.RLock()
478-
self._environment: Optional[Mapping[str, str]] = None
479-
self._environment_lock = threading.RLock()
474+
self._environment = dict(os.environ)
475+
self._environment.update(self.config.env)
480476

481477
self._library_files_cache = AsyncSimpleLRUCache()
482478
self._resource_files_cache = AsyncSimpleLRUCache()
483479
self._variables_files_cache = AsyncSimpleLRUCache()
484480

485481
@property
486482
def environment(self) -> Mapping[str, str]:
487-
with self._environment_lock:
488-
if self._environment is None:
489-
self._environment = dict(os.environ)
490-
491-
self._environment.update(self.config.env)
492-
493-
return self._environment
494-
495-
@property
496-
def python_path(self) -> List[str]:
497-
with self._python_path_lock:
498-
if self._python_path is None:
499-
self._python_path = sys.path
500-
501-
file = Path(__file__).resolve()
502-
top = file.parents[3]
503-
for p in filter(lambda v: path_is_relative_to(v, top), sys.path.copy()):
504-
self._python_path.remove(p)
505-
506-
for p in self.config.python_path:
507-
absolute_path = str(Path(p).absolute())
508-
if absolute_path not in self._python_path:
509-
self._python_path.insert(0, absolute_path)
510-
511-
return self._python_path
483+
return self._environment
512484

513485
@_logger.call
514486
async def get_command_line_variables(self) -> List[VariableDefinition]:
@@ -720,8 +692,6 @@ async def _find_library(self, name: str, base_dir: str, variables: Optional[Dict
720692
name,
721693
str(self.folder.to_path()),
722694
base_dir,
723-
self.config.python_path if self.config is not None else None,
724-
self.config.env if self.config is not None else None,
725695
self.config.variables if self.config is not None else None,
726696
variables,
727697
)
@@ -732,7 +702,7 @@ async def _find_library(self, name: str, base_dir: str, variables: Optional[Dict
732702
result = name
733703

734704
if is_library_by_path(result):
735-
result = find_file_ex(result, base_dir, self.python_path, "Library")
705+
result = find_file_ex(result, base_dir, "Library")
736706

737707
return result
738708

@@ -752,14 +722,12 @@ async def __find_resource(
752722
name,
753723
str(self.folder.to_path()),
754724
base_dir,
755-
self.config.python_path if self.config is not None else None,
756-
self.config.env if self.config is not None else None,
757725
self.config.variables if self.config is not None else None,
758726
variables,
759727
file_type,
760728
)
761729

762-
return str(find_file_ex(name, base_dir, self.python_path, file_type))
730+
return str(find_file_ex(name, base_dir, file_type))
763731

764732
async def find_variables(self, name: str, base_dir: str, variables: Optional[Dict[str, Any]] = None) -> str:
765733
return await self._variables_files_cache.get(self.__find_variables, name, base_dir, variables)
@@ -773,20 +741,18 @@ async def __find_variables(self, name: str, base_dir: str, variables: Optional[D
773741
name,
774742
str(self.folder.to_path()),
775743
base_dir,
776-
self.config.python_path if self.config is not None else None,
777-
self.config.env if self.config is not None else None,
778744
self.config.variables if self.config is not None else None,
779745
variables,
780746
)
781747

782748
if get_robot_version() >= (5, 0):
783749

784750
if is_variables_by_path(name):
785-
return str(find_file_ex(name, base_dir, self.python_path, "Library"))
751+
return str(find_file_ex(name, base_dir, "Library"))
786752

787753
return name
788754

789-
return str(find_file_ex(name, base_dir, self.python_path, "Library"))
755+
return str(find_file_ex(name, base_dir, "Library"))
790756

791757
@_logger.call
792758
async def get_libdoc_for_library_import(
@@ -815,8 +781,6 @@ async def _get_libdoc() -> LibraryDoc:
815781
args,
816782
str(self.folder.to_path()),
817783
base_dir,
818-
self.config.python_path if self.config is not None else None,
819-
self.config.env if self.config is not None else None,
820784
self.config.variables if self.config is not None else None,
821785
variables,
822786
),
@@ -998,8 +962,6 @@ async def _get_libdoc() -> VariablesDoc:
998962
args,
999963
str(self.folder.to_path()),
1000964
base_dir,
1001-
self.config.python_path if self.config is not None else None,
1002-
self.config.env if self.config is not None else None,
1003965
self.config.variables if self.config is not None else None,
1004966
variables,
1005967
),
@@ -1096,8 +1058,6 @@ async def complete_library_import(
10961058
name,
10971059
str(self.folder.to_path()),
10981060
base_dir,
1099-
self.config.python_path if self.config is not None else None,
1100-
self.config.env if self.config is not None else None,
11011061
self.config.variables if self.config is not None else None,
11021062
variables,
11031063
)
@@ -1109,8 +1069,6 @@ async def complete_resource_import(
11091069
name,
11101070
str(self.folder.to_path()),
11111071
base_dir,
1112-
self.config.python_path if self.config is not None else None,
1113-
self.config.env if self.config is not None else None,
11141072
self.config.variables if self.config is not None else None,
11151073
variables,
11161074
)
@@ -1123,22 +1081,15 @@ async def complete_variables_import(
11231081
name,
11241082
str(self.folder.to_path()),
11251083
base_dir,
1126-
self.config.python_path if self.config is not None else None,
1127-
self.config.env if self.config is not None else None,
11281084
self.config.variables if self.config is not None else None,
11291085
variables,
11301086
)
11311087

1132-
async def resolve_variable(
1133-
self, name: str, base_dir: str = ".", variables: Optional[Dict[str, Any]] = None, ignore_errors: bool = True
1134-
) -> Any:
1088+
def resolve_variable(self, name: str, base_dir: str = ".", variables: Optional[Dict[str, Any]] = None) -> Any:
11351089
return resolve_variable(
11361090
name,
11371091
str(self.folder.to_path()),
11381092
base_dir,
1139-
self.config.python_path if self.config is not None else None,
1140-
self.config.env if self.config is not None else None,
11411093
self.config.variables if self.config is not None else None,
11421094
variables,
1143-
ignore_errors,
11441095
)

0 commit comments

Comments
 (0)