Skip to content

Commit 854f6d7

Browse files
Merge pull request Pipelex#155 from Pipelex/dev
absolte path in make
2 parents a346188 + aba922f commit 854f6d7

2 files changed

Lines changed: 47 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
## [v0.6.0] - 2025-07-12
44

55
### Changed
6-
- The `Pipelex.make()` now takes a `relative_config_folder_path` argument, which is considered to be a relative path to the current working directory, pointing to the config folder path of pipelex. The argument `from_file` indicates if the path is relative to the caller's file path. If False, the path is considered to be absolute from where the process was started.
6+
- **Enhanced `Pipelex.make()` method**: Complete overhaul of the initialization method with new path configuration options and robust validation:
7+
- Added `relative_config_folder_path` and `absolute_config_folder_path` parameters for flexible config folder specification
8+
- The `from_file` parameter controls path resolution: if `True` (default), relative paths are resolved relative to the caller's file location; if `False`, relative to the current working directory (useful for CLI scenarios)
79

810
### Added
911
- Added github action for inference tests

pipelex/pipelex.py

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def setup(
188188

189189
log.debug(f"{PACKAGE_NAME} version {PACKAGE_VERSION} setup done for {get_config().project_name}")
190190

191-
def setup_libraries(self, relative_config_folder_path: str):
191+
def setup_libraries(self):
192192
try:
193193
self.template_provider.setup()
194194
self.llm_model_provider.setup()
@@ -242,22 +242,53 @@ def teardown(self):
242242

243243
# TODO: add kwargs to make() so that subclasses can employ specific parameters
244244
@classmethod
245-
def make(cls, relative_config_folder_path: str, from_file: Optional[bool] = True) -> Self:
246-
if from_file: # from_file means it's going to merge the relative_config_folder_path with the caller's file path
247-
current_frame = inspect.currentframe()
248-
if current_frame is None:
249-
raise RuntimeError("Failed to get current frame")
250-
if current_frame.f_back is None:
251-
raise RuntimeError("Failed to get caller frame")
252-
caller_file = current_frame.f_back.f_code.co_filename
253-
caller_dir = os.path.dirname(os.path.abspath(caller_file))
254-
config_folder_path = os.path.abspath(os.path.join(caller_dir, relative_config_folder_path))
245+
def make(
246+
cls, relative_config_folder_path: Optional[str] = None, absolute_config_folder_path: Optional[str] = None, from_file: Optional[bool] = True
247+
) -> Self:
248+
"""Create and initialize a Pipelex instance.
249+
250+
Args:
251+
relative_config_folder_path: Path to config folder relative to either the caller file or current working directory.
252+
Cannot be used together with absolute_config_folder_path.
253+
absolute_config_folder_path: Absolute path to config folder.
254+
Cannot be used together with relative_config_folder_path.
255+
from_file: Only used when relative_config_folder_path is provided.
256+
If True (default), the relative path is resolved relative to the file where make() was called.
257+
If False, the relative path is resolved relative to the current working directory (useful for CLI scenarios).
258+
259+
Returns:
260+
Initialized Pipelex instance.
261+
262+
Raises:
263+
ValueError: If both relative_config_folder_path and absolute_config_folder_path are provided.
264+
RuntimeError: If frame inspection fails when using relative paths with from_file=True.
265+
266+
Note:
267+
If neither path is provided, defaults to "./pipelex_libraries".
268+
"""
269+
if relative_config_folder_path is not None and absolute_config_folder_path is not None:
270+
raise ValueError("Cannot specify both relative_config_folder_path and absolute_config_folder_path")
271+
272+
if relative_config_folder_path is not None:
273+
if from_file:
274+
current_frame = inspect.currentframe()
275+
if current_frame is None:
276+
raise RuntimeError("Failed to get current frame")
277+
if current_frame.f_back is None:
278+
raise RuntimeError("Failed to get caller frame")
279+
caller_file = current_frame.f_back.f_code.co_filename
280+
caller_dir = os.path.dirname(os.path.abspath(caller_file))
281+
config_folder_path = os.path.abspath(os.path.join(caller_dir, relative_config_folder_path))
282+
else:
283+
config_folder_path = os.path.abspath(os.path.join(os.getcwd(), relative_config_folder_path))
284+
elif absolute_config_folder_path is not None:
285+
config_folder_path = absolute_config_folder_path
255286
else:
256-
config_folder_path = os.path.abspath(os.path.join(os.getcwd(), relative_config_folder_path))
287+
config_folder_path = "./pipelex_libraries"
257288

258289
pipelex_instance = cls(config_folder_path=config_folder_path)
259290
pipelex_instance.setup()
260-
pipelex_instance.setup_libraries(relative_config_folder_path=relative_config_folder_path)
291+
pipelex_instance.setup_libraries()
261292
log.info(f"Pipelex {PACKAGE_VERSION} initialized.")
262293
return pipelex_instance
263294

0 commit comments

Comments
 (0)