Skip to content

Commit

Permalink
wip2
Browse files Browse the repository at this point in the history
  • Loading branch information
rbreu committed Jun 2, 2024
1 parent d5cdf19 commit 5e6168e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
3 changes: 3 additions & 0 deletions beeref/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

CHANGED_SYMBOL = '✎'

# Backup filename for unsaved scenes:
BACKUP_FILENAME = 'BeeRef-d9257003-0191-4bc7-9c9c-3383eeddd739.bee~'

COLORS = {
# Qt:
'Active:Base': (60, 60, 60),
Expand Down
25 changes: 17 additions & 8 deletions beeref/fileio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
from beeref import commands
from beeref.fileio.errors import BeeFileIOError
from beeref.fileio.image import load_image
from beeref.fileio.sql import SQLiteIO, is_bee_file, read_uppdate_from_file
from beeref.fileio.sql import (
SQLiteIO,
copy_bee_file,
is_bee_file,
read_uppdate_from_file,
)
from beeref.items import BeePixmapItem


Expand Down Expand Up @@ -57,28 +62,32 @@ def save_bee(filename, scene, create_new=False, worker=None):

def save_backup(filename, backup_filename, scene, worker=None):
"""Save backup bee file."""
logger.info(f'Saving backup to file {filename}...')
logger.info(f'Saving backup to file {backup_filename}...')
copy_to_backup = False
past = datetime(2000, 1, 1, tzinfo=datetime.timezone.utc)
past = datetime.datetime(2000, 1, 1, tzinfo=datetime.timezone.utc)

if os.path.exists(filename):
if filename and os.path.exists(filename):
upddate_original = read_uppdate_from_file(filename) or past
if os.path.exists(backup_filename):
upddate_backup = read_uppdate_from_file(backup_filename) or past
if upddate_original > upddate_backup:
copy_to_backup = True
logger.debug('Original file newer than backup')
else:#tbd
else:
copy_to_backup = True
logger.debug('Backup file does\'t exist yet')

if copy_to_backup:
logger.debug('Copying original file to backup file...')
#tbd
copy_bee_file(filename, backup_filename)

#tbd backup
io = SQLiteIO(backup_filename,
scene,
create_new=not os.path.exists(backup_filename),
update_save_ids=False,
worker=worker)
io.write()

worker.finished.emit(filename, [])

def load_images(filenames, pos, scene, worker):
"""Add images to existing scene."""
Expand Down
19 changes: 16 additions & 3 deletions beeref/fileio/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def is_bee_file(path):
return os.path.splitext(path)[1] == '.bee'


def copy_bee_file(src_filename, dst_filename):
"""Copy a bee file to a new file."""

src = sqlite3.connect(src_filename)
dst = sqlite3.connect(dst_filename)
src.backup(dst)
src.close()
dst.close()


def read_uppdate_from_file(filename):
io = SQLiteIO(filename, None, create_new=False, readonly=True)
datestring = io.read_info_value('upddate')
Expand Down Expand Up @@ -82,11 +92,12 @@ def wrapper(self, *args, **kwargs):
class SQLiteIO:

def __init__(self, filename, scene, create_new=False, readonly=False,
worker=None):
update_save_ids=True, worker=None):
self.scene = scene
self.create_new = create_new
self.filename = filename
self.readonly = readonly
self.update_save_ids = update_save_ids
self.worker = worker
self.retry = False

Expand All @@ -109,7 +120,7 @@ def _establish_connection(self):
and os.path.exists(self.filename)):
os.remove(self.filename)

if self.create_new:
if self.create_new and self.update_save_ids:
self.scene.clear_save_ids()

uri = pathlib.Path(self.filename).resolve().as_uri()
Expand Down Expand Up @@ -330,7 +341,9 @@ def insert_item(self, item):
(item.TYPE, item.pos().x(), item.pos().y(), item.zValue(),
item.scale(), item.rotation(), item.flip(),
json.dumps(item.get_extra_save_data())))
item.save_id = self.cursor.lastrowid

if self.update_save_ids:
item.save_id = self.cursor.lastrowid

if hasattr(item, 'pixmap_to_bytes'):
pixmap, imgformat = item.pixmap_to_bytes()
Expand Down
20 changes: 5 additions & 15 deletions beeref/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def __init__(self, app, parent=None):
self.undo_stack.cleanChanged.connect(self.on_undo_clean_changed)

self.filename = None
self.backup_filename = None
self.previous_transform = None
self.active_mode = None

Expand Down Expand Up @@ -115,19 +114,7 @@ def filename(self, value):
def backup_filename(self):
if self.filename:
return f'{self.filename}~'

if not self._backup_filename:
self._backup_filename = tempfile.NamedTemporaryFile(
prefix=constants.APPNAME,
suffix='.bee~',
delete=False,
delete_on_close=False
).name
return self._backup_filename

@backup_filename.setter
def backup_filename(self, value):
self._backup_filename = value
return os.path.join(tempfile.gettempdir(), constants.BACKUP_FILENAME)

def save_backup(self):
if self.undo_stack.isClean():
Expand Down Expand Up @@ -217,7 +204,10 @@ def clear_scene(self):
self.scene.clear()
self.undo_stack.clear()
self.filename = None
self.backup_filename = None
try:
os.remove(self.backup_filename)
except FileNotFoundError:
pass
self.setTransform(QtGui.QTransform())

def reset_previous_transform(self, toggle_item=None):
Expand Down

0 comments on commit 5e6168e

Please sign in to comment.