Yuvipanda has submitted this change and it was merged.

Change subject: Create column objects as well
......................................................................


Create column objects as well

- Also move bootstrap.py to run.py
- Make tables be able to reconstitute themselves from yaml

Change-Id: Id5029b3703da1d3f9c2d5badc25cde762d08dd70
---
A auditor/column.py
R auditor/run.py
M auditor/table.py
3 files changed, 64 insertions(+), 9 deletions(-)

Approvals:
  Yuvipanda: Verified; Looks good to me, approved



diff --git a/auditor/column.py b/auditor/column.py
new file mode 100644
index 0000000..bf09aa8
--- /dev/null
+++ b/auditor/column.py
@@ -0,0 +1,19 @@
+class Column(object):
+    """
+    Represents a particular column in a particular table.
+    """
+    def __init__(self, name, whitelisted=True, condition=None, 
replacewith=None):
+        """
+        Create a new column
+
+        :param name: Name of this column
+        :param whitelisted: True if the column is whitelisted
+        :param condition: Conditions under which this column is replaced
+        :param replacewith: What to replace the column's value with if 
condition is matched
+        :return:
+        """
+        self.name = name
+        self.whitelisted = whitelisted
+        self.condition = condition
+        self.replacewith = replacewith
+
diff --git a/auditor/bootstrap.py b/auditor/run.py
similarity index 83%
rename from auditor/bootstrap.py
rename to auditor/run.py
index 774e27c..8a4758a 100644
--- a/auditor/bootstrap.py
+++ b/auditor/run.py
@@ -6,11 +6,13 @@
 from mwconfig import MWConfig
 from wikidatabase import WikiDatabase
 from table import Table
+from column import Column
 
 argparser = argparse.ArgumentParser()
 
 argparser.add_argument('--hosts', help='Hosts to connect to')
 argparser.add_argument('--mwconfig', help='Path to mediawiki-config 
repository')
+argparser.add_argument('--expectedconfig', help='Path to yaml file listing 
expected table config', nargs='+')
 argparser.add_argument('--db_suffix', help='Suffix to use for each database 
name',
                        default='')
 
@@ -47,7 +49,8 @@
             if tablename in tables:
                 tables[tablename].add_db(db)
             else:
-                table = Table(tablename, reflector.get_columns(dbname, 
tablename))
+                columnnames = reflector.get_columns(dbname, tablename)
+                table = Table(tablename, [Column(cn) for cn in columnnames])
                 tables[tablename] = table
                 db.add_table(table)
 
@@ -58,11 +61,6 @@
 }, open('dblists.yaml', 'w'))
 
 # Write out table schemas
-schemadata = {}
-for name, table in tables.items():
-    tabledata = {}
-    for columnname in table.columns:
-        tabledata[columnname] = {'whitelisted': True}
-    schemadata[name] = tabledata
+schemadata = {table.name: table.to_dict() for table in tables.values()}
 
 yaml.dump(schemadata, open('tableschema.yaml', 'w'), default_flow_style=False)
diff --git a/auditor/table.py b/auditor/table.py
index 8a94187..c7f412c 100644
--- a/auditor/table.py
+++ b/auditor/table.py
@@ -1,11 +1,49 @@
+from column import Column
+
 class Table(object):
-    def __init__(self, name, columns):
+    def __init__(self, name, columns=[], where=None):
         self.name = name
-        self.columns = columns
         self.dbs = {}
+        self.columns = columns
+        self.where = where
 
     def add_db(self, db):
         if db.name in self.dbs:
             return
         self.dbs[db.name] = db
         db.add_table(self)
+
+    def add_column(self, column):
+        self.columns.append(column)
+        column.table = self
+
+    def to_dict(self):
+        tabledict = {}
+        if self.where:
+            tabledict['where'] = self.where
+        if all([c.whitelisted for c in self.columns]):
+            # Everything is whitelisted!
+            tabledict['columns'] = [c.name for c in self.columns]
+        else:
+            tabledict['columns'] = {}
+            for c in self.columns:
+                columndict = {}
+                columndict['whitelisted'] = c.whitelisted
+                if c.condition:
+                    columndict['condition'] = c.condition
+                if c.replacewith:
+                    columndict['replacewith'] = c.replacewith
+                tabledict['columns'][c.name] = columndict
+        return tabledict
+
+    @classmethod
+    def from_dict(cls, tablename, tabledata):
+        table = cls(tablename, tabledata.get('where', None))
+        if isinstance(tabledata['columns'], list):
+            # Whitelisted table
+            for colname in tabledata['columns']:
+                table.add_column(Column(colname))
+        else:
+            # Greylisted table!
+            for colname, coldata in tabledata['columns'].items():
+                table.add_column(Column(colname, False, 
coldata.get('condition'), coldata.get('replacewith')))

-- 
To view, visit https://gerrit.wikimedia.org/r/180098
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Id5029b3703da1d3f9c2d5badc25cde762d08dd70
Gerrit-PatchSet: 3
Gerrit-Project: operations/software/labsdb-auditor
Gerrit-Branch: master
Gerrit-Owner: Yuvipanda <[email protected]>
Gerrit-Reviewer: Yuvipanda <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to