Exemple #1
0
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()
Exemple #2
0
        def __init__(self):
            # load config file
            config_file = os.path.join(os.path.expanduser("~"), ".config",
                                       "khard", "khard.conf")
            if os.path.exists(config_file) == False:
                print "Config file %s not available" % config_file
                sys.exit(2)
            self.config = ConfigObj(config_file, interpolation=False)

            # general settings
            if self.config.has_key("general") == False:
                print "Error in config file\nMissing main section \"[general]\"."
                sys.exit(2)
            if self.config['general'].has_key("editor") == False:
                print "Error in config file\nMissing editor parameter. Example: editor = /usr/bin/vim."
                sys.exit(2)
            elif os.path.exists(self.config['general']['editor']) == False:
                print "Error in config file\nInvalid editor path."
                sys.exit(2)
            if self.config['general'].has_key("default_country") == False:
                print "Error in config file\nMissing default country parameter."
                sys.exit(2)
            if self.config['general'].has_key("default_action") == False:
                print "Error in config file\nMissing default action parameter."
                sys.exit(2)
            elif self.config['general']['default_action'] not in [
                    "list", "details", "new", "modify", "remove"
            ]:
                print "Error in config file\nNon existing value for default action parameter. \
                        Possible values are: list, details, new, modify and remove"

                sys.exit(2)

            # load address books
            if self.config.has_key("addressbooks") == False:
                print "Error in config file\nMissing main section \"[addressbooks]\"."
                sys.exit(2)
            if self.config['addressbooks'].keys().__len__() == 0:
                print "Error in config file\nNo address book entries available."
                sys.exit(2)
            for name in self.config['addressbooks'].keys():
                addressbook = self.config['addressbooks'][name]
                if addressbook.has_key("path") == False:
                    print "Error in config file\nMissing path to the \"%s\" address book." % name
                    sys.exit(2)
                if addressbook['path'].startswith("~"):
                    addressbook['path'] = addressbook['path'].replace(
                        "~", os.path.expanduser("~"))
                if os.path.exists(addressbook['path']) == False:
                    print "Error in config file\nThe path %s to the address book %s does not exist." % (
                        addressbook['path'], name)
                    sys.exit(2)
                # set address book name
                addressbook['name'] = name
                # load all vcard files
                addressbook['vcards'] = []
                for filename in glob.glob(
                        os.path.join(addressbook['path'], "*.vcf")):
                    addressbook['vcards'].append(
                        CarddavObject(addressbook['name'], addressbook['path'],
                                      filename))