1919
2020from __future__ import absolute_import , division , print_function , unicode_literals
2121
22+ import StringIO
2223import os
2324
2425import six
4849from shinysdr .values import SubscriptionContext
4950
5051
51- def _make_static_resource (pathname ):
52+ def _make_static_resource (pathname , cls = static . File ):
5253 # str() because if we happen to pass unicode as the pathname then directory listings break (discovered with Twisted 16.4.1).
53- r = static . File (str (pathname ),
54+ r = cls (str (pathname ),
5455 defaultType = b'text/plain' ,
5556 ignoredExts = [b'.html' ])
5657 r .contentTypes [b'.csv' ] = b'text/csv'
@@ -172,6 +173,66 @@ def announce(self, open_client):
172173 self .__log .info ('Visit {url}' , url = url )
173174
174175
176+ class ConcatenatedReaders (object ):
177+ def __init__ (self , files ):
178+ self .__files = files
179+ self .__current_file = 0
180+
181+ def seek (self , offset ):
182+ for i , f in enumerate (self .__files ):
183+ f .seek (0 , os .SEEK_END )
184+ length = f .tell ()
185+ if offset > length :
186+ offset -= length
187+ continue
188+ f .seek (offset , os .SEEK_SET )
189+ self .__current_file = i
190+ return
191+
192+ def read (self , n = - 1 ):
193+ out = defaultstr ("" )
194+ while n != 0 and self .__current_file < len (self .__files ):
195+ part = self .__files [self .__current_file ].read (n )
196+ out += part
197+ if n < 0 or len (part ) < n :
198+ self .__current_file += 1
199+ if self .__current_file < len (self .__files ):
200+ self .__files [self .__current_file ].seek (0 , os .SEEK_SET )
201+ n -= len (part )
202+ return out
203+
204+ def close (self ):
205+ for f in self .__files :
206+ f .close ()
207+
208+
209+ class WrappedStaticFile (static .File ):
210+ prefix = ""
211+ suffix = ""
212+
213+ def openForReading (self ):
214+ f = self .open ()
215+ return ConcatenatedReaders ([
216+ StringIO .StringIO (defaultstr (self .prefix )),
217+ f ,
218+ StringIO .StringIO (defaultstr (self .suffix )),
219+ ])
220+
221+ def getFileSize (self ):
222+ return len (self .prefix ) + self .getsize () + len (self .suffix )
223+
224+
225+ class CommonJSStaticFile (WrappedStaticFile ):
226+ """
227+ Serves a CommonJS-style source file with a RequireJS wrapper.
228+ """
229+ prefix = """define(function (require, exports, module) {
230+ """
231+ suffix = """
232+ });
233+ """
234+
235+
175236def _put_root_static (wcommon , container_resource ):
176237 """Place all the simple resources, that are not necessarily sourced from files but at least are unchanging and public."""
177238
@@ -184,6 +245,12 @@ def _put_root_static(wcommon, container_resource):
184245 client .putChild (name , _make_static_resource (os .path .join (deps_path , name )))
185246 for name in ['measviz.js' , 'measviz.css' ]:
186247 client .putChild (name , _make_static_resource (os .path .join (deps_path , 'measviz/src' , name )))
248+ geodesy = SlashedResource ()
249+ client .putChild ('geodesy' , geodesy )
250+ for name in ['latlon-spherical.js' , 'dms.js' ]:
251+ geodesy .putChild (name , _make_static_resource (
252+ os .path .join (deps_path , 'geodesy' , name ),
253+ CommonJSStaticFile ))
187254
188255 # Link deps into /test/.
189256 test = container_resource .children ['test' ]
0 commit comments