00001
00002
00003
00004 '''
00005 Created on 20/06/2009
00006
00007 @author: Salvador de la Puente González
00008 '''
00009 from pysqlite2 import dbapi2 as sqlite3
00010
00011
00012 _DB_PATH = '../TIME'
00013
00014
00015 db = sqlite3.connect(_DB_PATH);
00016 db.row_factory = sqlite3.Row
00017
00018
00019 class Timelines(object):
00020
00021 class DoesNotExist(Exception):
00022
00023
00024 def __init__(self, info):
00025 self.info = info
00026
00027 def __str__(self):
00028 return u'Timeline object does not exist (%s)' % str(self.info)
00029
00030
00031
00032
00033
00034 @staticmethod
00035 def add_timeline(p):
00036 import time
00037 from os import path
00038
00039
00040 p = path.normpath(p)
00041 now = int(time.time())
00042 type = 'f'
00043 if (path.isdir(p)):
00044 type = 'd'
00045
00046 head, file = path.split(p)
00047
00048
00049 c = db.cursor()
00050
00051 parent = 0
00052 folders = head.split('/')[1:]
00053
00054 insert_mode = False
00055 for folder in folders:
00056
00057 if not insert_mode:
00058 node = c.execute(u"SELECT id FROM timelines WHERE parent=? AND name=?", (parent, folder)).fetchall()
00059 if node:
00060 parent = node[0]['id']
00061
00062 else:
00063 insert_mode = True
00064
00065
00066 if insert_mode:
00067 c.execute(u"INSERT INTO timelines (name, parent, start_time, type) VALUES (?, ?, ?, 'd')", (folder, parent, now))
00068 parent = c.execute(u"SELECT id FROM timelines ORDER BY id DESC LIMIT 1").fetchone()['id']
00069
00070
00071 c.execute(u"INSERT INTO timelines (name, parent, start_time, type) VALUES (?, ?, ?, ?)", (file, parent, now, type))
00072
00073 new_id = c.execute(u"SELECT id FROM timelines ORDER BY id DESC LIMIT 1").fetchone()['id']
00074
00075 db.commit()
00076 c.close()
00077
00078 return new_id
00079
00080
00081
00082
00083
00084 @staticmethod
00085 def get_path(id):
00086 folders = []
00087
00088 current = id
00089 while current:
00090 timeline = Timelines.get(current)
00091 folders.append(timeline['name'])
00092 current = timeline['parent']
00093
00094 folders.reverse()
00095 return '/' + '/'.join(folders)
00096
00097
00098
00099
00100
00101 @staticmethod
00102 def get_timeline(p):
00103 from os import path
00104 p = path.normpath(p)
00105 folders = p.split('/')[1:]
00106
00107 c = db.cursor()
00108
00109 parent = 0
00110 for folder in folders:
00111 node = c.execute(u"SELECT id FROM timelines WHERE parent=? AND name=?", (parent, folder)).fetchall()
00112 if node:
00113 parent = node[0]['id']
00114 else:
00115 raise Timeline.DoesNotExist(parent)
00116
00117 c.close()
00118
00119 return parent
00120
00121
00122
00123
00124 @staticmethod
00125 def get(id):
00126 c = db.cursor()
00127
00128 timeline = c.execute(u"SELECT * FROM timelines WHERE id=? LIMIT 1", (id, )).fetchone()
00129 if not timeline:
00130 raise Timeline.DoesNotExist(id)
00131
00132 c.close()
00133
00134 return timeline
00135
00136
00137
00138 @staticmethod
00139 def check_db():
00140 c = db.cursor()
00141
00142 is_empty = 0 == len(c.execute(u"SELECT id FROM timelines").fetchall())
00143 if is_empty:
00144
00145
00146 import time
00147 now = int(time.time())
00148 c.execute(u"INSERT INTO timelines (id, start_time, name, parent, type) VALUES (0, ?, '/', 0, 'd');" , (now, ))
00149 db.commit()
00150
00151 c.close()
00152
00153
00154 class Snapshots(object):
00155
00156 class DoesNotExist(Exception):
00157
00158
00159 def __init__(self, info):
00160 self.info = info
00161
00162 def __str__(self):
00163 return u'Snapshot object does not exist (%s)' % str(self.info)
00164
00165
00166
00167
00168
00169
00170 @staticmethod
00171 def new_base(timeline, timestamp, path):
00172 type = 'b'
00173
00174 c = db.cursor()
00175
00176 c.execute(u"INSERT INTO timelines (timeline, timestamp, type, path) VALUES (?, ?, ?, ?)", (timeline, timestamp, type, path))
00177 db.commit()
00178
00179 c.close()
00180
00181
00182
00183
00184
00185 @staticmethod
00186 def new_incremental(timeline, timestamp, path):
00187 type = 'i'
00188
00189 c = db.cursor()
00190
00191 c.execute(u"INSERT INTO timelines (timeline, timestamp, type, path) VALUES (?, ?, ?, ?)", (timeline, timestamp, type, path))
00192 db.commit()
00193
00194 c.close()
00195
00196
00197
00198
00199
00200
00201 @staticmethod
00202 def new_delete(timeline, timestamp, related_to=None):
00203 type = 'd'
00204
00205 c = db.cursor()
00206
00207 if related_to is None:
00208 c.execute(u"INSERT INTO timelines (timeline, timestamp, type) VALUES (?, ?, ?)", (timeline, timestamp, type))
00209 else:
00210 c.execute(u"INSERT INTO timelines (timeline, timestamp, type, related_timeline) VALUES (?, ?, ?, ?)", (timeline, timestamp, type, related_to))
00211 db.commit()
00212
00213 c.close()
00214
00215
00216
00217
00218
00219 @staticmethod
00220 def new_rename(old_timeline, new_timeline, timestamp):
00221 type = 'r'
00222
00223
00224 Snapshots.new_delete(old_timeline, timestamp, new_timeline)
00225
00226 c = db.cursor()
00227
00228 c.execute(u"INSERT INTO timelines (timeline, timestamp, type, related_timeline) VALUES (?, ?, ?, ?)", (new_timeline, timestamp, type, old_timeline))
00229 db.commit()
00230
00231 c.close()
00232
00233
00234
00235
00236
00237 @staticmethod
00238 def new_move(old_timeline, new_timeline, timestamp):
00239 type = 'm'
00240
00241
00242 Snapshots.new_delete(old_timeline, timestamp, new_timeline)
00243
00244 c = db.cursor()
00245
00246 c.execute(u"INSERT INTO timelines (timeline, timestamp, type, related_timeline) VALUES (?, ?, ?, ?)", (new_timeline, timestamp, type, old_timeline))
00247 db.commit()
00248
00249 c.close()
00250
00251 Timelines.check_db()
00252 print Timelines.get_path(6)