Skip to content

Commit dd4ec60

Browse files
committed
Some cleanup in contents; complete testing
1 parent 47e41af commit dd4ec60

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

paths_cli/commands/contents.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@
44
UNNAMED_SECTIONS = ['steps', 'movechanges', 'samplesets', 'trajectories',
55
'snapshots']
66

7+
NAME_TO_ATTR = {
8+
'CVs': 'cvs',
9+
'Volumes': 'volumes',
10+
'Engines': 'engines',
11+
'Networks': 'networks',
12+
'Move Schemes': 'schemes',
13+
'Simulations': 'pathsimulators',
14+
'Tags': 'tags',
15+
'Steps': 'steps',
16+
'Move Changes': 'movechanges',
17+
'SampleSets': 'samplesets',
18+
'Trajectories': 'trajectories',
19+
'Snapshots': 'snapshots'
20+
}
21+
722
@click.command(
823
'contents',
924
short_help="list named objects from an OPS .nc file",
@@ -26,13 +41,22 @@ def contents(input_file, table):
2641
try:
2742
store = getattr(storage, table_attr)
2843
except AttributeError:
29-
print("This needs to raise a good error; bad table name")
44+
raise click.UsageError("Unknown table: '" + table_attr + "'")
3045
else:
31-
if table_attr in UNNAMED_SECTIONS:
32-
print(get_unnamed_section_string(table_attr, store))
33-
else:
34-
print(get_section_string_nameable(table_attr, store,
35-
_get_named_namedobj))
46+
print(get_section_string(table_attr, store))
47+
48+
49+
def get_section_string(label, store):
50+
attr = NAME_TO_ATTR.get(label, label.lower())
51+
if attr in UNNAMED_SECTIONS:
52+
string = get_unnamed_section_string(label, store)
53+
elif attr in ['tag', 'tags']:
54+
string = get_section_string_nameable(label, store, _get_named_tags)
55+
else:
56+
string = get_section_string_nameable(label, store,
57+
_get_named_namedobj)
58+
return string
59+
3660

3761
def report_all_tables(storage):
3862
store_section_mapping = {

paths_cli/tests/commands/test_contents.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,11 @@ def test_contents_table(tps_fixture, table):
6767
}[table]
6868
assert results.output.split("\n") == expected
6969
assert results.exit_code == 0
70+
71+
def test_contents_table_error():
72+
runner = CliRunner()
73+
with runner.isolated_filesystem():
74+
storage = paths.Storage("temp.nc", mode='w')
75+
storage.close()
76+
results = runner.invoke(contents, ['temp.nc', '--table', 'foo'])
77+
assert results.exit_code != 0

0 commit comments

Comments
 (0)