Builds the library’s database.
Each table that needs to be created has to be implemented by subclassing a TableBuilder. The DatabaseBuilder is the central instance for managing the build process. As the creation of a table can depend on other tables the DatabaseBuilder keeps track of dependencies to process a build in the correct order.
Building is tested on the following storage methods:
The following examples should give a quick view into how to use this package.
Create the DatabaseBuilder object with default settings (read from cjklib.conf or using cjklib.db in same directory as default):
>>> from cjklib import build
>>> dbBuilder = build.DatabaseBuilder(dataPath=['./cjklib/data/'])
Removing conflicting builder(s) 'StrokeCountBuilder' in favour of 'CombinedStrokeCountBuilder'
Removing conflicting builder(s) 'CharacterResidualStrokeCountBuilder' in favour of 'CombinedCharacterResidualStrokeCountBuilder'
Build the table of Jyutping syllables from a csv file:
>>> dbBuilder.build(['JyutpingSyllables'])
building table 'JyutpingSyllables' with builder
'JyutpingSyllablesBuilder'...
Reading table definition from file './cjklib/data/jyutpingsyllables.sql'
Reading table 'JyutpingSyllables' from file
'./cjklib/data/jyutpingsyllables.csv'
Prints the given message to stderr with the system’s default encoding.
Parameter: | message (str) – message to print |
---|
DatabaseBuilder provides the main class for building up a database for the cjklib package.
It contains all TableBuilder classes and a dependency graph to handle build requests.
To modify the behaviour of TableBuilder instances, global or local options can be specified, see getBuilderOptions().
Parameters: |
|
---|---|
Raises ValueError: | |
if two different options from two different builder collide. |
Builds the given tables.
Parameter: | tables (list) – list of tables to build |
---|---|
Raises IOError: | if a table builder fails to read its data; only if noFail is set to False |
Create order in which the tables have to be created.
Parameter: | tableBuilderClasses (list of classobj) – list of TableBuilder classes |
---|---|
Return type: | list of classobj |
Returns: | the given classes ordered in build dependency order |
Gets the name of the tables that needs to be built to resolve dependencies.
Parameter: | tableNames (list of str) – list of tables to build |
---|---|
Return type: | list of str |
Returns: | names of tables needed to resolve dependencies |
Gets a dictionary of options for the given builder that were specified to the DatabaseBuilder.
Options included are global options understood by the builder (e.g. 'dataPath') or local options given in the formats '--BuilderClassName-option' or '--TableName-option'. For example '--Unihan-wideBuild' sets the option 'wideBuild' for all builders providing the Unihan table. '--BuilderClassName-option' has precedence over '--TableName-option'.
Parameters: |
|
---|---|
Return type: | dict |
Returns: | dictionary of options for the given table builder. |
Raises ValueError: | |
if unknown option is specified and ignoreUnknown is False |
Gets the build order for the given table names.
Parameter: | tableNames (list of str) – list of names of tables to build |
---|---|
Return type: | list of classobj |
Returns: | TableBuilder classes in build order |
Raises UnsupportedError: | |
if an unsupported table is given. |
Gets names of tables supported by this instance of the database builder.
This list can have more entries then getSupportedTables() as additional external builders can be supplied on instantiation.
Return type: | list of str |
---|---|
Returns: | names of tables |
Gets the name of the tables that depend on the given tables to be built and are not included in the given set.
Dependencies depend on the choice of table builders and thus may vary.
Parameter: | tableNames (list of str) – list of tables |
---|---|
Return type: | list of str |
Returns: | names of tables that depend on given tables |
Gets the name of the tables that depend on the given tables to be built and already exist similar to getRebuiltDependingTables() but only for tables of attached databases.
Parameter: | tableNames (list of str) – list of tables |
---|---|
Return type: | list of str |
Returns: | names of tables of attached databsaes that need to be rebuilt because of dependencies |
Gets the name of the tables that depend on the given tables to be built and already exist, thus need to be rebuilt.
Parameter: | tableNames (list of str) – list of tables |
---|---|
Return type: | list of str |
Returns: | names of tables that need to be rebuilt because of dependencies |
Gets names of supported tables.
Return type: | list of str |
---|---|
Returns: | names of tables |
Gets the TableBuilder used by this instance of the database builder to build the given table.
Parameter: | tableName (str) – name of table |
---|---|
Return type: | classobj |
Returns: | TableBuilder used to build the given table by this build instance. |
Raises UnsupportedError: | |
if an unsupported table is given. |
Gets all classes in module that implement TableBuilder.
Parameters: |
|
---|---|
Return type: | set |
Returns: | list of all classes inheriting form TableBuilder that provide a table (i.d. non abstract implementations), with its name as key |
Raises ValueError: | |
if two builders are preferred that provide the same table, if two different options with the same name collide |
Checks if the current database supports optimization.
Return type: | boolean |
---|---|
Returns: | True if optimizable, False otherwise |
Returns True if either rebuild is turned on by default or the table does not exist yet in any of the databases.
Parameter: | tableName (str) – table name |
---|---|
Return type: | bool |
Returns: | True, if table needs to be rebuilt |
Optimizes the current database.
Raises Exception: | |
---|---|
if database does not support optimization | |
Raises OperationalError: | |
if optimization failed |
Removes the given tables from the main database.
Parameter: | tables (list) – list of tables to remove |
---|---|
Raises UnsupportedError: | |
if an unsupported table is given. | |
Return type: | list |
Returns: | names of deleted tables, might be smaller than the actual list |
Returns a subset of TableBuilder classes so that every buildable table is only represented by exactly one builder.
Parameters: |
|
---|---|
Return type: | list of classobj |
Returns: | mapping of table names to builder classes that provide the given table |
Raises ValueError: | |
if two builders are preferred that provide the same table |
Sets the options for the given builder that were specified.
Parameters: |
|
---|---|
Raises ValueError: | |
if unknown option is specified |