Compare commits

..

No commits in common. "main" and "0.3.0" have entirely different histories.
main ... 0.3.0

2 changed files with 30 additions and 18 deletions

View File

@ -1,7 +1,7 @@
import cliutils, docopt, json, logging, os, nre, random, sequtils,
import cliutils, docopt, json, logging, os, re, random, sequtils,
times, timeutils
from posix import SIGTERM, SIGHUP, signal, kill, Pid
from posix import SIGTERM, SIGHUP, signal, kill
import strutils except toUpper
from unicode import toUpper
@ -11,7 +11,7 @@ type
DPConfig = tuple[planDir, dateFmt, pidfile, logfile, errfile: string,
notificationSecs: int]
const VERSION = "0.3.3"
const VERSION = "0.3.0"
const NOTE_TITLE = "Daily Notifier v" & VERSION
const timeFmt = "HH:mm"
@ -19,12 +19,12 @@ var args: Table[string, Value]
var cfg: DPConfig
let appName = getAppFilename()
let soundsDir: string = appName[0..(appName.len-15)] & "sounds/navi"
let soundFiles = seqUtils.toSeq(walkFiles(soundsDir & "/*"))
let soundFiles = toSeq(walkFiles(soundsDir & "/*"))
randomize()
proc parseDailyPlan(filename: string): seq[PlanItem] =
debug "Parsing daily plan file: " & filename
debug "debug - Parsing daily plan file: " & filename
result = @[]
@ -40,25 +40,37 @@ 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 "Found " & $result.len & " items."
debug "debug - Found " & $result.len & " items."
proc doAndLog(cmd: string): void =
debug "Executing '" & cmd & "'"
debug "debug - Executing '" & cmd & "'"
discard execShellCmd(cmd)
proc notifyDailyPlanItem(item: PlanItem): void =
let desc = item.time.format(timeFmt) & " - " & item.note
debug "Notifying: " & desc
debug "debug - Notifying: " & desc
let soundFile = soundFiles[random(soundFiles.len)]
case hostOS
@ -77,14 +89,14 @@ proc loadConfig(s: cint): void {. exportc, noconv .} =
# Find and parse the .dailynotificationrc file
let rcLocations = @[
if args["--config"]: $args["--config"] else:"",
if args["--config"]: $args["<cfgFile>"] else:"",
".dailynotificationrc", $getEnv("DAILY_NOTIFICATION_RC"),
$getEnv("HOME") & "/.dailynotificationrc"]
var rcFilename: string =
foldl(rcLocations, if len(a) > 0: a elif existsFile(b): b else: "")
debug "Reloading config file from " & rcFilename
debug "debug - Reloading config file from " & rcFilename
var jsonCfg: JsonNode
try: jsonCfg = parseFile(rcFilename)
@ -106,7 +118,7 @@ proc loadConfig(s: cint): void {. exportc, noconv .} =
proc mainLoop(args: Table[string, Value]): void =
debug "Started daemon main loop."
debug "debug - Started daemon main loop."
loadConfig(0)
signal(SIGHUP, loadConfig)
var curDay: TimeInfo = getLocalTime(fromSeconds(0))
@ -205,7 +217,7 @@ Options:
info "daily_notifier: not running"
quit(QuitSuccess)
let pid: Pid = cast[Pid] (parseInt(readFile(cfg.pidfile).strip))
let pid = parseInt(readFile(cfg.pidfile).strip)
info "daily_notifier: Killing process " & $pid
if args["stop"]: discard kill(pid, SIGTERM)

View File

@ -1,6 +1,6 @@
# Package
version = "0.3.3"
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.18.0", "docopt", "timeutils", "tempfile", "cliutils"]
requires @["nim >= 0.15.0", "docopt", "timeutils", "tempfile", "cliutils"]