Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
aff927b4f4 | |||
7c7695b891 | |||
6ab23c7c84 | |||
136e0ade02 | |||
3a4905ff6c | |||
15a893d99f |
@ -1,8 +1,8 @@
|
||||
## Personal Time Keeping API Interface
|
||||
## ===================================
|
||||
|
||||
import asyncdispatch, base64, bcrypt, cliutils, docopt, jester, json, logging,
|
||||
ospaths, sequtils, strutils, os, tables, times, uuids
|
||||
import asyncdispatch, base64, bcrypt, cliutils, docopt, httpcore, jester, json, logging,
|
||||
sequtils, strutils, os, tables, times, uuids
|
||||
|
||||
import nre except toSeq
|
||||
|
||||
@ -39,7 +39,7 @@ proc loadApiConfig*(json: JsonNode): PtkApiCfg =
|
||||
|
||||
template halt(code: HttpCode,
|
||||
headers: RawHeaders,
|
||||
content: string): typed =
|
||||
content: string) =
|
||||
## Immediately replies with the specified request. This means any further
|
||||
## code will not be executed after calling this template in the current
|
||||
## route.
|
||||
@ -61,10 +61,10 @@ template checkAuth(cfg: PtkApiCfg) =
|
||||
var user {.inject.}: PtkUser = PtkUser()
|
||||
|
||||
try:
|
||||
if not request.headers.hasKey("Authorization"):
|
||||
if not headers(request).hasKey("Authorization"):
|
||||
raiseEx "No auth token."
|
||||
|
||||
let headerVal = request.headers["Authorization"]
|
||||
let headerVal = headers(request)["Authorization"]
|
||||
if not headerVal.startsWith("Basic "):
|
||||
raiseEx "Invalid Authorization type (only 'Basic' is supported)."
|
||||
|
||||
|
@ -9,6 +9,8 @@ type
|
||||
Timeline* = tuple[name: string, marks: seq[Mark]]
|
||||
## Representation of a timeline: a name and sequence of Marks.
|
||||
|
||||
OffsetFrom = enum Year, Month, Day, None
|
||||
|
||||
const STOP_MSG* = "STOP"
|
||||
|
||||
let NO_MARK*: Mark = (
|
||||
@ -20,11 +22,18 @@ const ISO_TIME_FORMAT* = "yyyy-MM-dd'T'HH:mm:ss"
|
||||
## The canonical time format used by PTK.
|
||||
|
||||
const TIME_FORMATS* = @[
|
||||
"yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd HH:mm:ss",
|
||||
"yyyy-MM-dd'T'HH:mm", "yyyy-MM-dd HH:mm",
|
||||
"MM-dd'T'HH:mm:ss", "MM-dd HH:mm:ss",
|
||||
"MM-dd'T'HH:mm", "MM-dd HH:mm",
|
||||
"HH:mm:ss", "H:mm:ss", "H:mm", "HH:mm" ]
|
||||
(fmtStr: "yyyy-MM-dd'T'HH:mm:ss", offsetFrom: OffsetFrom.None),
|
||||
(fmtStr: "yyyy-MM-dd HH:mm:ss", offsetFrom: OffsetFrom.None),
|
||||
(fmtStr: "yyyy-MM-dd'T'HH:mm", offsetFrom: OffsetFrom.None),
|
||||
(fmtStr: "yyyy-MM-dd HH:mm", offsetFrom: OffsetFrom.None),
|
||||
(fmtStr: "MM-dd'T'HH:mm:ss", offsetFrom: OffsetFrom.Year),
|
||||
(fmtStr: "MM-dd HH:mm:ss", offsetFrom: OffsetFrom.Year),
|
||||
(fmtStr: "MM-dd'T'HH:mm", offsetFrom: OffsetFrom.Year),
|
||||
(fmtStr: "MM-dd HH:mm", offsetFrom: OffsetFrom.Year),
|
||||
(fmtStr: "HH:mm:ss", offsetFrom: OffsetFrom.Day),
|
||||
(fmtStr: "H:mm:ss", offsetFrom: OffsetFrom.Day),
|
||||
(fmtStr: "H:mm", offsetFrom: OffsetFrom.Day),
|
||||
(fmtStr: "HH:mm", offsetFrom: OffsetFrom.Day) ]
|
||||
## Other time formats that PTK will accept as input.
|
||||
|
||||
proc getOrFail*(n: JsonNode, key: string, objName: string = ""): JsonNode =
|
||||
@ -40,7 +49,23 @@ proc getIfExists*(n: JsonNode, key: string): JsonNode =
|
||||
proc parseTime*(timeStr: string): DateTime =
|
||||
## Helper to parse time strings trying multiple known formats.
|
||||
for fmt in TIME_FORMATS:
|
||||
try: return parse(timeStr, fmt)
|
||||
try:
|
||||
let now = now()
|
||||
let parsed = parse(timeStr, fmt.fmtStr)
|
||||
case fmt.offsetFrom:
|
||||
of OffsetFrom.None:
|
||||
return parsed
|
||||
of OffsetFrom.Year:
|
||||
return initDateTime(parsed.monthday, parsed.month, now.year,
|
||||
parsed.hour, parsed.minute, parsed.second, parsed.nanosecond,
|
||||
now.timezone)
|
||||
of OffsetFrom.Month:
|
||||
return initDateTime(parsed.monthday, now.month, now.year,
|
||||
parsed.hour, parsed.minute, parsed.second, parsed.nanosecond,
|
||||
now.timezone)
|
||||
of OffsetFrom.Day:
|
||||
return initDateTime(now.monthday, now.month, now.year, parsed.hour,
|
||||
parsed.minute, parsed.second, parsed.nanosecond, now.timezone)
|
||||
except: discard nil
|
||||
|
||||
raise newException(Exception, "unable to interpret as a date: " & timeStr)
|
||||
@ -112,5 +137,3 @@ proc getLastIndex*(marks: seq[Mark]): int =
|
||||
while idx >= 0 and marks[idx].summary == STOP_MSG: idx -= 1
|
||||
if idx < 0: result = -1
|
||||
else: result = idx
|
||||
|
||||
|
||||
|
@ -1 +1 @@
|
||||
const PTK_VERSION* = "1.0.6"
|
||||
const PTK_VERSION* = "1.0.11"
|
34
ptk.nim
34
ptk.nim
@ -4,9 +4,9 @@
|
||||
## Simple time keeping CLI
|
||||
|
||||
import algorithm, docopt, json, langutils, logging, os, nre, std/wordwrap,
|
||||
sequtils, sets, strutils, tempfile, terminal, times, uuids
|
||||
sequtils, sets, strutils, sugar, tempfile, terminal, times, uuids
|
||||
|
||||
import timeutils except `-`;
|
||||
import timeutils except `-`
|
||||
|
||||
import private/util
|
||||
import private/api
|
||||
@ -43,8 +43,7 @@ proc writeMarks(timeline: Timeline, indices: seq[int], includeNotes = false): vo
|
||||
writeLine(stdout, "No marks match the given criteria.")
|
||||
return
|
||||
|
||||
var idxs = indices.sorted(
|
||||
proc(a, b: int): int = cmp(marks[a].time, marks[b].time))
|
||||
var idxs = indices.sorted((a, b) => cmp(marks[a].time, marks[b].time))
|
||||
|
||||
let largestInterval = now - marks[idxs.first].time
|
||||
let timeFormat =
|
||||
@ -149,7 +148,8 @@ proc edit(mark: Mark): Mark =
|
||||
close(tempFile)
|
||||
tempFile = nil
|
||||
|
||||
discard os.execShellCmd "$EDITOR " & tempFileName & " </dev/tty >/dev/tty"
|
||||
let editor = getEnv("EDITOR", "vim")
|
||||
discard os.execShellCmd editor & " " & tempFileName & " </dev/tty >/dev/tty"
|
||||
|
||||
var markPart = Time
|
||||
var notes: seq[string] = @[]
|
||||
@ -172,15 +172,15 @@ proc filterMarkIndices(timeline: Timeline, args: Table[string, Value]): seq[int]
|
||||
|
||||
let marks = timeline.marks
|
||||
let now = getTime().local
|
||||
let allIndices = sequtils.toSeq(0..<marks.len).filterIt(marks[it].summary != STOP_MSG).toSet
|
||||
let allIndices = sequtils.toSeq(0..<marks.len).filterIt(marks[it].summary != STOP_MSG).toHashSet
|
||||
let union = args["--or"]
|
||||
|
||||
var selected =
|
||||
if union: initSet[int]()
|
||||
if union: initHashSet[int]()
|
||||
else: allIndices
|
||||
|
||||
template filterMarks(curSet: HashSet[int], pred: untyped): untyped =
|
||||
var res: HashSet[int] = initSet[int]()
|
||||
var res: HashSet[int] = initHashSet[int]()
|
||||
if union:
|
||||
for mIdx {.inject.} in allIndices:
|
||||
if pred: res.incl(mIdx)
|
||||
@ -278,8 +278,9 @@ Options:
|
||||
-e --edit Open the mark in an editor.
|
||||
-f --file <file> Use the given timeline file.
|
||||
-g --tags <tags> Add the given tags (comma-separated) to the selected marks.
|
||||
-G --remove-tags <tagx> Remove the given tag from the selected marks.
|
||||
-G --remove-tags <tags> Remove the given tag from the selected marks.
|
||||
-h --help Print this usage information.
|
||||
|
||||
-m --matching <pattern> Restric the selection to marks matching <pattern>.
|
||||
-n --notes <notes> For add and amend, set the notes for a time mark.
|
||||
-t --time <time> For add and amend, use this time instead of the current time.
|
||||
@ -311,11 +312,11 @@ Options:
|
||||
".ptkrc", $getEnv("PTKRC"), $getEnv("HOME") & "/.ptkrc"]
|
||||
|
||||
var ptkrcFilename: string =
|
||||
foldl(ptkrcLocations, if len(a) > 0: a elif existsFile(b): b else: "")
|
||||
foldl(ptkrcLocations, if len(a) > 0: a elif fileExists(b): b else: "")
|
||||
|
||||
var cfg: JsonNode
|
||||
var cfgFile: File
|
||||
if not existsFile(ptkrcFilename):
|
||||
if not fileExists(ptkrcFilename):
|
||||
warn "ptk: could not find .ptkrc file."
|
||||
ptkrcFilename = $getEnv("HOME") & "/.ptkrc"
|
||||
try:
|
||||
@ -337,7 +338,7 @@ Options:
|
||||
"ptk.log.json"]
|
||||
|
||||
var timelineLocation =
|
||||
foldl(timelineLocations, if len(a) > 0: a elif existsFile(b): b else: "")
|
||||
foldl(timelineLocations, if len(a) > 0: a elif fileExists(b): b else: "")
|
||||
|
||||
# Execute commands
|
||||
if args["init"]:
|
||||
@ -348,7 +349,7 @@ Options:
|
||||
let filesToMerge = args["<timeline>"]
|
||||
let timelines = filesToMerge.mapIt(loadTimeline(it))
|
||||
|
||||
let names = timelines.mapIt(it.name).toSet
|
||||
let names = timelines.mapIt(it.name).toHashSet
|
||||
let mergedName = sequtils.toSeq(names.items).foldl(a & " + " & b)
|
||||
var merged: Timeline = (
|
||||
name: mergedName,
|
||||
@ -386,7 +387,7 @@ Options:
|
||||
time: if args["--time"]: parseTime($args["--time"]) else: now,
|
||||
summary: STOP_MSG,
|
||||
notes: args["--notes"] ?: "",
|
||||
tags: (args["--tags"] ?: "").split({',', ';'}).filterIt(not it.isNilOrWhitespace))
|
||||
tags: (args["--tags"] ?: "").split({',', ';'}).filterIt(not it.isEmptyOrWhitespace))
|
||||
|
||||
timeline.marks.add(newMark)
|
||||
|
||||
@ -428,7 +429,7 @@ Options:
|
||||
time: if args["--time"]: parseTime($args["--time"]) else: now,
|
||||
summary: args["<summary>"] ?: "",
|
||||
notes: args["--notes"] ?: "",
|
||||
tags: (args["--tags"] ?: "").split({',', ';'}).filterIt(not it.isNilOrWhitespace))
|
||||
tags: (args["--tags"] ?: "").split({',', ';'}).filterIt(not it.isEmptyOrWhitespace))
|
||||
|
||||
if args["--edit"]: newMark = edit(newMark)
|
||||
|
||||
@ -490,8 +491,7 @@ Options:
|
||||
mark.tags = mark.tags.deduplicate
|
||||
if args["--remove-tags"]:
|
||||
let tagsToRemove = (args["--remove-tags"] ?: "").split({',', ';'})
|
||||
mark.tags = mark.tags.filter(proc (t: string): bool =
|
||||
anyIt(tagsToRemove, it == t))
|
||||
mark.tags = mark.tags.filter((t) => not anyIt(tagsToRemove, it == t))
|
||||
if args["--time"]:
|
||||
try: mark.time = parseTime($args["--time"])
|
||||
except: raise newException(ValueError,
|
||||
|
12
ptk.nimble
12
ptk.nimble
@ -1,6 +1,6 @@
|
||||
# Package
|
||||
|
||||
version = "1.0.6"
|
||||
version = "1.0.11"
|
||||
author = "Jonathan Bernard"
|
||||
description = "Personal Time Keeper"
|
||||
license = "MIT"
|
||||
@ -15,11 +15,11 @@ requires @[
|
||||
"tempfile",
|
||||
"isaac >= 0.1.3",
|
||||
"bcrypt",
|
||||
"jester 0.4.1",
|
||||
"https://git.jdb-labs.com/jdb/nim-lang-utils.git",
|
||||
"https://git.jdb-labs.com/jdb/nim-cli-utils.git",
|
||||
"https://git.jdb-labs.com/jdb/nim-time-utils.git >= 0.5.2",
|
||||
"https://git.jdb-labs.com/jdb/update-nim-package-version"
|
||||
"jester 0.5.0",
|
||||
"https://git.jdb-software.com/jdb/nim-lang-utils.git",
|
||||
"https://git.jdb-software.com/jdb/nim-cli-utils.git >= 0.6.5",
|
||||
"https://git.jdb-software.com/jdb/nim-time-utils.git >= 0.5.2",
|
||||
"https://git.jdb-software.com/jdb/update-nim-package-version"
|
||||
]
|
||||
|
||||
task updateVersion, "Update the version of this package.":
|
||||
|
Reference in New Issue
Block a user