da07642cc43c680b353acafbea9cadb025ff3c23
[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 (aif it (ref o '__dict__)
42 (let ((l (pylist)))
43 (for ((k v : it)) ()
44 (pylist-append! l k))
45 (pylist-sort! l)
46 l)
47 (let* ((h (make-hash-table))
48 (c (ref o '__class__))
49 (l '())
50 (f (lambda (k v) (set! l (cons k l)))))
51 (chash-for-each f o)
52 (get-from-class c f)
53 (hash-for-each (lambda (k v) (pylist-append! l k)) h)
54 (to-pylist (map symbol->string (sort l <))))))
55 (let* ((h (make-hash-table))
56 (c o)
57 (l '())
58 (f (lambda (k v) (hash-set! h k #t))))
59 (get-from-class c f)
60 (hash-for-each (lambda (k v) (set! l (cons k l))) h)
61 (to-pylist (map symbol->string (sort l <))))))
62
63 (define-method (dir (o <py-list>))
64 (let ((l1 (pk (pylist-listing))))
65 (if (is-a? o <p>)
66 (let* ((l2 (next-method))
67 (l (+ l1 l2)))
68 (pylist-sort! l)
69 l)
70 l1)))
71
72 (define-method (dir (o <py-hashtable>))
73 (let ((l1 (pyhash-listing)))
74 (if (is-a? o <p>)
75 (let* ((l2 (next-method))
76 (l (+ l1 l2)))
77 (pylist-sort! l)
78 l)
79 l1)))
80
81 (define-method (dir (o <py-string>))
82 (let ((l1 (pystring-listing)))
83 (if (is-a? o <p>)
84 (let* ((l2 (next-method))
85 (l (+ l1 l2)))
86 (pylist-sort! l)
87 l)
88 l1)))
89
90 (define-method (dir (o <py-int>))
91 (let ((l1 (pyint-listing)))
92 (if (is-a? o <p>)
93 (let* ((l2 (next-method))
94 (l (+ l1 l2)))
95 (pylist-sort! l)
96 l)
97 l1)))
98
99 (define-method (dir (o <py-float>))
100 (let ((l1 (pyfloat-listing)))
101 (if (is-a? o <p>)
102 (let* ((l2 (next-method))
103 (l (+ l1 l2)))
104 (pylist-sort! l)
105 l)
106 l1)))
107
108 (define-method (dir (o <py-complex>))
109 (let ((l1 (pycomplex-listing)))
110 (if (is-a? o <p>)
111 (let* ((l2 (next-method))
112 (l (+ l1 l2)))
113 (pylist-sort! l)
114 l)
115 l1)))
116
117 (define-method (dir (o <py-bytes>))
118 (let ((l1 (pybytes-listing)))
119 (if (is-a? o <p>)
120 (let* ((l2 (next-method))
121 (l (+ l1 l2)))
122 (pylist-sort! l)
123 l)
124 l1)))
125
126 (define-method (dir (o <py-bytearray>))
127 (let ((l1 (pybytesarray-listing)))
128 (if (is-a? o <p>)
129 (let* ((l2 (next-method))
130 (l (+ l1 l2)))
131 (pylist-sort! l)
132 l)
133 l1)))
134
135
136 (define-method (dir (o <hashtable> )) (pyhash-listing))
137 (define-method (dir (o <string> )) (pystring-listing))
138 (define-method (dir (o <complex> )) (pycomplex-listing))
139 (define-method (dir (o <real> )) (pyfloat-listing))
140 (define-method (dir (o <integer> )) (pyint-listing))
141 (define-method (dir (o <bytevector> )) (pybytes-listing))
142 (define-method (dir)
143 (let ((l '()))
144 (module-for-each (lambda (m . u)
145 (set! l (cons (symbol->string m) l)))
146 (current-module))
147 (let ((ret (to-pylist l)))
148 (pylist-sort! ret)
149 ret)))
150
151 (define-method (dir (o <procedure>))
152 (let ((ret (to-pylist (map (lambda (x)
153 (let ((x (car x)))
154 (if (symbol? x)
155 (symbol->string x)
156 x)))
157 (procedure-properties o)))))
158 (pylist-sort! ret)
159 ret))
160
161
162