def modify_existing_contact(old_contact): # create temp file and open it with the specified text editor tf = tempfile.NamedTemporaryFile(mode='w+t', delete=False) temp_file_name = tf.name tf.write("# Edit contact: %s\n%s" \ % (old_contact.get_full_name(), old_contact.get_template())) tf.close() temp_file_creation = helpers.file_modification_date(temp_file_name) while True: # start editor to edit contact template child = subprocess.Popen([Config().get_editor(), temp_file_name]) streamdata = child.communicate()[0] if temp_file_creation == helpers.file_modification_date(temp_file_name): print "not modified" new_contact = None os.remove(temp_file_name) break # read temp file contents after editing tf = open(temp_file_name, "r") new_contact_template = tf.read() tf.close() # try to create contact from user input try: new_contact = CarddavObject.from_existing_contact_with_new_user_input( old_contact, new_contact_template) except ValueError as e: print("\n%s\n" % e) while True: input_string = raw_input("Do you want to open the editor again (y/n)? ") if input_string.lower() in ["", "n", "q"]: print("Canceled") os.remove(temp_file_name) sys.exit(0) if input_string.lower() == "y": break else: os.remove(temp_file_name) break # check if the user changed anything if new_contact is None \ or old_contact == new_contact: print("Nothing changed\n\n%s" % old_contact.print_vcard()) else: new_contact.write_to_file(overwrite=True) print("Modification successful\n\n%s" % new_contact.print_vcard())
def create_new_contact(address_book): # create temp file tf = tempfile.NamedTemporaryFile(mode='w+t', delete=False) temp_file_name = tf.name old_contact_template = "# create new contact\n%s" % helpers.get_new_contact_template(address_book.get_name()) tf.write(old_contact_template) tf.close() temp_file_creation = helpers.file_modification_date(temp_file_name) while True: # start vim to edit contact template child = subprocess.Popen([Config().get_editor(), temp_file_name]) streamdata = child.communicate()[0] if temp_file_creation == helpers.file_modification_date(temp_file_name): new_contact = None os.remove(temp_file_name) break # read temp file contents after editing tf = open(temp_file_name, "r") new_contact_template = tf.read() tf.close() # try to create new contact try: new_contact = CarddavObject.from_user_input(address_book, new_contact_template) except ValueError as e: print("\n%s\n" % e) while True: input_string = raw_input("Do you want to open the editor again (y/n)? ") if input_string.lower() in ["", "n", "q"]: print("Canceled") os.remove(temp_file_name) sys.exit(0) if input_string.lower() == "y": break else: os.remove(temp_file_name) break # create carddav object from temp file if new_contact is None \ or old_contact_template == new_contact_template: print("Canceled") else: new_contact.write_to_file() print("Creation successful\n\n%s" % new_contact.print_vcard())
def merge_existing_contacts(source_contact, target_contact, delete_source_contact): # create temp files for each vcard # source vcard source_tf = tempfile.NamedTemporaryFile(mode='w+t', delete=False) source_temp_file_name = source_tf.name source_tf.write("# merge from %s\n%s" \ % (source_contact.get_full_name(), source_contact.get_template())) source_tf.close() # target vcard target_tf = tempfile.NamedTemporaryFile(mode='w+t', delete=False) target_temp_file_name = target_tf.name target_tf.write("# merge into %s\n%s" \ % (target_contact.get_full_name(), target_contact.get_template())) target_tf.close() target_temp_file_creation = helpers.file_modification_date(target_temp_file_name) while True: # start editor to edit contact template child = subprocess.Popen([Config().get_merge_editor(), source_temp_file_name, target_temp_file_name]) streamdata = child.communicate()[0] if target_temp_file_creation == helpers.file_modification_date(target_temp_file_name): merged_contact = None os.remove(source_temp_file_name) os.remove(target_temp_file_name) break # load target template contents target_tf = open(target_temp_file_name, "r") merged_contact_template = target_tf.read() target_tf.close() # try to create contact from user input try: merged_contact = CarddavObject.from_existing_contact_with_new_user_input( target_contact, merged_contact_template) except ValueError as e: print("\n%s\n" % e) while True: input_string = raw_input("Do you want to open the editor again (y/n)? ") if input_string.lower() in ["", "n", "q"]: print("Canceled") os.remove(source_temp_file_name) os.remove(target_temp_file_name) sys.exit(0) if input_string.lower() == "y": break else: os.remove(source_temp_file_name) os.remove(target_temp_file_name) break # compare them if merged_contact is None \ or target_contact == merged_contact: print("Target contact unmodified\n\n%s" % target_contact.print_vcard()) sys.exit(0) while True: if delete_source_contact: input_string = raw_input( "Merge contact %s from address book %s into contact %s from address book %s\n\n" \ "To be removed\n\n%s\n\nMerged\n\n%s\n\nAre you sure? (y/n): " \ % (source_contact.get_full_name(), source_contact.get_address_book().get_name(), merged_contact.get_full_name(), merged_contact.get_address_book().get_name(), source_contact.print_vcard(), merged_contact.print_vcard())) else: input_string = raw_input( "Merge contact %s from address book %s into contact %s from address book %s\n\n" \ "Keep unchanged\n\n%s\n\nMerged:\n\n%s\n\nAre you sure? (y/n): " \ % (source_contact.get_full_name(), source_contact.get_address_book().get_name(), merged_contact.get_full_name(), merged_contact.get_address_book().get_name(), source_contact.print_vcard(), merged_contact.print_vcard())) if input_string.lower() in ["", "n", "q"]: print("Canceled") return if input_string.lower() == "y": break # save merged_contact to disk and delete source contact merged_contact.write_to_file(overwrite=True) if delete_source_contact: source_contact.delete_vcard_file() print("Merge successful\n\n%s" % merged_contact.print_vcard())