Commits

ryuslash committed 55c115e

Add "remove" option to dispass-label

This also adds `remove' to the interactive editor.

  • Participants
  • Parent commits 4ebabc9

Comments (0)

Files changed (3)

File dispass/dispass.py

               '                add a new label to the labelfile, the\n'
               '                labelspec looks like this:\n'
               '                label[:size[:algorithm[:sequence_number]]]\n'
+              '-r, --remove <labelname>\n'
+              '                remove a label from the labelfile\n'
               "--script        optimize input/output for 'wrapping' "
               'dispass-label')
 
         l_flag = None
         script_flag = None
         a_flag = None
+        r_flag = None
 
         try:
-            opts, args = getopt.getopt(argv[1:], "a:f:hlV",
+            opts, args = getopt.getopt(argv[1:], "a:f:hlr:V",
                                        ["add=", "file=", "help", "list",
-                                        "script", "version"])
+                                        "remove=", "script", "version"])
         except getopt.GetoptError, err:
             print str(err), "\n"
             self.usage()
                 script_flag = True
             elif o in ("-a", "--add"):
                 a_flag = a.split(':')
+            elif o in ("-r", "--remove"):
+                r_flag = a
             else:
                 assert False, "unhandled option"
 
             else:
                 print('Label already exists in labelfile')
                 return 1
+        elif r_flag:
+            if lf.remove(r_flag):
+                lf.save()
+                print('Label removed')
+                return 0
+            else:
+                print("Label doesn't exist in labelfile")
+                return 1
 
         InteractiveEditor(settings, lf, interactive=True)
 

File dispass/filehandler.py

         self.labelfile.append((labelname, length, algo, seqno))
         return True
 
+    def remove(self, labelname):
+        '''Remove label from `labelfile`'''
+
+        removed = False
+
+        for i in range(len(self.labelfile)):
+            if self.labelfile[i][0] == labelname:
+                del self.labelfile[i]
+                removed = True
+                break
+
+        return removed
+
     def refresh(self, sort=True):
         '''Sort `labelfile` on labelname and get longest label'''
 

File dispass/interactive_editor.py

 
     def menu(self):
         print('add     Add label\n'
+              'remove  Remove label\n'
               'ls      List all labels\n'
               'help    Show this help information\n'
               'quit    Quit')
         elif command_char == 'a':
             self.add()
             self.prompt()
+        elif command_char == 'r':
+            self.remove()
+            self.prompt()
         elif command_char == 'q':
             print('Bye')
             sys.exit()
             print("Invalid option '{command}'".format(command=command))
             self.menu()
 
-    def add(self):
+    def read_label(self):
+        '''Keep bugging the user for a label until they crack and give us
+        one.
+
+        '''
         while True:
             try:
-                label = raw_input('Label: ').split()[0]
-                break
+                return raw_input('Label: ').split()[0]
             except IndexError:
                 print 'label cannot be empty - please try again'
                 continue
 
+    def add(self):
+        label = self.read_label()
+
         while True:
             try:
                 length = (
             self.filehandler.parse()
         else:
             print('Label already exists in labelfile')
+
+    def remove(self):
+        label = self.read_label()
+
+        if self.filehandler.remove(label):
+            self.filehandler.save()
+            print('Label removed')
+        else:
+            print("Label doesn't exist in labelfile")