def count(_args): """Get number picture files in the database.""" logger.info('Getting image files in the database...') with LocationDB() as database: file_count = database.count() print file_count
def index(): """Application main page.""" with LocationDB() as location_db: db_rows = list(location_db.select_all()) centroid = json.dumps([ average(db_rows, itemgetter('latitude')), average(db_rows, itemgetter('longitude')), ]) rows = json.dumps([row_to_serializable(db_row) for db_row in db_rows]) return render_template('index.html', centroid=centroid, rows=rows)
def test_database_exists(self): """Database not create if exists.""" filename = os.path.join(self.directory, 'location.db') with closing(sqlite3.connect(filename)) as connection: with closing(connection.cursor()) as cursor: cursor.execute( 'CREATE TABLE location (column_1 TEXT, column_2 TEXT)') location_db = LocationDB() self.assertListEqual( location_db.location_table.columns.keys(), ['column_1', 'column_2'], )
def add(args): """Add location information for pictures under directory.""" logger.info('Adding image files from %r...', args.directory) tree_explorer = TreeExplorer(args.directory) paths = tree_explorer.paths() gps_metadata_records = filter_gps_metadata(paths) logger.info('%d picture files with GPS metadata found under %s', len(gps_metadata_records), args.directory) location_rows = [ transform_metadata_to_row(metadata) for metadata in gps_metadata_records ] if location_rows: with LocationDB() as database: database.insert(location_rows)
def test_select_all(self): """Select all rows from location table.""" filename = os.path.join(self.directory, 'location.db') with closing(sqlite3.connect(filename)) as connection: with closing(connection.cursor()) as cursor: cursor.execute( 'CREATE TABLE location (name TEXT)') cursor.execute( 'INSERT INTO location VALUES ("Hello world!")') connection.commit() with LocationDB() as location_db: result = location_db.select_all() rows = result.fetchall() self.assertEqual(len(rows), 1) row = rows[0] self.assertSequenceEqual(row, (u'Hello world!',))
def test_count(self): """Count rows in database.""" file_count = 10 filename = os.path.join(self.directory, 'location.db') with closing(sqlite3.connect(filename)) as connection: with closing(connection.cursor()) as cursor: cursor.execute( 'CREATE TABLE location (filename TEXT)') for index in range(file_count): cursor.execute( 'INSERT INTO location VALUES ("{}.jpg")'.format(index)) connection.commit() with LocationDB() as location_db: result = location_db.count() self.assertEqual(result, file_count)
def test_remove(self): """Delete rows for files under a given directory.""" file_count = 10 filename = os.path.join(self.directory, 'location.db') with closing(sqlite3.connect(filename)) as connection: with closing(connection.cursor()) as cursor: cursor.execute( 'CREATE TABLE location (filename TEXT)') for directory in ['a', 'b']: for index in range(file_count): cursor.execute( 'INSERT INTO location VALUES ("{}/{}.jpg")' .format(directory, index)) connection.commit() with LocationDB() as location_db: result = location_db.delete('a') self.assertEqual(result.rowcount, file_count)
def test_insert(self): """Insert records in database.""" rows = [ { 'filename': 'a.jpg', 'latitude': 1.2, 'longitude': 2.1, 'datetime': datetime(2015, 1, 1, 12, 34, 56) }, { 'filename': 'b.jpg', 'latitude': 3.4, 'longitude': 4.3, 'datetime': datetime(2015, 1, 1, 12, 34, 56) }, ] with LocationDB() as location_db: location_db.insert(rows) filename = os.path.join(self.directory, 'location.db') with closing(sqlite3.connect(filename)) as connection: with closing(connection.cursor()) as cursor: result = cursor.execute('SELECT COUNT(*) FROM location') self.assertListEqual(result.fetchall(), [(2,)])
def remove(args): """Remove location information for pictures under directory.""" logger.info('Removing image files from %r...', args.directory) with LocationDB() as database: database.delete(args.directory)
def test_create_database(self): """Create database file.""" LocationDB() filename = os.path.join(self.directory, 'location.db') self.assertTrue(os.path.isfile(filename))