Skip to content

Commit d9bc589

Browse files
committed
Change how object copies are made to prevent unnecessary get_entries calls. Fix #169
1 parent 9ff0e67 commit d9bc589

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

keepmenu/keepmenu.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Read and copy Keepass database entries using dmenu style launchers
22
33
"""
4-
from copy import deepcopy
4+
from copy import copy
55
from dataclasses import dataclass
66
from datetime import datetime, timedelta
77
import errno
@@ -126,13 +126,13 @@ def get_database(open_databases=None, **kwargs):
126126
# Prefer db, autotype, totp passed via cli
127127
db_ = [i for i in open_databases.values() if i.dbase == clidb.dbase]
128128
if db_:
129-
dbs = [deepcopy(db_[0])]
129+
dbs = [copy(db_[0])]
130130
if clidb.atype:
131131
dbs[0].atype = clidb.atype
132132
if clidb.totp:
133133
dbs[0].totp = clidb.totp
134134
else:
135-
dbs = [deepcopy(clidb)]
135+
dbs = [copy(clidb)]
136136
# Use existing keyfile if available
137137
if not dbs[0].kfile and dbs[0].dbase in dbs_cfg_n:
138138
dbs[0].kfile = dbs_cfg[dbs_cfg_n.index(dbs[0].dbase)].kfile
@@ -142,15 +142,15 @@ def get_database(open_databases=None, **kwargs):
142142
elif (clidb.atype or clidb.totp) and open_databases:
143143
# If only autotype or totp is passed, use current db
144144
db_ = [i for i in open_databases.values() if i.is_active is True][0]
145-
dbs = [deepcopy(db_)]
145+
dbs = [copy(db_)]
146146
dbs[0].atype = clidb.atype
147147
dbs[0].totp = clidb.totp
148148
elif open_databases:
149149
# if there are dbs already open, make a list of those + dbs from config.ini
150-
dbs = [deepcopy(i) for i in open_databases.values()]
150+
dbs = [copy(i) for i in open_databases.values()]
151151
for db_ in dbs_cfg:
152152
if db_.dbase not in open_databases:
153-
dbs.append(deepcopy(db_))
153+
dbs.append(copy(db_))
154154
else:
155155
dbs = dbs_cfg
156156
if len(dbs) > 1:
@@ -169,10 +169,12 @@ def get_database(open_databases=None, **kwargs):
169169
dbs[0].pword = get_passphrase()
170170
if dbs[0].pword is None:
171171
return None, open_databases
172+
if dbs[0].kpo is None:
173+
dbs[0].kpo = get_entries(dbs[0])
172174
for db_ in open_databases.values():
173175
db_.is_active = False
174176
if dbs[0].dbase not in open_databases:
175-
open_databases[dbs[0].dbase] = deepcopy(dbs[0])
177+
open_databases[dbs[0].dbase] = copy(dbs[0])
176178
if dbs[0].dbase in dbs_cfg_n:
177179
db_cfg_atype = dbs_cfg[dbs_cfg_n.index(dbs[0].dbase)].atype
178180
open_databases[dbs[0].dbase].atype = db_cfg_atype
@@ -297,8 +299,8 @@ def __init__(self, server, **kwargs):
297299
keepmenu.reload_config(None if cfile is None else expanduser(cfile))
298300
self.server = server
299301
self.database, self.open_databases = get_database(**kwargs)
300-
if self.database:
301-
self.database.kpo = get_entries(self.database)
302+
#if self.database:
303+
# self.database.kpo = get_entries(self.database)
302304
if not self.database or not self.database.kpo:
303305
self.server.kill_flag.set()
304306
sys.exit()
@@ -493,12 +495,13 @@ def menu_open_another_database(self, **kwargs):
493495
Args: kwargs - possibly 'database', 'keyfile', 'autotype', 'totp'
494496
495497
"""
496-
prev_db, prev_open = self.database, deepcopy(self.open_databases)
498+
prev_db, prev_open = self.database, copy(self.open_databases)
497499
self.database, self.open_databases = get_database(self.open_databases, **kwargs)
498500
if self.database is None:
499501
self.database, self.open_databases = prev_db, prev_open
500502
return
501503
if not self.database.kpo:
504+
print("get_entries")
502505
self.database.kpo = get_entries(self.database)
503506
if self.database.kpo is None:
504507
self.database, self.open_databases = prev_db, prev_open

tests/tests.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ def test_open_database(self):
268268
KM.CONF.set('database', 'database_1', db_name)
269269
KM.CONF.write(conf_file)
270270
database, _ = KM.keepmenu.get_database()
271+
database.kpo = None # Can't compare kpo objects
271272
self.assertTrue(database == KM.keepmenu.DataBase(dbase=db_name, pword='password'))
272273
kpo = KM.keepmenu.get_entries(database)
273274
self.assertIsInstance(kpo, PyKeePass)
@@ -277,23 +278,25 @@ def test_open_database(self):
277278
KM.CONF.set('database', 'password_cmd_1', 'echo password')
278279
KM.CONF.write(conf_file)
279280
database, _ = KM.keepmenu.get_database()
281+
database.kpo = None # Can't compare kpo objects
280282
self.assertTrue(database == KM.keepmenu.DataBase(dbase=db_name, pword='password'))
281283
kpo = KM.keepmenu.get_entries(database)
282284
self.assertIsInstance(kpo, PyKeePass)
283285
with open(KM.CONF_FILE, 'w', encoding=KM.ENC) as conf_file:
284286
KM.CONF.set('database', 'autotype_default_1', '{TOTP}{ENTER}')
285287
KM.CONF.write(conf_file)
286288
database, _ = KM.keepmenu.get_database()
289+
database.kpo = None # Can't compare kpo objects
287290
self.assertTrue(database == KM.keepmenu.DataBase(dbase=db_name,
288291
pword='password',
289292
atype='{TOTP}{ENTER}'))
290293

291294
database, _ = KM.keepmenu.get_database(database=db_name)
295+
self.assertIsInstance(database.kpo, PyKeePass)
296+
database.kpo = None # Can't compare DataBase objects with another object in them
292297
self.assertTrue(database == KM.keepmenu.DataBase(dbase=db_name,
293298
pword='password',
294299
atype='{TOTP}{ENTER}'))
295-
kpo = KM.keepmenu.get_entries(database)
296-
self.assertIsInstance(kpo, PyKeePass)
297300

298301
def test_resolve_references(self):
299302
"""Test keepass references can be resolved to values

0 commit comments

Comments
 (0)