Skip to content

Commit 432326e

Browse files
committed
basic backup added
Repetitions of code and lack of tests Next step add tests
1 parent b0dd5f0 commit 432326e

File tree

6 files changed

+70
-13
lines changed

6 files changed

+70
-13
lines changed

dls_barcode/data_store/backup.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import time
2+
3+
from dls_barcode.data_store.record import Record
4+
from dls_util.file import FileManager
5+
6+
7+
class Backup:
8+
9+
def __init__(self, backup_file):
10+
self._backup_file = backup_file
11+
self._file_manager = FileManager()
12+
self._load_records_from_file()
13+
14+
def _load_records_from_file(self):
15+
""" Clear the current record store and load a new set of records from the specified file. """
16+
self.records = []
17+
18+
if not self._file_manager.is_file(self._backup_file):
19+
return
20+
21+
lines = self._file_manager.read_lines(self._backup_file)
22+
for line in lines:
23+
try:
24+
record = Record.from_string(line)
25+
self.records.append(record)
26+
except Exception:
27+
print("Failed to parse store Record: {}".format(line))
28+
29+
#self._truncate_record_list()
30+
31+
def _to_backup_file(self):
32+
""" Save the contents of the store to the backing file
33+
"""
34+
record_lines = [rec.to_string() + "\n" for rec in self.records]
35+
self._file_manager.write_lines(self._backup_file, record_lines)
36+
37+
def _truncate_record_list(self):
38+
for record in self.records:
39+
if self._is_old(record):
40+
self.records.remove(record)
41+
42+
def backup_records(self, to_back):
43+
self.records.extend(to_back)
44+
self._truncate_record_list()
45+
self._to_backup_file()
46+
47+
def _is_old(self,record):
48+
tm = time.time()
49+
record_time = record.timestamp
50+
delta = tm - record_time
51+
weeks = 3 * 604800 #3 weeks in seconds
52+
return delta > weeks
53+

dls_barcode/data_store/record.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,4 @@ def _formatted_date(self):
162162
"""
163163
return datetime.datetime.fromtimestamp(self.timestamp).strftime(Record.DATE_FORMAT)
164164

165+

dls_barcode/data_store/store.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
import uuid
22
import os
33

4+
from dls_barcode.data_store.backup import Backup
5+
from dls_util.file import FileManager
46
from .record import Record
57

68

79
class Store:
810
""" Maintains a list of records of previous barcodes scans. Any changes (additions
911
or deletions) are automatically written to the backing file.
1012
"""
11-
def __init__(self, directory, store_capacity, file_manager):
13+
def __init__(self, directory, store_capacity):
1214
""" Initializes a new instance of Store.
1315
"""
1416
self._store_capacity = store_capacity
1517
self._directory = directory
16-
self._file_manager = file_manager
18+
self._file_manager = FileManager()
1719
self._file = os.path.join(directory, "store.txt")
1820
self._csv_file = os.path.join(directory, "store.csv")
19-
self._backup_csv_file = os.path.join(directory, "backup.csv")
21+
self._backup = Backup(os.path.join(directory, "backup.txt"))
2022
self._img_dir = os.path.join(directory, "img_dir")
2123

2224
if not self._file_manager.is_dir(self._img_dir):
@@ -85,8 +87,8 @@ def delete_records(self, records_to_delete):
8587

8688
self._process_change()
8789

88-
def backup_records(self, records_to_back_up):
89-
self._to_backup_csv_file(records_to_back_up)
90+
def backup_records(self, records_to_backup):
91+
self._backup.backup_records(records_to_backup)
9092

9193
def _truncate_record_list(self):
9294
min_store_capacity = 2
@@ -122,11 +124,6 @@ def _to_csv_file(self):
122124
record_lines = [rec.to_csv_string() + "\n" for rec in self.records]
123125
self._file_manager.write_lines(self._csv_file, record_lines)
124126

125-
def _to_backup_csv_file(self, records):
126-
""" Save the contents of the store to the backup csv file
127-
"""
128-
record_lines = [rec.to_csv_string() + "\n" for rec in records]
129-
self._file_manager.write_lines(self._backup_csv_file, record_lines)
130127

131128
def _merge_holder_image_into_pins_image(self, holder_img, pins_img):
132129
factor = 0.22 * pins_img.width / holder_img.width

dls_barcode/gui/image_widget.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ def __init__(self, parent=None):
1515
def eventFilter(self, source, event):
1616
if source is self and event.type() == QtCore.QEvent.Resize:
1717
ma = self.pixmap()
18-
self.setPixmap(ma.scaled(self.size(), QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))
18+
if ma is not None:
19+
self.setPixmap(ma.scaled(self.size(), QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation))
1920
return QtWidgets.QWidget.eventFilter(self, source, event)

dls_barcode/gui/record_table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, barcode_table, image_frame, options):
2222
super(ScanRecordTable, self).__init__()
2323

2424
# Read the store from file
25-
self._store = Store(options.store_directory.value(), options.store_capacity, FileManager())
25+
self._store = Store(options.store_directory.value(), options.store_capacity)
2626
self._options = options
2727

2828
self._barcodeTable = barcode_table
@@ -115,7 +115,7 @@ def _record_selected(self):
115115
self._imageFrame.display_puck_image(marked_image)
116116
except IndexError:
117117
self._barcodeTable.clear()
118-
self._imageFrame.clear_frame("Record table empty\nNothing to display")
118+
# self._imageFrame.clear_frame("Record table empty\nNothing to display")
119119

120120
def _delete_selected_records(self):
121121
""" Called when the 'Delete' button is pressed. Deletes all of the selected records

dls_util/file/file_manager.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def write_lines(self, file_path, lines):
1414
with open(file_path, 'w') as file:
1515
file.writelines(lines)
1616

17+
def append_lines(self, file_path, lines):
18+
"""Calls file.writelines, so doesn't append any new line characters"""
19+
with open(file_path, 'a') as file:
20+
file.writelines(lines)
21+
1722
def is_file(self, path):
1823
return os.path.isfile(path)
1924

0 commit comments

Comments
 (0)