Drive GIMP's Python with Clojure forms instead of Python strings.
The facade over the strata of docs/py-strata.md: template collects the
forms, lower promotes them to the Python AST, render and result are
the pipeline, and exec-forms / eval-forms are the only boundary, one
round trip through hive-gimp.exec over the transport port.
(py/eval g
(def img (first (Gimp/get-images)))
[(.get-width img) (.get-height img) ~label])
;; => [2168 2096 "logo"]
eval makes GIMP print its last expression as JSON and parses it back, so
the answer is Clojure data, not a repr. GIMP objects come back as maps with
their id, type and name.
THE FORMS
Names Gimp/get-images is Gimp.get_images; - becomes _.
Calls (f a :k v) is f(a, k=v); a keyword in an argument list always
opens a keyword argument. (Cls. a) constructs.
Methods (.m obj a), (py. obj m a), (. obj m a); attributes
(.-attr obj), (py.- obj attr); chains (py.. obj -attr (m a) n).
Values strings, numbers, nil/true/false, keywords (as strings), vectors
(lists), maps (dicts), sets, (tuple a b).
Exprs arithmetic and comparison operators, and or not, get nth first second last slice count contains? inc dec str, (if c a b),
(fn [x] expr) (a lambda), (for [x xs :when c] expr).
Stmts def set! do let if when when-not doseq dotimes while defn return import try raw.
Imports (import json), (import [numpy :as np]),
(import gi.repository [Gio Gegl]).
Escape (raw "any python") emits its string unchanged.
A new form is a defmethod in hive-gimp.py.lower, never an edit here.
State persists between calls (the plugin execs into one context): a def
here is visible to the next call.
Drive GIMP's Python with Clojure forms instead of Python strings.
The facade over the strata of docs/py-strata.md: `template` collects the
forms, `lower` promotes them to the Python AST, `render` and `result` are
the pipeline, and `exec-forms` / `eval-forms` are the only boundary, one
round trip through `hive-gimp.exec` over the transport port.
(py/eval g
(def img (first (Gimp/get-images)))
[(.get-width img) (.get-height img) ~label])
;; => [2168 2096 "logo"]
`eval` makes GIMP print its last expression as JSON and parses it back, so
the answer is Clojure data, not a repr. GIMP objects come back as maps with
their id, type and name.
THE FORMS
Names `Gimp/get-images` is `Gimp.get_images`; `-` becomes `_`.
Calls `(f a :k v)` is `f(a, k=v)`; a keyword in an argument list always
opens a keyword argument. `(Cls. a)` constructs.
Methods `(.m obj a)`, `(py. obj m a)`, `(. obj m a)`; attributes
`(.-attr obj)`, `(py.- obj attr)`; chains `(py.. obj -attr (m a) n)`.
Values strings, numbers, nil/true/false, keywords (as strings), vectors
(lists), maps (dicts), sets, `(tuple a b)`.
Exprs arithmetic and comparison operators, `and or not`, `get nth first
second last slice count contains? inc dec str`, `(if c a b)`,
`(fn [x] expr)` (a lambda), `(for [x xs :when c] expr)`.
Stmts `def set! do let if when when-not doseq dotimes while defn return
import try raw`.
Imports `(import json)`, `(import [numpy :as np])`,
`(import gi.repository [Gio Gegl])`.
Escape `(raw "any python")` emits its string unchanged.
A new form is a `defmethod` in `hive-gimp.py.lower`, never an edit here.
State persists between calls (the plugin execs into one context): a `def`
here is visible to the next call.(->python forms)Forms as one Python source block.
Forms as one Python source block.
(eval target & body)Run Clojure forms as Python in GIMP. Returns the Outcome, :value being
the last form's value as data.
Run Clojure forms as Python in GIMP. Returns the `Outcome`, `:value` being the last form's value as data.
(eval! target & body)As eval, returning the value and throwing on failure.
As `eval`, returning the value and throwing on failure.
(eval-forms target forms)Run forms in GIMP. An Outcome whose :value is the last form's value as
Clojure data, and whose :stdout is anything the forms printed.
Run `forms` in GIMP. An `Outcome` whose `:value` is the last form's value as Clojure data, and whose `:stdout` is anything the forms printed.
(eval-source forms)The Python an eval of forms sends: every form but the last as a
statement, then the last one's value printed as JSON after a sentinel.
The Python an `eval` of `forms` sends: every form but the last as a statement, then the last one's value printed as JSON after a sentinel.
(exec target & body)Run Clojure forms as Python in GIMP. Returns the Outcome.
Run Clojure forms as Python in GIMP. Returns the `Outcome`.
(exec! target & body)As exec, returning the stdout and throwing on failure.
As `exec`, returning the stdout and throwing on failure.
(exec-forms target forms)Run forms in GIMP. An Outcome whose :value is the captured stdout.
Run `forms` in GIMP. An `Outcome` whose `:value` is the captured stdout.
(forms & body)body as data the compiler reads. ~x is the VALUE of Clojure x, sent as
a Python literal; ~@xs splices each value of xs.
`body` as data the compiler reads. `~x` is the VALUE of Clojure `x`, sent as a Python literal; `~@xs` splices each value of `xs`.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |