import pgutil script_name = os.path.basename(__file__) def usage(): print( "Usage: {} [-f conf_file] [-o db_order] [-i info_file] name short_desc dict_file".format(script_name), file=sys.stderr, ) print("Imports a bedic dictionary into pgsql.", file=sys.stderr) options, (name, short_desc, dict_file) = pgutil.get_pgsql_params("o:i:", 3, 3, usage) db_order = options.get("-o") if db_order is not None: db_order = int(db_order) info_file = options.get("-i") if info_file is not None: with open(info_file, encoding="utf-8") as f: info = f.read() else: info = None defs = []
script_name = os.path.basename(__file__) def usage(): print("Usage: {} [-f conf_file] [-o db_order] [-i info_file] virt_name short_desc dict_name dict_name [...]", file=sys.stderr) print("Adds virtual dictionaries in pgsql.", file=sys.stderr) def add_vdict(cur, schema, db_order, virt_name, short_desc, info, dict_names): cur.execute(insert_dictionary.format(schema), (db_order, virt_name, short_desc, info)) cur.execute(select_virt_id.format(schema), (virt_name, )) virt_id = cur.fetchone()[0] cur.execute(prepare_insert_virtual_dictionary.format(schema), (virt_id, )) for dict_name in dict_names: cur.execute(execute_insert_virtual_dictionary, (dict_name, )) options, args = pgutil.get_pgsql_params("o:i:", 4, None, usage) db_order = options.get("-o") if db_order is not None: db_order = int(db_order) info_file = options.get("-i") virt_name, short_desc, *dict_names = args if info_file is not None: with open(info_file, encoding="utf-8") as f: info = f.read() else: info = None
def read_def(f, offset, size): f.seek(offset) raw_def = bytes() while len(raw_def) < size: raw_def += f.read(size - len(raw_def)) definition = raw_def.decode("utf-8") return definition.replace("\r", "") def is_special(word): return word.startswith("00-database-") options, (name, index_file, dict_file) = pgutil.get_pgsql_params("o:", 3, 3, usage) db_order = options.get("-o") if db_order is not None: db_order = int(db_order) short_desc = None info = None defs = [] with open(index_file, encoding="utf-8") as index: with open(dict_file, "rb") as data: for entry in index: word, offset, size = entry.strip().split("\t") offset = decode(offset)
alter_dict_id_seq = "ALTER SEQUENCE {0}.dictionaries_dict_id_seq OWNED BY {0}.dictionaries.dict_id;" alter_virt_id_seq = "ALTER SEQUENCE {0}.dictionaries_virt_id_seq OWNED BY {0}.dictionaries.virt_id;" create_definitions_dict_id_word_idx = "CREATE INDEX definitions_dict_id_word_idx ON {}.definitions (dict_id, word);" script_name = os.path.basename(__file__) def usage(): print("Usage: {} [-f conf_file]".format(script_name), file=sys.stderr) print("Initializes a wordbase pgsql schema.", file=sys.stderr) def init_pgsql_task(cur, schema): if schema != "public": cur.execute(create_schema.format(schema)) cur.execute(create_dict_id_seq.format(schema)) cur.execute(create_virt_id_seq.format(schema)) cur.execute(create_dictionaries.format(schema)) cur.execute(create_definitions.format(schema)) cur.execute(create_virtual_dictionaries.format(schema)) cur.execute(alter_dict_id_seq.format(schema)) cur.execute(alter_virt_id_seq.format(schema)) cur.execute(create_definitions_dict_id_word_idx.format(schema)) pgutil.get_pgsql_params(None, 0, 0, usage) pgutil.process_pgsql_task(init_pgsql_task)
import sys import os import pgutil delete_dictionary = "DELETE FROM {}.dictionaries WHERE name IN %s;" script_name = os.path.basename(__file__) def usage(): print("Usage: {} [-f conf_file] dict [...]".format(script_name), file=sys.stderr) print("Removes wordbase dictionaries from pgsql.", file=sys.stderr) def del_pgsql_task(cur, schema, dict_names): cur.execute(delete_dictionary.format(schema), (tuple(dict_names), )) options, dict_names = pgutil.get_pgsql_params(None, 1, None, usage) del options if not len(dict_names): usage(2) sys.exit(2) pgutil.process_pgsql_task(del_pgsql_task, dict_names)
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import sys import os import pgutil STOP_DB_NAME = "--exit--" STOP_DB_DESCRIPTION = "Stop default search here." script_name = os.path.basename(__file__) def usage(): print("Usage: {} [-f conf_file] db_order", file=sys.stderr) print("Adds a search-stopping meta-dictionary marker in pgsql.", file=sys.stderr) options, (db_order,) = pgutil.get_pgsql_params(None, 1, 1, usage) db_order = int(db_order) pgutil.process_pgsql_task(pgutil.import_task, db_order, STOP_DB_NAME, STOP_DB_DESCRIPTION, None, [], True)