Commits

Ronny Pfannschmidt committed 02ef0d8

json collect & more

Comments (0)

Files changed (3)

filetrack/__init__.py

 __all__ = [
     'load',
     'repos',
+    'images',
     'do_migrate'
 ]
 
 from .utils import do_migrate
-from .analyze import repos
+from .analyze import repos, images
 from .load import load
 

filetrack/analyze.py

 '''
 
 
+IMAGES = '''
+    select basename(name) as basename, count(*) as c, collect_list(name) as json
+    from fts_filename
+    where name match 'jpg'
+    group by basename
+    having c > 4
+'''
+
 @appcommand
 def repos(db, type_):
     simplerows(db, REPOS, {'type': '.' + type_})
+
+
+
+@appcommand
+def images(db):
+    simplerows(db, IMAGES, None)

filetrack/utils.py

+from __future__ import print_function
 import py
 import sqlite3
-from contextlib import closing
+import json
+from contextlib import closing, contextmanager
 from functools import wraps
 
 from micromigrate import find_in_path, apply_migrations, missing_migrations
     return command(migrator, migrations)
 
 
+class CollectList(object):
+    def __init__(self):
+        self.data = []
+
+    def step(self, value):
+        self.data.append(value)
+
+    def finalize(self):
+        return json.dumps(self.data)
+
+
+@contextmanager
+def database(dbname):
+    conn = sqlite3.connect(dbname)
+    def my_basename(arg):
+        arg = py.std.os.path.basename(arg)
+        arg = arg.strip()
+        if arg.endswith('.i'):
+            arg = arg[:-2]
+        return  arg
+
+    conn.create_function('basename', 1, my_basename)
+    conn.create_aggregate('collect_list', 1, CollectList)
+    with closing(conn):
+        yield conn
+
+
 def appcommand(func):
     @wraps(func)
     def command(dbname, *args):
         missing = needs_migrate(dbname)
         if missing:
-            print 'missing migrations', sorted(missing)
+            print('missing migrations', sorted(missing))
             raise SystemExit(1)
-        db = sqlite3.connect(dbname)
-        with closing(db):
+        with database(dbname) as db:
             func(db, *args)
     return command
 
 
+
+_notify = print
+
 def simplerows(db, query, args):
-    for row in db.execute(query, args):
-        print(row[0])
+    print(args)
+    if args is not None:
+        rows = db.execute(query, args)
+    else:
+        rows = db.execute(query)
+    for row in rows:
+        _notify(*row)