document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/env python
  2. #file name : cm.py
  3. #Copyright (c) Arun.K.R
  4. #Creative Commons Attribution-Share Alike 3.0 Unported\' <http: 3.0="" by-sa="" creativecommons.org="" licenses="">
  5. #short for \'c\'ontact \'m\'anager
  6. #This is the entry point to the program
  7.  
  8. #loads my own module cb.py which contains ContactBook class
  9. #which is the base of our implementation
  10. import cb
  11. import sys
  12.  
  13. def showHelp():
  14. \'\'\'show help for cm program\'\'\'
  15. print \'\'\'Usage: cm OPTION(S) [CONTACT_NAME]
  16. This program manages your contacts efficiently.
  17.  
  18. OPTIONS without CONTACT_NAME
  19. -h  Help
  20. -v  Version
  21. -n  New contact
  22. -l  List all contact\'s name
  23.  
  24. OPTIONS with CONTACT_NAME
  25. -r   Remove contact
  26. -m  Modify contact
  27. -d   Display contact
  28. -a  Add info. to existing contact
  29.  
  30. No two OPTIONS can be specified at a time.
  31. example:
  32. cm -l   :lists all contact\'s name
  33. cm -r Ragu :removes contact of Ragu (if already there)\'\'\'
  34.  
  35. def showVer():
  36. \'\'\'prints version of program\'\'\'
  37. print \'\'\'cm (Contacts Manager) version 2.0
  38. Copyright (C) 2008 Arun.K.R, MegaWare(tm) <www.megaware.co.nr>
  39. This is free software.  You may redistribute copies of it under the terms of
  40. the \'Creative Commons Attribution-Share Alike 3.0 Unported\' <http: 3.0="" by-sa="" creativecommons.org="" licenses="">.\\
  41. There is NO WARRANTY, to the extent permitted by law.
  42. Written by Arun.K.R <code333 at="" com="" dot="" gmail="">\'\'\'
  43.  
  44. def addToList(lst, itm):
  45. \'\'\'append an item \'itm\' to list \'lst\'
  46.  
  47. care should be taken. ie., lst must be a list\'\'\'
  48. lst.append(itm)
  49. return lst
  50.  
  51. def isRepeated(customise):
  52. while True:
  53. itr = str(raw_input(\'Do you want to add %s (y/n) : \' % customise)).lower()
  54. if itr == \'y\' or itr == \'n\' :
  55. break
  56. return itr
  57.  
  58. cv = cb.ContactBook() #creates an object of ContactBook class
  59.  
  60. argLn = len(sys.argv) # this helps to speed up a littile bit (from my C++  experiance.)
  61. #this is because we don\'t need to calculate length again and again.
  62. if argLn < 2 or argLn > 3:
  63. showHelp()
  64. elif argLn == 2:
  65. if len(sys.argv[1]) > 2 : #ie., \'-\' and OPTION, OPTION is always one character.
  66. showHelp()
  67. else:
  68. op = sys.argv[1][1] #reads second argument without trailing \'-\'
  69. op = op.lower()
  70. if op == \'h\' : #shows help
  71. showHelp()
  72. elif op == \'v\': #shows version number
  73. showVer()
  74. elif op == \'n\': #add a new contact
  75. name = str(raw_input(\'Enter Name : \'))
  76. #ToDo: we can check for validity.ie., email address and phone number are in proper format.
  77. email = []
  78. phone = []
  79. while True:
  80. tmp_email = str(raw_input(\'Enter email, <enter> for none : \'))
  81. # I am surprised when I found during debugging face that tmp_email now contains \'\' not \\n
  82. if tmp_email == \'\':
  83. tmp_email = \' \'
  84. addToList(email, tmp_email)
  85. break #if the user choose to press <enter> ie., for none, why we ask to enter another one?
  86. addToList(email, tmp_email)
  87. if isRepeated(\'another email\') == \'n\' : break
  88. while True:
  89. tmp_phone = str(raw_input(\'Enter phone, <enter> for none : \'))
  90. if tmp_phone == \'\':
  91. tmp_phone = \' \'
  92. addToList(phone, tmp_phone)
  93. break #if the user choose to press <enter> ie., for none, why we ask to enter another one?
  94. addToList(phone, tmp_phone)
  95. if isRepeated(\'another phone\') == \'n\' : break
  96. cv.updateContact(name, email, phone)
  97. elif op == \'l\': #print all contact\'s name
  98. cv.showNames()
  99. else: #invalied argument format
  100. print \'Invalied argument(s)\'
  101. showHelp()
  102. else: #there are 3 arguments
  103. if len(sys.argv[1]) > 2 :  #ie., \'-\' and OPTION, OPTION is always one character.
  104. showHelp()
  105. else:
  106. op = sys.argv[1][1] #reads second argument without trailing \'-\'
  107. op = op.lower()
  108. name = sys.argv[2]
  109. if cv.hasContact(name) == False:
  110. print \'No contact for name : %s \' % name
  111. elif op == \'c\': #show specified contcat
  112. cv.showContact(name)
  113. elif op == \'m\': #modify given account
  114. #ToDo: we can check for validity.ie., email address and phone number are in proper format.
  115. elst = cv.getEmail(name) #gets email list for given name
  116. while True:
  117. email_old = str(raw_input(\'Enter old email, <enter> for exit : \'))
  118. if email_old == \'\' or elst.__contains__(email_old) :
  119. if email_old != \'\':
  120. email_new = str(raw_input(\'Enter new email : \'))
  121. elst[elst.index(email_old)] = email_new
  122. if isRepeated(\'another email\') == \'n\': break
  123. else: break
  124. else:
  125. print \'Entered email is non-existing:\'
  126.  
  127. plst = cv.getPhone(name)
  128. while True:
  129. phone_old = str(raw_input(\'Enter old phone, <enter> for exit : \'))
  130. if phone_old == \'\' or plst.__contains__(phone_old) :
  131. if phone_old != \'\':
  132. phone_new = str(raw_input(\'Enter new phone : \'))
  133. plst[plst.index(phone_old)] = phone_new
  134. if isRepeated(\'another phone\') == \'n\': break
  135. else: break
  136. else:
  137. print \'Entered phone is non-existing:\'
  138.  
  139. cv.updateContact(name, elst, plst)
  140. elif op == \'r\': #remove a contact
  141. if cv.hasContact(name) == True:
  142. print \'\\n following contact will be permanantly removed\'
  143. cv.showContact(name)
  144. if str(raw_input(\'do you want to continue (y/n): \')).lower() == \'y\':
  145. cv.rmContact(name)
  146. else:
  147. print \'There is no contact in this <%s> name\' % name
  148. elif op == \'a\': #adds info. to existing contact
  149. #ToDo: we can check for validity.ie., email address and phone number are in proper format.
  150. elst = cv.getEmail(name) #gets email list for given name
  151. if isRepeated(\'an email\') == \'y\':
  152. while True:
  153. email_new = str(raw_input(\'Enter new email : \'))
  154. elst.append(email_new)
  155. if isRepeated(\'another email\') == \'n\': break
  156.  
  157. plst = cv.getPhone(name)
  158. if isRepeated(\'a phone\') == \'y\':
  159. while True:
  160. phone_new = str(raw_input(\'Enter new phone : \'))
  161. plst.append(phone_new)
  162. if isRepeated(\'another phone\') == \'n\': break
  163.  
  164. cv.updateContact(name, elst, plst)
  165. else: #invalied argument format
  166. print \'Invalied arguments\'
  167. showHelp()
  168. #program ends here
');