(if (not windows?) (use-modules (ice-9 popen)))
(use-modules (ice-9 textual-ports) (json parser))

; We use Python to convert the data yamls like the authors.yml to json, that we can parse in scheme.
; Windows does not like Pipes, so we use a tmpfile instead.
; Be sure you have PyYAML installed. On Windows that could be done for example like "py -m pip install PyYAML"
(define (yml-file->scm filename)
  (if windows?
    (let* ((port (make-tmpfile #f))
            (tmpfilepath (port-filename port))
            (ignore (close-port port))
            (python_code (string-append "import sys, yaml, json; f = open(r'" tmpfilepath "', 'w'); f.write(json.dumps(yaml.safe_load(open(r'" filename "')))); f.close()"))
            (status (system (string-append (search-executable '("python3" "python" "py")) " -X utf8 -c \"" python_code "\"")))
            (readport (open-file tmpfilepath "r" #:encoding "UTF-8"))
            (json (get-string-all readport)))
      (close-port readport)
      (delete-file tmpfilepath)
      (json-string->scm (if (status:exit-val status) json "{}")))
    (let* ((python_code (string-append "import sys, yaml, json; print(json.dumps(yaml.safe_load(open(r'" filename "'))))"))
            (pipe (open-pipe (string-append "PYTHONHOME='' " (search-executable '("python3" "python" "py")) " -X utf8 -c \"" python_code "\"") OPEN_READ))
            (json (get-string-all pipe)))
      (close-pipe pipe)
      (json-string->scm json))))

(define (parse-yml-file filename) (resolve-inherits (yml-file->scm filename)))