[Commits] r460 - website

commits at geoext.org commits at geoext.org
Fri Apr 24 07:58:06 CEST 2009


Author: tschaub
Date: 2009-04-24 07:58:06 +0200 (Fri, 24 Apr 2009)
New Revision: 460

Added:
   website/jst.txt
Modified:
   website/jst.py
Log:
basic doctests

Modified: website/jst.py
===================================================================
--- website/jst.py	2009-04-24 00:59:50 UTC (rev 459)
+++ website/jst.py	2009-04-24 05:58:06 UTC (rev 460)
@@ -58,7 +58,7 @@
                 for filename in entries:
                     if filename.endswith(SUFFIX_JS) and not filename.startswith("."):
                         filepath = os.path.join(root, filename)[len(sourcedir)+1:]
-                        jsfile = SourceFile(sourcedir, filepath)
+                        jsfile = SourceFile.from_filename(os.path.join(sourcedir, filepath))
                         if jsfile.context:
                             template_filename = os.path.join(sourcedir, filepath.split(SUFFIX_JS)[0] + SUFFIX_JST)
                             if not os.path.exists(template_filename):
@@ -74,17 +74,24 @@
 
 class SourceFile(object):
     """
-    Represents a Javascript source code file.
+    Represents a JavaScript source code file.
     """
-    def __init__(self, sourcedir, filepath):
-        self.source = open(os.path.join(sourcedir, filepath), "U").readlines()
+    def __init__(self, source):
+        self.source = source
         self._data = _marker
-        self._jst_comments = _marker
+        self._comments = _marker
         self._context = _marker
     
+    @classmethod
+    def from_filename(cls, filename):
+        fh = open(filename, "U")
+        source = fh.readlines()
+        fh.close()
+        return cls(source)
+    
     @property
-    def jst_comments(self):
-        if self._jst_comments == _marker:
+    def comments(self):
+        if self._comments == _marker:
             comments = ()
             inblock = False
             label = None
@@ -113,18 +120,18 @@
                         if len(line) > spaces:
                             line = line[spaces:]
                         block += (line,)
-            self._jst_comments = comments
-        return self._jst_comments
+            self._comments = comments
+        return self._comments
     
     @property
     def data(self):
         if self._data == _marker:
             data = {}
-            for comment in self.jst_comments:
+            for comment in self.comments:
                 label = comment["label"]
                 block = comment["block"]
                 if label.startswith("("):
-                    if label == "(defs)":
+                    if label == "(define)":
                         for defline in block:
                             m = DEF_RE.match(defline)
                             if m:
@@ -147,7 +154,7 @@
                             data[name] += [block]
                     else:
                         data[label] = block
-            if len(self.jst_comments) > 0:
+            if len(self.comments) > 0:
                 self._data = data
             else:
                 self._data = None
@@ -160,7 +167,7 @@
             if data:
                 context = {}
                 for (key, val) in data.items():
-                    if type(val) is type({}):
+                    if isinstance(val, dict):
                         # create ordered lists out of dictionaries
                         context[key] = [val[k] for k in sorted(val.keys())]
                     else:

Added: website/jst.txt
===================================================================
--- website/jst.txt	                        (rev 0)
+++ website/jst.txt	2009-04-24 05:58:06 UTC (rev 460)
@@ -0,0 +1,102 @@
+``jst`` basics
+==============
+
+Consider some code with well formatted comment blocks::
+
+    >>> code = """
+    ... /**
+    ...  *  This is a plain old comment block.
+    ...  */
+    ...  
+    ... /** jst: foo
+    ...  *  This comment block has the foo label.
+    ...  *    There are multiple lines.  Indentation may not decrease
+    ...  *    below the level of the first line.
+    ...  */
+    ... 
+    ... /** jst: bar[]
+    ...  *  Create a list with brackets following the label.
+    ...  */
+    ... 
+    ... /** jst: bar[]
+    ...  *  Keep adding items to a list.
+    ...  */ 
+    ... 
+    ... /** jst: chicken[soup]
+    ...  *  Names in brackets for a sorted list.
+    ...  */
+    ... 
+    ... /** jst: chicken[pie]
+    ...  *  These will be dictionaries in the data and lists in the context.
+    ...  */
+    ... 
+    ... /** jst: chicken[rat]
+    ...  *  This will eventually allow for access to inherited data.
+    ...  */
+    ... 
+    ... /** jst: (define)
+    ...  *  convenient = way
+    ...  *  to = assign
+    ...  *  keys = values
+    ...  */
+    ... """
+
+
+Pretend we read the above from a file::
+
+    >>> import StringIO
+    >>> f = StringIO.StringIO(code)
+    >>> lines = f.readlines()
+    >>> f.close()
+
+Now import jst and create an object representing the lines of code::
+
+    >>> import jst
+    >>> obj = jst.SourceFile(lines)
+
+The comments are parsed as a tuple of dictionaries with 'label' and 'block'
+keys::
+
+    >>> comments = obj.comments
+    >>> len(comments)
+    7
+    >>> comments[0]['label']
+    'foo'
+    >>> comments[1]['label']
+    'bar[]'
+    >>> comments[2]['block']
+    ('Keep adding items to a list.\n',)
+    >>> comments[6]['label']
+    '(define)'
+
+The comment blocks are accessible through the data dictionary.  The values for
+simple labels are strings.  Label values with empty brackets have been
+converted to lists of strings.  Label values with names in brackets have been
+converted to dictionaries.  Items in blocks labeled (defines) have been parsed
+as key, value pairs::
+
+    >>> obj.data['foo']
+    'This comment block has the foo label.\n  There are multiple lines.  Indentation may not decrease\n  below the level of the first line.\n'
+    >>> obj.data['bar']
+    ['Create a list with brackets following the label.\n', 'Keep adding items to a list.\n']
+    >>> type(obj.data['chicken'])
+    <type 'dict'>
+    >>> obj.data['convenient']
+    'way'
+
+The context attribute is a simpler structure, similar to the data but with
+dictionaries as sorted lists::
+
+    >>> from pprint import pprint
+    >>> obj.context.keys()
+    ['bar', 'keys', 'convenient', 'to', 'chicken', 'foo']
+    >>> obj.data.keys() == obj.context.keys()
+    True
+    >>> pprint(obj.data['chicken'])
+    {'pie': 'These will be dictionaries in the data and lists in the context.\n',
+     'rat': 'This will eventually allow for access to inherited data.\n',
+     'soup': 'Names in brackets for a sorted list.\n'}
+    >>> pprint(obj.context['chicken'])
+    ['These will be dictionaries in the data and lists in the context.\n',
+     'This will eventually allow for access to inherited data.\n',
+     'Names in brackets for a sorted list.\n']



More information about the Commits mailing list