Commits

Anonymous committed f615c46 Merge

merged from original

  • Participants
  • Parent commits fbf74ef, 7364ad3

Comments (0)

Files changed (5)

+db: sample
+host: localhost
+passwd: passwd
+user: user1
      install_requires=[
         'setuptools',
         'sqlalchemy',
+        'pyyaml',
          # -*- Extra requirements: -*-
      ],
      extras_require=dict(

src/schema2rst/metadata.py

 # -*- coding: utf-8 -*-
 
 import os
+import re
 import sqlalchemy
 from operator import attrgetter
 
 
+def decode(string):
+    if string:
+        return string.decode('utf-8')
+    else:
+        return string
+
+
 class MySQLMetaData:
     def __init__(self):
         self.meta = sqlalchemy.MetaData()
     def reflect(self, engine):
         self.engine = engine
 
+        schema_name = os.path.basename(str(self.engine.url))
+        query = """SELECT TABLE_COMMENT
+                   FROM information_schema.Tables
+                   WHERE TABLE_SCHEMA = '%s' AND
+                         TABLE_NAME = '%s'""" % \
+                   (schema_name, self.name)
+        rs = self.engine.execute(query)
+        row = rs.fetchone()
+        self.fullname = decode(re.sub('(; )?InnoDB free.*$', '', row[0]))
+
     @property
     def name(self):
         return self.meta.name
 
     @property
+    def fullname(self):
+        return self.fullname
+
+    @property
     def columns(self):
         for column in self.meta.columns:
             column = MySQLColumn(column)
 class MySQLColumn:
     def __init__(self, meta):
         self.meta = meta
-        self.comment = None
 
     def reflect(self, engine):
         self.engine = engine
 
         schema_name = os.path.basename(str(self.engine.url))
-        query = """SELECT COLUMN_COMMENT, COLUMN_DEFAULT
+        query = """SELECT COLUMN_COMMENT, COLUMN_DEFAULT, COLLATION_NAME, EXTRA
                    FROM information_schema.Columns
                    WHERE TABLE_SCHEMA = '%s' AND
                          TABLE_NAME = '%s' AND
                    (schema_name, self.meta.table.name, self.name)
         rs = self.engine.execute(query)
         row = rs.fetchone()
-        self.comment = row[0]
-        self.default = row[1]
+        comment = decode(row[0])
+        self._default = decode(row[1])
+        self._collation_name = row[2]
+        self._extra = row[3]
+
+        match = re.match('^(.*?)(?:\(|��)(.*)(?:\)|��)\s*$', comment)
+        if match:
+            self._fullname = match.group(1)
+            self._comment = match.group(2)
+        else:
+            self._fullname = comment
+            self._comment = ''
 
     @property
     def fullname(self):
-        return self.comment or self.meta.name
+        return self._fullname or self.meta.name
 
     @property
     def name(self):
 
     @property
     def default(self):
-        return self.default
+        return self._default
 
     @property
     def doc(self):
-        return self.meta.doc or ""
+        options = []
+
+        if self._collation_name and self._collation_name != 'utf8_general_ci':
+            options.append(self._collation_name)
+        if self._extra:
+            options.append(self._extra)
+
+        if self._comment and options:
+            return "%s (%s)" % (self.comment, ", ".join(options))
+        elif options:
+            return ", ".join(options)
+        else:
+            return self._comment

src/schema2rst/schema2rst.py

 # -*- coding: utf-8 -*-
 
 import os
+import sys
+import yaml
 import sqlalchemy
 from metadata import MySQLMetaData
 from sphinx import SphinxDocGenerator
-from pit import Pit
 
 
 def main():
-    config = Pit.get('example', {'require': {'host': 'localhost',
-                                              'user': 'gmappers',
-                                              'passwd': '',
-                                              'db': 'gmappers'}})
+    if len(sys.argv) != 2:
+        sys.stderr.write('Usage: schema2rst CONFIG_FILE\n')
+        sys.exit(1)
+
+    config = yaml.load(file(sys.argv[1]))
 
     url = 'mysql://%(user)s:%(passwd)s@%(host)s/%(db)s' % config
     engine = sqlalchemy.create_engine(url)
     sphinx.header(u'Schema: %s' % config['db'])
 
     for table in m.tables.values():
-        sphinx.header(table.name, '-')
+        if table.fullname:
+            header = u"%s (%s)" % (table.fullname, table.name)
+            sphinx.header(header, '-')
+        else:
+            sphinx.header(table.name, '-')
         sphinx.listtable(m.headers)
         for c in table.columns:
             sphinx.listtable_column(m.columns(c))

src/schema2rst/sphinx.py

         self.output = output or sys.stdout
 
     def out(self, string):
-        self.output.write(str(string) + "\n")
+        self.output.write(string.encode('utf-8') + "\n")
 
     def header(self, string, char="="):
         self.out("")
     def listtable_column(self, columns):
         for i, column in enumerate(columns):
             if i == 0:
-                self.out("   * - %s" % str(column))
+                self.out("   * - %s" % column)
             else:
-                self.out("     - %s" % str(column))
+                self.out("     - %s" % column)