How do I do basic authentication in Emacs Lisp? - authentication

I'm trying to authenticate to an API using basic authentication. It seems like the code below should work, but it's breaking when I run it in the scratch buffer after running it with C-x C-e (says: debugger entered--Lisp error: (wrong-type-argument characterp)).
My code is below. If anyone knows what I need to change to make this work, please let me know! There are basically no complete Emacs Lisp basic authentication examples I could find online, so this would be a huge help.
(setq ztoken "areallyfaketokenrandomtokenwithfakechars")
(setq zsite "https://fake.zendesk.com/api/v2/users/1100000011/related.json")
(let ((url-request-method "GET")
(url-request-extra-headers '(("Content Type" . "application/json")
("Authorization" . ,(concat "Basic "
(base64-encode-string
(concat "joe#fake.com/token" ":" ztoken)))))))
(condition-case nil
(url-retrieve-synchronously
(format zsite))))

As lawlist mentions, your code uses a single-quote instead of a back-tick. Below is an alternative approach, that does not use the back-tick/comma. For more information on backquotes: https://www.gnu.org/software/emacs/manual/html_node/elisp/Backquote.html#Backquote
(let* ((url-request-method "GET")
(base64 (concat "Basic "
(base64-encode-string
(concat "joe#fake.com/token" ":" ztoken))))
(url-request-extra-headers (list (cons "Content Type" "application/json")
(cons "Authorization" base64))))
(condition-case nil
(url-retrieve-synchronously
(format zsite))))

Related

Designing a unit-test framework for writing custom tests in CLIPS for CLIPS rules, using a multi-file setup

I'd like to make a unit-test like framework that allows me to write custom tests for individual rules. I'd like each test to be in it's own file, i.e. test_R1.clp would be the test file for rule R1. Each test should be able to load it's own facts file. I've tried many variations of the following, including using a different defmodule for each file. Is what I'm trying to do even possible in CLIPS? If so, what else is needed to make this work?
I'd like to run my tests via:
$CLIPSDOS64.exe -f2 .\test_all.clp
With the current example, the error I get is
[EXPRNPSR3] Missing function declaration for setup-tests.
I've gotten a single test to work correctly using a unique defmodule for each file (i.e. UNITTEST for the testing framework and R1 for the test_R1 file). However, I would still get errors because of the automatic switching between focus statements when files are loaded, or when functions are defined in other files. I've looked at the basic and advanced CLIPS programming guides, but if I've missed something there, please let me know.
Other specific questions:
Since some tests may load facts that overwrite existing facts, how do I prevent getting errors from redefining existing facts? Do I need to do a (clear) in between running each test?
TestingFramework.clp:
;;; File: TestingFramework.clp
(defglobal ?*tests-counter* = 0)
(defglobal ?*all-tests-passed* = TRUE)
(defglobal ?*failed-tests-counter* = 0)
(deftemplate test_to_run
(slot testid)
(slot testname)
(slot testsetupfunc)
(slot testcheckfunc))
(deffunction test-check (?test-name ?test-condition)
(if (eval ?test-condition)
then (printout t "SUCCESS: Test " ?test-name crlf)
(printout test_results_file "SUCCESS: Test " ?test-name crlf)
(return TRUE)
else (printout t "FAILURE: Test " ?test-name crlf)
(printout test_results_file "FAILURE: Test " ?test-name crlf)
(return FALSE)))
(deffunction setup_tests ()
(open "test_summary_results.txt" test_results_file "w"))
(deffunction finish_tests ()
(close test_results_file))
(deffunction add_test (?test-name ?test-setup-func ?test-check-func)
(bind ?*tests-counter* (+ 1 ?*tests-counter*))
(assert (test_to_run (testid ?*tests-counter*)
(testname ?test-name)
(testsetupfunc ?test-setup-func)
(testcheckfunc ?test-check-func))))
(deffunction run_all_tests ()
(printout t "About to run " ?*tests-counter* " test(s):" crlf)
(do-for-all-facts ((?ttr_fact test_to_run)) TRUE
(funcall (fact-slot-value ?ttr_fact testsetupfunc))
(if (funcall (fact-slot-value ?ttr_fact testcheckfunc))
then (printout t " SUCCESS" crlf)
else (printout t " FAILURE" crlf)
(bind ?*failed-tests-counter* (+ 1 ?*failed-tests-counter*))
(bind ?*all-tests-passed* FALSE)))
(if ?*all-tests-passed*
then (printout t "All " ?*tests-counter* " tests passed successfully." crlf)
else (printout t ?*failed-tests-counter* "/" ?*tests-counter* " tests failed." crlf)))
tests\test_R1.clp:
;;; File: test_R1.clp
;;; Tests for Rule 1
(deffunction R1_TEST_1_SETUP ()
(load* "FluidSystem_facts_demo.clp")
(load* "FluidSystem_rules_demo.clp")
(reset))
(deffunction R1_TEST_1 ()
(send [JacketWaterInletTempReading] put-hasValue 35.0)
(send [JacketWaterInletTempReading] put-hasValueDefined DEFINED)
(send [JacketWaterOutletTempReading] put-hasValue 37.0)
(send [JacketWaterOutletTempReading] put-hasValueDefined DEFINED)
(run)
(return (member$ [DissimilarHighTempFlowRate] (send [CounterFlowHeatExchanger] get-hasIssue))))
test_all.clp:
;;; File: test_all.clp
;;; Run tests via:
;;; CLIPSDOS64.exe -f2 .\test_all.clp
(load* "TestingFramework.clp")
(setup-tests)
;;; Test R1
(load* "tests\\test_R1.clp")
(add_test (test_to_run "R1_TEST_1" R1_TEST_1_SETUP R1_TEST_1))
(clear) ;; unsure if this is needed
;;; ... more tests to follow
(run_all_tests)
(finish_tests)
The CLIPS regression tests use a framework that might serve your needs. You can download it (clips_feature_tests_640.zip) from one of the 6.4 download directories (sourceforge.net/projects/clipsrules/files/CLIPS/6.40_Beta_3/ for the current beta release). To run the tests, launch CLIPS in the same directory and execute a (batch "testall.tst") command. Using the CLIPS terminal application, you can also run them from the shell with "clips -f testall.tst". When execution completes you can look at the *.rsl files in the Results directory for the results. If difference occurs you can use a diff program to compare the contents of the .out file in the Actual directory with the contents of the Expected directory.
The framework uses the batch command to automatically load and run the test cases. The dribble-on command is used to capture the output of each test case and place it in its own file in the Actual directory. The framework allows you to run all of the test cases (using the testall.tst batch file) or you can run any of the individual test cases by running the .tst batch file associated with the test.

Is there an (idiomatic) way of testing the result of an IO function in Clojure?

I have a function that saves some text to a file:
(defn save-keypair
"saves keypair to ~/.ssb-clj/secret"
[pair file-path]
(let [public-key-string (->> (:public pair) (.array) (byte-array) (b64/encode) (bs/to-string))
secret-key-string (->> (:secret pair) (.array) (byte-array) (b64/encode) (bs/to-string))]
(spit file-path (str "Public Key: " public-key-string))
(spit file-path (str "\nPrivate Key: " secret-key-string) :append true)))
It works fine (currently checking via just opening the file and looking at it myself). However, I'd like to write an actual test to check that everything is working correctly. Is there an idiomatic way of doing this in Clojure?
Look into using with-redefs, as part of your unit tests. In your case, you probably want to merge the writing of the public and private keys into a single form which we can exploit for the test:
;; compute public-key-string and private-key-string as before
(let [contents (format "Public Key: %s\nPrivate Key: %s"
public-key-string secret-key-string)]
(spit file-path contents)
A test could be something like:
(deftest saving-keypair
(testing "Successful save"
(let [file-mock (atom nil)]
;; During this test we redefine `spit` to save into the atom defined above
(with-redefs [spit (fn [path data] (reset! file-mock {:path path :data data}))]
;; Perform IO action
(save-keypair "~/.ssb-clj/secret" {:public "XXXX" :private "YYYYY"})
;; Test if the expected data was saved in the file-mock
(is (= {:path "~/.ssb-clj/secret" :data "Public key: XXXYYYZZZ\nXXXYYYZZ"}
#file-mock))
Use Java interop with File
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/File.html
In particular, see File.createTempFile() and either file.delete() or file.deleteOnExit(). So you create a temp file, use that in your unit test, reading the file that you just wrote and verifying contents. Then either delete the file explicitly (ideally inside of try/finally) with auto-delete as a backup.
Depending on how you set up the expected results in your tests, you may find the following useful:
clojure.string/trim
tupelo.string/collapse-whitespace
tupelo.string/nonblank=
These helper functions are especially useful for text file output, where the presence of a trailing newline char can be OS dependent. They are also helpful to ignore differences due to the "newline" being CR, CR/LF, or LF.

Racket equivalents of python's __repr__ and __str__?

In python objects, overriding the methods __repr__ and __str__ of an object allows one to provide "unambiguous" and "human-readable" representations of the object, respectively. How does one achieve similar behavior in Racket?
I came across the printable<%> interface here, which seems like it should be usable for this purpose, but I haven't been able to get it to work quite as I expect it to. Building on the standard "fish" example from the docs:
(define fish%
(class* object% (printable<%>)
(init size) ; initialization argument
(super-new) ; superclass initialization
;; Field
(define current-size size)
;; Public methods
(define/public (get-size)
current-size)
(define/public (grow amt)
(set! current-size (+ amt current-size)))
(define/public (eat other-fish)
(grow (send other-fish get-size)))
;; implement printable interface
(define/public (custom-print port quoting-depth)
(print "Print called!"))
(define/public (custom-write port)
(print "Write called!"))
(define/public (custom-display port)
(print "Display called!"))))
This is the output I get:
> (define f (new fish% [size 10]))
> f
"Display called!""Display called!""Print called!"
> (print f)
"Write called!""Print called!"
> (display f)
"Display called!""Display called!"
> (write f)
"Write called!""Write called!"
>
So the question is threefold:
Why does it behave this way, i.e. with the multiple methods being invoked on an apparently singular rendering of the object?
What should the methods custom-print, custom-write, and custom-display evaluate to? Should they simply return a string, or should they actually entail the side effect of printing, writing, or displaying, as the case may be? E.g. should the custom-write method invoke the write function internally?
Is this the right construct to use for this purpose at all? If not, is there one / what is it?
As for
Why does it behave this way, i.e. with the multiple methods being invoked on an apparently singular rendering of the object?
You have accidently used print in write, so writing the value, will first print the value.
(define/public (custom-write port)
(print "Write called!"))
A similar problem is present in display.
Also remember to print/write/display to the proper port.
Try
#lang racket
(define fish%
(class* object% (printable<%>)
(init size) ; initialization argument
(super-new) ; superclass initialization
;; Field
(define current-size size)
;; Public methods
(define/public (get-size)
current-size)
(define/public (grow amt)
(set! current-size (+ amt current-size)))
(define/public (eat other-fish)
(grow (send other-fish get-size)))
;; implement printable interface
(define/public (custom-print port quoting-depth)
(print (~a "Print " current-size "\n") port))
(define/public (custom-write port)
(write (~a "Write " current-size "\n") port))
(define/public (custom-display port)
(display (~a "Display " current-size "\n") port))))
In the repl you will see:
> (define f (new fish% [size 10]))
> f
"Print 10\n"
> (display f)
Display 10
> (write f)
"Write 10\n"
Another answer has already helped you find the problem in your code—you need to use the port given as an argument, not the implicit (current-output-port)—but the explanation isn't quite right. To tackle your questions in reverse order:
Is this the right construct to use for this purpose at all? If not, is there one / what is it?
Yes, printable<%> is the right construct to use to customize printing of a class-based object. More generally, a struct type that is not a class can customize printing for instance through the gen:custom-write generic interface or the low-level prop:custom-write struct type property, which is used to implement printable<%>.
What should the methods custom-print, custom-write, and custom-display evaluate to? Should they simply return a string, or should they actually entail the side effect of printing, writing, or displaying, as the case may be? E.g. should the custom-write method invoke the write function internally?
The methods should actually do the side-effect of performing IO on the port they are given as an argument. They should use the corresponding function (e.g. write for custom-write, print for custom-print) internally for recursively printing/writing/displaying values in fields. On the other hand, when directly emitting particular characters, they should generally use functions like write-char, write-string, or printf. The docs for gen:custom-write give an example of a tuple datatype that prints as <1, 2, "a">: it uses write-string for the angle-brackets and commas, but recursive print/write/display for the elements of the tuple.
Why does it behave this way, i.e. with the multiple methods being invoked on an apparently singular rendering of the object?
This is the most involved part of your question. Printing in Racket is customizable through several hooks: for a few examples, see current-print, port-write-handler, global-port-print-handler, and make-tentative-pretty-print-output-port. Many of these customization hooks use intermediate ports in the process of producing output.
One thing that is not a part of the explanation is the fact that you used print in your implementation, particularly as print is bound to the normal Racket function by lexical scope, not to a method of your object.
As an illustration, consider the following adaptation of your example, which reports to the (current-output-port) the identity of the port given as an argument to the method:
#lang racket
(define report
(let ([next-id 0]
[id-cache (make-hash)])
(λ (op port)
(printf "~a ~a ~v\n"
op
(hash-ref id-cache
port
(λ ()
(define id next-id)
(hash-set! id-cache port id)
(set! next-id (add1 next-id))
id))
port))))
(define fish%
(class* object% (printable<%>)
(super-new)
;; implement printable interface
(define/public (custom-print port quoting-depth)
(report "custom-print " port))
(define/public (custom-write port)
(report "custom-write " port))
(define/public (custom-display port)
(report "custom-display" port))))
(define f (new fish%))
f
(print f)
(newline)
(display f)
(newline)
(write f)
In DrRacket, this generates the output:
custom-display 0 #<output-port:null>
custom-display 1 #<output-port:null>
custom-print 2 #<printing-port>
custom-display 3 #<output-port:null>
custom-display 4 #<output-port:null>
custom-print 5 #<printing-port>
custom-display 6 #<output-port:null>
custom-display 7 #<printing-port>
custom-display 8 #<output-port:null>
custom-write 9 #<printing-port>
while at the command line, the output is:
$ racket demo.rkt
custom-write 0 #<output-port:null>
custom-print 1 #<output-port:redirect>
custom-write 2 #<output-port:null>
custom-print 3 #<output-port:redirect>
custom-display 4 #<output-port:null>
custom-display 5 #<output-port:redirect>
custom-write 6 #<output-port:null>
custom-write 7 #<output-port:redirect>

Is there an Awk- or Lisp-like programming language that can process a stream of s-expressions?

I have been creating some PCB footprints in KiCad recently, which are stored in s-expression files with data that looks like this:
(fp_text user %R (at 0 5.08) (layer F.Fab)
(effects (font (size 1 1) (thickness 0.15)))
)
(fp_line (start -27.04996 -3.986) (end -27.24996 -3.786) (layer F.Fab) (width 0.1))
(pad "" np_thru_hole circle (at 35.56 0) (size 3.175 3.175) (drill 3.175) (layers *.Cu *.Mask)
(clearance 1.5875))
(pad 96 smd rect (at 1.25 3.08473) (size 0.29972 1.45034) (layers F.Cu F.Paste F.Mask)
(clearance 0.09906))
I would like to be able to write shell one-liners to efficiently edit multiple parameters. I would normally use Awk for something like this, but the recursive nature of s-expressions makes it ill-suited for the task. I would like to know if there is a programming language with an interpreter designed to handle piped data and can process s-expressions natively. Perhaps a data-driven dialect of Lisp would do this, but I'm not sure where to look.
In summary, I would like to be able to make quick edits to an s-expression file in a similar manner to the way Awk lets me process columns of data line-by-line; only in the case of s-expressions the processing would be performed level-by-level.
Example: find all of the pad expressions of type smd with (size 0.29972 1.45034), and renumber each one based its position.
Simple script
Here is an example in Common Lisp, assuming your input is in file "/tmp/ex.cad" (it could also be obtained by reading the output stream of a process).
The main processing loop consists in opening the file in order to obtain an input stream in (which is automatically closed at the end of with-open-file), loop over all forms in the file, process them and possibly output them to standard output. You could complexify the process as much as you want, but the following is good enough:
(with-open-file (in #"/tmp/ex.cad")
(let ((*read-eval* nil))
(ignore-errors
(loop (process-form (read in))))))
Suppose you want to increase the width of fp_line entries, ignore fp_text and otherwise print the form unmodified, you could define process-form as follows:
(defun process-form (form)
(destructuring-bind (header . args) form
(print
(case header
(fp_line (let ((width (assoc 'width args)))
(when width (incf (second width) 3)))
form)
(fp_text (return-from process-form))
(t form)))))
Running the previous loop would then output:
(FP_LINE (START -27.04996 -3.986) (END -27.24996 -3.786) (LAYER F.FAB) (WIDTH 3.1))
(PAD "" NP_THRU_HOLE CIRCLE (AT 35.56 0) (SIZE 3.175 3.175) (DRILL 3.175) (LAYERS *.CU *.MASK) (CLEARANCE 1.5875))
(PAD 96 SMD RECT (AT 1.25 3.08473) (SIZE 0.29972 1.45034) (LAYERS F.CU F.PASTE F.MASK) (CLEARANCE 0.09906))
More safety
From there, you can build more elaborate pipelines, with the help of pattern matching or macros if you want. You have to take into account some safety measures, like binding *read-eval* to nil, using with-standard-io-syntax
and binding *print-circte* to T as suggested by tfb, disallowing fully qualified symbols (by having #\: signal an error), etc. Ultimately, like Shell scripts one-liners, the amount of precautions you add is based on how much you trust your inputs:
;; Load libraries
(ql:quickload '(:alexandria :optima))
;; Import symbols in current package
(use-package :optima)
(use-package :alexandria)
;; Transform source into a stream
(defgeneric ensure-stream (source)
(:method ((source pathname)) (open source))
(:method ((source string)) (make-string-input-stream source))
(:method ((source stream)) source))
;; make reader stop on illegal characters
(defun abort-reader (&rest values)
(error "Aborting reader: ~s" values))
Dedicated package for KiCad symbols (exporting is optional):
(defpackage :kicad
(:use)
(:export #:fp_text
#:fp_line
#:pad
#:size))
Loop over forms:
(defmacro do-forms ((form source &optional result) &body body)
"Loop over forms from source, eventually return result"
(with-gensyms (in form%)
`(with-open-stream (,in (ensure-stream ,source))
(with-standard-io-syntax
(let ((*read-eval* nil)
(*print-circle* t)
(*package* (find-package :kicad))
(*readtable* (copy-readtable)))
(set-macro-character #\: #'abort-reader nil)
(loop
:for ,form% := (read ,in nil ,in)
:until (eq ,form% ,in)
:do (let ((,form ,form%)) ,#body)
:finally (return ,result)))))))
Example:
;; Print lines at which there is a size parameter, and its value
(let ((line 0))
(labels ((size (alist) (second (assoc 'kicad:size alist)))
(emit (size) (when size (print `(:line ,line :size ,size))))
(process (options) (emit (size options))))
(do-forms (form #P"/tmp/ex.cad")
(match form
((list* 'kicad:fp_text _ _ options) (process options))
((list* 'kicad:fp_line options) (process options))
((list* 'kicad:pad _ _ _ options) (process options)))
(incf line))))
Output
(:LINE 2 :SIZE 3.175)
(:LINE 3 :SIZE 0.29972)
Just write a simple Lisp or Scheme script which loops on reading and processes recursively your s-expr as required. On Linux I would recommend using Guile (a good Scheme interpreter) or perhaps Clisp (a simple Common Lisp implementation) or even SBCL (a very powerful Common Lisp).
(You might consider DSSSL, but in your case it is overkill)
Notice that your sample input is not an S-expression, because (layer F.Fab) is not one (since after the dot you should have another s-expression, not an atom like Fab). I guess it is a typo and should be (layer "F.Fab"); or maybe your KiCad software don't process S-expressions, but some other input language (which should be specified, probably in EBNF notation) inspired by S-expressions.
Notice also that KiCad is a free software and has a community with forums and a mailing list. Perhaps you should ask your actual problem there?
PS. We don't know what transformation you have in mind, but Scheme and Common Lisp are really fit for such tasks. In most cases they are extremely simple to code (probably a few lines only).

How do I create an Emacs SQL buffer?

I can connect manually by doing:
M-x sql-postgres
and type the User, Database, Host, Server
I'd like to do something like:
(connect-foo (user "me")(database "bar")(host "earth"))
Solution so far:
I grabbed some code from sql.el and changed it so I can use it non-interactively.
(defun sql-create-buffer (profile)
"derived from sql-product-interactive in sql.el which is"
"covered by GNU General Public License version 3."
(setq sql-user (cdr (assoc 'user profile))
sql-password (cdr (assoc 'password profile))
sql-server (cdr (assoc 'server profile))
sql-database (cdr (assoc 'database profile)))
(setq product 'postgres) ;;(cdr (assoc 'product profile)))
(when (sql-product-feature :sqli-connect product)
(if (comint-check-proc "*SQL*")
(pop-to-buffer "*SQL*")
;; Connect to database.
(message "Login...")
(funcall (sql-product-feature :sqli-connect product))
;; Set SQLi mode.
(setq sql-interactive-product product)
(setq sql-buffer (current-buffer))
(sql-interactive-mode)
;; All done.
(message "Login...done")
(pop-to-buffer sql-buffer))))
(setq bar-profile '(
(product . "postgres")
(user . "me")
(password . "")
(server . "earth")
(database . "bar")))
I don't know what to do with the 'product' parameter, so I left it hard coded for now.
It is 'postgres in sql.el.
(sql-create-buffer bar-profile)
It seems that package is not intended to be used non-interactively (probably to discourage writing login info into init files, which is a perfectly sound principle).
However, if you really want to avoid answering the questions, you can simply redefine the query function, like this:
(defun sql-get-login (&rest what)
(setq sql-user "me"
sql-password "secret"
sql-server "localhost"
sql-database "MyDB"))