def createFile(taskDir,taskFilename,taskTitle, tagListString, templateFilename, \ authorInitialsList): """ Create the task file from the provided template. """ templatePath = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) \ + '/templates/' + templateFilename taskPath = taskDir + '/' + taskFilename # Set the task title, the tags, and possibly also the author templateFile = io.open(templatePath, 'r', encoding='utf-8') taskFile = io.open(taskPath,'w', encoding='utf-8') for line in templateFile: # the task title line = line.replace('@taskTitle',taskTitle) # the task label root,ext = os.path.splitext(taskFilename) line = line.replace('@label', root) # the tags line = line.replace('@tags', tagListString) # If not empty, also set the author initials if authorInitialsList: line = line.replace('%\\authors{}', \ '\\authors{'+ ','.join(authorInitialsList) +'}') taskFile.write(line) templateFile.close() taskFile.close()
def writeAuthorsToTexDictionary(authorInitialsList): """ Write the authors to the tag dictionary file in the build files folder. """ authorNameList, authorEmailList = getAuthorNamesAndEmail( authorInitialsList) buildFilesDir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__))\ + '/buildFiles' if not os.path.exists(buildFilesDir): os.makedirs(buildFilesDir) authorDictionaryFile = io.open(buildFilesDir + '/authorDictionary.tex',\ 'w',encoding='utf-8') nAuthors = len(authorInitialsList) for iAuthor in range(nAuthors): authorName = authorNameList[iAuthor] authorEmail = authorEmailList[iAuthor] authorDictionaryFile.write('\expandafter\\newcommand\csname author' + \ authorInitialsList[iAuthor] + \ 'name\endcsname{' + authorName +'}\n') authorDictionaryFile.write('\expandafter\\newcommand\csname author' + \ authorInitialsList[iAuthor] + \ 'email\endcsname{' + \ authorEmail +'}\n') authorDictionaryFile.close()
def createFile(taskDir,taskFilename,taskTitle, tagListString, templateFilename, \ authorInitialsList): """ Create the task file from the provided template. """ templatePath = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) \ + '/templates/' + templateFilename taskPath = taskDir + '/' + taskFilename # Set the task title, the tags, and possibly also the author templateFile = io.open(templatePath, 'r', encoding='utf-8') taskFile = io.open(taskPath, 'w', encoding='utf-8') for line in templateFile: # the task title line = line.replace('@taskTitle', taskTitle) # the task label root, ext = os.path.splitext(taskFilename) line = line.replace('@label', root) # the tags line = line.replace('@tags', tagListString) # If not empty, also set the author initials if authorInitialsList: line = line.replace('%\\authors{}', \ '\\authors{'+ ','.join(authorInitialsList) +'}') taskFile.write(line) templateFile.close() taskFile.close()
def createBuildFile(taskDict): """ Create the build file. """ oldYear = 1970 oldMonth = 0 oldDay = 0 buildFilesDir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__))\ + '/buildFiles' buildFile = io.open(buildFilesDir + '/taskList.tex','w',encoding='utf-8') for date, taskPathList in sorted(taskDict.items()): # If a new year, month, and/or are started, add a new part, chapter, # and/or section year = date.year month = date.month day = date.day if oldYear!=year: buildFile.write('\part{'+ \ commonDiaryFunctions.unicodeStr(year) +'}\n') oldYear = year if oldMonth!=month: buildFile.write('\chapter{'+ \ commonDiaryFunctions.unicodeStr(calendar.month_name[month]) +'}\n') oldMonth=month if oldDay!=day: buildFile.write('\section{'+ \ commonDiaryFunctions.unicodeStr(calendar.month_name[month]) + \ ' ' + commonDiaryFunctions.unicodeStr(day) + ', '\ + commonDiaryFunctions.unicodeStr(year) +'}\n') oldMonth=month # Add all tasks for taskPath in taskPathList: buildFile.write('\input{' + taskPath + '}\n') buildFile.close()
def updateDiaryTasks(oldAuthorInitials, newAuthorInitials): """ Update the author initials in the diary entries """ dateList = newBuild.getAllDatesWithEntries() diaryDir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) tmpTaskFilePath = diaryDir + '/buildFiles/tmpTask.tex' authorStringPattern = re.compile(r'^\s*\\authors\{(([a-zA-Z0-9\s]*,)*)' + \ oldAuthorInitials + '(([a-zA-Z0-9\s]*,)*)([a-zA-Z0-9\s]*)\}\s*$') for date in dateList: relativeDateDir = 'entries/' + str(date.year) + '/' + \ str(date.month).zfill(2) + '/' + str(date.day).zfill(2) # The file name of a task must match the pattern YYYYMMDD_XXXI.tex # where XXX are optional initials (letters a-zA-Z) and I is a number. fileNamePattern = re.compile(r'^' + str(date.year) + \ str(date.month).zfill(2) + str(date.day).zfill(2) + \ '_([a-zA-Z]*)([0-9]+)\.tex$') # Retrieve a sorted list of all files and folders in relativeDateDir filesAndFoldersList = \ sorted(os.listdir(diaryDir + '/' + relativeDateDir)) validTaskPathList = list() for fileOrFolder in filesAndFoldersList: relativeTaskPath = relativeDateDir + '/' + fileOrFolder taskPath = diaryDir + '/' + relativeTaskPath if os.path.isfile(taskPath) and \ re.match(fileNamePattern, fileOrFolder): oldTaskFile = io.open(taskPath, 'r', encoding='utf-8') tmpTaskFile = io.open(tmpTaskFilePath, 'w', encoding='utf-8') authorIsUpdated = False # search for the old author initials for line in oldTaskFile: # to avoid scanning all lines in the file, a flag is set # when the author initials have been updated if not authorIsUpdated and re.match( authorStringPattern, line): authorPattern = \ re.compile(r'^\s*\\authors\{(!*[a-zA-Z0-9,\s]+)\}\s*$') authorList = \ re.search(authorPattern,line).group(1).split(',') updatedAuthorList = list() for author in authorList: # remove white space author = author.strip() # update the old author initials with the new. # Remove any duplicates. if author == oldAuthorInitials: updatedAuthorList.append(newAuthorInitials) elif author != newAuthorInitials: updatedAuthorList.append(author) # write the updated author line line = commonDiaryFunctions.unicodeStr('\\authors{' +\ ','.join(updatedAuthorList) + '}\n') authorIsUpdated = True tmpTaskFile.write(line) oldTaskFile.close() tmpTaskFile.close() # the temporary task file contains the updated author. Replace the # old task file with this new one shutil.move(tmpTaskFilePath, taskPath)
def updateDiaryTasks(oldAuthorInitials,newAuthorInitials): """ Update the author initials in the diary entries """ dateList = newBuild.getAllDatesWithEntries() diaryDir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) tmpTaskFilePath = diaryDir + '/buildFiles/tmpTask.tex' authorStringPattern = re.compile(r'^\s*\\authors\{(([a-zA-Z0-9\s]*,)*)' + \ oldAuthorInitials + '(([a-zA-Z0-9\s]*,)*)([a-zA-Z0-9\s]*)\}\s*$') for date in dateList: relativeDateDir = 'entries/' + str(date.year) + '/' + \ str(date.month).zfill(2) + '/' + str(date.day).zfill(2) # The file name of a task must match the pattern YYYYMMDD_XXXI.tex # where XXX are optional initials (letters a-zA-Z) and I is a number. fileNamePattern = re.compile(r'^' + str(date.year) + \ str(date.month).zfill(2) + str(date.day).zfill(2) + \ '_([a-zA-Z]*)([0-9]+)\.tex$') # Retrieve a sorted list of all files and folders in relativeDateDir filesAndFoldersList = \ sorted(os.listdir(diaryDir + '/' + relativeDateDir)) validTaskPathList = list() for fileOrFolder in filesAndFoldersList: relativeTaskPath = relativeDateDir + '/' + fileOrFolder taskPath = diaryDir + '/' + relativeTaskPath if os.path.isfile(taskPath) and \ re.match(fileNamePattern, fileOrFolder): oldTaskFile = io.open(taskPath,'r',encoding='utf-8') tmpTaskFile = io.open(tmpTaskFilePath,'w',encoding='utf-8') authorIsUpdated = False # search for the old author initials for line in oldTaskFile: # to avoid scanning all lines in the file, a flag is set # when the author initials have been updated if not authorIsUpdated and re.match(authorStringPattern,line): authorPattern = \ re.compile(r'^\s*\\authors\{(!*[a-zA-Z0-9,\s]+)\}\s*$') authorList = \ re.search(authorPattern,line).group(1).split(',') updatedAuthorList = list() for author in authorList: # remove white space author = author.strip() # update the old author initials with the new. # Remove any duplicates. if author == oldAuthorInitials: updatedAuthorList.append(newAuthorInitials) elif author != newAuthorInitials: updatedAuthorList.append(author) # write the updated author line line = commonDiaryFunctions.unicodeStr('\\authors{' +\ ','.join(updatedAuthorList) + '}\n') authorIsUpdated = True tmpTaskFile.write(line) oldTaskFile.close() tmpTaskFile.close() # the temporary task file contains the updated author. Replace the # old task file with this new one shutil.move(tmpTaskFilePath,taskPath)
def setUp(self): # Move current database to a temporary file if it exists self.testDir = \ commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) self.databaseFile = self.testDir + '/../database/diaryDatabase.db' self.databaseFileBackup = \ self.testDir + '/../database/diaryDatabase.backup.db' if os.path.isfile(self.databaseFile): shutil.move(self.databaseFile, self.databaseFileBackup)
def dateHasEntry(date): """ Returns true if a folder structure exists for the day """ dir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) + \ '/entries/' + str(date.year) + '/' + str(date.month).zfill(2) + \ '/' + str(date.day).zfill(2) if os.path.isdir(dir): return True else: return False
def createFolders(year, month, day): """ Create the year, month, and day folders if needed. """ entriesDir = \ commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) + '/entries' if not os.path.isdir(entriesDir + '/' + year): os.makedirs(entriesDir + '/' + year) if not os.path.isdir(entriesDir + '/' + year + '/' + month): os.makedirs(entriesDir + '/' + year + '/' + month) if not os.path.isdir(entriesDir + '/' + year + '/' + month + '/' + day): os.makedirs(entriesDir + '/' + year + '/' + month + '/' + day) return entriesDir + '/' + year + '/' + month + '/' + day
def validateInputsAndSetDefaults(argv): """ Validate the provided input and set the defaults values for the optional parameters if not specified or empty. """ nInputs = len(argv) # Validate that 2 to 5 input variables are given if nInputs<2 or nInputs>5: print("Error: You must specify 2 to 5 input parameters.") print("addTask.py \"tagA tagB\" \"Task Title\" authorInitials " + \ "template YYYY-MM-DD") sys.exit(2) tagListString = argv[0] taskTitle = argv[1] # Validate the tag # The provided tag(s) must be compared to the tag database. If not # found, it must be generated by calling the newTag function. tagListString = checkTags(tagListString)[0] # Set or validate author initials if nInputs<3 or not argv[2]: authorInitialsList = "" else: # The provided author initials must be compared to the author database. # If not found, it must be generated by calling the newAuthor function. authorInitialsList = checkAuthors(argv[2]) # Set or validate the template if nInputs<4 or not argv[3]: templateFilename = 'default.tpl.tex' else: templateFilename = argv[3] + '.tpl.tex' templateDir = \ commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) + \ '/templates' if not os.path.isfile(templateDir + '/' + templateFilename): print("Error: The specified template file does not exist in " + \ "the template folder. Please create it.") # Set or validate the date if nInputs<5 or not argv[4]: now = datetime.datetime.now() year = commonDiaryFunctions.unicodeStr(now.year) month = commonDiaryFunctions.unicodeStr(now.month).zfill(2) day = commonDiaryFunctions.unicodeStr(now.day).zfill(2) else: try: datetime.datetime.strptime(argv[4], '%Y-%m-%d') except ValueError: raise ValueError("Incorrect date or date format." + \ "Should be YYYY-MM-DD") year, month, day = argv[4].split('-') return taskTitle, tagListString, templateFilename, authorInitialsList, \ year, month, day
def validateInputsAndSetDefaults(argv): """ Validate the provided input and set the defaults values for the optional parameters if not specified or empty. """ nInputs = len(argv) # Validate that 2 to 5 input variables are given if nInputs < 2 or nInputs > 5: print("Error: You must specify 2 to 5 input parameters.") print("addTask.py \"tagA tagB\" \"Task Title\" authorInitials " + \ "template YYYY-MM-DD") sys.exit(2) tagListString = argv[0] taskTitle = argv[1] # Validate the tag # The provided tag(s) must be compared to the tag database. If not # found, it must be generated by calling the newTag function. tagListString = checkTags(tagListString)[0] # Set or validate author initials if nInputs < 3 or not argv[2]: authorInitialsList = "" else: # The provided author initials must be compared to the author database. # If not found, it must be generated by calling the newAuthor function. authorInitialsList = checkAuthors(argv[2]) # Set or validate the template if nInputs < 4 or not argv[3]: templateFilename = 'default.tpl.tex' else: templateFilename = argv[3] + '.tpl.tex' templateDir = \ commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) + \ '/templates' if not os.path.isfile(templateDir + '/' + templateFilename): print("Error: The specified template file does not exist in " + \ "the template folder. Please create it.") # Set or validate the date if nInputs < 5 or not argv[4]: now = datetime.datetime.now() year = commonDiaryFunctions.unicodeStr(now.year) month = commonDiaryFunctions.unicodeStr(now.month).zfill(2) day = commonDiaryFunctions.unicodeStr(now.day).zfill(2) else: try: datetime.datetime.strptime(argv[4], '%Y-%m-%d') except ValueError: raise ValueError("Incorrect date or date format." + \ "Should be YYYY-MM-DD") year, month, day = argv[4].split('-') return taskTitle, tagListString, templateFilename, authorInitialsList, \ year, month, day
def __init__(self): databaseDir = \ commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) databaseName = 'diaryDatabase.db' databasePath = databaseDir + '/' + databaseName if not os.path.isfile(databasePath): # Create the database files and all tables self.connection = self.__connect(databasePath) self.__createTables() else: # Open a connection to the database self.connection = self.__connect(databasePath) # Support mapping access by column name and index, iteration, # representation, equality testing and len(). # http://docs.python.org/2/library/sqlite3.html#sqlite3.Row self.connection.row_factory = sqlite3.Row
def getTaskDictionary(includeTagList, excludeTagList, dateList, \ taskLabelList): """ Find the tasks with valid tags and the selected dates. The key of the returned dictionary is the date and the values are the file names of the tags. """ taskDict = dict() extractedTagsList = list() extractedAuthorList = list() diaryDir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) for date in dateList: relativeDateDir = 'entries/' + str(date.year) + '/' + \ str(date.month).zfill(2) + '/' + str(date.day).zfill(2) # The file name of a task must match the pattern YYYYMMDD_XXXI.tex # where XXX are optional initials (letters a-zA-Z) and I is a number. fileNamePattern = re.compile(r'^' + str(date.year) + \ str(date.month).zfill(2) + str(date.day).zfill(2) + \ '_([a-zA-Z]*)([0-9]+)\.tex$') # Retrieve a sorted list of all files and folders in relativeDateDir filesAndFoldersList = \ sorted(os.listdir(diaryDir + '/' + relativeDateDir)) validTaskPathList = list() for fileOrFolder in filesAndFoldersList: relativeTaskPath = relativeDateDir + '/' + fileOrFolder taskPath = diaryDir + '/' + relativeTaskPath if os.path.isfile(taskPath) and \ re.match(fileNamePattern, fileOrFolder): # If the taskLabelList is not empty, check if the file name # is in the list if len(taskLabelList ) == 0 or fileOrFolder[:-4] in taskLabelList: extractedTags = extractTagsFromValidTask(taskPath, \ includeTagList, excludeTagList) if len(extractedTags) > 0: extractedAuthors = extractAuthorsFromTask(taskPath) if len(extractedAuthors) > 0: extractedAuthorList.extend(extractedAuthors) validTaskPathList.append(relativeTaskPath) extractedTagsList.extend(extractedTags) # If a least one task path has been added, add it to the dictionary if len(validTaskPathList) > 0: taskDict[date] = validTaskPathList # return the task dictionary and the unique extracted tags and authors return taskDict, sorted(list(set(extractedTagsList))), \ sorted(list(set(extractedAuthorList)))
def updateDiaryTasks(oldTagName,newTagName): """ update the tags in the diary entries """ dateList = newBuild.getAllDatesWithEntries() taskDict, extractedTagsList, extractedAuthorList = \ newBuild.getTaskDictionary(oldTagName, list(), dateList, list()) # the task dict contains a list of all the tasks tagged with the old tag. # We now cycle through all these files and update this tag diaryPath = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) tmpTaskFilePath = diaryPath + '/buildFiles/tmpTask.tex' pattern = re.compile(r'^\s*\\tags\{(([a-zA-Z0-9\s]*,)*)' + \ oldTagName + '(([a-zA-Z0-9\s]*,)*)([a-zA-Z0-9\s]*)\}\s*$') for date, taskPathList in sorted(taskDict.items()): for relativeTaskPath in taskPathList: taskPath = diaryPath + '/' + relativeTaskPath oldTaskFile = io.open(taskPath,'r',encoding='utf-8') tmpTaskFile = io.open(tmpTaskFilePath,'w',encoding='utf-8') tagIsUpdated = False for line in oldTaskFile: # to avoid scanning all lines in the file, a flag is set when # the tag has been updated if not tagIsUpdated and re.match(pattern,line): tagPattern = \ re.compile(r'^\s*\\tags\{(!*[a-zA-Z0-9,\s]+)\}\s*$') tagList = \ re.search(tagPattern,line).group(1).split(',') updatedTagList = list() for tag in tagList: # remove white space tag = tag.strip() # Remove any duplicates. if tag == oldTagName: updatedTagList.append(newTagName) elif tag != newTagName: updatedTagList.append(tag) # write the updated tag line line = commonDiaryFunctions.unicodeStr('\\tags{' + \ ','.join(updatedTagList) + '}\n') tagIsUpdated = True tmpTaskFile.write(line) oldTaskFile.close() tmpTaskFile.close() # the temporary task file contains the updated tag. Replace the # old task file with this new one shutil.move(tmpTaskFilePath,taskPath)
def updateDiaryTasks(oldTagName, newTagName): """ update the tags in the diary entries """ dateList = newBuild.getAllDatesWithEntries() taskDict, extractedTagsList, extractedAuthorList = \ newBuild.getTaskDictionary(oldTagName, list(), dateList, list()) # the task dict contains a list of all the tasks tagged with the old tag. # We now cycle through all these files and update this tag diaryPath = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) tmpTaskFilePath = diaryPath + '/buildFiles/tmpTask.tex' pattern = re.compile(r'^\s*\\tags\{(([a-zA-Z0-9\s]*,)*)' + \ oldTagName + '(([a-zA-Z0-9\s]*,)*)([a-zA-Z0-9\s]*)\}\s*$') for date, taskPathList in sorted(taskDict.items()): for relativeTaskPath in taskPathList: taskPath = diaryPath + '/' + relativeTaskPath oldTaskFile = io.open(taskPath, 'r', encoding='utf-8') tmpTaskFile = io.open(tmpTaskFilePath, 'w', encoding='utf-8') tagIsUpdated = False for line in oldTaskFile: # to avoid scanning all lines in the file, a flag is set when # the tag has been updated if not tagIsUpdated and re.match(pattern, line): tagPattern = \ re.compile(r'^\s*\\tags\{(!*[a-zA-Z0-9,\s]+)\}\s*$') tagList = \ re.search(tagPattern,line).group(1).split(',') updatedTagList = list() for tag in tagList: # remove white space tag = tag.strip() # Remove any duplicates. if tag == oldTagName: updatedTagList.append(newTagName) elif tag != newTagName: updatedTagList.append(tag) # write the updated tag line line = commonDiaryFunctions.unicodeStr('\\tags{' + \ ','.join(updatedTagList) + '}\n') tagIsUpdated = True tmpTaskFile.write(line) oldTaskFile.close() tmpTaskFile.close() # the temporary task file contains the updated tag. Replace the # old task file with this new one shutil.move(tmpTaskFilePath, taskPath)
def getTaskDictionary(includeTagList, excludeTagList, dateList, \ taskLabelList): """ Find the tasks with valid tags and the selected dates. The key of the returned dictionary is the date and the values are the file names of the tags. """ taskDict = dict() extractedTagsList = list() extractedAuthorList = list() diaryDir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) for date in dateList: relativeDateDir = 'entries/' + str(date.year) + '/' + \ str(date.month).zfill(2) + '/' + str(date.day).zfill(2) # The file name of a task must match the pattern YYYYMMDD_XXXI.tex # where XXX are optional initials (letters a-zA-Z) and I is a number. fileNamePattern = re.compile(r'^' + str(date.year) + \ str(date.month).zfill(2) + str(date.day).zfill(2) + \ '_([a-zA-Z]*)([0-9]+)\.tex$') # Retrieve a sorted list of all files and folders in relativeDateDir filesAndFoldersList = \ sorted(os.listdir(diaryDir + '/' + relativeDateDir)) validTaskPathList = list() for fileOrFolder in filesAndFoldersList: relativeTaskPath = relativeDateDir + '/' + fileOrFolder taskPath = diaryDir + '/' + relativeTaskPath if os.path.isfile(taskPath) and \ re.match(fileNamePattern, fileOrFolder): # If the taskLabelList is not empty, check if the file name # is in the list if len(taskLabelList)==0 or fileOrFolder[:-4] in taskLabelList: extractedTags = extractTagsFromValidTask(taskPath, \ includeTagList, excludeTagList) if len(extractedTags)>0: extractedAuthors = extractAuthorsFromTask(taskPath) if len(extractedAuthors)>0: extractedAuthorList.extend(extractedAuthors) validTaskPathList.append(relativeTaskPath) extractedTagsList.extend(extractedTags) # If a least one task path has been added, add it to the dictionary if len(validTaskPathList)>0: taskDict[date] = validTaskPathList # return the task dictionary and the unique extracted tags and authors return taskDict, sorted(list(set(extractedTagsList))), \ sorted(list(set(extractedAuthorList)))
def writeTagsToTexDictionary(tagList): """ Write the tags and titles to the tag dictionary file in the build files folder. """ tagTitleList = getTagTitles(tagList) buildFilesDir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__))\ + '/buildFiles' if not os.path.exists(buildFilesDir): os.makedirs(buildFilesDir) tagDictionaryFile = io.open(buildFilesDir + '/tagDictionary.tex',\ 'w',encoding='utf-8') nTags = len(tagList) for iTag in range(nTags): tagTitle = tagTitleList[iTag] tagDictionaryFile.write('\expandafter\\newcommand\csname tag' + \ tagList[iTag] + '\endcsname{' + tagTitle + '}\n') tagDictionaryFile.close()
def setUp(self): # Move current database to a temporary file if it exists self.testDir = \ commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) self.databaseFile = self.testDir + '/../database/diaryDatabase.db' self.databaseFileBackup = \ self.testDir + '/../database/diaryDatabase.backup.db' if os.path.isfile(self.databaseFile): shutil.move(self.databaseFile, self.databaseFileBackup) # Move all entries to a backup self.entriesDir = self.testDir + '/../entries' self.entriesDirBackup = self.testDir + '/../entriesBackup' shutil.copytree(self.entriesDir, self.entriesDirBackup) shutil.rmtree(self.entriesDir) # Setup the new entries self.testEntriesDir = self.testDir + '/test_change_author_files/entries' shutil.copytree(self.testEntriesDir, self.entriesDir) self.entryPath = self.testDir + '/../entries/2014/08/24/20140824_jkn0.tex'
def setUp(self): self.testDir = \ commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) # Move all entries to a backup self.entriesDir = self.testDir + '/../entries' self.entriesDirBackup = self.testDir + '/../entriesBackup' shutil.copytree(self.entriesDir, self.entriesDirBackup) shutil.rmtree(self.entriesDir) # Setup the new empty entries dir os.makedirs(self.entriesDir) # Setup the test database self.databaseFile = self.testDir + '/../database/diaryDatabase.db' self.databaseFileBackup = \ self.testDir + '/../database/diaryDatabase.backup.db' self.testDatabaseFile = self.testDir + '/test_db/diaryDatabase.db' if os.path.isfile(self.databaseFile): shutil.move(self.databaseFile,self.databaseFileBackup) shutil.copyfile(self.testDatabaseFile,self.databaseFile)
def getAllDatesWithEntries(fromDate=datetime.date(1970, 1, 1)): """ Get all dates from a specific date. """ dateList = list() entriesDir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) + \ '/entries' yearFolders = getDigitFolders(entriesDir) for yearFolder in yearFolders: iPath = entriesDir + '/' + yearFolder monthFolders = getDigitFolders(iPath) for monthFolder in monthFolders: jPath = iPath + '/' + monthFolder dayFolders = getDigitFolders(jPath) for dayFolder in dayFolders: candidateDate = datetime.date(year=int(yearFolder),\ month=int(monthFolder),day=int(dayFolder)) if fromDate <= candidateDate: dateList.append(candidateDate) return sorted(dateList)
def getAllDatesWithEntries(fromDate=datetime.date(1970,1,1), \ toDate=datetime.date.today()): """ Get all dates with entries between two dates (both inclusive). """ dateList = list() entriesDir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) + \ '/entries' yearFolders = getDigitFolders(entriesDir) for yearFolder in yearFolders: iPath = entriesDir + '/' + yearFolder monthFolders = getDigitFolders(iPath) for monthFolder in monthFolders: jPath = iPath + '/' + monthFolder dayFolders = getDigitFolders(jPath) for dayFolder in dayFolders: candidateDate = datetime.date(year=int(yearFolder),\ month=int(monthFolder),day=int(dayFolder)) if fromDate<=candidateDate and candidateDate<=toDate: dateList.append(candidateDate) return sorted(dateList)
def setUp(self): # If they exists, move current build files to temporary files self.testDir = \ commonDiaryFunctions.unicodeDir(os.path.abspath(__file__)) self.taskList = self.testDir + '/../buildFiles/taskList.tex' self.tagDictionary = self.testDir + '/../buildFiles/tagDictionary.tex' self.authorDictionary = \ self.testDir + '/../buildFiles/authorDictionary.tex' self.taskListBackup = \ self.testDir + '/../buildFiles/taskList.backup.tex' self.tagDictionaryBackup = \ self.testDir + '/../buildFiles/tagDictionary.backup.tex' self.authorDictionaryBackup = \ self.testDir + '/../buildFiles/authorDictionary.backup.tex' if os.path.isfile(self.taskList): shutil.move(self.taskList, self.taskListBackup) if os.path.isfile(self.tagDictionary): shutil.move(self.tagDictionary, self.tagDictionaryBackup) if os.path.isfile(self.authorDictionary): shutil.move(self.authorDictionary, self.authorDictionaryBackup) # Move all entries to a backup self.entriesDir = self.testDir + '/../entries' self.entriesDirBackup = self.testDir + '/../entriesBackup' shutil.copytree(self.entriesDir, self.entriesDirBackup) shutil.rmtree(self.entriesDir) # Setup the new entries self.testEntriesDir = self.testDir + '/test_new_build_files/entries' shutil.copytree(self.testEntriesDir, self.entriesDir) # Setup the test database self.databaseFile = self.testDir + '/../database/diaryDatabase.db' self.databaseFileBackup = \ self.testDir + '/../database/diaryDatabase.backup.db' self.testDatabaseFile = self.testDir + '/test_db/diaryDatabase.db' if os.path.isfile(self.databaseFile): shutil.move(self.databaseFile, self.databaseFileBackup) shutil.copyfile(self.testDatabaseFile, self.databaseFile)
def writeAuthorsToTexDictionary(authorInitialsList): """ Write the authors to the tag dictionary file in the build files folder. """ authorNameList, authorEmailList = getAuthorNamesAndEmail(authorInitialsList) buildFilesDir = commonDiaryFunctions.unicodeDir(os.path.abspath(__file__))\ + '/buildFiles' if not os.path.exists(buildFilesDir): os.makedirs(buildFilesDir) authorDictionaryFile = io.open(buildFilesDir + '/authorDictionary.tex',\ 'w',encoding='utf-8') nAuthors = len(authorInitialsList) for iAuthor in range(nAuthors): authorName = authorNameList[iAuthor] authorEmail = authorEmailList[iAuthor] authorDictionaryFile.write('\expandafter\\newcommand\csname author' + \ authorInitialsList[iAuthor] + \ 'name\endcsname{' + authorName +'}\n') authorDictionaryFile.write('\expandafter\\newcommand\csname author' + \ authorInitialsList[iAuthor] + \ 'email\endcsname{' + \ authorEmail +'}\n') authorDictionaryFile.close()