コード例 #1
0
ファイル: khard.py プロジェクト: DamienCassou/khard
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(), helpers.get_existing_contact_template(source_contact)))
    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(), helpers.get_existing_contact_template(target_contact)))
    target_tf.close()

    # 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]

    # template of source vcard is not required anymore
    os.remove(source_temp_file_name)

    # instead we are interested in the target template contents
    target_tf = open(target_temp_file_name, "r")
    merged_contact = CarddavObject.from_existing_contact_with_new_user_input(target_contact, target_tf.read())
    target_tf.close()
    os.remove(target_temp_file_name)

    # compare them
    if target_contact == merged_contact:
        print("Merge unsuccessfull: Target contact was not modified")
        return

    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())
コード例 #2
0
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(), helpers.get_existing_contact_template(old_contact)))
    tf.close()

    # start editor 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 = CarddavObject.from_existing_contact_with_new_user_input(
        old_contact, tf.read())
    tf.close()
    os.remove(temp_file_name)

    # check if the user changed anything
    if old_contact == new_contact:
        print("Nothing changed.")
    else:
        new_contact.write_to_file(overwrite=True)
        print("Modification successful\n\n%s" % new_contact.print_vcard())
コード例 #3
0
ファイル: khard.py プロジェクト: untitaker/khard
def modify_existing_contact(vcard):
    # get content template for contact
    old_contact_template = helpers.get_existing_contact_template(vcard)
    # 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(old_contact_template)
    tf.close()
    # start editor 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)
    # check if the user changed anything
    if old_contact_template != new_contact_template:
        vcard.process_user_input(new_contact_template)
        vcard.write_to_file(overwrite=True)
        print "Creation successful\n\n%s" % vcard.print_vcard()
    else:
        print "Nothing changed."
コード例 #4
0
def modify_existing_contact(vcard):
    # get content template for contact
    old_contact_template = helpers.get_existing_contact_template(vcard)
    # 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(old_contact_template)
    tf.close()
    # start editor 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)
    # check if the user changed anything
    if old_contact_template != new_contact_template:
        vcard.process_user_input(new_contact_template)
        vcard.write_to_file(overwrite=True)
        print "Creation successful\n\n%s" % vcard.print_vcard()
    else:
        print "Nothing changed."
コード例 #5
0
ファイル: khard.py プロジェクト: DamienCassou/khard
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(), helpers.get_existing_contact_template(old_contact)))
    tf.close()

    # start editor 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 = CarddavObject.from_existing_contact_with_new_user_input(old_contact, tf.read())
    tf.close()
    os.remove(temp_file_name)

    # check if the user changed anything
    if old_contact == new_contact:
        print("Nothing changed.")
    else:
        new_contact.write_to_file(overwrite=True)
        print("Modification successful\n\n%s" % new_contact.print_vcard())
コード例 #6
0
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(), helpers.get_existing_contact_template(source_contact)))
    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(), helpers.get_existing_contact_template(target_contact)))
    target_tf.close()

    # 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]

    # template of source vcard is not required anymore
    os.remove(source_temp_file_name)

    # instead we are interested in the target template contents
    target_tf = open(target_temp_file_name, "r")
    merged_contact = CarddavObject.from_existing_contact_with_new_user_input(
        target_contact, target_tf.read())
    target_tf.close()
    os.remove(target_temp_file_name)

    # compare them
    if target_contact == merged_contact:
        print("Merge unsuccessfull: Target contact was not modified")
        return

    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())