From 33068122ef1300f97787002fc1411413c3fd47f1 Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Fri, 26 Apr 2019 12:15:35 -0400 Subject: Implement configuration files --- brisket.hy | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/brisket.hy b/brisket.hy index 7d45d36..7e14926 100644 --- a/brisket.hy +++ b/brisket.hy @@ -1,4 +1,6 @@ -(import [os.path [expanduser]]) +(import [configparser [ConfigParser]]) +(import [os [getenv makedirs]]) +(import [os.path [dirname expanduser]]) (import [sys [argv exit stderr]]) (import re) (import [nntplib [NNTP-SSL]]) @@ -7,6 +9,7 @@ (require [hy.contrib.walk [let]]) (setv *auth-source* (expanduser "~/.authinfo.gpg")) +(setv *config-path* (expanduser "~/.config/brisket/config.ini")) (setv *recognized-commands* ["describe-group" "help" "list-group" @@ -26,6 +29,40 @@ (let [handler (get (globals) (.replace command "-" "_"))] (print (. handler __doc__))))) +(defn make-config [] + "Create a default configuration file. + +Creates the configuration directory if it does not exist, as well as an INI file +populated with default configuration values." + (let [config (ConfigParser)] + (assoc config "Server" {}) + (assoc (get config "Server") "Host" "news.eternal-september.org") + (makedirs (dirname *config-path*)) + (with [out (open *config-path* "w+")] + (.write config out)) + config)) + +(defn load-config [] + "Load the configuration file for Brisket. + +Returns a default configuration, written to `*config-path*`, if a configuration +file cannot be found." + (let [config (ConfigParser)] + (if (= 1 (len (.read config *config-path*))) + config + (make-config)))) + +(defn find-host [config] + "Returns the host that Brisket should connect to. + +The lookup order is: + - NNTPSERVER environment variable. + - 'Host' in the 'Server' section of the configuration file." + (or (getenv "NNTPSERVER") + (if (and (in "Server" config) + (in "Host" (get config "Server"))) + (get (get config "Server") "Host")))) + (defn get-user-info [host] (let [process (Popen ["gpg" "-q" "-d" *auth-source*] :stdout PIPE) output (first (.communicate process))] @@ -45,7 +82,13 @@ (err (.format " - {}" command))) (exit 1)) - (let [command (nth argv 1)] + (let [config (load-config) + host (find-host config) + command (nth argv 1)] + (when (is host None) + (err (.format "no host specified")) + (exit 1)) + (unless (in command *recognized-commands*) (err (.format "unrecognized command: {}" command)) (exit 1)) @@ -64,31 +107,31 @@ ;; ;; def list_groups(n, *args): ;; pattern = args[0] if len(args) >= 1 else None -;; +;; ;; _, groups = n.list() -;; +;; ;; for group in groups: ;; if pattern is None or re.match(pattern, group.group): ;; print(group.group) -;; +;; ;; def describe_group(n, *args): ;; if len(args) < 1: ;; err("usage: describe-group [group]") ;; return -;; +;; ;; _, descs = n.descriptions(args[0]) -;; +;; ;; for desc in descs: ;; print(desc) -;; +;; ;; def list_group(n, *args): ;; if len(args) < 1: ;; err("usage: list-group [group]") ;; return -;; +;; ;; _, _, first, last, _ = n.group(args[0]) ;; _, overviews = n.over((first, last)) -;; +;; ;; for article_num, overview in overviews: ;; print("\"{}\" by {} on {}".format( ;; overview.get("subject"), -- cgit v1.3