def __init__(self, dbstate, parent=None): """ Create the top level window from the glade description, and extracts the GTK widgets that are needed. """ CLIDbManager.__init__(self, dbstate) self.glade = Glade(toplevel='dbmanager') self.top = self.glade.toplevel if parent: self.top.set_transient_for(parent) for attr in ['connect', 'cancel', 'new', 'remove', 'copy', 'dblist', 'rename', 'repair', 'rcs', 'msg']: setattr(self, attr, self.glade.get_object(attr)) self.model = None self.column = None self.lock_file = None self.data_to_delete = None self.selection = self.dblist.get_selection() self.__connect_signals() self.__build_interface() self._populate_model()
def __init__(self, uistate, dbstate, parent=None): """ Create the top level window from the glade description, and extracts the GTK widgets that are needed. """ self.uistate = uistate CLIDbManager.__init__(self, dbstate) self.glade = Glade(toplevel='dbmanager') self.top = self.glade.toplevel if parent: self.top.set_transient_for(parent) for attr in [ 'connect', 'cancel', 'new', 'remove', 'info', 'dblist', 'rename', 'repair', 'rcs', 'msg' ]: setattr(self, attr, self.glade.get_object(attr)) self.model = None self.column = None self.lock_file = None self.data_to_delete = None self.selection = self.dblist.get_selection() self.__connect_signals() self.__build_interface() self._populate_model()
def __init__(self, name, include_private=True, include_living=True): """Initialize the database object for family tree `name`. This will raise if the database backend is not `sqlite`. The constructor does not open/lock the database yet. Parameters: - `include_private`: include records marked as private. Default True - `include_living`: include living people. Default True """ self.name = name self.include_private = include_private self.include_living = include_living self.dbstate = DbState() self.dbman = CLIDbManager(self.dbstate) self.user = User() self.smgr = CLIManager(self.dbstate, True, self.user) self.path = self.dbman.get_family_tree_path(name) if not self.path: raise ValueError( "Family tree {} not found. Known trees: {}".format( name, self.dbman.family_tree_list())) self.db_backend = self.get_dbid() if self.db_backend not in ALLOWED_DB_BACKENDS: raise ValueError( "Database backend '{}' of tree '{}' not supported.".format( self.db_backend, name))
def __init__(self, uistate, dbstate, parent=None): """ Create the top level window from the glade description, and extracts the GTK widgets that are needed. """ self.uistate = uistate CLIDbManager.__init__(self, dbstate) self.glade = Glade(toplevel="dbmanager") self.top = self.glade.toplevel if parent: self.top.set_transient_for(parent) for attr in ["connect", "cancel", "new", "remove", "info", "dblist", "rename", "repair", "rcs", "msg"]: setattr(self, attr, self.glade.get_object(attr)) self.model = None self.column = None self.lock_file = None self.data_to_delete = None self.selection = self.dblist.get_selection() self.__connect_signals() self.__build_interface() self._populate_model()
def write(self) -> None: """Write the example DB to a SQLite DB and change the DB path.""" self.tmp_dbdir = tempfile.mkdtemp() self.old_path = getconfig("database.path") setconfig("database.path", self.tmp_dbdir) dbman = CLIDbManager(DbState()) dbman.import_new_db(self.path, User())
def setUp(self): def dummy_callback(dummy): pass self._tmpdir = tempfile.mkdtemp() self._db = DbBsddb() dbman = CLIDbManager(None) self._filename, title = dbman.create_new_db_cli(title="Test") self._db.load(self._filename, dummy_callback, "w")
def setUp(self): def dummy_callback(dummy): pass self.dbstate = DbState() self.dbman = CLIDbManager(self.dbstate) dirpath, name = self.dbman.create_new_db_cli("Test: bsddb", dbid="bsddb") self._db = make_database("bsddb") self._db.load(dirpath, None)
class TestUserCreateOwner(unittest.TestCase): """Test cases for the /api/user/create_owner endpoint.""" def setUp(self): self.name = "Test Web API" self.dbman = CLIDbManager(DbState()) _, _name = self.dbman.create_new_db_cli(self.name, dbid="sqlite") with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): self.app = create_app() self.app.config["TESTING"] = True self.client = self.app.test_client() sqlauth = self.app.config["AUTH_PROVIDER"] sqlauth.create_table() self.ctx = self.app.test_request_context() self.ctx.push() def tearDown(self): self.ctx.pop() self.dbman.remove_database(self.name) def test_create_owner(self): rv = self.client.get(f"{BASE_URL}/token/create_owner/") assert rv.status_code == 200 token = rv.json["access_token"] assert self.app.config["AUTH_PROVIDER"].get_number_users() == 0 # data missing rv = self.client.post( f"{BASE_URL}/users/tree_owner/create_owner/", headers={"Authorization": "Bearer {}".format(token)}, json={"full_name": "My Name"}, ) assert rv.status_code == 422 rv = self.client.post( f"{BASE_URL}/users/tree_owner/create_owner/", headers={"Authorization": "Bearer {}".format(token)}, json={ "password": "******", "email": "*****@*****.**", "full_name": "My Name", }, ) assert rv.status_code == 201 assert self.app.config["AUTH_PROVIDER"].get_number_users() == 1 # try posting again rv = self.client.post( f"{BASE_URL}/users/tree_owner_2/create_owner/", headers={"Authorization": "Bearer {}".format(token)}, json={ "password": "******", "email": "*****@*****.**", "full_name": "My Name", }, ) assert rv.status_code == 405 assert self.app.config["AUTH_PROVIDER"].get_number_users() == 1 rv = self.client.get(f"{BASE_URL}/token/create_owner/") assert rv.status_code == 405
def _get_path(self) -> str: """Get the path of the family tree database.""" dbstate = DbState() # dbstate instance used only for this method dbman = CLIDbManager(dbstate) path = dbman.get_family_tree_path(self.name) if path is None: raise ValueError( "Database path for family tree '{}' not found in databse directory {}" .format(self.name, config.get("database.path"))) return path
def setUp(self): def dummy_callback(dummy): pass self._tmpdir = tempfile.mkdtemp() self._db = DbBsddb() dbman = CLIDbManager(None) self._filename, title = dbman.create_new_db_cli(title=cuni("Test")) self._db.load(self._filename, dummy_callback, "w")
def setUp(self): from gramps.cli.clidbman import CLIDbManager from gramps.gen.config import set as setconfig, get as getconfig from gramps.gen.dbstate import DbState self.newpath = os.path.join(os.path.dirname(__file__), '\u0393\u03c1\u03b1\u03bc\u03c3\u03c0') self.newtitle = 'Gr\u00e4mps T\u00e9st' os.makedirs(self.newpath) self.old_path = getconfig('database.path') setconfig('database.path', self.newpath) self.cli = CLIDbManager(DbState())
def setUp(self): self.name = "Test Web API" self.dbman = CLIDbManager(DbState()) _, _name = self.dbman.create_new_db_cli(self.name, dbid="sqlite") with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): self.app = create_app() self.app.config["TESTING"] = True self.client = self.app.test_client() sqlauth = self.app.config["AUTH_PROVIDER"] sqlauth.create_table() self.ctx = self.app.test_request_context() self.ctx.push()
def __init__(self, name: str = None) -> None: """Prepare and import the example DB.""" ExampleDbBase.__init__(self) self.db_path = os.path.join(os.environ["GRAMPSHOME"], "gramps", "grampsdb") os.makedirs(self.db_path, exist_ok=True) setconfig("database.path", self.db_path) dbstate = DbState() dbman = CLIDbManager(dbstate) user = User() smgr = CLIManager(dbstate, True, user) smgr.do_reg_plugins(dbstate, uistate=None) self.path, self.name = dbman.import_new_db(self.path, User()) WebDbManager.__init__(self, self.name)
class UnicodeTest(unittest.TestCase): def setUp(self): from gramps.cli.clidbman import CLIDbManager from gramps.gen.config import set as setconfig, get as getconfig from gramps.gen.dbstate import DbState self.newpath = os.path.join(os.path.dirname(__file__), '\u0393\u03c1\u03b1\u03bc\u03c3\u03c0') self.newtitle = 'Gr\u00e4mps T\u00e9st' os.makedirs(self.newpath) self.old_path = getconfig('behavior.database-path') setconfig('behavior.database-path', self.newpath) self.cli = CLIDbManager(DbState()) def tearDown(self): from gramps.gen.config import set as setconfig for (dirpath, dirnames, filenames) in os.walk(self.newpath, False): for afile in filenames: os.remove(os.path.join(dirpath, afile)) for adir in dirnames: os.rmdir(os.path.join(dirpath, adir)) os.rmdir(self.newpath) setconfig('behavior.database-path', self.old_path) # Test that clidbman will open files in a path containing # arbitrary Unicode characters. def test4_arbitrary_uncode_path(self): (dbpath, title) = self.cli.create_new_db_cli(self.newtitle) self.assertEqual(self.newpath, os.path.dirname(dbpath), "Compare paths %s and %s" % (repr(self.newpath), repr(dbpath))) self.assertEqual(self.newtitle, title, "Compare titles %s and %s" % (repr(self.newtitle), repr(title)))
class UnicodeTest(unittest.TestCase): def setUp(self): from gramps.cli.clidbman import CLIDbManager from gramps.gen.config import set as setconfig, get as getconfig from gramps.gen.dbstate import DbState self.newpath = os.path.join(os.path.dirname(__file__), '\u0393\u03c1\u03b1\u03bc\u03c3\u03c0') self.newtitle = 'Gr\u00e4mps T\u00e9st' os.makedirs(self.newpath) self.old_path = getconfig('database.path') setconfig('database.path', self.newpath) self.cli = CLIDbManager(DbState()) def tearDown(self): from gramps.gen.config import set as setconfig for (dirpath, dirnames, filenames) in os.walk(self.newpath, False): for afile in filenames: os.remove(os.path.join(dirpath, afile)) for adir in dirnames: os.rmdir(os.path.join(dirpath, adir)) os.rmdir(self.newpath) setconfig('database.path', self.old_path) # Test that clidbman will open files in a path containing # arbitrary Unicode characters. def test4_arbitrary_uncode_path(self): (dbpath, title) = self.cli.create_new_db_cli(self.newtitle) self.assertEqual(self.newpath, os.path.dirname(dbpath), "Compare paths %s and %s" % (repr(self.newpath), repr(dbpath))) self.assertEqual(self.newtitle, title, "Compare titles %s and %s" % (repr(self.newtitle), repr(title)))
def __init__(self, user=None, dbstate=None): ## Setup: from gramps.cli.clidbman import CLIDbManager self.dbstate = dbstate or DbState() #we need a manager for the CLI session self.user = user or User() self.climanager = CLIManager(self.dbstate, setloader=True, user=self.user) self.clidbmanager = CLIDbManager(self.dbstate)
def setUpClass(cls): cls.name = "Test Web API" cls.dbman = CLIDbManager(DbState()) _, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_CONFIG}): cls.app = create_app() cls.app.config["TESTING"] = True cls.client = cls.app.test_client()
def __init__(self, dbstate, engine, onselectcallback): """ The manager is initialised with a dbstate on which GRAMPS is working, and the engine to use context from. """ self.__busy = False self.__onselect = onselectcallback QtCore.QObject.__init__(self) CLIDbManager.__init__(self, dbstate) #constants needed in the QML self.const = { 'titlelabel': "Gramps - %s" % _("Family Trees"), 'addbtnlbl': _("Add a Family Tree"), 'famtreeicon': FAMTREE_ICONPATH } #there is one DeclarativeEngine for global settings self.__build_main_window(engine)
def setUpClass(cls): # print("doing setup with", cls.param) set_det_id(True) # handles must be deterministic for this test cls.dbstate = DbState() cls.dbman = CLIDbManager(cls.dbstate) dirpath, _name = cls.dbman.create_new_db_cli("Test: %s" % cls.param, dbid=cls.param) cls.db = make_database(cls.param) cls.db.load(dirpath, None)
def setUpClass(cls): cls.name = "Test Web API" cls.dbman = CLIDbManager(DbState()) _, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): cls.app = create_app() cls.app.config["TESTING"] = True cls.client = cls.app.test_client() sqlauth = cls.app.config["AUTH_PROVIDER"] sqlauth.create_table() sqlauth.add_user(name="user", password="******")
def setUp(self): from gramps.cli.clidbman import CLIDbManager from gramps.gen.config import set as setconfig, get as getconfig from gramps.gen.dbstate import DbState self.newpath = os.path.join(os.path.dirname(__file__), '\u0393\u03c1\u03b1\u03bc\u03c3\u03c0') self.newtitle = 'Gr\u00e4mps T\u00e9st' os.makedirs(self.newpath) self.old_path = getconfig('behavior.database-path') setconfig('behavior.database-path', self.newpath) self.cli = CLIDbManager(DbState())
def setUp(self): self.name = "Test Web API" self.dbman = CLIDbManager(DbState()) _, _name = self.dbman.create_new_db_cli(self.name, dbid="sqlite") with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): self.app = create_app() self.app.config["TESTING"] = True self.client = self.app.test_client() sqlauth = self.app.config["AUTH_PROVIDER"] sqlauth.create_table() sqlauth.add_user(name="user", password="******", email="*****@*****.**", role=ROLE_MEMBER) sqlauth.add_user(name="owner", password="******", email="*****@*****.**", role=ROLE_OWNER) self.assertTrue(self.app.testing) self.ctx = self.app.test_request_context() self.ctx.push()
def __init__(self, uistate, dbstate, viewmanager, parent=None): """ Create the top level window from the glade description, and extracts the GTK widgets that are needed. """ window_id = self ManagedWindow.__init__(self, uistate, [], window_id, modal=True) CLIDbManager.__init__(self, dbstate) self.glade = Glade(toplevel='dbmanager') self.top = self.glade.toplevel self.set_window(self.top, None, None) self.setup_configs('interface.dbmanager', 780, 350) self.viewmanager = viewmanager for attr in [ 'connect_btn', 'cancel_btn', 'new_btn', 'remove_btn', 'info_btn', 'dblist', 'rename_btn', 'convert_btn', 'repair_btn', 'rcs_btn', 'msg', 'close_btn' ]: setattr(self, attr, self.glade.get_object(attr)) self.model = None self.column = None self.lock_file = None self.data_to_delete = None self.selection = self.dblist.get_selection() # For already loaded database: self._current_node = None self.__connect_signals() self.__build_interface() self._populate_model() self.before_change = "" self.after_change = "" self._select_default() self.user = User(error=ErrorDialog, parent=parent, callback=self.uistate.pulse_progressbar, uistate=self.uistate)
def __init__(self, uistate, dbstate, viewmanager, parent=None): """ Create the top level window from the glade description, and extracts the GTK widgets that are needed. """ self.uistate = uistate CLIDbManager.__init__(self, dbstate) self.glade = Glade(toplevel='dbmanager') self.top = self.glade.toplevel self.viewmanager = viewmanager self.parent = parent if parent: self.top.set_transient_for(parent) for attr in [ 'connect', 'cancel', 'new', 'remove', 'info', 'dblist', 'rename', 'convert', 'repair', 'rcs', 'msg', 'close' ]: setattr(self, attr, self.glade.get_object(attr)) self.model = None self.column = None self.lock_file = None self.data_to_delete = None self.selection = self.dblist.get_selection() # For already loaded database: self._current_node = None self.__connect_signals() self.__build_interface() self._populate_model() self.before_change = "" self.after_change = "" self._select_default() self.user = User(error=ErrorDialog, callback=self.uistate.pulse_progressbar, uistate=self.uistate)
def setUpClass(cls): cls.name = "Test Web API" cls.dbman = CLIDbManager(DbState()) cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): cls.app = create_app() cls.app.config["TESTING"] = True cls.media_base_dir = tempfile.mkdtemp() cls.app.config["MEDIA_BASE_DIR"] = cls.media_base_dir cls.client = cls.app.test_client() sqlauth = cls.app.config["AUTH_PROVIDER"] sqlauth.create_table() sqlauth.add_user(name="user", password="******", role=ROLE_GUEST) sqlauth.add_user(name="admin", password="******", role=ROLE_OWNER)
def __init__(self, uistate, dbstate, viewmanager, parent=None): """ Create the top level window from the glade description, and extracts the GTK widgets that are needed. """ self.uistate = uistate CLIDbManager.__init__(self, dbstate) self.glade = Glade(toplevel='dbmanager') self.top = self.glade.toplevel self.viewmanager = viewmanager self.parent = parent if parent: self.top.set_transient_for(parent) for attr in ['connect', 'cancel', 'new', 'remove', 'info', 'dblist', 'rename', 'convert', 'repair', 'rcs', 'msg', 'close']: setattr(self, attr, self.glade.get_object(attr)) self.model = None self.column = None self.lock_file = None self.data_to_delete = None self.selection = self.dblist.get_selection() # For already loaded database: self._current_node = None self.__connect_signals() self.__build_interface() self._populate_model() self.before_change = "" self.after_change = "" self._select_default() self.user = User(error=ErrorDialog, callback=self.uistate.pulse_progressbar, uistate=self.uistate)
def __init__(self, uistate, dbstate, viewmanager, parent=None): """ Create the top level window from the glade description, and extracts the GTK widgets that are needed. """ window_id = self ManagedWindow.__init__(self, uistate, [], window_id, modal=True) CLIDbManager.__init__(self, dbstate) self.glade = Glade(toplevel='dbmanager') self.top = self.glade.toplevel self.set_window(self.top, None, None) self.setup_configs('interface.dbmanager', 780, 350) self.viewmanager = viewmanager for attr in ['connect_btn', 'cancel_btn', 'new_btn', 'remove_btn', 'info_btn', 'dblist', 'rename_btn', 'convert_btn', 'repair_btn', 'rcs_btn', 'msg', 'close_btn']: setattr(self, attr, self.glade.get_object(attr)) self.model = None self.column = None self.lock_file = None self.data_to_delete = None self.selection = self.dblist.get_selection() # For already loaded database: self._current_node = None self.__connect_signals() self.__build_interface() self._populate_model() self.before_change = "" self.after_change = "" self._select_default() self.user = User(error=ErrorDialog, parent=parent, callback=self.uistate.pulse_progressbar, uistate=self.uistate)
def setUpClass(cls): cls.name = "Test Web API" cls.dbman = CLIDbManager(DbState()) cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): cls.app = create_app() cls.app.config["TESTING"] = True cls.client = cls.app.test_client() sqlauth = cls.app.config["AUTH_PROVIDER"] sqlauth.create_table() sqlauth.add_user(name="user", password="******", role=ROLE_GUEST) sqlauth.add_user(name="contributor", password="******", role=ROLE_CONTRIBUTOR) sqlauth.add_user(name="admin", password="******", role=ROLE_OWNER) sqlauth.add_user(name="editor", password="******", role=ROLE_EDITOR) sqlauth.add_user(name="member", password="******", role=ROLE_MEMBER)
def setUp(self): self.name = "Test Web API" self.dbman = CLIDbManager(DbState()) _, _name = self.dbman.create_new_db_cli(self.name, dbid="sqlite") with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): self.app = create_app() self.app.config["TESTING"] = True self.client = self.app.test_client() sqlauth = self.app.config["AUTH_PROVIDER"] sqlauth.create_table() sqlauth.add_user(name="user", password="******", email="*****@*****.**", role=ROLE_MEMBER) sqlauth.add_user(name="admin", password="******", email="*****@*****.**", role=ROLE_OWNER) self.ctx = self.app.test_request_context() self.ctx.push() rv = self.client.post(BASE_URL + "/token/", json={ "username": "******", "password": "******" }) self.header_member = { "Authorization": f"Bearer {rv.json['access_token']}" } rv = self.client.post(BASE_URL + "/token/", json={ "username": "******", "password": "******" }) self.header_owner = { "Authorization": f"Bearer {rv.json['access_token']}" }
def setUpClass(cls): cls.name = "Test Web API" cls.dbman = CLIDbManager(DbState()) _, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") cls.config_file = tempfile.NamedTemporaryFile(delete=False) cls.user_db = tempfile.NamedTemporaryFile(delete=False) config = """TREE="Test Web API" SECRET_KEY="C2eAhXGrXVe-iljXTjnp4paeRT-m68pq" USER_DB_URI="sqlite:///{}" """.format(cls.user_db.name) with open(cls.config_file.name, "w") as f: f.write(config) with patch.dict("os.environ", {ENV_CONFIG_FILE: cls.config_file.name}): cls.app = create_app() cls.app.config["TESTING"] = True cls.client = cls.app.test_client() cls.runner = CliRunner()
def test_example_db_sqlite(self): """Test the SQLite example DB.""" test_grampshome = tempfile.mkdtemp() os.environ["GRAMPSHOME"] = test_grampshome db = ExampleDbSQLite() self.assertEqual(getconfig("database.path"), db.db_path) dbman = CLIDbManager(DbState()) db_info = dbman.current_names # there is only one DB here self.assertEqual(len(db_info), 1) # DB name self.assertEqual(db_info[0][0], "example") # DB path self.assertEqual(os.path.dirname(db_info[0][1]), db.db_path) # DB cleanup shutil.rmtree(test_grampshome) self.assertTrue(not os.path.exists(test_grampshome))
def test_example_db_sqlite(self): """Test the SQLite example DB.""" db = ExampleDbSQLite() db.write() self.assertEqual(getconfig("database.path"), db.tmp_dbdir) dbman = CLIDbManager(DbState()) db_info = dbman.current_names # there is only one DB here self.assertEqual(len(db_info), 1) # DB name self.assertEqual(db_info[0][0], "example") # DB path self.assertEqual(os.path.dirname(db_info[0][1]), db.tmp_dbdir) db.delete() # just check that this is not at the tmp dir anymore self.assertNotEqual(getconfig("database.path"), db.tmp_dbdir) # ... and that the tmp dir is gone self.assertTrue(not os.path.exists(db.tmp_dbdir))
def setUpClass(cls): cls.name = "Test Web API" cls.dbman = CLIDbManager(DbState()) _, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): cls.app = create_app() cls.app.config["TESTING"] = True cls.client = cls.app.test_client() sqlauth = cls.app.config["AUTH_PROVIDER"] sqlauth.create_table() sqlauth.add_user(name="user", password="******", role=ROLE_GUEST) sqlauth.add_user(name="admin", password="******", role=ROLE_OWNER) dbstate = cls.app.config["DB_MANAGER"].get_db(force_unlock=True) with DbTxn("Add test objects", dbstate.db) as trans: _add_person(Person.MALE, "John", "Allen", trans, dbstate.db) _add_person( Person.FEMALE, "Jane", "Secret", trans, dbstate.db, private=True ) dbstate.db.close()
def setUpClass(cls): """Test class setup.""" cls.name = "empty" cls.dbman = CLIDbManager(DbState()) _, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") with patch.dict( "os.environ", { ENV_CONFIG_FILE: TEST_EMPTY_GRAMPS_AUTH_CONFIG, "TREE": "empty", }, ): cls.test_app = create_app() cls.test_app.config["TESTING"] = True cls.client = cls.test_app.test_client() sqlauth = cls.test_app.config["AUTH_PROVIDER"] sqlauth.create_table() for role in TEST_USERS: sqlauth.add_user( name=TEST_USERS[role]["name"], password=TEST_USERS[role]["password"], role=role, )
class GrampsDbBaseTest(unittest.TestCase): """Base class for unittest that need to be able to create test databases.""" def setUp(self): def dummy_callback(dummy): pass self.dbstate = DbState() self.dbman = CLIDbManager(self.dbstate) dirpath, name = self.dbman.create_new_db_cli("Test: bsddb", dbid="bsddb") self._db = make_database("bsddb") self._db.load(dirpath, None) def tearDown(self): self._db.close() self.dbman.remove_database("Test: bsddb") def _populate_database( self, num_sources=1, num_persons=0, num_families=0, num_events=0, num_places=0, num_media=0, num_links=1 ): # start with sources sources = [] for i in range(num_sources): sources.append(self._add_source()) # now for each of the other tables. Give each entry a link # to num_link sources, sources are chosen on a round robin # basis for num, add_func in ( (num_persons, self._add_person_with_sources), (num_families, self._add_family_with_sources), (num_events, self._add_event_with_sources), (num_places, self._add_place_with_sources), (num_media, self._add_media_with_sources), ): source_idx = 1 for person_idx in range(num): # Get the list of sources to link lnk_sources = set() for i in range(num_links): lnk_sources.add(sources[source_idx - 1]) source_idx = (source_idx + 1) % len(sources) try: add_func(lnk_sources) except: print("person_idx = ", person_idx) print("lnk_sources = ", repr(lnk_sources)) raise return def _add_source(self, repos=None): # Add a Source with DbTxn("Add Source and Citation", self._db) as tran: source = Source() if repos is not None: repo_ref = RepoRef() repo_ref.set_reference_handle(repos.get_handle()) source.add_repo_reference(repo_ref) self._db.add_source(source, tran) self._db.commit_source(source, tran) citation = Citation() citation.set_reference_handle(source.get_handle()) self._db.add_citation(citation, tran) self._db.commit_citation(citation, tran) return citation def _add_repository(self): # Add a Repository with DbTxn("Add Repository", self._db) as tran: repos = Repository() self._db.add_repository(repos, tran) self._db.commit_repository(repos, tran) return repos def _add_object_with_source(self, citations, object_class, add_method, commit_method): object = object_class() with DbTxn("Add Object", self._db) as tran: for citation in citations: object.add_citation(citation.get_handle()) add_method(object, tran) commit_method(object, tran) return object def _add_person_with_sources(self, citations): return self._add_object_with_source(citations, Person, self._db.add_person, self._db.commit_person) def _add_family_with_sources(self, citations): return self._add_object_with_source(citations, Family, self._db.add_family, self._db.commit_family) def _add_event_with_sources(self, citations): return self._add_object_with_source(citations, Event, self._db.add_event, self._db.commit_event) def _add_place_with_sources(self, citations): return self._add_object_with_source(citations, Place, self._db.add_place, self._db.commit_place) def _add_media_with_sources(self, citations): return self._add_object_with_source(citations, Media, self._db.add_media, self._db.commit_media)
def get(self) -> Response: """Get active database and application related metadata information.""" catalog = GRAMPS_LOCALE.get_language_dict() for entry in catalog: if catalog[entry] == GRAMPS_LOCALE.language[0]: language_name = entry break db_handle = self.db_handle db_name = db_handle.get_dbname() db_type = "Unknown" dbstate = DbState() db_summary = CLIDbManager(dbstate).family_tree_summary( database_names=[db_name]) if len(db_summary) == 1: db_key = GRAMPS_LOCALE.translation.sgettext("Database") for key in db_summary[0]: if key == db_key: db_type = db_summary[0][key] result = { "database": { "id": db_handle.get_dbid(), "name": db_name, "type": db_type, }, "default_person": db_handle.get_default_handle(), "gramps": { "version": ENV["VERSION"], }, "gramps_webapi": { "schema": VERSION, "version": VERSION, }, "locale": { "lang": GRAMPS_LOCALE.lang, "language": GRAMPS_LOCALE.language[0], "description": language_name, "incomplete_translation": bool(GRAMPS_LOCALE.language[0] in INCOMPLETE_TRANSLATIONS), }, "object_counts": { "people": db_handle.get_number_of_people(), "families": db_handle.get_number_of_families(), "sources": db_handle.get_number_of_sources(), "citations": db_handle.get_number_of_citations(), "events": db_handle.get_number_of_events(), "media": db_handle.get_number_of_media(), "places": db_handle.get_number_of_places(), "repositories": db_handle.get_number_of_repositories(), "notes": db_handle.get_number_of_notes(), "tags": db_handle.get_number_of_tags(), }, "researcher": db_handle.get_researcher(), "surnames": db_handle.get_surname_list(), } data = db_handle.get_summary() db_version_key = GRAMPS_LOCALE.translation.sgettext("Database version") db_module_key = GRAMPS_LOCALE.translation.sgettext( "Database module version") db_schema_key = GRAMPS_LOCALE.translation.sgettext("Schema version") for item in data: if item == db_version_key: result["database"]["version"] = data[item] elif item == db_module_key: result["database"]["module"] = data[item] elif item == db_schema_key: result["database"]["schema"] = data[item] return self.response(200, result)
db.transaction_commit(tran) tran = None def print_db_content(db): for h in db.get_person_handles(): print("DB contains: person %s" % h) for h in db.get_source_handles(): print("DB contains: source %s" % h) tmpdir = tempfile.mkdtemp() try: filename1 = os.path.join(tmpdir,'test1.grdb') filename2 = os.path.join(tmpdir,'test2.grdb') print("\nUsing Database file: %s" % filename1) dbstate = DbState() dbman = CLIDbManager(dbstate) dirpath, name = dbman.create_new_db_cli(filename1, dbid="bsddb") db = make_database("bsddb") db.load(dirpath, None) print("Add person 1") add_person( db,"Anton", "Albers",True,False) print("Add source") add_source( db,"A short test",True,False) print("Add person 2 without commit") add_person( db,"Bernd","Beta",False,False) print("Add source") add_source( db,"A short test",True,False) print("Add person 3") add_person( db,"Chris","Connor",True,False) print_db_content( db) print("Closing Database file: %s" % filename1)
def setUpClass(cls): cls.name = "Test Web Db Manager" cls.dbman = CLIDbManager(DbState()) dirpath, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") cls.db = make_database("sqlite")
class Db(): """Class for database handling.""" def __init__(self, name, include_private=True, include_living=True): """Initialize the database object for family tree `name`. This will raise if the database backend is not `sqlite`. The constructor does not open/lock the database yet. Parameters: - `include_private`: include records marked as private. Default True - `include_living`: include living people. Default True """ self.name = name self.include_private = include_private self.include_living = include_living self.dbstate = DbState() self.dbman = CLIDbManager(self.dbstate) self.user = User() self.smgr = CLIManager(self.dbstate, True, self.user) self.path = self.dbman.get_family_tree_path(name) if not self.path: raise ValueError( "Family tree {} not found. Known trees: {}".format( name, self.dbman.family_tree_list())) self.db_backend = self.get_dbid() if self.db_backend not in ALLOWED_DB_BACKENDS: raise ValueError( "Database backend '{}' of tree '{}' not supported.".format( self.db_backend, name)) @property def db(self): """Return the database or a proxy database.""" _db = self.dbstate.db if not self.include_private: _db = PrivateProxyDb(_db) if not self.include_living: _db = LivingProxyDb(_db, LivingProxyDb.MODE_INCLUDE_FULL_NAME_ONLY) return _db def get_dbid(self): """Get the database backend.""" return get_dbid_from_path(self.path) def is_locked(self): """Returns a boolean whether the database is locked.""" return os.path.isfile(os.path.join(self.path, "lock")) def open(self, force=False): """Open the database. If `force` is `True`, will break an existing lock (use with care!). """ if force: self.dbman.break_lock(self.path) return self.smgr.open_activate(self.path) def close(self, *args, **kwargs): """Close the database (if it is open).""" if self.dbstate.is_open(): return self.dbstate.db.close(*args, **kwargs)