changeset 2:70d20a057e84

factored out sjsbox.fs module
author Atul Varma <avarma@mozilla.com>
date Mon, 31 May 2010 05:27:25 -0700
parents c5d1a2d60e45
children b935781e3f89
files sjsbox/box.py sjsbox/fs.py sjsbox/server.py
diffstat 3 files changed, 50 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/sjsbox/box.py	Mon May 31 04:58:05 2010 -0700
+++ b/sjsbox/box.py	Mon May 31 05:27:25 2010 -0700
@@ -2,13 +2,12 @@
 import multiprocessing as mproc
 
 class BoxChild(object):
-    def __init__(self, filename, fullpath, pipe):
-        if filename.endswith('.js'):
+    def __init__(self, f, pipe):
+        if f.name.endswith('.js'):
             import sjsbox.js
-            self.__impl = sjsbox.js.JsBox(open(fullpath).read(),
-                                          fullpath)
+            self.__impl = sjsbox.js.JsBox(f.contents, f.name)
         else:
-            raise ValueError('unknown box type: %s' % filename)
+            raise ValueError('unknown box type: %s' % f)
         self.pipe = pipe
 
     def run(self):
@@ -28,9 +27,8 @@
 class BoxParent(object):
     TIMEOUT = 3.0
 
-    def __init__(self, filename, fullpath):
-        self.filename = filename
-        self.fullpath = fullpath
+    def __init__(self, f):
+        self.file = f
         self.child = None
         self.child_pipe = None
         self.restart()
@@ -39,9 +37,7 @@
         if self.child:
             self.shutdown()
         self.child_pipe, pipe = mproc.Pipe()
-        kwargs = dict(filename=self.filename,
-                      fullpath=self.fullpath,
-                      pipe=pipe)
+        kwargs = dict(f=self.file, pipe=pipe)
         self.child = mproc.Process(target=BoxChild.start,
                                    kwargs=kwargs)
         self.child.start()
@@ -54,10 +50,10 @@
         self.child_pipe.send(('shutdown', None))
         self.child.join(self.TIMEOUT)
         if self.child.is_alive():
-            logging.warn('terminating child process: %s' % self.filename)
+            logging.warn('terminating child process: %s' % self.file)
             self.child_pipe.close()
             self.child.terminate()
         else:
-            logging.info('child process shut down: %s' % self.filename)
+            logging.info('child process shut down: %s' % self.file)
         self.child_pipe = None
         self.child = None
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sjsbox/fs.py	Mon May 31 05:27:25 2010 -0700
@@ -0,0 +1,34 @@
+import os
+
+def is_readable(path):
+    try:
+        os.stat(path)
+    except Exception:
+        return False
+    return True
+
+class File(object):
+    def __init__(self, fullpath, name):
+        self.fullpath = fullpath
+        self.name = name
+
+    @property
+    def mtime(self):
+        return os.stat(self.fullpath).st_mtime
+
+    @property
+    def contents(self):
+        return open(self.fullpath).read()
+
+    def __str__(self):
+        return self.name
+
+class Dir(object):
+    def __init__(self, rootdir):
+        self.rootdir = rootdir
+
+    def __iter__(self):
+        for filename in os.listdir(self.rootdir):
+            fullpath = os.path.join(self.rootdir, filename)
+            if is_readable(fullpath):
+                yield File(fullpath, filename)
--- a/sjsbox/server.py	Mon May 31 04:58:05 2010 -0700
+++ b/sjsbox/server.py	Mon May 31 05:27:25 2010 -0700
@@ -1,4 +1,3 @@
-import os
 import logging
 
 try:
@@ -6,35 +5,26 @@
 except ImportError:
     import simplejson as json
 
+import sjsbox.fs
 from sjsbox.box import BoxParent
 
-def is_readable(path):
-    try:
-        os.stat(path)
-    except Exception:
-        return False
-    return True
-
 class Boxes(object):
     def __init__(self, rootdir):
-        self.rootdir = rootdir
+        self.dir = sjsbox.fs.Dir(rootdir)
         self.boxes = {}
         self.box_mtimes = {}
         self.update()
 
     def update(self):
         visited = {}
-        for filename in os.listdir(self.rootdir):
-            fullpath = os.path.join(self.rootdir, filename)
-            boxname = os.path.splitext(filename)[0]
+        for f in self.dir:
+            boxname = f.name.rsplit('.', 1)[0]
             if boxname not in self.boxes:
-                if not is_readable(fullpath):
-                    continue
                 logging.info('creating box %s' % boxname)
-                self.box_mtimes[boxname] = os.stat(fullpath).st_mtime
-                self.boxes[boxname] = BoxParent(filename, fullpath)
+                self.box_mtimes[boxname] = f.mtime
+                self.boxes[boxname] = BoxParent(f)
             else:
-                box_mtime = os.stat(fullpath).st_mtime
+                box_mtime = f.mtime
                 if box_mtime > self.box_mtimes[boxname]:
                     self.box_mtimes[boxname] = box_mtime
                     self.boxes[boxname].restart()