summaryrefslogtreecommitdiff
path: root/modules/language/python/module.scm
blob: 9884c2b466aa9c91efa14b7ce8841120400bc973 (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
(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)
  #:export (Module private public import))

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

(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-python-class Module ()
  (define _modules (make-hash-table))
  (define __setprivate__
    (lambda (self p)
      (rawset self '_isprivate p)))

  (define _cont
    (lambda (self id pre l nm)
      (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)))
	  (_make self pre nm))))

  (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)
	(set self '_path (reverse (cons name pre)))	      
	(_cont self #f   (cons name pre) #f (cons name nm)))
       
       ((name . (and l (name2 . _)))
	(set self '_path (reverse (cons name pre)))
	(_cont self name2 (cons name pre) l  (cons name nm)))))
       

     ((self l nm)
      (_cont 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)))
	      (__init__ self (reverse '(language python module)) (cdddr l) '())
	      #f)
	  (__init__ self
		    (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)
      (rawset self '_private #f)
      (if (not (rawref self '_module))
	  (begin
	    (set self '__dict__ self)
	    (set self '__name__ (string-join
				 (map symbol->string (reverse nm)) "."))
	    (let ((_module (in-scheme (resolve-module (reverse l)))))
	      (set self '_export (module-public-interface _module))
	      (set self '_module _module)
	      (hash-set! _modules l self))))))
      
  (define __getattribute__
    (lambda (self k)
      (define (fail)
	(raise KeyError "getattr in Module"))
	       
      (if (rawref self '_module)
	  (let ((k (_k k))
		(m (_m self)))
	    (let ((x (module-ref m k e)))
	      (if (eq? e x)
		  (fail)
		  x)))
	  (fail))))

  (define __setattr__
    (lambda (self k v)
      (let ((k     (_k k))
	    (fail  (lambda () (raise KeyError "getattr in Module"))))
	(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 x)))
		 (fail))))))

  (define __delattr__
    (lambda (self k)
      (define (fail) (raise KeyError "getattr 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 __repr__
    (lambda (self) (format #f "Module(~a)" (ref self '__name__))))

  (define __getitem__
    (lambda (self k)
      (define k (if (string? k) (string->symbol k) k))
      (__getattr__ self k)))
  
  (define __iter__
    (lambda (self)
      (define m (_m self))
      ((make-generator ()
       (lambda (yield)
	 (define l '())
	 (define (f k v) (set! l (cons (list (symbol->string k) v) l)))
	 (module-for-each f m)
	 (let lp ((l l))
	   (if (pair? l)
	       (begin
		 (apply yield (car l))
		 (lp (cdr l)))))))))))



(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__) l) x)
	  (apply f l))
      (apply f l)))