summaryrefslogtreecommitdiff
path: root/modules/language/python/module.scm
blob: eff20f048d5128de2a5658aad7ef35cde5de3dab (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
(define-module (language python module)
  #:use-module (oop pf-objects)
  #:use-module (oop goops)
  #:use-module (ice-9 match)
  #:use-module (system syntax)
  #:use-module (language python exceptions)
  #:use-module (language python yield)
  #:use-module (language python try)
  #:use-module (language python dir)
  #:use-module (language python list)
  #:use-module (language python dict)
  #:export (Module private public import __import__ modules))

(define-syntax-rule (aif it p . x) (let ((it p)) (if it . x)))

(define-syntax-rule (in-scheme x)
  (let ((lan (current-language)))
    (dynamic-wind
	(lambda () (current-language 'scheme))
	(lambda () x)
	(lambda () (current-language lan)))))

(define (private mod)
  ((ref mod '__setprivate__) #t))
(define (public mod)
  ((ref mod '__setprivate__) #f))

(define e (list 'e))

(define _k
  (lambda (k)
    (if (string? k)
	(string->symbol k)
	k)))

(define _m
  (lambda (self)
    (if (rawref self '_private)
	(rawref self '_module)
	(rawref self '_export))))

(define (globals self)  
  (aif it (rawref self '_export)
       it
       (rawref self '_module)))

(define-python-class Module ()
  (define _modules (make-hash-table))
  (define __setprivate__
    (lambda (self p)
      (rawset self '_private p)))

  (define _cont
    (lambda (self id pre l nm skip-error?)
      (if id
	  (aif it (rawref self id)
	       ((ref it '__init__) pre l nm)
	       (begin
		 (rawset self id (Module pre l nm))
		 (_make self pre nm skip-error?)))
          (aif it (and (module-defined? (current-module) (car nm))
                       (module-ref (current-module) (car nm)))
               (if (module? it)
                   (begin
                     ((rawref it '__init__) pre l nm)
                     it)
                   (_make self pre nm skip-error?))
               (_make self pre nm skip-error?)))))

  (define _contupdate
    (lambda (self id pre l nm)
      (if id
	  (aif it (rawref self id)
	       ((ref it '__update__) pre l nm)
	       (rawset self id (Module pre l nm)))
	  #f)))

    (define __init__
      (case-lambda
        ((self pre l nm)
         (match l
           ((name)
            (rawset self '_path (reverse (cons name pre)))	      
            (_cont self #f   (cons name pre) #f (cons name nm) #f))
       
           ((name . (and l (name2 . _)))
            (rawset self '_path (reverse (cons name pre)))
            (_cont self name2 (cons name pre) l  (cons name nm) #t))))
       
        
        ((self l nm)
         (_cont self #f l #f nm #f))

        ((self l)
         (if (pair? l)
             (if (and (> (length l) 3)
                      (equal? (list (list-ref l 0)
                                    (list-ref l 1)
                                    (list-ref l 2))
                              '(language python module)))
                 (__init__ self (reverse '(language python module)) (cdddr l)
                           '())
                 (__init__ self '() (reverse l) '()))
             (__init__ self
                       (append
                        '(language python module)
                        (map string->symbol
                             (string-split l #\.))))))))

  (define __update__
    (case-lambda
     ((self pre l nm)
      (match l
       ((name)
	(_contupdate self #f   (cons name pre) #f (cons name nm)))
       
       ((name . (and l (name2 . _)))
	(_contupdate self name2 (cons name pre) l  (cons name nm)))))
       

     ((self l nm)
      (_contupdate self #f l #f nm))

     ((self l)
      (if (pair? l)
	  (if (and (> (length l) 3)
		   (equal? (list (list-ref l 0)
				 (list-ref l 1)
				 (list-ref l 2))
			   '(language python module)))
	      (__update__ self (reverse '(language python module))
			   (cdddr l) '()))	      
	  (__update__ self
		      (map string->symbol
			   (string-split l #\.)))))))
  
  (define _make
    (lambda (self l nm skip-error?)
      (rawset self '_private #t)
      (if (not (rawref self '_module))
	  (begin
	    (rawset self '__name__ (string-join
                                    (map symbol->string (reverse nm)) "."))
	    (let* ((_module (in-scheme (resolve-module (reverse l))))
                   (public-i (and _module (module-public-interface _module))))
              (if (and (not skip-error?) (not public-i))
                  (raise (ModuleNotFoundError
                          (format #f "No module named ~a"
                                  (rawref self '__name__)))))
              
	      (rawset self '_export   (module-public-interface _module))
	      (rawset self '_module   _module)
	      (hash-set! _modules l self))))))
      
  (define __getattribute__
    (lambda (self k)
      (define (fail)
	(raise (AttributeError "getattr in Module")))
      (let ((k (_k k)))
        (cond
         ((memq k '(__iter__ __repr__ __dir__))
          (lambda () ((rawref self k) self)))
         (else
          (let ((x (aif it (rawref self '_export)
                        (module-ref it k e)
                        e)))
            (if (eq? e x)
                (let ((x (aif it (_m self)
                              (module-ref it k e)
                              e)))
                  (if (eq? e x)
                      (let ((x (rawref self k e)))
                        (if (eq? e x)
                            (fail)
                            x))
                      x))
                x)))))))
  
  (define __setattr__
    (lambda (self k v)
      (let ((k     (_k k))
	    (fail  (lambda () (raise KeyError "setattr in Module" k))))
	(if (rawref self k)
	    (fail)
	    (aif m (rawref self '_module)
		 (catch #t
                   (lambda ()
                     (if (module-defined? m k)
                         (module-set! m k v)
                         (module-define! m k v)))			
                   (lambda x (fail)))
		 (fail))))))

  (define __global_setitem__
    (lambda (self k v)
      (let ((k     (_k k))
	    (fail  (lambda () (raise KeyError "setattr in Module" k))))
        (aif m (rawref self '_module)
             (catch #t
               (lambda ()
                 (if (module-defined? m k)
                     (module-set! m k v)
                     (begin
                       (module-define! m k v)
                       (module-export! m (list k)))))
               (lambda x (fail)))
             (fail)))))

  (define __global_getitem__
    (lambda (self k)
      (let ((k     (_k k))
	    (fail  (lambda () (raise KeyError "global setattr in Module" k))))
        (aif m (rawref self '_export)
             (catch #t
               (lambda ()
                 (if (module-defined? m k)
                     (module-ref m k)
                     (fail)))
               (lambda x (fail)))
             (fail)))))

  (define __global_get__
    (lambda (self k . es)
      (let ((k     (_k k))
	    (fail  (lambda () (raise KeyError "global setattr in Module" k))))
        (aif m (rawref self '_export)
             (catch #t
               (lambda ()
                 (if (module-defined? m k)
                     (module-ref m k)
                     (if (pair? es) (car es) #f)))
               (lambda x (fail)))
             (fail)))))
  
  (define __delattr__
    (lambda (self k)
      (define (fail) (raise KeyError "delattr in Module"))
      (aif m (rawref self '_module)
	  (let ((k (_k k)))
	    (if (module-defined? m k)
		(module-remove!  m k)
		(raise KeyError "delattr of missing key in Module")))
	  (fail))))

  (define __dir__
    (lambda (self)
      (let* ((h (slot-ref self 'h))
	     (l '())
             (m (_m self))
	     (add (lambda (k . u)
                    (if (not (in "-" (symbol->string k)))
                        (set! l (cons (symbol->string k) l))))))
	(hash-for-each add h)
        (if m (module-for-each add m))
	(aif it (rawref self '_export) (module-for-each add it))
        (hash-for-each add (slot-ref self 'h))
	(py-list l))))


  (define __iter__
    (lambda (self)
      (let* ((h (slot-ref self 'h))
	     (l '())
             (m (_m self))
	     (add (lambda (k v)
                    (let ((k (symbol->string k)))
                      (if (and (not (in "-" k)) (variable-bound? v))
                          (set! l (cons (list k (variable-ref v))
                                        l)))))))
        (module-for-each add m)
	(module-for-each add (rawref self '_export))
	l)))

  (define __global_iter__
    (lambda (self)
      (let* ((m (globals self))
             (l '())
	     (add (lambda (k v)
                    (let ((k (symbol->string k)))
                      (if (and (not (in "-" k)) (variable-bound? v))
                          (set! l (cons (list k (variable-ref v))
                                        l)))))))
        (module-for-each add m)
	l)))

	
  
  (define __repr__
    (lambda (self) (format #f "Module(~a)" (rawref self '__name__))))

  (define __getitem__
    (lambda (self k)
      (define k (if (string? k) (string->symbol k) k))
      (__getattribute__ self k)))) 


(define-syntax import
  (lambda (x)
    (syntax-case x ()
      ((_ (a ...) var)
       #`(import-f #,(case (syntax-local-binding #'var)
		       ((lexical)
			#'var)
		       ((global)
			#'(if (module-defined? (current-module)
					       (syntax->datum #'var))
			      var
			      #f))
		       (else
			#f)) a ...)))))

(define (m? x) ((@ (language python module _python) isinstance) x Module))
(define (import-f x f . l)
  (if x
      (if (m? x)
	  (begin (apply (rawref x '__update__) x l) x)
	  (apply f l))
      (apply f l)))

(define-python-class ms (dict)
  (define __getitem__
    (lambda (self k)
      (if (string? k)
          (aif it (py-get (slot-ref self 't) k #f)
               it
               (let* ((l   (map string->symbol (string-split k #\.)))
                      (pth (cons* 'language 'python 'module l)))
                 (Module (reverse pth) (reverse l))))
          (pylist-ref (slot-ref self 't) k))))

  (define get
    (lambda* (self k #:optional (e #f))
      (if (string? k)
          (aif it (py-get (slot-ref self 't) k #f)
               it
               (let* ((l   (map string->symbol (string-split k #\.)))
                      (pth (cons* 'language 'python 'module l)))
                 (Module (reverse pth) (reverse l))))
          (py-get (slot-ref self 't) k e)))))
      
          

(define modules (ms))
(define (__import__ x)
  (let ((x (py-get modules x #f)))
    (if x
        (values)
        (let ((e (Module x)))
          (pylist-set! modules x e)
          e))))

(set! (@@ (oop pf-objects) Module) Module)