(define-module (language python dir) #:use-module (language python list) #:use-module (language python for) #:use-module (language python dict) #:use-module (language python string) #:use-module (language python bytes) #:use-module (language python number) #:use-module (language python bytes) #:use-module (oop goops) #:use-module (ice-9 vlist) #:use-module (oop pf-objects) #:export (dir)) (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y))) (define in-p (make-fluid #f)) (define-method (dir x) (py-list)) (define (cont l1 l2) (let ((h (make-hash-table)) (l (py-list))) (for ((x : l1)) () (hash-set! h x #t)) (for ((x : l2)) () (hash-set! h x #t)) (for ((k v : h)) () (pylist-append! l k)) (pylist-sort! l) l)) (define (p x) (if (symbol? x) (symbol->string x) x)) (define (chash-for-each t c) (let ((h (slot-ref c 'h))) (if (is-a? c ) (vhash-fold (lambda (k v s) (hash-set! t (p k) #t)) #f h) (hash-for-each (lambda (k v) (hash-set! t (p k) #t)) h)))) (define (find-in o h c) (aif it (and o (find-in-class c '__dir__ #f)) (for ((k : (it o))) () (hash-set! h (p k) #t)) (chash-for-each h c))) (define (find-in-mro o h l) (let lp ((l l)) (if (pair? l) (begin (find-in o h (car l)) (lp (cdr l)))))) (define-method (dir (o

)) (if (fluid-ref in-p) (next-method) (with-fluids ((in-p #t)) (cont (next-method) (let ((h (make-hash-table))) (find-in-mro #f h (find-in-class o '__mro__ (list o))) (aif cl (find-in-class o '__class__ #f) (find-in-mro o h (find-in-class cl '__mro__ (list cl))) #f) (let ((l (py-list))) (hash-for-each (lambda (k v) (pylist-append! l k)) h) (pylist-sort! l) l)))))) (define-method (dir (o )) (cont (next-method) (let ((l1 (pk (pylist-listing)))) (if (is-a? o

) (let* ((l2 (next-method)) (l (+ l1 l2))) (pylist-sort! l) l) l1)))) (define-method (dir (o )) (cont (next-method) (let ((l1 (pyhash-listing))) (if (is-a? o

) (let* ((l2 (next-method)) (l (+ l1 l2))) (pylist-sort! l) l) l1)))) (define-method (dir (o )) (cont (next-method) (let ((l1 (pystring-listing))) (if (is-a? o

) (let* ((l2 (next-method)) (l (+ l1 l2))) (pylist-sort! l) l) l1)))) (define-method (dir (o )) (cont (next-method) (let ((l1 (pyint-listing))) (if (is-a? o

) (let* ((l2 (next-method)) (l (+ l1 l2))) (pylist-sort! l) l) l1)))) (define-method (dir (o )) (cont (next-method) (let ((l1 (pyfloat-listing))) (if (is-a? o

) (let* ((l2 (next-method)) (l (+ l1 l2))) (pylist-sort! l) l) l1)))) (define-method (dir (o )) (cont (next-method) (let ((l1 (pycomplex-listing))) (if (is-a? o

) (let* ((l2 (next-method)) (l (+ l1 l2))) (pylist-sort! l) l) l1)))) (define-method (dir (o )) (cont (next-method) (let ((l1 (pybytes-listing))) (if (is-a? o

) (let* ((l2 (next-method)) (l (+ l1 l2))) (pylist-sort! l) l) l1)))) (define-method (dir (o )) (cont (next-method) (let ((l1 (pybytesarray-listing))) (if (is-a? o

) (let* ((l2 (next-method)) (l (+ l1 l2))) (pylist-sort! l) l) l1)))) (define-method (dir (o )) (pyhash-listing)) (define-method (dir (o )) (pystring-listing)) (define-method (dir (o )) (pycomplex-listing)) (define-method (dir (o )) (pyfloat-listing)) (define-method (dir (o )) (pyint-listing)) (define-method (dir (o )) (pybytes-listing)) (define-method (dir) (let ((l '())) (module-for-each (lambda (m . u) (set! l (cons (symbol->string m) l))) (current-module)) (let ((ret (to-pylist l))) (pylist-sort! ret) ret)))