-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDB.py
More file actions
100 lines (75 loc) · 2.92 KB
/
Copy pathFileDB.py
File metadata and controls
100 lines (75 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
################################################################################
# Copyright 2010 Andreas Neustifter (andreas.neustifter@gmail.com)
#
# This file is part of PhotoImport.
#
# PhotoImport is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# PhotoImport is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# PhotoImport. If not, see <http://www.gnu.org/licenses/>.
################################################################################
"""
This module stores per folder the information about copied and excluded files.
"""
import os
class FileDB:
"""
This class reads, stores and caches information about copied files.
"""
def __init__ (self):
self._db = {}
def readorcache(self, folder):
"""
This reads in the PhotoImport.db for a folder, if this file is missing
it creates an empty dictionary.
"""
if not self._db.has_key(folder):
filelist = {}
try:
dbfile = open(os.path.join(folder, "PhotoImport.db"), "r")
except:
self._db[folder] = filelist
return
filename = dbfile.readline()
while filename != "":
filestatus = dbfile.readline()
filelist[filename.strip()] = filestatus.strip()
filename = dbfile.readline()
dbfile.close()
self._db[folder] = filelist
def getstatus(self, filepath):
"""
This splits the filename into folder, filename and calls getstatus on
that.
"""
(folder, filename) = os.path.split(filepath)
return self.getstatus2(folder, filename)
def getstatus2(self, folder, filename):
"""
This reads the PhotoImport.db from folder and reads file information.
"""
self.readorcache(folder)
try:
return self._db[folder][filename]
except:
return None
def setstatus(self, filepath, status):
"""
This first checks that file is not in PhotoImport.db and then sets status.
"""
(folder, filename) = os.path.split(filepath)
if self.getstatus2(folder, filename) is not None:
raise Exception("Setting status of a file that has already status attached")
self._db[folder][filename] = status
dbfile = open(os.path.join(folder, "PhotoImport.db"), "a+")
dbfile.write(filename + "\n")
dbfile.write(status + "\n")
dbfile.close()