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() # start vim to edit contact template child = subprocess.Popen([Config().get_editor(), temp_file_name]) streamdata = child.communicate()[0] # read temp file contents after editing tf = open(temp_file_name, "r") new_contact_template = tf.read() tf.close() os.remove(temp_file_name) # create carddav object from temp file if old_contact_template == new_contact_template: print("Canceled") else: new_contact = CarddavObject.from_user_input(address_book, new_contact_template) new_contact.write_to_file() print("Creation 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() # start vim to edit contact template child = subprocess.Popen([Config().get_editor(), temp_file_name]) streamdata = child.communicate()[0] # read temp file contents after editing tf = open(temp_file_name, "r") new_contact_template = tf.read() tf.close() os.remove(temp_file_name) # create carddav object from temp file if old_contact_template == new_contact_template: print("Canceled") else: new_contact = CarddavObject.from_user_input(address_book, new_contact_template) new_contact.write_to_file() print("Creation 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 create_new_contact(addressbook): # create temp file tf = tempfile.NamedTemporaryFile(mode='w+t', delete=False) temp_file_name = tf.name tf.write(helpers.get_new_contact_template(addressbook['name'])) tf.close() # start vim to edit contact template child = subprocess.Popen([Config().get_editor(), temp_file_name]) streamdata = child.communicate()[0] # read temp file contents after editing tf = open(temp_file_name, "r") new_contact_template = tf.read() tf.close() os.remove(temp_file_name) # create carddav object from temp file vcard = CarddavObject(addressbook['name'], addressbook['path']) vcard.process_user_input(new_contact_template) vcard.write_to_file() print "Creation successful\n\n%s" % vcard.print_vcard()