mirror of
https://github.com/emmett-framework/granian.git
synced 2025-08-04 17:08:02 +00:00
Add working directory option (#619)
This commit is contained in:
parent
977b6fa97f
commit
7949c46045
4 changed files with 14 additions and 3 deletions
|
@ -291,6 +291,8 @@ Options:
|
|||
should be invoked to build the actual target
|
||||
[env var: GRANIAN_FACTORY; default:
|
||||
(disabled)]
|
||||
--working-dir DIRECTORY Set the working directory [env var:
|
||||
GRANIAN_WORKING_DIR]
|
||||
--env-files FILE Environment file(s) to load (requires
|
||||
granian[dotenv] extra) [env var:
|
||||
GRANIAN_ENV_FILES]
|
||||
|
|
|
@ -52,8 +52,8 @@ def load_module(module_name: str, raise_on_failure: bool = True) -> Optional[Mod
|
|||
return sys.modules[module_name]
|
||||
|
||||
|
||||
def load_target(target: str, factory: bool = False) -> Callable[..., None]:
|
||||
sys.path.insert(0, '')
|
||||
def load_target(target: str, wd: Optional[str] = None, factory: bool = False) -> Callable[..., None]:
|
||||
sys.path.insert(0, wd or '')
|
||||
path, name = get_import_components(target)
|
||||
path = prepare_import(path) if path else None
|
||||
name = name or 'app'
|
||||
|
|
|
@ -244,6 +244,11 @@ def option(*param_decls: str, cls: Optional[Type[click.Option]] = None, **attrs:
|
|||
default=False,
|
||||
help='Treat target as a factory function, that should be invoked to build the actual target',
|
||||
)
|
||||
@option(
|
||||
'--working-dir',
|
||||
type=click.Path(exists=True, file_okay=False, dir_okay=True, readable=True, path_type=pathlib.Path),
|
||||
help='Set the working directory',
|
||||
)
|
||||
@option(
|
||||
'--env-files',
|
||||
type=click.Path(exists=True, file_okay=True, dir_okay=False, readable=True, path_type=pathlib.Path),
|
||||
|
@ -368,6 +373,7 @@ def cli(
|
|||
workers_lifetime: Optional[int],
|
||||
workers_kill_timeout: Optional[int],
|
||||
factory: bool,
|
||||
working_dir: Optional[pathlib.Path],
|
||||
env_files: Optional[List[pathlib.Path]],
|
||||
static_path_route: str,
|
||||
static_path_mount: Optional[pathlib.Path],
|
||||
|
@ -442,6 +448,7 @@ def cli(
|
|||
workers_lifetime=workers_lifetime,
|
||||
workers_kill_timeout=workers_kill_timeout,
|
||||
factory=factory,
|
||||
working_dir=working_dir,
|
||||
env_files=env_files,
|
||||
static_path_route=static_path_route,
|
||||
static_path_mount=static_path_mount,
|
||||
|
|
|
@ -110,6 +110,7 @@ class AbstractServer(Generic[WT]):
|
|||
workers_lifetime: Optional[int] = None,
|
||||
workers_kill_timeout: Optional[int] = None,
|
||||
factory: bool = False,
|
||||
working_dir: Optional[Path] = None,
|
||||
env_files: Optional[Sequence[Path]] = None,
|
||||
static_path_route: str = '/static',
|
||||
static_path_mount: Optional[Path] = None,
|
||||
|
@ -159,6 +160,7 @@ class AbstractServer(Generic[WT]):
|
|||
self.workers_lifetime = workers_lifetime
|
||||
self.workers_kill_timeout = workers_kill_timeout
|
||||
self.factory = factory
|
||||
self.working_dir = str(working_dir.resolve()) if working_dir else None
|
||||
self.env_files = env_files or ()
|
||||
self.static_path = (
|
||||
(static_path_route, str(static_path_mount.resolve()), str(static_path_expires))
|
||||
|
@ -504,7 +506,7 @@ class AbstractServer(Generic[WT]):
|
|||
if wrap_loader:
|
||||
target_loader = partial(target_loader, self.target)
|
||||
else:
|
||||
target_loader = partial(load_target, self.target, factory=self.factory)
|
||||
target_loader = partial(load_target, self.target, wd=self.working_dir, factory=self.factory)
|
||||
|
||||
if not spawn_target:
|
||||
spawn_target = default_spawners[self.interface]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue