Package caldavclientlibrary :: Package admin :: Package xmlaccounts :: Package commands :: Module removerecord
[hide private]
[frames] | no frames]

Source Code for Module caldavclientlibrary.admin.xmlaccounts.commands.removerecord

  1  ## 
  2  # Copyright (c) 2007-2016 Apple Inc. All rights reserved. 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  # http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  ## 
 16   
 17  from caldavclientlibrary.admin.xmlaccounts.commands.command import Command 
 18  import getopt 
 19   
20 -class RemoveRecord(Command):
21 22 CMDNAME = "remove" 23
24 - def __init__(self):
25 super(RemoveRecord, self).__init__(self.CMDNAME, "Remove a record of the specified type.") 26 self.uid = None
27
28 - def usage(self):
29 print """USAGE: %s TYPE [OPTIONS] 30 31 TYPE: One of "users", "groups", "locations" or "resources". Also, 32 "u", "g", "l" or "r" as shortcuts. 33 34 Options: 35 -f file path to accounts.xml 36 --uid UID to remove 37 """ % (self.cmdname,)
38
39 - def execute(self, argv):
40 """ 41 Execute the command specified by the command line arguments. 42 43 @param argv: command line arguments. 44 @type argv: C{list} 45 46 @return: 1 for success, 0 for failure. 47 @rtype: C{int} 48 """ 49 50 # Check first argument for type 51 argv = self.getTypeArgument(argv) 52 if argv is None: 53 return 0 54 55 opts, args = getopt.getopt(argv, 'f:h', ["help", "uid=",]) 56 57 for name, value in opts: 58 if name == "-f": 59 self.path = value 60 elif name in ("-h", "--help"): 61 self.usage() 62 return 1 63 elif name == "--uid": 64 self.uid = value 65 else: 66 print "Unknown option: %s." % (name,) 67 self.usage() 68 return 0 69 70 if not self.path: 71 print "Must specify a path." 72 self.usage() 73 return 0 74 if not self.uid: 75 print "Must specify a UID." 76 self.usage() 77 return 0 78 if args: 79 print "Arguments not allowed." 80 self.usage() 81 return 0 82 83 if not self.loadAccounts(): 84 return 0 85 return self.doCommand()
86
87 - def doCommand(self):
88 """ 89 Run the command. 90 """ 91 if self.doRemove(): 92 return self.writeAccounts() 93 return 0
94
95 - def doRemove(self):
96 """ 97 Removes an existing record from the directory. 98 """ 99 100 # First check record exists 101 record = self.directory.getRecord(self.recordType, self.uid) 102 if record is None: 103 print "No '%s' record matching uid '%s'" % (self.recordType, self.uid,) 104 return 0 105 106 107 confirm = raw_input("Really delete the record for '%s' in '%s' [y/n]?" % (self.uid, self.recordType,)) 108 if confirm != "y": 109 return 0 110 111 self.directory.removeRecord(self.recordType, self.uid) 112 return 1
113