Skip to content

Commit 5332897

Browse files
committed
Adding CLI Capabilities
✨ CLI tool now exists to manage common lookup items CLI tool can now correctly handle updates. Tests written as well. Working on #3
1 parent 1a68ed3 commit 5332897

File tree

3 files changed

+227
-70
lines changed

3 files changed

+227
-70
lines changed

process_tracker/cli.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,5 @@ def delete(topic, name):
7171
@click.option('-n', '--name', help='The new name for the topic.')
7272
def update(topic, initial_name, name):
7373

74-
topic = data_store.topic_validator(topic)
75-
76-
if topic:
77-
click.echo('Attempting to update %s with name %s to %s' % (topic, initial_name, name))
78-
item = data_store.get_item(topic=topic, name=initial_name)
79-
data_store.topic_updater(topic=topic, item=item, name=name)
80-
else:
81-
raise Exception('Invalid topic type.')
74+
click.echo('Attempting to update %s with name %s to %s' % (topic, initial_name, name))
75+
data_store.topic_updater(topic=topic, initial_name=initial_name, name=name)

process_tracker/data_store.py

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,6 @@ def __init__(self):
3737
self.data_store_port = data_store['data_store_port']
3838
self.data_store_name = data_store['data_store_name']
3939

40-
def get_item(self, topic, name):
41-
"""
42-
For the command line tool, find the given item and return it. Intentionally not as flexible as
43-
get_or_create_item.
44-
:param topic:
45-
:param name:
46-
:return:
47-
"""
48-
if topic == Actor:
49-
item = self.get_or_create_item(model=topic, create=False, actor_name=name)
50-
elif topic == ExtractStatus and name not in preload_extract_status_types:
51-
item = self.get_or_create_item(model=topic, create=False, extract_status_name=name)
52-
elif topic == ErrorType and name not in preload_error_types:
53-
item = self.get_or_create_item(model=topic, create=False, error_type_name=name)
54-
elif topic == ProcessType and name not in preload_process_types:
55-
item = self.get_or_create_item(model=topic, create=False, process_type_name=name)
56-
elif topic == ProcessStatus and name not in preload_process_status_types:
57-
item = self.get_or_create_item(model=topic, create=False, process_status_name=name)
58-
elif topic == Source:
59-
item = self.get_or_create_item(model=topic, create=False, source_name=name)
60-
elif topic == Tool:
61-
item = self.get_or_create_item(model=topic, create=False, tool_name=name)
62-
else:
63-
ClickException('The item is a protected record.').show()
64-
65-
return item
66-
6740
def get_or_create_item(self, model, create=True, **kwargs):
6841
"""
6942
Testing if an entity instance exists or not. If does, return entity key. If not, create entity instance
@@ -218,37 +191,50 @@ def topic_deleter(self, topic, name):
218191
if item_delete:
219192
self.session.commit()
220193

221-
@staticmethod
222-
def topic_updater(topic, item, name):
194+
def topic_updater(self, topic, initial_name, name):
223195
"""
224196
For the command line tool, validate that the topic name is not a default value and if not, update it.
225-
:param topic: The SQLAlchemy object type
226-
:type topic: SQLAlchemy object
227-
:param item: The SQLALchemy record to be updated.
228-
:type item: SQLAlchemy record
229-
:param name: The name of the item to be deleted.
230-
:type name: str
197+
:param topic: name of the SQLAlchemy object
198+
:type topic: string
199+
:param initial_name: The name of the object to be updated.
200+
:type initial_name: string
201+
:param name: The updated name of the object to be updated.
202+
:type name: string
231203
:return:
232204
"""
233-
if topic == Actor:
234-
item.actor_name = name
235-
elif topic == ExtractStatus and item.extract_status_name not in preload_extract_status_types:
236-
item.extract_status_name = name
237-
elif topic == ErrorType and item.error_type_name not in preload_error_types:
238-
item.error_type_name = name
239-
elif topic == ProcessType and item.process_type_name not in preload_process_types:
240-
item.process_type_name = name
241-
elif topic == ProcessStatus and item.process_status_name not in preload_process_status_types:
242-
item.process_status_name = name
243-
elif topic == Source:
244-
item.source_name = name
245-
elif topic == Tool:
246-
item.tool_name = name
247-
else:
248-
raise Exception('The item could not be updated because it is a protected record.')
205+
if self.topic_validator(topic=topic):
206+
if topic == 'actor':
207+
item = self.get_or_create_item(model=Actor, create=False, actor_name=initial_name)
208+
item.actor_name = name
209+
210+
elif topic == 'extract status' and initial_name not in preload_extract_status_types:
211+
item = self.get_or_create_item(model=ExtractStatus, create=False, extract_status_name=initial_name)
212+
item.extract_status_name = name
213+
214+
elif topic == 'error type' and initial_name not in preload_error_types:
215+
item = self.get_or_create_item(model=ErrorType, create=False, error_type_name=initial_name)
216+
item.error_type_name = name
217+
218+
elif topic == 'process type' and initial_name not in preload_process_types:
219+
item = self.get_or_create_item(model=ProcessType, create=False, process_type_name=initial_name)
220+
item.process_type_name = name
221+
222+
elif topic == 'process status' and initial_name not in preload_process_status_types:
223+
item = self.get_or_create_item(model=ProcessStatus, create=False, process_status_name=initial_name)
224+
item.process_status_name = name
225+
elif topic == 'source':
226+
item = self.get_or_create_item(model=Source, create=False, source_name=initial_name)
227+
item.source_name = name
228+
elif topic == 'tool':
229+
item = self.get_or_create_item(model=Tool, create=False, tool_name=initial_name)
230+
item.tool_name = name
231+
else:
232+
ClickException('The item could not be updated because it is a protected record.').show()
249233

250-
item_session = Session.object_session(item)
251-
item_session.commit()
234+
self.session.commit()
235+
236+
else:
237+
ClickException('Invalid topic. Unable to update instance.').show()
252238

253239
def topic_validator(self, topic):
254240
"""

0 commit comments

Comments
 (0)