File: api-python.lisp

package info (click to toggle)
albert 0.4.10.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,556 kB
  • ctags: 2,014
  • sloc: lisp: 13,587; ansic: 7,729; xml: 843; makefile: 99; sh: 28
file content (307 lines) | stat: -rw-r--r-- 10,140 bytes parent folder | download | duplicates (2)
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
;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: APISPEC -*-

#|

DESC: apispec/api-python.lisp - the code for generating python APIs
Copyright (c) 2000 - Karl Trygve Kalleberg and Stig Erik Sand

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

|#

(in-package :sds-api-apispec)

(defun pythonify-word (word &optional class-name)
  (let* ((wlen (length word))
	 (new-word (make-string wlen :initial-element #\Space))
	 (loc 0)
	 (caps-next nil))
    
    (declare (type fixnum loc wlen))

    (setq wlen (- wlen 1))
    
    (loop for i of-type fixnum from 0 to wlen
	  for c of-type character = (schar word i)
	  do
	  (cond ((eq c #\-)
		 (setq caps-next t))
		(t
		 (if (or caps-next (and class-name (eq i 0)))
		     (setf (schar new-word loc) (char-upcase c))
		   (setf (schar new-word loc) c))
		 (setq caps-next nil)
		 (incf loc))))
    (string-right-trim '(#\Space) new-word)))

;;(trace pythonify-word)
		 	   

(defun py-get-constant-name (const)
  (format nil "GENERATED_CONSTANT_~:@(~a~)" 
	  (pythonify-word (if (typep const 'apispec-constant)
			    (car (apispec-constant.name const))
			  const))))

(defun py-get-class-name (api-name obj)
  (declare (ignore api-name))
;;  (string-downcase (car (apispec-class.name obj))))
  (pythonify-word (car (apispec-class.name obj)) t))

;;(trace py-get-class-name)

(defmethod output-code ((obj apispec-toplevel) (lang api-lang-python) stream (domain (eql :domain)))
  (let ((api-name (car (apispec-toplevel.name obj))))
    (format stream "# This file is autogenerated by the SDS API Generator.~2%")

;    (format stream "package sds.api.~a;~2%" (string-downcase api-name))

    (format stream "import sds.base~2%")
	    
    (format stream "class ~a:~2%" (pythonify-word api-name t))

    (format stream " def printXMLHeader(self, os, tool): ~%")
    (format stream "  self.printXMLHeader(os, tool, \"~a\")~2%" api-name)

    (let ((constants (apispec-toplevel.constants obj)))
      (dolist (c constants)
	(let ((value (car (apispec-constant.val c)))
	      (name (car (apispec-constant.name c))))
	  (format stream " ~a = \"~a\"~%"
		  (py-get-constant-name c)
		  (if value value name)))))
    (format stream "~3%")

    ))

(defmethod output-code ((obj apispec-toplevel) (lang api-lang-python) stream (exports (eql :exports)))
  (let ((api-name (car (apispec-toplevel.name obj)))
	(classes (apispec-toplevel.classes obj)))
    
    (format stream "# This file is autogenerated by the SDS API Generator.~2%")

    (dolist (c classes) 
      (let ((cl-name (py-get-class-name api-name c)))
	(format stream "from ~a import *~2%" cl-name)))

    (format stream "from factory import Factory~2%")

    ;; this one is an evil hack to allow people to stuff in
    ;; a common.py
    (format stream "try:~%  from common import *~%except ImportError:~%  pass~2%")

    
    ))

(defmethod output-code ((obj apispec-toplevel) (lang api-lang-python) stream (factory (eql :factory)))
  
  (let ((api-name (car (apispec-toplevel.name obj))))

    (format stream "# This file is autogenerated by the SDS API Generator.~2%")

;    (format stream "package sds.api.~a;~2%" (string-downcase api-name))

    (format stream "import sds.base~%")
    (format stream "import string~%")
    (format stream "import types~%")
    (format stream "from ~a import ~a~%" (string-downcase api-name) (pythonify-word api-name t))
    
    (dolist (c (apispec-toplevel.classes obj))
      (let ((sm-name (py-get-class-name api-name c)))
	(format stream "import ~a~%" sm-name)))

    (terpri stream)
    
    (format stream "class Factory (sds.base.Factory): ~2%") ;; api-name
    (format stream " def create(self, theClass):~%")
    (format stream "~%")
    (format stream "  self.ptr = None;~%")
    (format stream "  classname = string.lower(theClass);~2%")
      
    (format stream "  if 0:~%")
    (format stream "    pass~%")
    (dolist (c (apispec-toplevel.classes obj))
      (let ((elmname (car (apispec-class.elmname c)))
	    (name (car (apispec-class.name c)))
	    (sm-name (py-get-class-name api-name c)))
	(format stream "  elif string.find(classname, string.lower(~a.~a)) == 0:~%"
		api-name
		(py-get-constant-name (if elmname elmname name)))
	(format stream "   self.ptr = ~a.~a()~%"
		sm-name sm-name)))
      
    (format stream "  else:~%")
    (format stream "   DEBUG_PUT(\"Unknown class '\" + theClass + \"'\")~%")
    (format stream "  return self.ptr~%")
    (format stream "~2%")

    ))

(defmethod output-code ((obj apispec-class) (lang api-lang-python) stream api-name)

  (let ((cl-name (py-get-class-name api-name obj))
	(elmname (car (apispec-class.elmname obj)))
	(name (car (apispec-class.name obj)))
	(base-class "sds.base.Parseable"))

    (format stream "# This file is autogenerated by the SDS API Generator.~2%")

;    (format stream "package sds.api.~a;~2%" (string-downcase api-name))

    (format stream "import sds.base~%")
    (format stream "from sds.base import AttrType~%")
    (format stream "from sds.base import SubElemType~%")
    (format stream "from ~a import ~a~%" (string-downcase api-name) (pythonify-word api-name t))

    (terpri stream)
    
    (format stream "class ~a (~a): ~%" (pythonify-word cl-name t) base-class)
;    (format stream "~%")

    ;;; very important that all attributes will be written before all subelements!!!
    ;;; this should either be enforced better, or be fixed in sds.base.XML_Parseable.get/setContent()

#||    
    (loop for v in (apispec-class.vars obj)
	for iter upfrom 0 by 1 do
	  (format stream " _~a = ~a;~%" 
		  (pythonify-word (car (apispec-var.name v)))
		  iter))
||#

    (format stream "~%")
    
    (format stream " def __init__(self): ~%")
    (format stream "  ~a.__init__(self)~%" base-class)
    
#||
    (let ((attrlen (length (apispec-class.attrs obj))))
      (if (> attrlen 0)
	  (let ((content 
		 (loop for v in (apispec-class.attrs obj) 
		   collect (strcat "\"" (car (apispec-attr.name v)) "\"" ":None"))))
	    (format stream "  self.attrInfo = {~{ ~a,~}} # ~a~%" 
		    content attrlen)
	  )))

    (let ((subelemlen (length (apispec-class.subelems obj))))
      (if (> subelemlen 0)
	  (let ((content 
		 (loop for v in (apispec-class.subelems obj) 
		   collect (strcat "\"" (car (apispec-subelem.name v)) "\":None"))))
	    (format stream "  self.subelemInfo = {~{ ~a,~}} # ~a~%" 
		    content subelemlen)
	  )))
||#
    (format stream "  # self.attrInfo = {}~%")
    (format stream "  # self.subelemInfo = {}~%")

    (let ((childrenlen (length (apispec-class.vars obj))))
      (if (> childrenlen 0)
	  (let ((content (loop for v in (apispec-class.vars obj) 
			   collect (strcat "\"" (car (apispec-var.name v)) "\":" 
					   (if (or
						(string-equal (car (apispec-var.type v))
						    "ptrlist")
						(string-equal (car (apispec-var.type v))
						    "stringlist"))
					       "[]"
					     "None")))))
	    (format stream "  self.children = {~{ ~a,~}} # ~a~%" content childrenlen)
	 ;; (format stream "  self.children = [] # ~a~%" childrenlen)
	  )))

    (format stream "~%")
    
    (let ((attrs (apispec-class.attrs obj)))
      (when attrs
	(loop for a in attrs for iter upfrom 0 by 1 do
	      (format stream "  self.attrInfo[\"~a\"] = sds.base.AttrInfo(\"~a\", AttrType.ATTR_~:@(~a~), \"~a\")~%"
		      (car (apispec-attr.name a))
		      (car (apispec-attr.name a))
		      (car (apispec-attr.type a))
		      (pythonify-word (car (apispec-attr.var  a)))
		      ))))

    (let ((attrs (apispec-class.subelems obj)))
      (when attrs
	(loop for a in attrs for iter upfrom 0 by 1 do
	      (format stream "  self.subelemInfo[\"~a\"] = sds.base.SubElemInfo(~a.~a, SubElemType.SUBELEM_~:@(~a~), \"~a\")~%"
		      (car (apispec-subelem.name a))
		      api-name
		      (py-get-constant-name (car (apispec-subelem.name a)))
		      (car (apispec-subelem.type a))
		      (pythonify-word (car (apispec-subelem.var  a)))
		      ))))

    (format stream "~2%")


    (format stream " def copy(self): ~%")
    (format stream "  return ~a(self);~%" cl-name)
    (format stream "~2%")

    (format stream " def getElementName(self): ~%")
    (format stream "  return ~a.~a;~%"
	    api-name
	    (py-get-constant-name (if elmname elmname name)))
    (format stream "~2%")

;;    (format stream " def __init__(self, c): ~%" cl-name cl-name)
;;    (format stream "  parseable.__init__(c)~%")
;;    (format stream "~2%")
    ))

  
(defmethod output-code ((obj apispec-toplevel) (lang api-lang-python) stream dir-name)

  (declare (ignore stream))
  ;; (format t "Generating to ~a~%" some-name)
  
  (let ((api-name (car (apispec-toplevel.name obj)))
	(classes (apispec-toplevel.classes obj)))
    
    (make-sure-dirs-exist (ensure-dir-name dir-name))

    ;; first elements
    (dolist (c classes)
      (let ((cl-name (py-get-class-name api-name c)))
        (with-open-file (str (merge-pathnames (pathname (strcat dir-name "/" cl-name ".py")))
			     :direction :output
			     :if-exists :supersede
			     :if-does-not-exist :create)
	  (output-code c lang str api-name))))
    
    ;; then factory
    (with-open-file (str (merge-pathnames (pathname (strcat dir-name "/" "factory.py")))
			 :direction :output
			 :if-exists :supersede
			 :if-does-not-exist :create)
      (output-code obj lang str :factory))

    ;; then domain
    (with-open-file (str (merge-pathnames (pathname (strcat dir-name "/" (string-downcase api-name) ".py")))
			 :direction :output
			 :if-exists :supersede
			 :if-does-not-exist :create)
      (output-code obj lang str :domain))

    ;; then module exports
    (with-open-file (str (merge-pathnames (pathname (strcat dir-name "/__init__.py")))
			 :direction :output
			 :if-exists :supersede
			 :if-does-not-exist :create)
      (output-code obj lang str :exports))
    
  ))

#||
(defmethod output-code ((obj apispec-class) (lang api-lang-python) stream api-name)

  (let ((cl-name (py-get-class-name api-name obj)))

    ))
||#