gensaschema package

Submodules

Module contents

Copyright:

Copyright 2014 - 2023 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.

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

Schema config container

tables

Table list

Type:

list

schemas

Alien schema mapping

Type:

dict

_lines

Original config lines (or None)

Type:

list

_CONFIG_TPL = <gensaschema._template.Template object>

Template for empty config file

Type:

Template

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

Initialization

Parameters:
  • tables (list) – Table list

  • schemas (dict) – (Alien) Schema mapping

  • lines (iterable) – 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)[source]

Dump config to a file

Parameters:

fp (file) – Stream to dump to

classmethod from_file(name_or_file)[source]

Construct from config file

Parameters:

name_or_file (str or file) – Config filename or file pointer

Returns:

New Config instance

Return type:

Config

Raises:

IOError – Error reading the file (except for ENOENT, which treats the file as empty)

classmethod from_lines(lines)[source]

Create from config lines

Parameters:

lines (iterable) – List of config lines

Returns:

New Config instance

Return type:

Config

classmethod from_parser(parser, lines=None)[source]

Construct from config parser

Parameters:
  • parser (ConfigParser.RawConfigParser) – Configparser instance

  • lines (iterable) – Original config lines

Returns:

New Config instance

Return type:

Config

exception gensaschema.Error[source]

Base exception for this package

__module__ = 'gensaschema._exceptions'
__weakref__

list of weak references to the object (if defined)

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

Schema container

_dialect

Dialect name

Type:

str

_tables

Table collection

Type:

TableCollection

_schemas

Schema -> module mapping

Type:

dict

_symbols

Symbol table

Type:

gensaschema.Symbols

_dbname

DB identifier

Type:

str or None

_MODULE_TPL = <gensaschema._template.Template object>

Template for the module

Type:

Template

__dict__ = mappingproxy({'__module__': 'gensaschema._schema', '__doc__': '\n    Schema container\n\n    Attributes:\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>, '__annotations__': {}})
__init__(conn, tables, schemas, symbols, dbname=None, types=None)[source]

Initialization

Parameters:
  • conn (Connection or Engine) – SQLAlchemy connection or engine

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

  • schemas (dict) – schema -> module mapping

  • symbols (gensaschema.Symbols) – Symbol table

  • dbname (str) – Optional db identifier. Used for informational purposes. If omitted or None, the information just won’t be emitted.

  • types (callable) – 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)[source]

Dump schema module to fp

Parameters:

fp (file) – File to write to

exception gensaschema.SymbolException[source]

Symbol error

__annotations__ = {}
__module__ = 'gensaschema._symbols'
class gensaschema.Symbols(symbols=None, imports=None)[source]

Symbol table

_symbols

Symbols

Type:

dict

imports

Import container

Type:

_Imports

types

Type container

Type:

_Types

__contains__(name)[source]

Check symbol table entry

Parameters:

name (str) – Symbol identifier

Returns:

Does the symbol entry exist?

Return type:

bool

__delitem__(name)[source]

Remove symbol entry if available

Parameters:

name (str) – Symbol identifier

__dict__ = mappingproxy({'__module__': 'gensaschema._symbols', '__doc__': '\n    Symbol table\n\n    Attributes:\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>, '__annotations__': {}})
__getitem__(name)[source]

Get symbol table entry

Parameters:

name (str) – Symbol identifier

Returns:

The symbol

Return type:

str

Raises:

KeyError – Symbol not found

__init__(symbols=None, imports=None)[source]

Initialization

Parameters:
  • symbols (dict) – Initial symbols

  • imports (iterable) – List of initial (id, import statement) tuples. If omitted or None, it’s empty.

__iter__()[source]

Make item iterator (id, value)

Returns:

The iterator

Return type:

iterable

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

Set symbol table entry

Parameters:
  • name (str) – Symbol identifier

  • symbol (str) – Symbol

Raises:

SymbolException – Symbol could not be set because of some conflict

__weakref__

list of weak references to the object (if defined)

class gensaschema.Type(ctype, dialect_name, symbols)[source]

Type container

_ctype

Column type

Type:

SA type

_dialect

Dialect name

Type:

str

_symbols

Symbol table

Type:

gensaschema.Symbols

__dict__ = mappingproxy({'__module__': 'gensaschema._type', '__doc__': '\n    Type container\n\n    Attributes:\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(<function Type.by_column>)>, '__repr__': <function Type.__repr__>, '__dict__': <attribute '__dict__' of 'Type' objects>, '__weakref__': <attribute '__weakref__' of 'Type' objects>, '__annotations__': {}})
__init__(ctype, dialect_name, symbols)[source]

Initialization

Parameters:
  • ctype (SA type) – Column type

  • dialect_name (str) – Dialect name

  • symbols (gensaschema.Symbols) – Symbol table

__module__ = 'gensaschema._type'
__repr__()[source]

Make string representation

Returns:

The string representation

Return type:

str

__weakref__

list of weak references to the object (if defined)

classmethod by_column(column, symbols)[source]

Construct by SA column

Parameters:

column (SA column) – SA column

Returns:

New Type instance

Return type:

Type

exception gensaschema.Warning[source]

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)[source]

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.