gensaschema package

Module contents

Copyright

Copyright 2014 - 2019 André Malo or his licensors, as applicable

License

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

GenSASchema - Static SQLAlchemy Schema Generator

GenSASchema generates static schema definitions using SQLAlchemy’s inspection capabilities.

exception gensaschema.Error

Base exception for this package

__module__ = 'gensaschema._exceptions'
__weakref__

list of weak references to the object (if defined)

exception gensaschema.Warning

Base warning for this package

>>> with _warnings.catch_warnings(record=True) as record:
...     Warning.emit('my message')
...     assert len(record) == 1
...     str(record[0].message)
'my message'
>>> _warnings.simplefilter('error')
>>> Warning.emit('lalala')
Traceback (most recent call last):
...
Warning: lalala
__module__ = 'gensaschema._exceptions'
__weakref__

list of weak references to the object (if defined)

classmethod emit(message, stacklevel=1)

Emit a warning of this very category

This method is pure convenience. It saves you the unfriendly warnings.warn syntax (and the warnings import).

Parameters
messageany

The warning message

stacklevelint

Number of stackframes to go up in order to place the warning source. This is useful for generic warning-emitting helper functions. The stacklevel of this helper function is already taken into account.

class gensaschema.Config(tables, schemas, lines=None)

Schema config container

IVariables
tableslist

Table list

_linessequence

Original config lines (or None)

__dict__ = mappingproxy({'__module__': 'gensaschema._config', '__doc__': '\n Schema config container\n\n :IVariables:\n `tables` : ``list``\n Table list\n\n `_lines` : sequence\n Original config lines (or ``None``)\n ', '_CONFIG_TPL': <gensaschema._template.Template object>, '__init__': <function Config.__init__>, 'from_file': <classmethod object>, 'from_lines': <classmethod object>, 'from_parser': <classmethod object>, 'dump': <function Config.dump>, '__dict__': <attribute '__dict__' of 'Config' objects>, '__weakref__': <attribute '__weakref__' of 'Config' objects>})
__init__(tables, schemas, lines=None)

Initialization

Parameters
tableslist

Table list

schemasdict

(Alien) Schema mapping

linessequence

Original config lines. If omitted or None, the config lines are not available.

__module__ = 'gensaschema._config'
__weakref__

list of weak references to the object (if defined)

dump(fp)

Dump config to a file

Parameters
fpfile

Stream to dump to

classmethod from_file(name_or_file)

Construct from config file

Parameters
name_or_filestr or file

Config filename or file pointer

Return

New Config instance

Rtype

Config

Exceptions
  • IOError : Error reading the file (except for ENOENT, which treats the file as empty)

classmethod from_lines(lines)

Create from config lines

Parameters
linessequence

List of config lines

Return

New Config instance

Rtype

Config

classmethod from_parser(parser, lines=None)

Construct from config parser

Parameters
parserConfigParser.RawConfigParser

Configparser instance

linessequence

Original config lines

Return

New Config instance

Rtype

Config

class gensaschema.Schema(conn, tables, schemas, symbols, dbname=None, types=None)

Schema container

CVariables
_MODULE_TPLTemplate

Template for the module

IVariables
_dialectstr

Dialect name

_tablesTableCollection

Table collection

_schemasdict

Schema -> module mapping

_symbolsSymbols

Symbol table

_dbnamestr or None

DB identifier

__dict__ = mappingproxy({'__module__': 'gensaschema._schema', '__doc__': '\n Schema container\n\n :CVariables:\n `_MODULE_TPL` : ``Template``\n Template for the module\n\n :IVariables:\n `_dialect` : ``str``\n Dialect name\n\n `_tables` : `TableCollection`\n Table collection\n\n `_schemas` : ``dict``\n Schema -> module mapping\n\n `_symbols` : `Symbols`\n Symbol table\n\n `_dbname` : ``str`` or ``None``\n DB identifier\n ', '_MODULE_TPL': <gensaschema._template.Template object>, '__init__': <function Schema.__init__>, 'dump': <function Schema.dump>, '__dict__': <attribute '__dict__' of 'Schema' objects>, '__weakref__': <attribute '__weakref__' of 'Schema' objects>})
__init__(conn, tables, schemas, symbols, dbname=None, types=None)

Initialization

Parameters
connConnection or Engine

SQLAlchemy connection or engine

tableslist

List of tables to reflect, (local name, table name) pairs

schemasdict

schema -> module mapping

symbolsSymbols

Symbol table

dbnamestr

Optional db identifier. Used for informational purposes. If omitted or None, the information just won’t be emitted.

typescallable

Extra type loader. If the type reflection fails, because SQLAlchemy cannot resolve it, the type loader will be called with the type name, (bound) metadata and the symbol table. It is responsible for modifying the symbols and imports and the dialect’s ischema_names. If omitted or None, the reflector will always fail on unknown types.

__module__ = 'gensaschema._schema'
__weakref__

list of weak references to the object (if defined)

dump(fp)

Dump schema module to fp

Parameters
fpfile

File to write to

class gensaschema.Symbols(symbols=None, imports=None)

Symbol table

IVariables
_symbolsdict

Symbols

imports_Imports

Import container

types_Types

Type container

__contains__(name)

Check symbol table entry

Parameters
namestr

Symbol identifier

Return

The symbol

Rtype

str

Exceptions
  • KeyError : Symbol not found

__delitem__(name)

Remove symbol entry if available

__dict__ = mappingproxy({'__module__': 'gensaschema._symbols', '__doc__': '\n Symbol table\n\n :IVariables:\n `_symbols` : ``dict``\n Symbols\n\n `imports` : `_Imports`\n Import container\n\n `types` : `_Types`\n Type container\n ', '__init__': <function Symbols.__init__>, '__delitem__': <function Symbols.__delitem__>, '__setitem__': <function Symbols.__setitem__>, '__getitem__': <function Symbols.__getitem__>, '__contains__': <function Symbols.__contains__>, '__iter__': <function Symbols.__iter__>, '__dict__': <attribute '__dict__' of 'Symbols' objects>, '__weakref__': <attribute '__weakref__' of 'Symbols' objects>})
__getitem__(name)

Get symbol table entry

Parameters
namestr

Symbol identifier

Return

The symbol

Rtype

str

Exceptions
  • KeyError : Symbol not found

__init__(symbols=None, imports=None)

Initialization

Parameters
symbolsdict

Initial symbols

__iter__()

Make item iterator

Return

The iterator

Rtype

iterable

__module__ = 'gensaschema._symbols'
__setitem__(name, symbol)

Set symbol table entry

Parameters
namestr

Symbol identifier

symbolstr

Symbol

Exceptions
  • SymbolException : Symbol could not be set because of some conflict

__weakref__

list of weak references to the object (if defined)

exception gensaschema.SymbolException

Symbol error

__module__ = 'gensaschema._symbols'
__weakref__

list of weak references to the object (if defined)

class gensaschema.Type(ctype, dialect_name, symbols)

Type container

IVariables
_ctypeSA type

Column type

_dialectstr

Dialect name

_symbolsSymbols

Symbol table

__dict__ = mappingproxy({'__module__': 'gensaschema._type', '__doc__': '\n Type container\n\n :IVariables:\n `_ctype` : SA type\n Column type\n\n `_dialect` : ``str``\n Dialect name\n\n `_symbols` : ``Symbols``\n Symbol table\n ', '__init__': <function Type.__init__>, 'by_column': <classmethod object>, '__repr__': <function Type.__repr__>, '__dict__': <attribute '__dict__' of 'Type' objects>, '__weakref__': <attribute '__weakref__' of 'Type' objects>})
__init__(ctype, dialect_name, symbols)

Initialization

Parameters
ctypeSA type

Column type

dialect_namestr

Dialect name

symbolsSymbols

Symbol table

__module__ = 'gensaschema._type'
__repr__()

Make string representation

Return

The string representation

Rtype

str

__weakref__

list of weak references to the object (if defined)

classmethod by_column(column, symbols)

Construct by SA column

Parameters
columnSA column

SA column

Return

New Type instance

Rtype

Type