summaryrefslogtreecommitdiff
path: root/modules/language/python/dir.scm
blob: ecd85cf7bb97d03cc70077c403d8f7510c064d6f (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(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 (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-method (dir) (pylist))

(define (get-from-class c f)
  (let lp ((c c))
    (hash-for-each f c)
    (let lpp ((pl (ref c '__parents__)))
      (if (pair? pl)
          (begin
            (lp (car pl))
            (lpp (cdr pl)))))))

(define (get-from-class-f c f)
  (let lp ((c c))
    (vhash-fold f 0 c)
    (let lpp ((pl (ref c '__parents__)))
      (if (pair? pl)
          (begin
            (lp (car pl))
            (lpp (cdr pl)))))))

(define-method (dir (o <p>))
  (if (pyclass? o)
      (aif it (ref o '__dir__)
           (it)
           (aif it (ref o '__dict__)
                (let ((l (pylist)))
                  (for ((k v : it)) ()
                       (pylist-append! l k))
                  l)
                (let* ((h (make-hash-table))
                       (c (ref o '__class__))
                       (l (pylist))
                       (f (lambda (k v) (pylist-append! h k #t))))
                  (hash-for-each f o)
                  (get-from-class c f)
                  (hash-for-each (lambda (k v) (pylist-append! l k)) h)
                  (pylist-sort! l)
                  l)))
      (let* ((h (make-hash-table))
             (c o)
             (l '())
             (f (lambda (k v) (pylist-append! h k #t))))
        (get-from-class c f)
        (hash-for-each (lambda (k v) (set! l (cons k l))) h)
        (to-pylist (map symbol->string (sort l <))))))

(define-method (dir (o <pf>))
  (if (pyclass? o)
      (aif it (ref o '__dir__)
           (it)
           (aif it (ref o '__dict__)
                (let ((l (pylist)))
                  (for ((k v : it)) ()
                       (pylist-append! l k))
                  l)
                (let* ((h (make-hash-table))
                       (c (ref o '__class__))
                       (l (pylist))
                       (f (lambda (k v s) (pylist-append! h k #t))))
                  (vhash-fold f 0 o)
                  (get-from-class-f c f)
                  (hash-for-each (lambda (k v) (pylist-append! l k)) h)
                  (pylist-sort! l)
                  l)))
      (let* ((h (make-hash-table))
             (c o)
             (l '())
             (f (lambda (k v s) (pylist-append! h k #t))))
        (get-from-class-f c f)
        (hash-for-each (lambda (k v) (set! l (cons k l))) h)
        (to-pylist (map symbol->string (sort l <))))))

(define-method (dir (o <py-list>     )) (pylist-listing))
(define-method (dir (o <hashtable>   )) pyhash-listing)
(define-method (dir (o <py-hashtable>)) pyhash-listing)
(define-method (dir (o <string>      )) string-listing)