Anonymous avatar Anonymous committed 49703a9

added nom/session.py. can't believe it's been missing all this time.

Comments (0)

Files changed (1)

+
+import nom.filesystem
+from nom.task import Task, TaskExit
+
+import logging
+
+import re, time
+
+class Session(object):
+    '''
+    Session is a not-very-well-defined class that defines a .run()
+    method that is the "main loop" of nom: it runs over all input
+    files and spawns and activates tasks to process them.  It will
+    probably get some revision before too long (at the very least to
+    decouple the source_files iterator from the task spawning stuff,
+    since we need different feeders for incremental updates and the
+    eventual cgi/wsgi backend.
+
+    Basic Usage (as seen in the existing "nomnom" frontend):
+
+        s = Session(sourcedir, outdir)
+        s.run()
+    '''
+
+    def __init__(self,sourcedir, outdir,
+                 exclude_dirs=r'(?:^[._])', exclude_files=r'(?i)(?:\.exe$|~$|^#.*#$|^[._])'):
+
+        self.tasks = []
+
+        self.sourcedir = sourcedir
+        self.outdir = outdir
+
+        if isinstance(exclude_dirs, basestring):
+            exclude_dirs = re.compile(exclude_dirs)
+
+        if isinstance(exclude_files, basestring):
+            exclude_files = re.compile(exclude_files)
+
+        self.exclude_dirs = exclude_dirs
+        self.exclude_files = exclude_files
+
+    def new_task(self, data, env, at_front = False):
+        self.add_task(Task(self, data, env), at_front)
+
+    def add_task(self, task, at_front = False):
+        if at_front:
+            self.tasks.insert(0, task)
+        else:
+            self.tasks.append(task)
+            
+    def next_task(self):
+        try:
+            return self.tasks.pop(0)
+        except IndexError:
+            return None
+
+    def run(self):
+        # XXX move this tracker stuff to another class
+        self.tracker = {}
+        self._run()
+        self.tracker['__last_published'] = time.time()
+
+    def _run(self):
+        src = self.sourcedir
+        out = self.outdir
+
+        for f in nom.filesystem.source_files(self.sourcedir, [self.exclude_dirs], [self.exclude_files]):
+            env = {'input-filename' : f, 'input-dir' : src, 'output-dir' : out} 
+            t = Task(self, None, env)
+            try:
+                t.execute()
+            except TaskExit:
+                logging.debug('caught TaskExit from %s' % t)
+                # normal exit, continue on
+                pass
+
+        for t in self.tasks:
+            try:
+                t.execute()
+            except TaskExit:
+                # normal exit, continue on
+                pass
+            
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.