Commits

David Chambers committed 372034f

Reordered class definitions.

Comments (0)

Files changed (1)

 update_template = loader.get_template('update.dhtml')
 youtube_template = loader.get_template('video/youtube.dhtml')
 
+
 class Resource(object):
     def __init__(self, path):
         super(Resource, self).__init__()
             aliases = updated
         self._aliases = aliases
 
+
+class Category(Resource):
+    @classmethod
+    def toplevel(cls):
+        if INDEX_CACHE_SECONDS:
+            cache_key = 'mango:toplevel:%s' % BASE_URL
+            toplevel = cache.get(cache_key)
+            if toplevel is not None:
+                logger.debug('Document tree retrieved from cache')
+            else:
+                toplevel = Category(DOCUMENTS_PATH)
+                cache.set(cache_key, toplevel, INDEX_CACHE_SECONDS)
+                logger.debug('Document tree created and cached')
+        else:
+            toplevel = Category(DOCUMENTS_PATH)
+
+        return toplevel
+
+    def __init__(self, dirpath):
+        super(Category, self).__init__(path=dirpath)
+        self._dirpath = dirpath
+        self.name = re.match(RE['alias=canon'],
+                os.path.basename(dirpath)).group('canon')
+        self.pages = []
+        self.posts = []
+        self.subcategories = []
+
+        for name in [f for f in os.listdir(dirpath) if not f.startswith('.') and not re.search(RE['control_characters'], f)]:
+            path = os.path.join(dirpath, name)
+            if os.path.isdir(path):
+                category = Category(path)
+                self.subcategories.append(category)
+            else:
+                self.add_document(path)
+
+        self.subcategories.sort(key=lambda category: category.name)
+        self.pages.sort(key=lambda page: page.title)
+
+    def add_document(self, filepath):
+        if POST_CACHE_SECONDS:
+            cache_key = 'mango:%s' % filepath
+            document, mod_time = cache.get(cache_key, (None, None)) # retrieve Document
+            if document is not None and mod_time == os.path.getmtime(filepath):
+                logger.debug('Document object retrieved from cache: %s' % filepath)
+            else: # modified or not in cache, so create and cache a new Document object
+                document = Document(filepath)
+                cache.set(cache_key, (document, os.path.getmtime(filepath)), POST_CACHE_SECONDS)
+                logger.debug('Document object created and cached: %s' % filepath)
+        else:
+            document = Document(filepath)
+
+        if document.type == 'page':
+            self.pages.append(document)
+        else:
+            length = len(self.posts)
+            for index, post in enumerate(self.posts):
+                if document.datetime < post.datetime:
+                    break
+            else:
+                index = length
+
+            prev_index = index - 1
+            next_index = index
+
+            if 0 <= prev_index < length:
+                self.posts[prev_index].next = document
+                document.previous = self.posts[prev_index]
+
+            if 0 <= next_index < length:
+                self.posts[next_index].previous = document
+                document.next = self.posts[next_index]
+
+            self.posts.insert(index, document)
+
+        return self
+
+    def descendants(self, pages=False):
+        documents = self.posts[::-1]
+        if pages:
+            documents += self.pages[:]
+        for subcategory in self.subcategories:
+            documents += subcategory.descendants(pages)
+        return documents
+
+    def archives(self):
+        archives = []
+        posts = self.descendants()
+        if posts:
+            posts.sort(key=lambda post: post.datetime, reverse=True)
+            year = posts[0].datetime.year
+            month = posts[0].datetime.month
+            these_posts = []
+            for post in posts:
+                if post.datetime.year == year and post.datetime.month == month:
+                    these_posts.append(post)
+                else:
+                    archives.append((year, month, these_posts))
+                    year, month = post.datetime.year, post.datetime.month
+                    these_posts = [post]
+            archives.append((year, month, these_posts))
+        return archives
+
+    def find_match(self, urlpath):
+        if urlpath in self._aliases:
+            return self
+        for document in self.pages + self.posts:
+            if urlpath in document._aliases:
+                return document
+        for subcategory in self.subcategories:
+            match = subcategory.find_match(urlpath)
+            if match:
+                return match
+        return None
+
+    def _tags(self):
+        tags = []
+        for document in self.pages + self.posts:
+            tags += [tag for tag in document.meta.get('tags', [])]
+        for subcategory in self.subcategories:
+            tags += subcategory._tags()
+        return tags
+
+    def tags(self):
+        tags = {}
+        for tag in self._tags():
+            tags[tag] = tags.get(tag, 0) + 1
+        return [(key, value) for key, value in sorted(tags.items(),
+                key=lambda pair: pair[0].lower())]
+
+    def __str__(self):
+        return self.name
+
+    def __unicode__(self):
+        return self.name
+
+
 class Document(Resource):
     def __init__(self, filepath=None, parent=None):
         if parent is None and filepath:
 
     def __unicode__(self):
         return u''.join((getattr(self.master(), 'title', 'Untitled Document'), self.apt(' (update)')))
-
-class Category(Resource):
-    @classmethod
-    def toplevel(cls):
-        if INDEX_CACHE_SECONDS:
-            cache_key = 'mango:toplevel:%s' % BASE_URL
-            toplevel = cache.get(cache_key)
-            if toplevel is not None:
-                logger.debug('Document tree retrieved from cache')
-            else:
-                toplevel = Category(DOCUMENTS_PATH)
-                cache.set(cache_key, toplevel, INDEX_CACHE_SECONDS)
-                logger.debug('Document tree created and cached')
-        else:
-            toplevel = Category(DOCUMENTS_PATH)
-
-        return toplevel
-
-    def __init__(self, dirpath):
-        super(Category, self).__init__(path=dirpath)
-        self._dirpath = dirpath
-        self.name = re.match(RE['alias=canon'],
-                os.path.basename(dirpath)).group('canon')
-        self.pages = []
-        self.posts = []
-        self.subcategories = []
-
-        for name in [f for f in os.listdir(dirpath) if not f.startswith('.') and not re.search(RE['control_characters'], f)]:
-            path = os.path.join(dirpath, name)
-            if os.path.isdir(path):
-                category = Category(path)
-                self.subcategories.append(category)
-            else:
-                self.add_document(path)
-
-        self.subcategories.sort(key=lambda category: category.name)
-        self.pages.sort(key=lambda page: page.title)
-
-    def add_document(self, filepath):
-        if POST_CACHE_SECONDS:
-            cache_key = 'mango:%s' % filepath
-            document, mod_time = cache.get(cache_key, (None, None)) # retrieve Document
-            if document is not None and mod_time == os.path.getmtime(filepath):
-                logger.debug('Document object retrieved from cache: %s' % filepath)
-            else: # modified or not in cache, so create and cache a new Document object
-                document = Document(filepath)
-                cache.set(cache_key, (document, os.path.getmtime(filepath)), POST_CACHE_SECONDS)
-                logger.debug('Document object created and cached: %s' % filepath)
-        else:
-            document = Document(filepath)
-
-        if document.type == 'page':
-            self.pages.append(document)
-        else:
-            length = len(self.posts)
-            for index, post in enumerate(self.posts):
-                if document.datetime < post.datetime:
-                    break
-            else:
-                index = length
-
-            prev_index = index - 1
-            next_index = index
-
-            if 0 <= prev_index < length:
-                self.posts[prev_index].next = document
-                document.previous = self.posts[prev_index]
-
-            if 0 <= next_index < length:
-                self.posts[next_index].previous = document
-                document.next = self.posts[next_index]
-
-            self.posts.insert(index, document)
-
-        return self
-
-    def descendants(self, pages=False):
-        documents = self.posts[::-1]
-        if pages:
-            documents += self.pages[:]
-        for subcategory in self.subcategories:
-            documents += subcategory.descendants(pages)
-        return documents
-
-    def archives(self):
-        archives = []
-        posts = self.descendants()
-        if posts:
-            posts.sort(key=lambda post: post.datetime, reverse=True)
-            year = posts[0].datetime.year
-            month = posts[0].datetime.month
-            these_posts = []
-            for post in posts:
-                if post.datetime.year == year and post.datetime.month == month:
-                    these_posts.append(post)
-                else:
-                    archives.append((year, month, these_posts))
-                    year, month = post.datetime.year, post.datetime.month
-                    these_posts = [post]
-            archives.append((year, month, these_posts))
-        return archives
-
-    def find_match(self, urlpath):
-        if urlpath in self._aliases:
-            return self
-        for document in self.pages + self.posts:
-            if urlpath in document._aliases:
-                return document
-        for subcategory in self.subcategories:
-            match = subcategory.find_match(urlpath)
-            if match:
-                return match
-        return None
-
-    def _tags(self):
-        tags = []
-        for document in self.pages + self.posts:
-            tags += [tag for tag in document.meta.get('tags', [])]
-        for subcategory in self.subcategories:
-            tags += subcategory._tags()
-        return tags
-
-    def tags(self):
-        tags = {}
-        for tag in self._tags():
-            tags[tag] = tags.get(tag, 0) + 1
-        return [(key, value) for key, value in sorted(tags.items(),
-                key=lambda pair: pair[0].lower())]
-
-    def __str__(self):
-        return self.name
-
-    def __unicode__(self):
-        return self.name