Commits

Evgeniy Tatarkin committed 25254aa

add include/exclude/list options for reflect command, change name type order

Comments (0)

Files changed (4)

+Copyright (c) 2011 by Evgeniy Tatarkin and contributors.
+
+Some rights reserved.
+
+Redistribution and use in source and binary forms of the software as well
+as documentation, with or without modification, are permitted provided
+that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright
+  notice, this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above
+  copyright notice, this list of conditions and the following
+  disclaimer in the documentation and/or other materials provided
+  with the distribution.
+
+* The names of the contributors may not be used to endorse or
+  promote products derived from this software without specific
+  prior written permission.
+
+THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
+NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGE.
 
 About
 =====
-Simple package for describing SQLAlchemy schema.
-Works with mapped classes. Relation detecting by `ForeignKey` columns.
-Supports mapped class inherit.
-
+Simple package for describing SQLAlchemy schema and display raw database
+tables. Relation detecting by `ForeignKey` columns.
+Supports mapped class inherit. BSD licensed.
 
 Output formats:
 
  * `PlantUML <http://plantuml.sourceforge.net/>`_ class diagram
-
+ * `DOT <http://www.graphviz.org/>`_ graphviz directed graphs
 
 
 Requirements
 
     # or for svg format
     $ java -jar plantuml.jar -Tsvg schema.plantuml
+
+
+Also you can display you sql database tables::
+
+    $ sadisplay -u <URL connection string to db> -r dot > schema.dot
+    $ dot -Tpng schema.dot > schema.png

sadisplay/reflect.py

 
 \n\nDatabase connection string - http://goo.gl/3GpnE
 """
-
+import operator
+import string
 from optparse import OptionParser
 from sqlalchemy import create_engine, MetaData
 from sadisplay import describe, render, __version__
     parser.add_option('-u', '--url', dest='url',
                     help='Database URL (connection string)')
 
-    parser.add_option('-r', '--render', dest='render', default='plantuml',
+    parser.add_option('-r', '--render', dest='render', default='dot',
                     choices=['plantuml', 'dot'],
                     help='Output format - plantuml or dot')
 
+    parser.add_option('-l', '--list', dest='list', action='store_true',
+                    help='Output database list of tables and exit')
+
+    parser.add_option('-i', '--include', dest='include',
+                    help='List of tables to include through ","')
+
+    parser.add_option('-e', '--exclude', dest='exclude',
+                    help='List of tables to exlude through ","')
+
     (options, args) = parser.parse_args()
 
     if not options.url:
 
     meta.reflect(bind=engine)
 
-    desc = describe(meta.tables.values())
+    if options.list:
+        print 'Database tables:'
+        tables = sorted(meta.tables.keys())
+
+        for i in xrange(0, len(tables), 2):
+            print '  %s' % tables[i:i + 1][0] \
+                + ' ' * (38 - len(tables[i:i + 1][0])) \
+                + tables[i + 1:i + 2][0]
+
+        exit(0)
+
+    tables = set(meta.tables.keys())
+
+    if options.include:
+        tables &= set(map(string.strip, options.include.split(',')))
+
+    if options.exclude:
+        tables -= set(map(string.strip, options.exclude.split(',')))
+
+    desc = describe(map(lambda x: operator.getitem(meta.tables, x), tables))
     print getattr(render, options.render)(desc)

sadisplay/render.py

 
     CLASS_TEMPLATE = "Class %(name)s {\n%(cols)s\n%(props)s\n%(methods)s\n}\n"
 
-    COLUMN_TEMPLATE = "\t%(type)s \t\t%(name)s"
+    COLUMN_TEMPLATE = "\t%(name)s \t\t%(type)s"
 
     PROPERTY_TEMPLATE = "\t+\t\t%(name)s"
 
         result.append(RELATION_TEMPLATE % item)
 
     result += [
-        'right footer sadisplay v%s' % __version__,
+        'right footer generated by sadisplay v%s' % __version__,
         '@enduml',
     ]
 
     CLASS_BODY = "\n\t\tlabel=\"{%(name)s|%(cols)s|%(props)s|%(methods)s\l}\""
     CLASS_TEMPLATE = "\t%(name)s [" + CLASS_BODY + "\n\t]\n"
 
-    COLUMN_TEMPLATE = "\t%(type)s \t\t%(name)s\l"
+    COLUMN_TEMPLATE = "\t%(name)s \t\t%(type)s\l"
 
     PROPERTY_TEMPLATE = "\t%(name)s\l"