Skip to content

Commit

Permalink
screens: Continue on open file cancel. (#183)
Browse files Browse the repository at this point in the history
Rather than throwing an exception when restoring a wallet from file,
just return, allowing the user to do something else.

Create a logging helper for errors that logs to stdErr and the main ui
window.

Do not move a user's wallet file without warning. Copy it instead.

Check that the file is a sqlite database, otherwise you get stuck in a
loop until you delete or move the file manually.
  • Loading branch information
JoeGruffins authored May 1, 2020
1 parent defd011 commit ecdefe7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
19 changes: 19 additions & 0 deletions decred/decred/util/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ class NoValueError(DecredError):
KVFirst = "SELECT k, v FROM {tablename} ORDER BY k LIMIT 1;"


def isSqlite3DB(filepath):
"""
Returns whether file at filepath is a sqlite3 database.
Args:
filepath (str): The file to check.
Returns:
bool: Whether the database could be opened and queried.
"""
try:
conn = sqlite3.connect(filepath)
conn.execute("pragma schema_version;")
except Exception:
return False
conn.close()
return True


class KeyValueDatabase:
"""
A KeyValueDatabase is a sqlite3 database specialized for two-column tables
Expand Down
32 changes: 24 additions & 8 deletions tinywallet/tinywallet/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@
app = None


def logError(err):
"""
Log an error to stderr and to the ui main window.
Args:
err (str): The error to log and display.
"""
log.error(err)
app.appWindow.showError(err)


def openInBrowser(url):
"""
Open a URL in the user's browser.
Expand Down Expand Up @@ -971,26 +982,31 @@ def loadClicked(self):
fd.setViewMode(QtWidgets.QFileDialog.Detail)
qdir = QtCore.QDir
fd.setFilter(qdir.Dirs | qdir.Files | qdir.NoDotAndDotDot | qdir.Hidden)
noFileMsg = "no file selected"
if fd.exec_():
fileNames = fd.selectedFiles()
if len(fileNames) != 1:
msg = "more than one file selected for importing"
log.error(msg)
raise Exception(msg)
logError(msg)
return
else:
raise Exception("no file selected")
log.info(noFileMsg)
return
walletPath = fileNames[0]
log.debug("loading wallet from %r" % walletPath)
log.debug(f"loading wallet from {walletPath}")
if walletPath == "":
app.appWindow.showError("no file selected")
logError(noFileMsg)
return
elif not os.path.isfile(walletPath):
log.error("no file found at %s" % walletPath)
app.showMessaage("file error. try again")
msg = f"no file found at {walletPath}"
logError(msg)
return
elif not database.isSqlite3DB(walletPath):
logError(f"{walletPath} is not a sqlite3 database")
return

destination = app.walletFilename()
shutil.move(walletPath, destination)
shutil.copy(walletPath, destination)
app.initialize()

def restoreClicked(self):
Expand Down

0 comments on commit ecdefe7

Please sign in to comment.