def save(record, oldRecord=None): """ Convert the record to Xml code, and Write the code to the disk, record id is the basename of the record file. If the oldRecord is provided, this is to change an existing record. When to change an existing log, the new log may be saved to a new directory if its timestamp been changed, in such case the old log will be deleted. """ timestamp = record.time dateEle = isodate(timestamp).split("-") absDirPath = os.path.join(XmlStorage.dataDir, *dateEle) paths = [] if not oldRecord: # add new record record.id = XmlStorage.genId(timestamp) commitMsg = "Add record\n\n%s" % record.id else: commitMsg = "Change record\n\n%s" % record.id if record != oldRecord: path = XmlStorage.idToPath(oldRecord.id) paths.append(path) XmlStorage.__delete(None, path=path) else: return os.makedirs(absDirPath, exist_ok=True) path = os.path.join(absDirPath, record.id) code = XmlStorage.recordToSource(record) open(path, "w").write(code) paths.append(path) # create a git commit XmlStorage.git.commit(paths, commitMsg)
def saveRecord(recordData, dir=None): if not dir: dir = XmlStorage.dataDir dateEle = isodate(recordData['time']).split('-') absDirPath = os.path.join(dir, *dateEle) os.makedirs(absDirPath, exist_ok=True) path = os.path.join(absDirPath, recordData['id']) code = XmlStorage.recordToSource(recordData) open(path, 'w').write(code) return path