Created
January 29, 2012 12:36
-
-
Save mohayonao/1698617 to your computer and use it in GitHub Desktop.
いつ髪の毛を切ったか管理する
This file contains hidden or 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 -*- | |
import os, sys | |
import datetime | |
import dateutil.parser | |
def growl(message): | |
title = u"髪切った?".encode("utf-8") | |
message = message.encode("utf-8") | |
os.system('/usr/local/bin/growlnotify -t "%s" -m "%s"' % (title, message)) | |
def main(): | |
filepath = os.path.join(os.path.abspath(os.path.dirname(__file__)), ".hairdays") | |
reset_date = None | |
if len(sys.argv) == 2: | |
command = sys.argv[1] | |
if command == "r" or command == "reset": | |
reset_date = datetime.date.today() | |
else: | |
try: | |
reset_date = dateutil.parser.parse(command) | |
except ValueError: | |
reset_date = None | |
days = 0 | |
if reset_date is None and os.path.exists(filepath): | |
line = open(filepath).read().strip() | |
try: | |
prev = dateutil.parser.parse(line.strip()).date() | |
days = (datetime.date.today() - prev).days | |
except ValueError: | |
pass | |
if days > 0: | |
growl(u"%d日前に切りました" % days) | |
else: | |
if reset_date is None: | |
reset_date = datetime.date.today() | |
f = open(filepath, "w") | |
f.write(reset_date.isoformat()) | |
f.close() | |
growl(u"リセットしました") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment