Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import pdb
- # import re
- from tune import Tune
- from tune_list import TuneList
- class Command(object):
- def __init__(self, argv):
- self.tune = Tune()
- self.tunes = TuneList()
- self.tunes.load()
- # prefixes
- self.separator = ':'
- self.pre = '-'
- # separate option and data argumets
- self.option = argv[1]
- self.data = self._format_to_dict(argv[2:])
- # possible options to pass as aruments
- self.add_key = (self.pre + 'a', self.pre + 'add')
- self.show_key = (self.pre + 's', self.pre + 'show')
- self.change_key = (self.pre + 'c', self.pre + 'change')
- self.info_key = (self.pre + 'i', self.pre + 'info')
- self.delete_key =\
- (self.pre + 'd', self.pre + 'del', self.pre + 'delete')
- # possible information about tune to pass
- self.artist_key = ('A', 'Artist')
- self.title_key = ('T', 'Title')
- self.genres_key = ('g', 'genre')
- self.sub_genres_key = ('s', 'sub_genre', 'sub-genre', 'sub')
- self.tags = ('t', 'tags')
- def _format_to_dict(self, args):
- '''Converts passed arguments into dictionary to easy'''
- data = dict()
- for element in args:
- # colon is main element of an argument so we need check
- # if argument contains it
- try:
- separator_index = element.index(self.separator)
- except ValueError:
- raise ValueError('Invalid syntax')
- # separate key and value, then update dictionary
- key = element[:separator_index]
- value = element[separator_index + 1:]
- data.update({key: value})
- return data
- def update(self):
- '''Main function in this class. First it recognizes,
- action should perform add, change or delete.
- Then call appropirate functions.'''
- # Add behaviour
- if self.option in self.add_key:
- self._add()
- elif self.option in self.show_key:
- self._show()
- elif self.option in self.change_key:
- self._change()
- elif self.option in self.info_key:
- self._info()
- elif self.option in self.delete_key:
- self._delete()
- def _add(self):
- # you should consider using property here instead
- # this function
- self._claim_data()
- self.tunes.add(self.tune)
- self.tunes.save()
- self.tunes.show()
- def _show(self):
- print('running _show function')
- self.tunes.show()
- def _change(self):
- if not self.tunes.dct:
- self.tunes.show()
- self.tunes.modify()
- self.tunes.save()
- def _info(self):
- if not self.tunes.dct:
- self.tunes.show()
- self.tunes.info()
- def _delete(self):
- print('running _delete function')
- if not self.tunes.dct:
- self._show()
- self.tunes.delete()
- self.tunes.save()
- def _claim_data(self):
- '''Updates single tune based on the users input'''
- for k, v in self.data.items():
- if k in self.artist_key:
- self.tune['artist'] = v
- elif k in self.title_key:
- self.tune['title'] = v
- elif k in self.genres_key:
- self.tune['genres'] = v.split()
- elif k in self.sub_genres_key:
- self.tune['sub_genres'] = v.split()
- elif k in self.tags:
- self.tune['tags'] = v.split()
- else:
- raise KeyError(f'No such attribute: "{k}"')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement