8 Commits
0.2.0 ... 0.3.0

7 changed files with 76 additions and 37 deletions

View File

@ -1,4 +1,6 @@
import daemonize, docopt, json, os, nre, sequtils, times, timeutils
import cliutils, docopt, json, logging, os, re, random, sequtils,
times, timeutils
from posix import SIGTERM, SIGHUP, signal, kill
import strutils except toUpper
from unicode import toUpper
@ -9,31 +11,22 @@ type
DPConfig = tuple[planDir, dateFmt, pidfile, logfile, errfile: string,
notificationSecs: int]
CombinedConfig = object
docopt: Table[string, Value]
json: JsonNode
proc getVal(cfg: CombinedConfig, key, default: string): string =
let argKey = "--" & key
let envKey = key.replace('-', '_').toUpper
let jsonKey = key.replace(re"(-\w)", proc (m: RegexMatch): string = ($m)[1..1].toUpper)
if cfg.docopt[argKey]: return $cfg.docopt[argKey]
elif existsEnv(envKey): return getEnv(envKey)
elif cfg.json.hasKey(jsonKey): return cfg.json[jsonKey].getStr
else: return default
const VERSION = "0.2.0"
const VERSION = "0.3.0"
const NOTE_TITLE = "Daily Notifier v" & VERSION
const timeFmt = "HH:mm"
var args: Table[string, Value]
var cfg: DPConfig
let appName = getAppFilename()
let soundsDir: string = appName[0..(appName.len-15)] & "sounds/navi"
let soundFiles = toSeq(walkFiles(soundsDir & "/*"))
randomize()
proc parseDailyPlan(filename: string): seq[PlanItem] =
result = @[]
debug "debug - Parsing daily plan file: " & filename
if not existsFile(filename): return
result = @[]
var parseState = BeforeHeading
let planItemRe = re"\s*(\d{4})\s+(.*)"
@ -47,25 +40,48 @@ proc parseDailyPlan(filename: string): seq[PlanItem] =
if line.strip.startsWith("## Timeline"): parseState = ReadingPlans
of ReadingPlans:
let match = line.find(planItemRe)
if match.isSome(): result.add((
time: parse(match.get().captures[0], timeFmt),
note: match.get().captures[1]))
# TODO: This is the better code using the nre module:
# let match = line.find(planItemRe)
# if match.isSome(): result.add((
# time: parse(match.get().captures[0], timeFmt),
# note: match.get().captures[1]))
# else: parseState = AfterPlans
#
# Curently there is an incompatibility between the os and nre modules
# (see https://github.com/nim-lang/Nim/issues/4996) so the following code
# is used to avoid using nre until the bug is fixed.
if line.match(planItemRe):
let stripped = line.strip
result.add((
time: parse(stripped[0..4], timeFmt),
note: stripped.substr(5)))
else: parseState = AfterPlans
of AfterPlans: break
else: break
debug "debug - Found " & $result.len & " items."
proc doAndLog(cmd: string): void =
debug "debug - Executing '" & cmd & "'"
discard execShellCmd(cmd)
proc notifyDailyPlanItem(item: PlanItem): void =
let desc = item.time.format(timeFmt) & " - " & item.note
debug "debug - Notifying: " & desc
let soundFile = soundFiles[random(soundFiles.len)]
case hostOS
of "macosx":
discard execShellCmd("osascript -e 'display notification \"" &
item.time.format(timeFmt) & " - " & item.note &
"\" with title \"Daily Notifier v" & VERSION &
"\" sound name \"default\"'")
doAndLog "osascript -e 'display notification \"" &
desc & "\" with title \"" & NOTE_TITLE & "\"'"
doAndLog "ogg123 \"" & soundFile & "\""
of "linux":
discard execShellCmd("notify-send 'Daily Notifier v" & VERSION & "' '" & item.note & "'")
doAndLog "notify-send '" & NOTE_TITLE & "' '" & desc & "'"
doAndLog "paplay \"" & soundFile & "\""
else: quit("Unsupported host OS: '" & hostOS & "'.")
@ -80,6 +96,8 @@ proc loadConfig(s: cint): void {. exportc, noconv .} =
var rcFilename: string =
foldl(rcLocations, if len(a) > 0: a elif existsFile(b): b else: "")
debug "debug - Reloading config file from " & rcFilename
var jsonCfg: JsonNode
try: jsonCfg = parseFile(rcFilename)
except: jsonCfg = newJObject()
@ -100,22 +118,35 @@ proc loadConfig(s: cint): void {. exportc, noconv .} =
proc mainLoop(args: Table[string, Value]): void =
debug "debug - Started daemon main loop."
loadConfig(0)
signal(SIGHUP, loadConfig)
var curDay: TimeInfo = getLocalTime(fromSeconds(0))
var todaysFile = "nofile"
var lastModTime = fromSeconds(0)
var todaysItems: seq[PlanItem] = @[]
while true:
# Check the date
let now = getLocalTime(getTime())
var today = startOfDay(now)
let today = startOfDay(now)
# Load today's plan items
# If we need to change day, look for a new file.
if today != curDay:
curDay = today
let todaysPlanFile =
cfg.planDir & "/" & today.format(cfg.dateFmt) & "-plan.md"
todaysItems = parseDailyPlan(todaysPlanFile)
todaysFile = cfg.planDir & "/" & today.format(cfg.dateFmt) & "-plan.md"
todaysItems = @[]
lastModTime = fromSeconds(0)
# Check for our plan file.
if fileExists(todaysFile):
let fileModTime = getLastModificationTime(todaysFile)
# Check if the file has been modified or we have no itmes
if fileModTime > lastModTime or todaysItems.len == 0:
lastModTime = fileModTime
todaysItems = parseDailyPlan(todaysFile)
# Check to see if any items are happening soon.
let cutoff = now + seconds(cfg.notificationSecs)
@ -155,6 +186,7 @@ Options:
-h --help Print this usage information.
-s --notification-seconds <sec>
-v --verbose Enable verbose output.
Notification period for plan items.
@ -168,18 +200,25 @@ Options:
loadConfig(0)
addHandler(newConsoleLogger(
if args["--verbose"]: lvlAll
else: lvlInfo))
if args["start"]:
# Start our daemon process (if needed)
daemonize(cfg.pidfile, "/dev/null", cfg.logfile, cfg.errfile):
mainLoop(args)
info "daily_notifier: Starting daemon."
let childPid = daemonize(cfg.pidfile, "/dev/null", cfg.logfile, cfg.errfile, proc(): void = mainLoop(args))
if childPid == 0: quit(QuitSuccess) # We are the child... don't need to do anything else.
info "daily_notifier: Started, pid: " & $childPid
elif args["stop"] or args["reconfigure"]:
if not fileExists(cfg.pidfile):
echo "daily_notifier is not running"
info "daily_notifier: not running"
quit(QuitSuccess)
let pid = parseInt(readFile(cfg.pidfile).strip)
info "daily_notifier: Killing process " & $pid
if args["stop"]: discard kill(pid, SIGTERM)
else: discard kill(pid, SIGHUP)

View File

@ -1,6 +1,6 @@
# Package
version = "0.2.0"
version = "0.3.0"
author = "Jonathan Bernard"
description = "Little programs that reads my daily plan and notifies me of upcoming events."
license = "MIT"
@ -8,5 +8,5 @@ bin = @["daily_notifier", "deploy_plans_via_ftp"]
# Dependencies
requires @["nim >= 0.15.0", "docopt", "timeutils", "tempfile", "daemonize"]
requires @["nim >= 0.15.0", "docopt", "timeutils", "tempfile", "cliutils"]

BIN
sounds/navi/hello.ogg Normal file

Binary file not shown.

BIN
sounds/navi/hey.ogg Normal file

Binary file not shown.

BIN
sounds/navi/listen.ogg Normal file

Binary file not shown.

BIN
sounds/navi/look.ogg Normal file

Binary file not shown.

BIN
sounds/navi/watch-out.ogg Normal file

Binary file not shown.