Advertisement
Guest User

jour.py

a guest
Jan 26th, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1.  
  2. import sys
  3. import time
  4. import os
  5. import configparser
  6.  
  7. journal_dirname = ""
  8. journal_cwd = ""
  9. journal_date = ""
  10. journal_filename = ""
  11. journal_ext = ""
  12. journal_editor = ""
  13. journal_datefmt = ""
  14.  
  15. #
  16. # Example jour.ini (blog.ini, work.ini, ...)
  17. #
  18. # [settings]
  19. # path=C:\Source\journal
  20. # editor=notepad
  21. # ext=.md
  22. # dateformat=%%Y\%%m-%%d
  23. #
  24. def read_settings(inifile):
  25.     global journal_dirname
  26.     global journal_cwd
  27.     global journal_ext
  28.     global journal_editor
  29.     global journal_datefmt
  30.    
  31.     journal_cwd = sys.path[0]
  32.  
  33.     config = configparser.ConfigParser()
  34.     config.read(os.path.join(journal_cwd, inifile))
  35.  
  36.     try:
  37.         journal_dirname = config.get("settings", "path")
  38.     except:
  39.         journal_dirname = journal_cwd
  40.        
  41.     try:
  42.         journal_editor = config.get("settings", "editor")
  43.     except:
  44.         journal_editor = "notepad"
  45.  
  46.     try:
  47.         journal_datefmt = config.get("settings", "dateformat")
  48.     except:
  49.         print("Unexpected error:", sys.exc_info()[0])
  50.         journal_datefmt = "%Y-%b-%d"
  51.  
  52.     try:
  53.         journal_ext = config.get("settings", "ext")
  54.     except:
  55.         journal_ext = ".txt"
  56.  
  57.     if journal_ext == "":
  58.         journal_ext = ".txt"
  59.        
  60. def main():
  61.     global journal_filename
  62.     global journal_date
  63.     global journal_datefmt
  64.    
  65.     # Default ini file
  66.     inifile = "jour.ini"
  67.  
  68.     # Read optional ini file override as first argument
  69.     if len(sys.argv) > 1:
  70.         inifile = sys.argv[1] + ".ini"
  71.  
  72.     read_settings(inifile)
  73.  
  74.     # Create a new filename of the format "2004-01-03".
  75.     journal_date = time.strftime(journal_datefmt, time.localtime())
  76.  
  77.     # Allow an optional prefix for subjournals.
  78.     journal_name = journal_date
  79.     if len(sys.argv) > 2:
  80.         journal_name = journal_date + "-" + sys.argv[2]
  81.  
  82.     journal_filename = os.path.join(journal_dirname, journal_name + journal_ext)
  83.    
  84.     # Edit the journal entry
  85.     os.system(journal_editor + " " + journal_filename)
  86.    
  87. if __name__ == "__main__":
  88.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement