示例#1
0
def delete(my_dbtool, search_string, conf):
    vcards = my_dbtool.search(search_string, conf.sync.accounts,
                              conf.query.where)
    if len(vcards) is 0:
        sys.exit('Found no matching cards.')
    elif len(vcards) is 1:
        card = vcards[0]
    else:
        from pycarddav import ui
        href_account_list = [(c.href, c.account) for c in vcards]
        pane = ui.VCardChooserPane(my_dbtool,
                                   href_account_list=href_account_list)
        ui.start_pane(pane)
        card = pane._walker.selected_vcard
    if card.href in my_dbtool.get_new(card.account):
        # cards not yet on the server get deleted directly, otherwise we
        # will try to delete them on the server later (where they don't
        # exist) and this will raise an exception
        my_dbtool.delete_vcard_from_db(card.href, card.account)
    else:
        my_dbtool.mark_delete(card.href, card.account)
        print(u'vcard {0} - "{1}" deleted from local db, '
              'will be deleted on the server on the next '
              'sync'.format(card.href, card.fname))
示例#2
0
文件: query.py 项目: chenyf/pycarddav
def delete(my_dbtool, search_string, conf):
    vcards = my_dbtool.search(search_string, conf.sync.accounts,
            conf.query.where)
    if len(vcards) is 0:
        sys.exit('Found no matching cards.')
    elif len(vcards) is 1:
        card = vcards[0]
    else:
        from pycarddav import ui
        href_account_list = [(c.href, c.account) for c in vcards]
        pane = ui.VCardChooserPane(my_dbtool,
                                   href_account_list=href_account_list)
        ui.start_pane(pane)
        card = pane._walker.selected_vcard
    if card.href in my_dbtool.get_new(card.account):
        # cards not yet on the server get deleted directly, otherwise we
        # will try to delete them on the server later (where they don't
        # exist) and this will raise an exception
        my_dbtool.delete_vcard_from_db(card.href, card.account)
    else:
        my_dbtool.mark_delete(card.href, card.account)
        print(u'vcard {0} - "{1}" deleted from local db, '
              'will be deleted on the server on the next '
              'sync'.format(card.href, card.fname))
示例#3
0
def query(conf):
    # testing if the db exists
    if not path.exists(path.expanduser(conf.sqlite.path)):
        sys.exit(str(conf.sqlite.path) + " file does not exist, please sync"
                 " with pycardsyncer first.")

    search_string = conf.query.search_string.decode("utf-8")

    my_dbtool = backend.SQLiteDb(db_path=path.expanduser(conf.sqlite.path),
                                 encoding="utf-8",
                                 errors="stricts",
                                 debug=False)

    #import:
    if conf.query.importing:
        cards = model.cards_from_file(conf.query.importing)
        for card in cards:
            my_dbtool.update(card, conf.sync.accounts[0], status=backend.NEW)
        sys.exit()

    # backup:
    if conf.query.backup:
        with open(conf.query.backup, 'w') as vcf_file:
            if search_string == "":
                href_account_list = my_dbtool.get_all_href_from_db(
                    conf.sync.accounts)
            else:
                href_account_list = my_dbtool.search(search_string,
                                                     conf.sync.accounts)
            for href, account in href_account_list:
                vcard = my_dbtool.get_vcard_from_db(href, account)
                vcf_file.write(vcard.vcf.encode('utf-8'))
        sys.exit()

    # editing a card:
    #if conf.query.edit:
    #    names = my_dbtool.select_entry2(search_string)
    #    href = ui.select_entry(names)
    #    if href is None:
    #        sys.exit("Found no matching cards.")

    # mark a card for deletion
    if conf.query.delete:
        href_account_list = my_dbtool.search(search_string,
                                             conf.sync.accounts)
        if len(href_account_list) is 0:
            sys.exit('Found no matching cards.')
        elif len(href_account_list) is 1:
            href, account = href_account_list[0]
            card = my_dbtool.get_vcard_from_db(href, account)
        else:
            pane = ui.VCardChooserPane(my_dbtool,
                                       href_account_list=href_account_list)
            ui.start_pane(pane)
            card = pane._walker.selected_vcard
        if card.href in my_dbtool.get_new(card.account):
            # cards not yet on the server get deleted directly, otherwise we
            # will try to delete them on the server later (where they don't
            # exist) and this will raise an exception
            my_dbtool.delete_vcard_from_db(card.href, card.account)
        else:
            my_dbtool.mark_delete(card.href, card.account)
            print(u'vcard {0} - "{1}" deleted from local db, '
                  'will be deleted on the server on the next '
                  'sync'.format(card.href, card.fname))
        sys.exit()

    print("searching for " + conf.query.search_string + "...")

    result = my_dbtool.search(search_string, conf.sync.accounts)
    for href, account in result:
        vcard = my_dbtool.get_vcard_from_db(href, account)
        if conf.query.mutt_format:
            lines = vcard.print_email()
        elif conf.query.tel:
            lines = vcard.print_tel()
        elif conf.query.display_all:
            lines = vcard.pretty
        else:
            lines = vcard.pretty_min
        if not lines == '':
            print(lines.encode('utf-8'))

    return 0
示例#4
0
def query(conf):
    # testing if the db exists
    if not path.exists(conf.sqlite__path):
        sys.exit(str(conf.sqlite__path) + " file does not exist, please sync"
                                          " with pycardsyncer first.")

    search_string = conf.cmd__search_string.decode("utf-8")

    my_dbtool = backend.SQLiteDb(conf.sqlite__path, "utf-8", "stricts", False)

    #import:
    if conf.cmd__importing:
        cards = model.cards_from_file(conf.cmd__importing)
        for card in cards:
            my_dbtool.update(card, status=backend.NEW)
        sys.exit()

    # backup:
    if conf.cmd__backup:
        with open(conf.cmd__backup, 'w') as vcf_file:
            if search_string == "":
                hreflist = my_dbtool.get_all_vref_from_db()
            else:
                hreflist = my_dbtool.search(search_string)
            for href in hreflist:
                vcard = my_dbtool.get_vcard_from_db(href)
                vcf_file.write(vcard.vcf.encode('utf-8'))
        sys.exit()

    # editing a card:
    #if conf.cmd__edit:
    #    names = my_dbtool.select_entry2(search_string)
    #    href = ui.select_entry(names)
    #    if href is None:
    #        sys.exit("Found no matching cards.")

    # mark a card for deletion
    if conf.cmd__delete:
        hrefs = my_dbtool.search(search_string)
        if len(hrefs) is 0:
            sys.exit('Found no matching cards.')
        elif len(hrefs) is 1:
            href = hrefs[0]
        else:
            pane = ui.VCardChooserPane(my_dbtool, hrefs)
            ui.start_pane(pane)
            card = pane._walker.selected_vcard
            href = card.href
        if href in my_dbtool.get_new():
            # cards not yet on the server get deleted directly, otherwise we
            # will try to delete them on the server later (where they don't
            # exist) and this will raise an exception
            my_dbtool.delete_vcard_from_db(href)
        else:
            my_dbtool.mark_delete(href)
            print('vcard "%s" deleted from local db, will be deleted ' % href +
                  'on the server on the next sync')
        sys.exit()

    print("searching for " + conf.cmd__search_string + "...")
    result = my_dbtool.search(search_string)
    for one in result:
        vcard = my_dbtool.get_vcard_from_db(one)
        if conf.cmd__mutt:
            lines = vcard.print_email()
        elif conf.cmd__tel:
            lines = vcard.print_tel()
        elif conf.cmd__display_all:
            lines = vcard.pretty
        else:
            lines = vcard.pretty_min
        if not lines == '':
            print(lines.encode('utf-8'))

    return 0