def record_terms(args):
    config = settings.Settings(args.ini_file)

    if args.date:
        now = args.date
    else:
        from datetime import datetime
        now = datetime.utcnow()
        now = now.strftime('%Y-%m-%d %H:%M:%S')

    ftc = FileTermChecker(args.track_file)
    terms = list(ftc.update_tracking_terms())
    
    rows = [(term, now) for term in terms]

    db = settings.connect_to_database(config)
    if db:
        # Create the table if it doesn't exist
        db.query(CREATE_TABLE_STMT)

        # Get a database cursor
        cursor = db.cursor()

        # insert the things
        inserted = cursor.executemany(TERM_INSERT_STMT, rows)
        
        logger.info("Inserted %d terms", inserted)
    else:
        logger.error("Failed to connect to database")
        exit(1)
class TestFileTermChecker(unittest.TestCase):
    def setUp(self):
        self.file = tempfile.NamedTemporaryFile(delete=False)
        self.file.close()

        self.checker = FileTermChecker(self.file.name)

    def test_update_tracked_terms(self):
        terms = self.checker.update_tracking_terms()
        self.assertEqual(terms, set(), "Returns empty set if file is empty")

        with open(self.file.name, mode='w+b') as tfile:
            tfile.write(b"one\n")
            tfile.write(b"two three\n")
            tfile.write(b"two three\n")
            tfile.write(b"  four  \n")
            tfile.write(b"\n")
            tfile.write(b"\tfive; six\'\t\n")

        terms = self.checker.update_tracking_terms()
        self.assertEqual(terms,
                         set(["one", "two three", "four", "five; six'"]),
                         "Read terms from the file: %s" % repr(terms))

    def tearDown(self):
        self.file.close()
        os.unlink(self.file.name)
class TestFileTermChecker(unittest.TestCase):
    def setUp(self):
        self.file = tempfile.NamedTemporaryFile(delete=False)
        self.file.close()

        self.checker = FileTermChecker(self.file.name)

    def test_update_tracked_terms(self):
        terms = self.checker.update_tracking_terms()
        self.assertEqual(terms, set(), "Returns empty set if file is empty")

        with open(self.file.name, mode='w+b') as tfile:
            tfile.write(b"one\n")
            tfile.write(b"two three\n")
            tfile.write(b"two three\n")
            tfile.write(b"  four  \n")
            tfile.write(b"\n")
            tfile.write(b"\tfive; six\'\t\n")

        terms = self.checker.update_tracking_terms()
        self.assertEqual(terms, set(["one", "two three", "four",
                                     "five; six'"]),
                         "Read terms from the file: %s" % repr(terms))

    def tearDown(self):
        self.file.close()
        os.unlink(self.file.name)
    def setUp(self):
        self.file = tempfile.NamedTemporaryFile(delete=False)
        self.file.close()

        self.checker = FileTermChecker(self.file.name)
    def setUp(self):
        self.file = tempfile.NamedTemporaryFile(delete=False)
        self.file.close()

        self.checker = FileTermChecker(self.file.name)