def test_input_select_objects(self): full_items = ["1", "2", "3", "4", "5"] # Test no self.io.addinput("n") items = ui.input_select_objects("Prompt", full_items, self._print_helper) self.assertEqual(items, []) # Test yes self.io.addinput("y") items = ui.input_select_objects("Prompt", full_items, self._print_helper) self.assertEqual(items, full_items) # Test selective 1 self.io.addinput("s") self.io.addinput("n") self.io.addinput("y") self.io.addinput("n") self.io.addinput("y") self.io.addinput("n") items = ui.input_select_objects("Prompt", full_items, self._print_helper) self.assertEqual(items, ["2", "4"]) # Test selective 2 self.io.addinput("s") self.io.addinput("y") self.io.addinput("y") self.io.addinput("n") self.io.addinput("y") self.io.addinput("n") items = ui.input_select_objects("Prompt", full_items, lambda s: self._print_helper2(s, "Prefix")) self.assertEqual(items, ["1", "2", "4"])
def test_input_select_objects(self): full_items = ['1', '2', '3', '4', '5'] # Test no self.io.addinput('n') items = ui.input_select_objects( "Prompt", full_items, self._print_helper) self.assertEqual(items, []) # Test yes self.io.addinput('y') items = ui.input_select_objects( "Prompt", full_items, self._print_helper) self.assertEqual(items, full_items) # Test selective 1 self.io.addinput('s') self.io.addinput('n') self.io.addinput('y') self.io.addinput('n') self.io.addinput('y') self.io.addinput('n') items = ui.input_select_objects( "Prompt", full_items, self._print_helper) self.assertEqual(items, ['2', '4']) # Test selective 2 self.io.addinput('s') self.io.addinput('y') self.io.addinput('y') self.io.addinput('n') self.io.addinput('y') self.io.addinput('n') items = ui.input_select_objects( "Prompt", full_items, lambda s: self._print_helper2(s, "Prefix")) self.assertEqual(items, ['1', '2', '4']) # Test selective 3 self.io.addinput('s') self.io.addinput('y') self.io.addinput('n') self.io.addinput('y') self.io.addinput('q') items = ui.input_select_objects( "Prompt", full_items, self._print_helper) self.assertEqual(items, ['1', '3'])
def modify_items(self, lib, mods, dels, query, write, move, album, confirm): """Modifies matching items according to user-specified assignments and deletions. `mods` is a dictionary of field and value pairse indicating assignments. `dels` is a list of fields to be deleted. """ # Parse key=value specifications into a dictionary. model_cls = library.Album if album else library.Item # Get the items to modify. items, albums = _do_query(lib, query, album, False) objs = albums if album else items reconfirm = self.check_sanity(mods, dels, objs, album) if reconfirm: confirm = True # Apply changes *temporarily*, preview them, and collect modified # objects. print_(u'Modifying {0} {1}s.'.format(len(objs), u'album' if album else u'item')) changed = [] for obj in objs: obj_mods = {} for key, value in mods.items(): value = obj.evaluate_template(value) obj_mods[key] = model_cls._parse(key, value) if print_and_modify(obj, obj_mods, dels) and obj not in changed: changed.append(obj) # Still something to do? if not changed: print_(u'No changes to make.') return # Confirm action. if confirm: if write and move: extra = u', move and write tags' elif write: extra = u' and write tags' elif move: extra = u' and move' else: extra = u'' changed = ui.input_select_objects( u'Really modify%s' % extra, changed, lambda o: print_and_modify(o, mods, dels)) # Apply changes to database and files with lib.transaction(): for obj in changed: obj.try_sync(write, move)