Inspect pytables files for getting tables info
authorJavier Sancho <jsf@jsancho.org>
Mon, 7 May 2018 10:53:47 +0000 (12:53 +0200)
committerJavier Sancho <jsf@jsancho.org>
Mon, 7 May 2018 10:53:47 +0000 (12:53 +0200)
datasette_pytables/__init__.py

index a3b1844..8516315 100644 (file)
@@ -1,3 +1,26 @@
+import tables
+
 def inspect(path):
     "Open file and return tables info"
-    return [], []
+    h5tables = {}
+    views = []
+    h5file = tables.open_file(path)
+
+    for table in filter(lambda node: not(isinstance(node, tables.group.Group)), h5file):
+        colnames = []
+        if isinstance(table, tables.table.Table):
+            colnames = table.colnames
+
+        h5tables[table._v_pathname] = {
+            'name': table._v_pathname,
+            'columns': colnames,
+            'primary_keys': [],
+            'count': table.nrows,
+            'label_column': None,
+            'hidden': False,
+            'fts_table': None,
+            'foreign_keys': {'incoming': [], 'outgoing': []},
+        }
+
+    h5file.close()
+    return h5tables, views