Last active
August 29, 2015 14:05
-
-
Save robinandeer/28798d28a6277e421594 to your computer and use it in GitHub Desktop.
Script to automatically populate a today.txt file. Read more: http://johnhenrymuller.com/today.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
click |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""If nothing else, today I am going to %(goal)s. | |
I am going to do this by %(task_one)s then %(task_two)s then %(task_three)s. | |
If I do this and only this, today will be a good day. | |
Have a great day! :) | |
""" | |
from __future__ import unicode_literals | |
from codecs import open | |
import os | |
import shutil | |
import time | |
import click | |
from click.termui import style | |
@click.command() | |
@click.option( | |
'-b', '--backup', type=click.Path(), default='~/today-backup', | |
help='location to backup old today.txt files') | |
def today(backup): | |
"""Write tasks in progressive present tense; "doing", "eating".""" | |
# set up prompt prefix | |
app = style('[today] ', fg='green') | |
# find out absolute path to text file | |
today_path = os.path.expanduser('~/Desktop/today.txt') | |
backup_path = os.path.expanduser(backup) | |
if os.path.exists(today_path): | |
# get time of file creation | |
created_at = time.ctime(os.path.getmtime(today_path)) | |
# create the backup folder unless it exists | |
if not os.path.exists(backup_path): | |
os.makedirs(backup_path) | |
# move the existing file by renaming it with creation time | |
shutil.move(today_path, os.path.join(backup_path, created_at + '.txt')) | |
# find out what is to be accomplished and how | |
goal = click.prompt(app + 'If nothing else, today I am going to') | |
task_one = click.prompt(app + 'I am going to do this by') | |
task_two = click.prompt(app + '...then') | |
task_three = click.prompt(app + '...then') | |
# open new file for writing | |
with open(today_path, 'w', encoding='utf-8') as handle: | |
# use docstring as template for new file, fill in blanks | |
handle.write(__doc__ % dict( | |
goal=goal, | |
task_one=task_one, | |
task_two=task_two, | |
task_three=task_three | |
)) | |
click.echo(app + style('Have a great day! :)', fg='yellow')) | |
if __name__ == '__main__': | |
today() |
Now the script backs up old today.txt files in ~/today-backup/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script will also back up earlier files by renaming them with the date of file creation. For now it keeps all files on the desktop though.