Skip to content

References

Documentation extracted from the docstrings.

Fbexport Tool

A tool to export data from a firebird database in bulk. Run fbexport-tool.py -h to see details on how to use.

The input to this program is an fdb database and a text file with a list of space separated names of tables to be exported or a list of the names of such tables.

This is a thin wrap around fbexport, so you will need to install it before. In case your OS does not ship with this programm, see

http://www.firebirdfaq.org/fbexport.php

export_cmd(database, destination, table, password='masterkey')

return a fbexport statement suitable for use in the terminal.

Parameters

@param database: Database location or path. @password @destination: Output location or path. @table: The name of the table to export.

Returns

A string representing the command that should be executed to commit the export.

Example

export_cmd('database.fbd', destination='database.csv', table='MYTABLE') ['fbexport', '-H', '', '-Sc', '-J', 'Y-M-D', '-D', 'database.fbd', '-F', 'database.csv', '-V', 'MYTABLE']

Source code in fbexport_tool.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def export_cmd(
    database: str, destination: str, table: str, password: str = "masterkey"
) -> list[str]:
    """
    return a `fbexport` statement suitable for use in the terminal.

    Parameters

    @param database: Database location or path.
    @password
    @destination: Output location or path.
    @table: The name of the table to export.

    Returns

    A string representing the command that should be executed to commit the export.

    Example

    >>> export_cmd('database.fbd', destination='database.csv', table='MYTABLE')
    ['fbexport', '-H', '', '-Sc', '-J', 'Y-M-D', '-D', 'database.fbd', '-F', 'database.csv', '-V', 'MYTABLE']
    """
    return [
        "fbexport",
        "-H",
        "",
        "-Sc",
        "-J",
        "Y-M-D",
        "-D",
        database,
        "-F",
        destination,
        "-V",
        table,
    ]

iter_words(stream)

Return an iterator to the words contained in stream.

Example

from io import StringIO buffer = StringIO(initial_value=' word1 word2 \n word3 ') [w for w in iter_words(buffer)] ['word1', 'word2', 'word3']

Source code in fbexport_tool.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def iter_words(stream: io.TextIOWrapper) -> Iterator[str]:
    """
    Return an iterator to the words contained in `stream`.

    Example

    >>> from io import StringIO
    >>> buffer = StringIO(initial_value=' word1 word2 \\n word3 ')
    >>> [w for w in iter_words(buffer)]
    ['word1', 'word2', 'word3']
    """
    for line in stream:
        for word in line.split():
            yield word

outputdir_filename(database, when)

Returns the name of the output directory in format YYYY-MM-DD-hh-mm-ss-database with the database suffix removed. If the database name has spaces they are replaced by underscores (i.e. "database name".fdb is replaced by database_name)

Example

from datetime import datetime when = datetime(2023, 1, 1, 14,0,0) outputdir_filename('database.fdb', when) '2023-01-01-14-00-00-database'

outputdir_filename('another database.fdb', when) '2023-01-01-14-00-00-another_database'

Source code in fbexport_tool.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def outputdir_filename(database: Path | str, when: datetime) -> str:
    """
    Returns the name of the output directory in format YYYY-MM-DD-hh-mm-ss-database
    with the database suffix removed.
    If the database name has spaces they are replaced by underscores (i.e. `"database name".fdb` is replaced by `database_name`)

    Example

    >>> from datetime import datetime
    >>> when = datetime(2023, 1, 1, 14,0,0)
    >>> outputdir_filename('database.fdb', when)
    '2023-01-01-14-00-00-database'

    >>> outputdir_filename('another database.fdb', when)
    '2023-01-01-14-00-00-another_database'
    """
    if isinstance(database, str):
        database = Path(database)
    dtime = when.strftime("%Y-%m-%d-%H-%M-%S")
    fname = database.stem.replace(" ", "_")
    return f"{dtime}-{fname}"

table_csv_filename(tablename)

Returns a sanitized version of the name of a table. The name is converted to lowercase, any space is replaced by underscores and special caracters like quotes are stripped away.

Examples

table_csv_filename('TABLE') 'table.csv'

table_csv_filename('A TABLE') 'a_table.csv'

table_csv_filename('"Decimal"') 'decimal.csv'

Source code in fbexport_tool.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def table_csv_filename(tablename: str) -> str:
    """
    Returns a sanitized version of the name of a table. The name is converted to
    lowercase, any space is replaced by underscores and special caracters like
    quotes are stripped away.

    Examples

    >>> table_csv_filename('TABLE')
    'table.csv'

    >>> table_csv_filename('A TABLE')
    'a_table.csv'

    >>> table_csv_filename('"Decimal"')
    'decimal.csv'
    """
    translation_table = str.maketrans("", "", string.punctuation)
    cleaned = tablename.translate(translation_table)
    return cleaned.replace(" ", "_").lower() + ".csv"
    return tablename