diff --git a/src/qbpm/__init__.py b/src/qbpm/__init__.py index a1833fb..58d2f00 100644 --- a/src/qbpm/__init__.py +++ b/src/qbpm/__init__.py @@ -20,14 +20,11 @@ class Profile: self.profile_dir = profile_dir self.root = self.profile_dir / name - def check(self) -> Optional["Profile"]: - if "/" in self.name: - error("profile name cannot contain slashes") - return None - return self - - def exists(self) -> bool: - return self.root.exists() and self.root.is_dir() + def check_name(self) -> bool: + if "/" in self.name or self.name in [".", ".."]: + error("profile name cannot be a path") + return False + return True def cmdline(self) -> list[str]: return [ diff --git a/src/qbpm/main.py b/src/qbpm/main.py index 263a0bb..6d090ba 100644 --- a/src/qbpm/main.py +++ b/src/qbpm/main.py @@ -190,6 +190,8 @@ def launch_profile( All QB_ARGS are passed on to qutebrowser.""" profile = Profile(profile_name, **vars(context)) + if not profiles.check(profile): + sys.exit(1) exit_with(launch_qutebrowser(profile, foreground, qb_args)) @@ -223,8 +225,7 @@ def choose( def edit(context: Context, profile_name: str) -> None: """Edit a profile's config.py.""" profile = Profile(profile_name, **vars(context)) - if not profile.exists(): - error(f"profile {profile.name} not found at {profile.root}") + if not profiles.check(profile): sys.exit(1) click.edit(filename=str(profile.root / "config" / "config.py")) diff --git a/src/qbpm/operations.py b/src/qbpm/operations.py index 0b9d671..fd4a3c8 100644 --- a/src/qbpm/operations.py +++ b/src/qbpm/operations.py @@ -24,9 +24,7 @@ def from_session( def desktop(profile: Profile) -> bool: - exists = profile.exists() + exists = profiles.check(profile) if exists: create_desktop_file(profile) - else: - error(f"profile {profile.name} not found at {profile.root}") return exists diff --git a/src/qbpm/profiles.py b/src/qbpm/profiles.py index ae048fe..3e19e15 100644 --- a/src/qbpm/profiles.py +++ b/src/qbpm/profiles.py @@ -23,7 +23,7 @@ MIME_TYPES = [ def create_profile(profile: Profile, overwrite: bool = False) -> bool: - if not profile.check(): + if not profile.check_name(): return False if not overwrite and profile.root.exists(): @@ -53,13 +53,19 @@ def create_config( out(f"config.source(r'{qb_config_dir / 'config.py'}')") -def exists(profile: Profile) -> bool: - if profile.root.exists() and not profile.root.is_dir(): - error(f"{profile.root} is not a directory") +def check(profile: Profile) -> bool: + if not profile.check_name(): return False - if not profile.root.exists(): + exists = profile.root.exists() + if not exists: error(f"{profile.root} does not exist") return False + if not profile.root.is_dir(): + error(f"{profile.root} is not a directory") + return False + if not (profile.root / "config").is_dir(): + error(f"no config directory in {profile.root}, is it a profile?") + return False return True