Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Viewmanager.py: add check disk space #1212

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions data/tips.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@

<tip number="20"><b>Unsure of a Date?</b><br/>If you're unsure about the date an event occurred, Gramps allows you to enter a wide range of date formats based on a guess or an estimate. For instance, &quot;about 1908&quot; is a valid entry for a birth date in Gramps. Click the Date button next to the date field and see the Gramps Manual to learn more.</tip>

<tip number="21"><b>Duplicate Entries</b><br/>&quot;Tools &gt; Database Processing &gt; Find Possible Duplicate People...&quot; allows you to locate (and merge) entries of the same person entered more than once in the database.</tip>
<tip number="21"><b>Duplicate Entries</b><br/>The function &quot;Tools &gt; Family Tree Processing &gt; Find Possible Duplicate People...&quot; allows you to locate (and merge) entries of the same person entered more than once in the database.</tip>

<tip number="22"><b>Merging Entries</b><br/>The function &quot;Edit &gt; Compare and Merge...&quot; allows you to combine separately listed people into one. Select the second entry by holding the Control key as you click. This is very useful for combining two databases with overlapping people, or combining erroneously entered differing names for one individual. This also works for the Places, Sources and Repositories views.</tip>
<tip number="22"><b>Merging Entries</b><br/>The function &quot;Edit &gt; Merge...&quot; allows you to combine separately listed people into one. Select the second entry by holding the Control key as you click. This is very useful for combining two databases with overlapping people, or combining erroneously entered differing names for one individual. This also works for the Places, Sources and Repositories views.</tip>

<tip number="23"><b>Organising the Views</b><br/>Many of the views can present your data as either a hierarchical tree or as a simple list. Each view can also be configured to the way you like it. Have a look to the right of the top toolbar or under the &quot;View&quot; menu.</tip>

Expand Down
69 changes: 68 additions & 1 deletion gramps/gui/viewmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
from gramps.gen.const import (HOME_DIR, ICON, URL_BUGTRACKER, URL_HOMEPAGE,
URL_MAILINGLIST, URL_MANUAL_PAGE, URL_WIKISTRING,
WIKI_EXTRAPLUGINS, URL_BUGHOME)
from gramps.gen.constfunc import is_quartz
from gramps.gen.constfunc import is_quartz, win
from gramps.gen.config import config
from gramps.gen.errors import WindowActiveError
from .dialog import ErrorDialog, WarningDialog, QuestionDialog2, InfoDialog
Expand Down Expand Up @@ -580,10 +580,47 @@ def no_del_event(self, *obj):
hits 'x' multiple times. """
return True

def checkdisk(self, dbfolder): # TODO check this function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This belongs in gramps/gen/utils/file.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in new PR this function move to file.py.

"""
Check disk space
"""
if win(): # for windows:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should we do for Windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gramps work in Windows too. it's draft for Windows. I check this code in my Linux system, but I have not Windows.

# import psutil
# DISK = "C:"
# free = psutil.disk_usage(DISK).free/(1024*1024)
# print(f"{free:.4} Mb free on disk {DISK}")
pass
else: # for linux:
fd = os.open(dbfolder, os.O_RDONLY)
stats = os.fstatvfs(fd)
os.close(fd)
freedisk = round(stats[1] * stats[4]/(1024*1024))
return freedisk

def quit(self, *obj):
"""
Closes out the program, backing up data
"""
#****************
# We must check disk space before:
# - run (Gramps need space for load config file)
# - quit (Gramps need some space for:
# - db.commit and close database,
# - save config file)
# - every backup (we need space 'last_backup_file_size'+ 5%)
diskspace = 20 # Disk space for Linux - 20 MB, for Gramps - 5 MB, for backup 'last_backup_file_size' + 5%
if (self.checkdisk(config.get('database.path')) < diskspace):
self.uistate.push_message(self.dbstate, _("Disk space < " + str(diskspace) + " MB. Quit impossible. Clear your disk and repeat this operation."))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use format strings instead of concatenation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not Python man.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrite this code. New PR have normal string concatenation.
I check code, all translated strings are present in .po file.

WarningDialog(
_("Message for user:"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps "Low disk space" instead?

Copy link
Contributor Author

@PushKK PushKK May 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course.
For me - after this message I must run BleachBit and clear disk.
Gramps wait me and not freeze.

_('You have less ' + str(diskspace) + ' MB on system disk.\n'
'Gramps need more space for save database and config file before quit.\n'
'Clear your disk and repeat this operation again.'), parent=self.uistate.window)
return
else:
pass
# ***********

# mark interface insenstitive to prevent unexpected events
self.uistate.set_sensitive(False)
# the following prevents reentering quit if user hits 'x' again
Expand Down Expand Up @@ -1188,6 +1225,35 @@ def autobackup(self):
"""
Backup the current family tree.
"""
#************
# Before every backup Gramps must check disk space.
# Needed space = last_backup_file_size + 5%
#************

backup_name = self.dbstate.db.get_dbname()
backup_ext = ".gramps"
path = config.get('database.backup-path')
bpath = sorted(os.listdir(path))
for n, bfile in enumerate(bpath):
if os.path.isfile(os.path.join(path, bfile)) and bfile.startswith(backup_name) and bfile.endswith(backup_ext):
blastfile = bfile

bfile_size = round(os.path.getsize(os.path.join(path, blastfile))/(1024))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A utility function to return the largest backup size would be neater.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think about this - we must use biggest or last backup file.
In result - Gramps must use last backup file for active database.
Reason:
If user delete non used info from database -> new_database_size < old_database_size.
And Gramps create new_database_backup with small size.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It just needs to be put into a utility function in gramps/gen/utils/file.py.

Copy link
Contributor Author

@PushKK PushKK May 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I test this code two day.
In result - backup_file_size must be calculated from active_database_size.
For SQLite3 - database with testdata have 71 MB, backup have 4,1 MB file (> 7%).
If user create new Family tree and import to tree data from other base - Gramps must say to user:

  1. You need least 'calculated backup_file_size' MB
    or (easy way on 'try - except')
  2. You cannot save your file. Clear your disk and try save again.

bfile_new_size = round(bfile_size/100*105)

diskspace = bfile_new_size # Disk space for Linux - 20 MB, for Gramps - 5 MB, for backup 'last_backup_file_size' + 5%
if (self.checkdisk(config.get('database.backup-path')) < diskspace):
self.uistate.push_message(self.dbstate, _("Disk space < " + str(diskspace) + " MB. Backup impossible. Clear your disk and Gramps create backup next time."))
print(_("Disk space < " + str(diskspace) + " kB. Backup impossible. Clear your disk and Gramps create backup next time."))
WarningDialog(
_("Message for user:"),
_('You have less ' + str(diskspace) + ' kB on backup disk.\n'
'Gramps need more space for save backup.\n'
'Clear your disk and Gramps create backup next time.'), parent=self.uistate.window)
return
else:
pass

if self.delay_timer is not None:
GLib.source_remove(self.delay_timer)
self.delay_timer = None
Expand Down Expand Up @@ -1848,3 +1914,4 @@ def media_toggle(self, widget, file_entry):
file_entry.set_text("%s.%s" % (base, extension))
else:
file_entry.set_text("%s.%s" % (filename, extension))

Loading