Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
authorRadek Brich <radek.brich@devl.cz>
Sat, 13 Apr 2013 15:22:05 +0200
changeset 83 515fadd3d286
parent 82 7b82dc1fb6f5
child 84 3b5dd9efba35
Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
README
analyzeall.py
batchquery.py
batchupdate.py
bigtables.py
listdepends.py
listserial.py
listtables.py
longqueries.py
pgtoolkit/coloredformatter.py
pgtoolkit/highlight.py
pgtoolkit/pgdatadiff.py
pgtoolkit/pgdiff.py
pgtoolkit/prettysize.py
pgtoolkit/toolbase.py
sql/metadb.sql
tablediff.py
tests.py
tests/test_prettysize.py
--- a/README	Wed Apr 10 11:22:15 2013 +0200
+++ b/README	Sat Apr 13 15:22:05 2013 +0200
@@ -7,15 +7,16 @@
 
  * Python 3.2+
  * Psycopg2
+ * pycolib <http://hg.devl.cz/pycolib>
 
 Optional:
- * MyManager:
+ * for MyManager:
    * MySQLdb 1.2.3
    * see extras for Python3 patch
- * pgconsole:
+ * for pgconsole.py:
    * GTK3 + Python bindings + Cairo + GtkSource
    * packages in Debian: python3-gi python3-gi-cairo gir1.2-gtk-3.0 gir1.2-gtksource-3.0
- * browser:
+ * for browser.py:
    * tuikit (http://hg.devl.cz/tuikit)
 
 General Python Modules
--- a/analyzeall.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/analyzeall.py	Sat Apr 13 15:22:05 2013 +0200
@@ -9,7 +9,6 @@
 """
 
 from pgtoolkit import pgbrowser, toolbase
-from pgtoolkit.highlight import highlight
 
 
 class AnalyzeAllTool(toolbase.SimpleTool):
--- a/batchquery.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/batchquery.py	Sat Apr 13 15:22:05 2013 +0200
@@ -1,7 +1,6 @@
 #!/usr/bin/env python3.2
 
 from pgtoolkit import toolbase
-from pgtoolkit.highlight import highlight
 
 
 class BatchQueryTool(toolbase.SimpleTool):
@@ -53,7 +52,7 @@
                     d = dict()
                     exec(f.read(), d)
                     format_row = d['format_row']
-            
+
             with open(self.args.output, 'w', encoding='utf8') as f:
                 for args, rows in results:
                     if format_row:
--- a/batchupdate.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/batchupdate.py	Sat Apr 13 15:22:05 2013 +0200
@@ -3,7 +3,6 @@
 import time
 
 from pgtoolkit import toolbase
-from pgtoolkit.highlight import highlight
 from pgtoolkit.pgmanager import OperationalError
 
 
--- a/bigtables.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/bigtables.py	Sat Apr 13 15:22:05 2013 +0200
@@ -1,6 +1,7 @@
 #!/usr/bin/env python3.2
 
-from pgtoolkit import pgbrowser, toolbase, prettysize
+from pgtoolkit import pgbrowser, toolbase
+from pycolib import prettysize
 
 
 class BigTablesTool(toolbase.SimpleTool):
@@ -19,7 +20,7 @@
             for table in tables:
                 table_name = '%s.%s' % (schema['name'], table['name'])
                 all_tables.append({'name': table_name, 'size': table['size']})
-        
+
         # print names and sizes of 20 largest tables
         for table in sorted(all_tables, reverse=True, key=lambda x: x['size'])[:20]:
             print(prettysize.prettysize_short(table['size']).rjust(8), table['name'], sep='  ')
--- a/listdepends.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/listdepends.py	Sat Apr 13 15:22:05 2013 +0200
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3.2
 
-from pgtoolkit import pgbrowser, toolbase, prettysize
+from pgtoolkit import pgbrowser, toolbase
 
 
 class ListDependsTool(toolbase.SimpleTool):
--- a/listserial.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/listserial.py	Sat Apr 13 15:22:05 2013 +0200
@@ -1,16 +1,16 @@
 #!/usr/bin/env python3.2
 
 from pgtoolkit import toolbase, pgbrowser
-from pgtoolkit.highlight import highlight, WHITE, YELLOW, RED, BOLD
+from pycolib.ansicolor import highlight, WHITE, YELLOW, RED, BOLD
 
 
 class ListSerialTool(toolbase.SimpleTool):
     max_int = 2147483647
-    
+
     def __init__(self):
         toolbase.SimpleTool.__init__(self, name='listserial', desc='List sequences attached to column of type integer with dangerous last_value.')
         self.init()
-    
+
     def main(self):
         conn = self.pgm.get_conn('target')
         browser = pgbrowser.PgBrowser(conn)
--- a/listtables.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/listtables.py	Sat Apr 13 15:22:05 2013 +0200
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3.2
 
-from pgtoolkit import pgbrowser, toolbase, prettysize
+from pgtoolkit import pgbrowser, toolbase
 
 
 class ListTablesTool(toolbase.SimpleTool):
--- a/longqueries.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/longqueries.py	Sat Apr 13 15:22:05 2013 +0200
@@ -1,7 +1,7 @@
 #!/usr/bin/env python3.2
 
 from pgtoolkit import pgstats, toolbase
-from pgtoolkit.highlight import highlight, YELLOW, BOLD
+from pycolib.ansicolor import highlight, YELLOW, BOLD
 
 
 class LongQueriesTool(toolbase.SimpleTool):
--- a/pgtoolkit/coloredformatter.py	Wed Apr 10 11:22:15 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,23 +0,0 @@
-import logging
-
-from pgtoolkit import highlight
-
-
-class ColoredFormatter(logging.Formatter):
-    def __init__(self, fmt, datefmt):
-        logging.Formatter.__init__(self, fmt, datefmt)
-
-    def format(self, record):
-        color = ()
-        if record.levelno == logging.ERROR and record.exc_info is not None:
-            color = (11, 1)
-        if color:
-            origmsg = record.msg
-            f,b = color
-            record.msg = highlight(1, f, b) + record.msg + highlight(0)
-            res = logging.Formatter.format(self, record)
-            record.msg = origmsg
-        else:
-            res = logging.Formatter.format(self, record)
-        return res
-
--- a/pgtoolkit/highlight.py	Wed Apr 10 11:22:15 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-# -*- coding: utf-8 -*-
-
-(BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, BOLD) = range(0,9)
-
-names = ['black','red','green','yellow','blue','magenta','cyan','white']
-
-disabled = False  # set True to disable all colors
-
-def highlight(enable, fg=None, bg=None):
-    '''
-
-    highlight(1) -- switch to bold
-    highlight(1,1) -- red foreground
-    highlight(1,3,4) -- red on blue
-    highlight(0) -- reset
-
-    '''
-    global disabled
-    if disabled:
-        return ''
-
-    if enable:
-        code = '1'
-        if fg is not None:
-            code = ''
-            if fg >= 8:
-                fg -= 8
-                code += '1;'
-            code += str(30 + fg)
-        if bg is not None:
-            code += ';'
-            if bg >= 8:
-                bg -= 8
-                code += '1;'
-            code += str(40 + bg)
-        return "\033[" + code + "m"
-    else:
-        return "\033[0m"
-
-def sethighlight(enable, fg=None, bg=None):
-    print(highlight(enable, fg, bg), end='')
-
-
-if __name__ == '__main__':
-    for c in range(0,8):
-        print(
-            highlight(1,c), names[c].ljust(20),
-            highlight(1,8+c), names[c].ljust(20),
-            highlight(0), sep='')
-
--- a/pgtoolkit/pgdatadiff.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/pgtoolkit/pgdatadiff.py	Sat Apr 13 15:22:05 2013 +0200
@@ -26,7 +26,7 @@
 from collections import OrderedDict
 
 from pgtoolkit import pgbrowser
-from pgtoolkit.highlight import *
+from pycolib.ansicolor import *
 
 
 class DiffData:
--- a/pgtoolkit/pgdiff.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/pgtoolkit/pgdiff.py	Sat Apr 13 15:22:05 2013 +0200
@@ -25,7 +25,7 @@
 # THE SOFTWARE.
 
 
-from pgtoolkit.highlight import *
+from pycolib.ansicolor import *
 
 import re
 import difflib
--- a/pgtoolkit/prettysize.py	Wed Apr 10 11:22:15 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,67 +0,0 @@
-# -*- coding: utf-8 -*-
-#
-# prettysize - print data sizes in format easily readable by humans
-#
-# Copyright (c) 2011  Radek Brich <radek.brich@devl.cz>
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
-iec_prefixes = ('Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi')
-si_prefixes = 'kMGTPEZY'
-big_prefixes = 'KMGTPEZY'
-
-
-def prettysize(num, base=1024, prefixes=si_prefixes, unit='B', sep=' '):
-    '''Convert data size in bytes to human readable aproximation.
-
-    Beware that default output is invalid by recent standardization.
-    Still, this form was in use for ages.
-
-    For standardized output, see prettysize_si and prettysize_iec.
-
-    '''
-    prefixes = list(prefixes)
-    prefix = ''
-    num = float(num)
-    while True:
-        if num < base:
-            # format number
-            out = '{:.2f}'.format(num)
-            # cut off trailing zeros
-            out = out.rstrip('0')
-            if out.endswith('.'):
-                out = out[:-1]
-            return out + sep + prefix + unit
-        num /= base
-        prefix = prefixes.pop(0)
-
-    raise OverflowError('Number is too large.')
-
-
-def prettysize_si(num, **kw):
-    return prettysize(num, base=1000, **kw)
-
-
-def prettysize_short(num):
-    return prettysize(num, unit='', sep='', prefixes=big_prefixes)
-
-
-def prettysize_iec(num):
-    return prettysize(num, prefixes=iec_prefixes)
-
--- a/pgtoolkit/toolbase.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/pgtoolkit/toolbase.py	Sat Apr 13 15:22:05 2013 +0200
@@ -2,9 +2,11 @@
 import logging
 import re
 
-from pgtoolkit import pgmanager, pgbrowser, configparser
-from pgtoolkit.coloredformatter import ColoredFormatter
-from pgtoolkit.highlight import highlight
+from pycolib.configparser import ConfigParser
+from pycolib.coloredformatter import ColoredFormatter
+from pycolib.ansicolor import highlight
+
+from pgtoolkit import pgmanager, pgbrowser
 
 
 class ConnectionInfoNotFound(Exception):
@@ -21,10 +23,10 @@
         self.parser.add_argument('-d', dest='debug', action='store_true',
             help='Debug mode - print database queries.')
 
-        self.config = configparser.ConfigParser()
-        self.config.add_argument('databases', type=dict)
-        self.config.add_argument('meta_db')
-        self.config.add_argument('meta_query')
+        self.config = ConfigParser()
+        self.config.add_option('databases', type=dict)
+        self.config.add_option('meta_db')
+        self.config.add_option('meta_query')
 
         self.pgm = pgmanager.get_instance()
         self.target_isolation_level = None
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sql/metadb.sql	Sat Apr 13 15:22:05 2013 +0200
@@ -0,0 +1,15 @@
+CREATE SCHEMA config;
+
+CREATE TABLE config.databases
+(
+    id serial NOT NULL,
+    name varchar NOT NULL,
+    host varchar,
+    port smallint,
+    dbname varchar,
+    "user" varchar,
+    password varchar,
+
+    PRIMARY KEY (id),
+    UNIQUE (name)
+);
--- a/tablediff.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/tablediff.py	Sat Apr 13 15:22:05 2013 +0200
@@ -9,7 +9,7 @@
 #
 
 from pgtoolkit import toolbase, pgmanager, pgdatadiff
-from pgtoolkit.highlight import *
+from pycolib.ansicolor import highlight, BOLD, YELLOW
 
 
 class TableDiffTool(toolbase.SrcDstTablesTool):
--- a/tests.py	Wed Apr 10 11:22:15 2013 +0200
+++ b/tests.py	Sat Apr 13 15:22:05 2013 +0200
@@ -4,7 +4,6 @@
 
 from tests import test_rowdict
 from tests import test_pgmanager
-from tests import test_prettysize
 
 enable_mysql = True
 try:
@@ -18,7 +17,6 @@
 suite = unittest.TestSuite()
 suite.addTests(loader.loadTestsFromModule(test_rowdict))
 suite.addTests(loader.loadTestsFromModule(test_pgmanager))
-suite.addTests(loader.loadTestsFromModule(test_prettysize))
 
 if enable_mysql:
     suite.addTests(loader.loadTestsFromModule(test_mymanager))
--- a/tests/test_prettysize.py	Wed Apr 10 11:22:15 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,29 +0,0 @@
-#!/usr/bin/env python3
-
-from pgtoolkit import prettysize
-
-import unittest
-
-
-class TestPrettySize(unittest.TestCase):
-    def test_prettysize(self):
-        psize = prettysize.prettysize_short
-        expected = (
-            (0, '0'),
-            (1000, '1000'),
-            (1024, '1K'),
-            (1050, '1.03K'),
-            (2050, '2K'),
-            (333333, '325.52K'),
-            (1048576, '1M'),
-            (1050000, '1M'),
-            (5555555, '5.3M'),
-            (1073741824, '1G'),
-            )
-        for input, result in expected:
-            self.assertEqual(psize(input), result)
-
-
-if __name__ == '__main__':
-    unittest.main()
-