Skip to content

Commit

Permalink
add list CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
quillcraftsman committed Mar 14, 2024
1 parent 52f835e commit 1a93126
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 4 deletions.
9 changes: 8 additions & 1 deletion replay_wizard/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Package console entrypoint
"""
from replay_wizard.cli import capture_cli, replay_cli, combine_cli
from replay_wizard.cli import capture_cli, replay_cli, combine_cli, sequence_list_cli


def capture():
Expand All @@ -23,3 +23,10 @@ def combine():
combine CLI
"""
combine_cli()


def sequence_list():
"""
sequence list CLI
"""
sequence_list_cli()
1 change: 1 addition & 0 deletions replay_wizard/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .replay import replay_cli
from .capture import capture_cli
from .combine import combine_cli
from .utils import sequence_list_cli
12 changes: 10 additions & 2 deletions replay_wizard/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,23 @@ def str2bool(v):
raise argparse.ArgumentTypeError('Boolean value expected.')


def get_parser(command_name):
def get_only_parser(command_name):
"""
Get argument parser
Get parser without params
"""
parser = argparse.ArgumentParser(
prog=command_name,
description=PROGRAM_DESCRIPTION,
epilog=f'Use {command_name} -h to get help'
)
return parser


def get_parser(command_name):
"""
Get argument parser
"""
parser = get_only_parser(command_name)

parser.add_argument('sequence')
return parser
Expand Down
42 changes: 42 additions & 0 deletions replay_wizard/cli/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Useful commands
"""
import os
from .parser import get_only_parser


def is_sequence_file(filename, extension, isfile):
"""
Is sequence file
:param filename: filename
:param extension: file extension
"""
return filename.endswith(f'.{extension}') and isfile(filename)


def show_sequences(all_files, extension, isfile, show=print):
"""
Show sequence
:param all_files: files to check
:param extension: sequence file extension
:param isfile: function to check not dir
:param show: show function, default print
"""
for file in [file for file in all_files if is_sequence_file(file, extension, isfile)]:
show(file)


def sequence_list_cli():
"""
Get sequence list
"""
parser = get_only_parser('wizard-list')
parser.add_argument('-e', '--extension', default='sequence', type=str)

args = parser.parse_args()

extension = args.extension

show_sequences(os.listdir(), extension, os.path.isfile, print)
2 changes: 1 addition & 1 deletion replay_wizard/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
Package info
"""
name = 'replay-wizard'
version = '2.1.0'
version = '2.2.0'
status = '4 - Beta'
8 changes: 8 additions & 0 deletions sequence_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
Sequence list command manual run
"""
from replay_wizard.__main__ import sequence_list


if __name__ == '__main__':
sequence_list()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,6 @@ def get_entrypoint(script_name, function_name):
get_entrypoint("wizard-capture", "capture"),
get_entrypoint("wizard-replay", "replay"),
get_entrypoint("wizard-combine", "combine"),
get_entrypoint("wizard-list", "sequence_list"),
]},
)
37 changes: 37 additions & 0 deletions testing/test_replay_wizard/test_cli/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,40 @@ def parse_args(self):
return MockArgs()

return MockArgumentParser


@fixture
def mock_list_argument_parser():
"""
Mock class for argparse.ArgumentParser
"""

class MockArgs:
"""
Mock ArgumentParser args
"""

def __init__(self):
self.extension = 's'

class MockArgumentParser:
"""
Mock for ArgumentParser
"""

def __init__(self, *args, **kwargs):
pass

def add_argument(self, *args, **kwargs):
"""
Mock add argument method
"""

def parse_args(self):
"""
Mock parse args method
"""
return MockArgs()


return MockArgumentParser
44 changes: 44 additions & 0 deletions testing/test_replay_wizard/test_cli/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Test utils CLI module
"""
from unittest import mock
from replay_wizard.cli.utils import is_sequence_file, show_sequences, sequence_list_cli


def test_is_sequence_file():
"""
Test is_sequence_file
"""
assert is_sequence_file('one.txt', 'txt', lambda x: True)
assert is_sequence_file('one.txt', 'txt', lambda x: False) is False
assert is_sequence_file('one.other', 'txt', lambda x: True) is False


def test_show_sequences():
"""
test show sequences
"""
def show(name):
show.count += 1
show.files.append(name)

show.count = 0
show.files = []

def is_file(name):
return '.' in name

all_files = ['one.s', 'two.s', 'dir', 'three.m']
show_sequences(all_files, 's', is_file, show)
assert show.count == 2
assert show.files == ['one.s', 'two.s']


def test_sequence_list_cli(mock_list_argument_parser):
"""
Test CLI
"""
with mock.patch('argparse.ArgumentParser', mock_list_argument_parser):
with mock.patch('os.listdir'):
with mock.patch('os.path.isfile'):
sequence_list_cli()

0 comments on commit 1a93126

Please sign in to comment.