示例#1
0
def main(args):
    sys.argv = args
    args = docopt(__doc__)
    contact = Contact()
    # contact.set_bday(args["--bday"])
    schema = Schema({
        '--contact_id':
        Or(
            None,
            And(
                Use(int),
                Use(contact.set_cid,
                    error='id is not correct, it should be integer number'))),
        '--first_name':
        Or(None, Use(contact.set_fname, error='fname is not correct')),
        '--last_name':
        Or(None, Use(contact.set_lname, error='lname is not correct')),
        '--middle_name':
        Or(None, Use(contact.set_mname, error='mname is not correct')),
        '--phone':
        Or(None, Use(contact.set_phone, error='phone is not correct')),
        '--bday':
        Or(
            None,
            Use(contact.set_bday,
                error='birtday is not correct it should be one of the formats '
                + str(contact.bday_types))),
        '--data':
        Or(None, Use(set_data, error='name was not correct')),
        '--reverse':
        Or(None, True, False),
        '--replace':
        Or(None, True, False),
        '--sort':
        Or(None,
           "fname",
           "lname",
           "mname",
           "phone",
           "bday",
           error="--sort should be one of the fname,lname,mname,phone,bday"),
        'add':
        Or(False, True),
        'del':
        Or(False, True),
        'find':
        Or(False, True),
        'list':
        Or(False, True),
    })
    try:
        schema.validate(args)
    except SchemaError as e:
        exit(e)
    try:
        connection = sqlite3.connect(database)
        c = connection.cursor()
        c.execute(
            "create table contacts(id integer primary key autoincrement, fname text, lname text, mname text, phone text, bday text)"
        )
        print("new database " + database + " was created")
    except sqlite3.Error as e:
        print("Existing database " + database)

    if args["add"]:
        added, phoneexist, comment = contact.add(contact, c, args)
        if added:
            print(comment + " " + str(contact.get_tuple()))
        else:
            print(comment)
    elif args["find"]:
        finded = contact.find(contact, c)
        if finded:
            print(
                tabulate(finded,
                         headers=[
                             "Id", "first name", "last name", "middle name",
                             "phone", "birthday date"
                         ]))
        else:
            print("there is no any contact " +
                  (("like:" + str(contact)) if str(contact) else ""))
    elif args["del"]:
        result, string = contact.delete(contact, c)
        print(string)
        if result:
            for r in result:
                print(r)
        else:
            sys.exit(-1)
    elif args["list"]:
        result = contact.lst(args, c)
        if result:
            print(
                tabulate(result,
                         headers=[
                             "ID", "first name", "last name", "middle name",
                             "phone", "birthday date"
                         ]))
        else:
            print("there is empty database=" + database)

    else:
        remind = contact.reminder(c)
        print(
            "This contact will have their birthdays in this and next months:")
        if remind:
            print(
                tabulate(remind,
                         headers=[
                             "Id", "first name", "last name", "middle name",
                             "phone", "birthday date"
                         ]))

    connection.commit()
    connection.close()
def main(args):
    sys.argv=args
    args= docopt(__doc__)
    contact = Contact()
    # contact.set_bday(args["--bday"])
    schema = Schema({
        '--contact_id': Or(None,And(Use(int),Use(contact.set_cid, error='id is not correct, it should be integer number'))),
        '--first_name': Or(None,Use(contact.set_fname, error='fname is not correct')),
        '--last_name': Or(None,Use(contact.set_lname, error='lname is not correct')),
        '--middle_name': Or(None,Use(contact.set_mname, error='mname is not correct')),
        '--phone': Or(None,Use(contact.set_phone, error='phone is not correct')),
        '--bday': Or(None,Use(contact.set_bday, error='birtday is not correct it should be one of the formats ' + str(contact.bday_types))),
        '--data': Or(None,Use(set_data, error='name was not correct')),
        '--reverse': Or(None,True,False),
        '--replace': Or(None,True,False),
        '--sort': Or(None,"fname","lname","mname","phone","bday", error="--sort should be one of the fname,lname,mname,phone,bday"),
        'add': Or(False,True),
        'del': Or(False, True),
        'find': Or(False,True),
        'list': Or(False,True),
        })
    try:
        schema.validate(args)
    except SchemaError as e:
        exit(e)
    try:
        connection = sqlite3.connect(database)
        c = connection.cursor()
        c.execute("create table contacts(id integer primary key autoincrement, fname text, lname text, mname text, phone text, bday text)")
        print("new database " + database + " was created")
    except sqlite3.Error as e:
        print("Existing database " + database)

    if args["add"]:
        added, phoneexist, comment = contact.add(contact, c, args)
        if added:
            print(comment+" " + str(contact.get_tuple()))
        else:
            print(comment)
    elif args["find"]:
        finded=contact.find(contact, c)
        if finded:
            print(tabulate(finded, headers=["Id","first name","last name","middle name","phone","birthday date"]))
        else:
            print("there is no any contact "+(("like:"+str(contact)) if str(contact) else ""))
    elif args["del"]:
        result, string=contact.delete(contact, c)
        print(string)
        if result:
            for r in result:
                print(r)
        else:
            sys.exit(-1)
    elif args["list"]:
        result = contact.lst(args, c)
        if result:
            print(tabulate(result, headers=["ID","first name","last name","middle name","phone","birthday date"]))
        else:
            print("there is empty database="+database)

    else:
        remind=contact.reminder(c)
        print("This contact will have their birthdays in this and next months:")
        if remind:
            print(tabulate(remind, headers=["Id","first name","last name","middle name","phone","birthday date"]))

    connection.commit()
    connection.close()
示例#3
0
 def test_add_empty(self):
     contact = Contact()
     added, phoneexist, comment = Contact.add(contact, self.c, ())
     self.assertFalse(added)
     self.assertFalse(phoneexist)
示例#4
0
    contact = Contact()
    contact.check_if_txt_exists()
    contact.your_contacts(list_of_contacts, dic)

    if contact.is_empty():
        print()
        print('{}'.format('Your contacts list is empty!'))
        print()

        print('{}\n{}'.format('1 ==> ADD', 'anything else ==> QUIT'))

        options = input('What would you like to do? ').strip()
        # Add contact
        if options == '1':
            system('cls')
            contact.add(list_of_contacts, dic)

        # Exit
        else:
            print('Bye Bye...')
            sleep(5)
            system('cls')
            break

    else:
        print('{}\n{}\n{}\n{}\n{}'.format('1 ==> ADD', '2 ==> DELETE',
                                          '3 ==> CHANGE', '4 ==> SEARCH',
                                          'anything else ==> QUIT'))

        options = input('What would you like to do? ').strip()
示例#5
0
 def test_add(self):
     contact = randomContact()
     self.added.append(contact)
     added, phoneexist, comment = Contact.add(contact,self.c, ())
     self.assertTrue(added)