Advertisement
Guest User

Thomas Riggs

a guest
May 23rd, 2009
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #Filename: wc.py
  3. #Makes a word count for the previous day.
  4. #First version. Written by Thomas Riggs and released into the public domain. www.twostopsdown.com
  5.  
  6. import os, sys # to work with paths and files.
  7. from datetime import date, timedelta # to work with dates.
  8. import pickle # to save objects.
  9.  
  10. # Configuration
  11. default_threshold = timedelta(days=1) # The default threshold for what is considered recent, as a timedelta.
  12. default_path = os.path.expanduser('~/Writing') # Default path to search.
  13.  
  14. # Functions
  15. def findRecentFiles(path = default_path, threshold = default_threshold):
  16.     "This function recurses through a folder, and returns a list of files modified within the threshold value."
  17.     cutoff = date.today() - threshold
  18.     if path == None or threshold == None:
  19.         print("findRecentFiles requires a path and threshold.")
  20.         return void
  21.        
  22.     recentfiles = []
  23.    
  24.     for file in os.listdir(path):
  25.         if file[0] == ".":
  26.             continue
  27.        
  28.         filepath = path + "/" + file
  29.         if date.fromtimestamp(os.path.getmtime(filepath)) > cutoff and os.path.isfile(filepath) == True and file[-3:] == 'txt':
  30.             recentfiles.append(filepath)
  31.         elif os.path.isdir(filepath) == True:
  32.             for item in findRecentFiles(filepath):
  33.                 recentfiles.append(item)
  34.    
  35.     return recentfiles
  36.  
  37. def countWords(files = []):
  38.     "This function returns a dictionary of files and their current word count."
  39.     if files == None:
  40.         print("countWords requires a list of file paths.")
  41.         return void
  42.    
  43.     wordcounts = {}
  44.    
  45.     for file in files:
  46.         words = []
  47.         data = open(file)
  48.         lines = data.readlines()
  49.         for line in lines:
  50.               for word in line.split(None):
  51.                   words.append(word)
  52.                  
  53.         wordcounts[file] = len(words)
  54.        
  55.     return wordcounts
  56.  
  57. def writeWordCounts(wordcounts = {}, path = default_path):
  58.     "Writes the given wordcounts object using Pickle."
  59.     wordcountfile = path + "/" + ".wordcount"
  60.     data = open(wordcountfile, 'wb')
  61.     pickle.dump(wordcounts, data)
  62.     data.close()
  63.  
  64. def loadWordCounts(path = default_path):
  65.     "Uses Pickle to open a path's wordcount object."
  66.     wordcountfile = path + "/" + ".wordcount"
  67.     data = open(wordcountfile, 'rb')
  68.     wordcounts = pickle.load(data)
  69.     return wordcounts
  70.     data.close()
  71.  
  72. def updateWordCounts(old = {}, new = {}):
  73.     "Combines an old set of word counts with a current one. Replaces existing file's data and keeps unupdated ones."
  74.     updated = old
  75.     updated.update(new)
  76.     writeWordCounts(updated)
  77.  
  78. def relativePath(path, root = default_path):
  79.     "Gives the given file path, minus a starting root, by counting the directory tree branches of the root. Assumes path is within root."
  80.     rootlength = len(root.split("/"))
  81.     relativepathparts = path.split("/")
  82.     relativepath = "/".join(relativepathparts[rootlength:])
  83.     return relativepath
  84.  
  85. def currentWordCounts(path):
  86.     "Chains together findRecentFiles and countWords into a single function."
  87.     return countWords(findRecentFiles(path))
  88.  
  89. def recentChanges(raw = False, path = default_path):
  90.     "This function takes a path and finds recent word counts for files, using currentWordCounts."
  91.     currentwordcount = currentWordCounts(path)
  92.     oldwordcount = loadWordCounts()
  93.    
  94.     filecount = 0
  95.     totalwordcount = 0
  96.    
  97.     for (file, count) in currentwordcount.items():
  98.         try:
  99.             newwords = count - oldwordcount[file]
  100.             if newwords == 0:
  101.                 continue
  102.             status = str(newwords) + " new words since yesterday."
  103.             totalwordcount = totalwordcount + newwords
  104.         except KeyError:
  105.             status = str(count) + " words in total, but no previous record."
  106.             totalwordcount = totalwordcount + count
  107.            
  108.         if raw == False:
  109.             print(relativePath(file), "has", status)
  110.            
  111.         filecount = filecount + 1
  112.        
  113.     if raw == True:
  114.         print(totalwordcount)
  115.         return
  116.    
  117.     if filecount == 0 or totalwordcount == 0:
  118.         print("No updates today!")
  119.     else:
  120.         print("In total,", str(totalwordcount), "words have been written today!")
  121.  
  122. # On with the show!
  123.  
  124. try:
  125.     arg = sys.argv[1]
  126. except IndexError:
  127.     recentChanges()
  128.     sys.exit()
  129.  
  130. if arg == "update":
  131.     updateWordCounts(loadWordCounts(), currentWordCounts())
  132. elif arg == "raw":
  133.     recentChanges(True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement