Skip to content
This repository has been archived by the owner on Jul 6, 2024. It is now read-only.

Commit

Permalink
Allow files as arguments to pls
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb committed Apr 2, 2022
1 parent 8397125 commit c279e7f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
7 changes: 4 additions & 3 deletions src/pls/config/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,17 @@ def get_home_conf() -> Optional[Path]:
return test_path if _is_valid(test_path) else None


def find_configs(curr_dir: Path) -> list[Path]:
def find_configs(node: Path) -> list[Path]:
"""
Get the paths for all the relevant ``.pls.yml`` files.
:param curr_dir: the directory whose contents are being listed
:param node: the file or directory being listed
:return: a list of paths for all ``pls`` config files
"""

conf_paths = []
curr_dir = node if node.is_dir() else node.parent

conf_paths = []
if cwd_conf := get_cwd_conf(curr_dir):
conf_paths.append(cwd_conf)
if ancestor_confs := get_ancestor_confs(curr_dir):
Expand Down
16 changes: 12 additions & 4 deletions src/pls/fs/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,17 @@ def passes_filters(node: Node) -> bool:
return True


def parse_node(node_name: str) -> Optional[Node]:
def parse_node(parent_path: Path, node_name: str) -> Optional[Node]:
"""
Parse the node name into a ``Node`` instance. Most of the heavy lifting is
handled in the ``Node`` class definition itself.
:param parent_path: the path to the parent of the node
:param node_name: the name of a node inside the working directory
:return: a ``Node`` instance
"""

node_path: Path = args.args.directory.joinpath(node_name)
node_path: Path = parent_path.joinpath(node_name)

if node_path.is_dir():
if not args.args.dirs:
Expand All @@ -72,7 +73,13 @@ def read_input() -> tuple[dict[str, Node], list[Node]]:
:return: the list of directories and files inside the given directory
"""

all_nodes = os.listdir(args.args.directory)
arg_path: Path = args.args.node
if arg_path.is_dir():
parent_path = arg_path
all_nodes = os.listdir(arg_path)
else:
parent_path = arg_path.parent
all_nodes = [arg_path.name]

node_map = {}
node_list = []
Expand All @@ -86,7 +93,8 @@ def read_input() -> tuple[dict[str, Node], list[Node]]:
node_map = {
parsed_node.name: parsed_node
for node in all_nodes
if (parsed_node := parse_node(node)) and passes_filters(parsed_node)
if (parsed_node := parse_node(parent_path, node))
and passes_filters(parsed_node)
}
node_list = list(node_map.values())
node_list.sort(key=sort_key, reverse=args.args.sort.endswith("-"))
Expand Down
5 changes: 3 additions & 2 deletions src/pls/globals/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ def setup_user_groups(self):
group.gr_gid for group in getgrall() if username in group.gr_mem
)

def setup_git(self, directory: Path):
def setup_git(self, node: Path):
"""
Set up the Git root of the directory whose contents are being listed.
:param directory: the directory whose contents are being listed
:param node: the file or directory being listed
"""

directory = node if node.is_dir() else node.parent
self.git_root = get_git_root(directory)
if self.git_root is not None:
assert self.git_root is not None
Expand Down
6 changes: 3 additions & 3 deletions src/pls/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def init(argv=None):
cli_prefs = parser.parse_args(argv)
logger.debug(f"CLI arguments: {cli_prefs}")

args.args.directory = cli_prefs.directory
args.args.node = cli_prefs.node

state.state = state_obj = state.State()

state_obj.setup_home()
state_obj.setup_user_groups()
state_obj.setup_git(cli_prefs.directory)
state_obj.setup_git(cli_prefs.node)

conf_files = find_configs(cli_prefs.directory)
conf_files = find_configs(cli_prefs.node)
logger.debug(f"Config files read: {conf_files}")

logger.info("Reading config files")
Expand Down
16 changes: 6 additions & 10 deletions src/pls/parser/args/pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from pls.exceptions import ArgException


def _directory(path_str: str) -> Path:
def _node(path_str: str) -> Path:
"""
Parse the given path into a ``Path`` instance. The path is considered valid
if it points to an existing directory.
if it points to an existing file or directory.
:param path_str: the path supplied as a CLI argument
:return: the ``Path`` instance wrapping the supplied path
Expand All @@ -17,11 +17,7 @@ def _directory(path_str: str) -> Path:
path = Path(path_str).resolve()
if not path.exists():
raise ArgException(
f"Path [repr.path]{path_str}[/] does not exist.", arg_name="directory"
)
if not path.is_dir():
raise ArgException(
f"Path [repr.path]{path_str}[/] is not a directory.", arg_name="directory"
f"Path [repr.path]{path_str}[/] does not exist.", arg_name="node"
)
return path

Expand All @@ -34,9 +30,9 @@ def add_args(parser: argparse.ArgumentParser):
"""

parser.add_argument(
"directory",
type=_directory,
"node",
type=_node,
nargs=argparse.OPTIONAL,
default=Path.cwd(),
help="the directory whose contents are to be listed",
help="the node to list self or contents",
)
2 changes: 1 addition & 1 deletion tests/e2e/test_aux_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_help(arg: str):
expected_lines = [
"usage: pls [-h] [-v]",
"`pls` is a prettier and powerful `ls` for the pros.",
"directory",
"node",
"--help/-h",
"--version/-v",
]
Expand Down

0 comments on commit c279e7f

Please sign in to comment.