fe7edaeefff0db9ad6638ec8b6cabb7d8a44e701
[software/python-on-guile.git] / modules / language / python / dir.scm
1 (define-module (language python dir)
2 #:use-module (language python list)
3 #:use-module (language python for)
4 #:use-module (language python dict)
5 #:use-module (language python string)
6 #:use-module (language python bytes)
7 #:use-module (language python number)
8 #:use-module (language python bytes)
9 #:use-module (oop goops)
10 #:use-module (ice-9 vlist)
11 #:use-module (oop pf-objects)
12 #:export (dir))
13
14 (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
15
16 (define-method (dir) (pylist))
17
18 (define (chash-for-each f c)
19 (let ((h (slot-ref c 'h)))
20 (if (is-a? c <pf>)
21 (let ((hh (make-hash-table)))
22 (vhash-fold
23 (lambda (k v s)
24 (when (not (hash-ref hh k))
25 (hash-set! hh k #t)
26 (f k v))
27 s) #f h))
28 (hash-for-each f h))))
29
30 (define (get-from-class c f)
31 (let lp ((pl (ref c '__mro__)))
32 (if (pair? pl)
33 (begin
34 (chash-for-each f (car pl))
35 (lp (cdr pl))))))
36
37 (define-method (dir (o <p>))
38 (if (not (pyclass? o))
39 (aif it (ref o '__dir__)
40 (it)
41 (begin
42 (let ((l1 (aif it (ref o '__dict__)
43 (let ((l (pylist)))
44 (for ((k v : it)) ()
45 (pylist-append! l k))
46 (pylist-sort! l)
47 l)
48 (pylist))))
49 (let* ((h (make-hash-table))
50 (c (ref o '__class__))
51 (l '())
52 (f (lambda (k v) (set! l (cons k l)))))
53 (chash-for-each f o)
54 (get-from-class c f)
55 (hash-for-each (lambda (k v) (pylist-append! l k)) h)
56 (+ (pylist (map symbol->string (sort l <))) l1)))))
57 (let* ((h (make-hash-table))
58 (c o)
59 (l '())
60 (f (lambda (k v) (hash-set! h k #t))))
61 (get-from-class c f)
62 (hash-for-each (lambda (k v) (set! l (cons k l))) h)
63 (to-pylist (map symbol->string (sort l <))))))
64
65 (define-method (dir (o <py-list>))
66 (let ((l1 (pk (pylist-listing))))
67 (if (is-a? o <p>)
68 (let* ((l2 (next-method))
69 (l (+ l1 l2)))
70 (pylist-sort! l)
71 l)
72 l1)))
73
74 (define-method (dir (o <py-hashtable>))
75 (let ((l1 (pyhash-listing)))
76 (if (is-a? o <p>)
77 (let* ((l2 (next-method))
78 (l (+ l1 l2)))
79 (pylist-sort! l)
80 l)
81 l1)))
82
83 (define-method (dir (o <py-string>))
84 (let ((l1 (pystring-listing)))
85 (if (is-a? o <p>)
86 (let* ((l2 (next-method))
87 (l (+ l1 l2)))
88 (pylist-sort! l)
89 l)
90 l1)))
91
92 (define-method (dir (o <py-int>))
93 (let ((l1 (pyint-listing)))
94 (if (is-a? o <p>)
95 (let* ((l2 (next-method))
96 (l (+ l1 l2)))
97 (pylist-sort! l)
98 l)
99 l1)))
100
101 (define-method (dir (o <py-float>))
102 (let ((l1 (pyfloat-listing)))
103 (if (is-a? o <p>)
104 (let* ((l2 (next-method))
105 (l (+ l1 l2)))
106 (pylist-sort! l)
107 l)
108 l1)))
109
110 (define-method (dir (o <py-complex>))
111 (let ((l1 (pycomplex-listing)))
112 (if (is-a? o <p>)
113 (let* ((l2 (next-method))
114 (l (+ l1 l2)))
115 (pylist-sort! l)
116 l)
117 l1)))
118
119 (define-method (dir (o <py-bytes>))
120 (let ((l1 (pybytes-listing)))
121 (if (is-a? o <p>)
122 (let* ((l2 (next-method))
123 (l (+ l1 l2)))
124 (pylist-sort! l)
125 l)
126 l1)))
127
128 (define-method (dir (o <py-bytearray>))
129 (let ((l1 (pybytesarray-listing)))
130 (if (is-a? o <p>)
131 (let* ((l2 (next-method))
132 (l (+ l1 l2)))
133 (pylist-sort! l)
134 l)
135 l1)))
136
137
138 (define-method (dir (o <hashtable> )) (pyhash-listing))
139 (define-method (dir (o <string> )) (pystring-listing))
140 (define-method (dir (o <complex> )) (pycomplex-listing))
141 (define-method (dir (o <real> )) (pyfloat-listing))
142 (define-method (dir (o <integer> )) (pyint-listing))
143 (define-method (dir (o <bytevector> )) (pybytes-listing))
144 (define-method (dir)
145 (let ((l '()))
146 (module-for-each (lambda (m . u)
147 (set! l (cons (symbol->string m) l)))
148 (current-module))
149 (let ((ret (to-pylist l)))
150 (pylist-sort! ret)
151 ret)))
152
153
154