Commits

Luke Plant committed 226181c

Renamed 'read' -> 'parse'

Comments (0)

Files changed (4)

bibleverses/build.py

 # Builds Bible reading schemes from scheme definitions and verse lists
 
-from read import Scheme
+from parse import Scheme
 
 class SchemeItem(object):
     """Represents an item in a scheme, which is a verse with the 

bibleverses/parse.py

+import yaml
+import sqlite3
+import math
+
+# The YAML library correctly interprets UTF8 sequences in the input
+# data, but it returns Python 'unicode' objects only if unicode
+# characters are found, 'str' objects otherwise.  This is annoying,
+# but we simply build custom objects for everything, and initialise
+# using 'foo = unicode(yaml_foo_data)', which is safe.
+
+from config import VERSEYAMLPAGENAME, TOPICYAMLPAGENAME, SCHEMEYAMLPAGENAME, DATABASE
+
+from sword import get_bible_text, correct_verse_ref
+
+def get_page(name):
+    c = sqlite3.connect(DATABASE)
+    c.row_factory = sqlite3.Row
+    data = c.execute("SELECT * FROM wiki WHERE name = ? ORDER BY version DESC;", [name]).fetchone()
+    c.close()
+    return data
+
+def get_literal_block(text):
+    # Strip off the leading {{{ and trailing }}}
+    start = text.find('{{{')
+    end = text.find('}}}')
+    return text[(start+3):end]
+
+def get_verses_raw_data():
+    text = get_page(VERSEYAMLPAGENAME)['text']
+    return get_literal_block(text)
+
+def get_topictree_raw_data():
+    return get_page(TOPICYAMLPAGENAME)['text']
+
+def get_schemes_raw_data():
+    return get_literal_block(get_page(SCHEMEYAMLPAGENAME)['text'])
+
+class Verse(object):
+    def __init__(self, verse, texts=None, categories=None):
+        self.verse = correct_verse_ref(verse)
+        if not texts:
+            texts = {}
+        if not texts.has_key('ESV'):
+            texts['ESV'] = get_bible_text(self.verse, 'ESV')
+        for k, v in texts.items():
+            texts[k] = unicode(v)
+        self.texts = texts
+        self.categories = categories or {}        
+
+    def __str__(self):
+        return self.verse
+
+    def __repr__(self):
+        return u"<Verse: %s>" % self
+
+def get_verses():
+    """Returns the verse data, in a format directly based on the source YAML (lists, strings, dicts)"""
+    text = get_verses_raw_data() 
+    verses_data = list(yaml.load_all(text))
+
+    # Check the data
+    verses = []
+    for v in filter(bool, verses_data):
+        categories_data = v.get('categories')
+        if categories_data is None:
+            raise Exception("No categories defined for %r" % v)
+        verse = v.get('bible')
+        if verse is None:
+            raise Exception("No verse defined for %r" % v)
+        texts = v.get('texts')
+        
+        categories = dict((name, TopicEntry(name, verse, entry.get('comment', ''), entry.get('level', 1)))
+                           for name, entry in categories_data.items())
+        verse = Verse(verse, texts=texts, categories=categories)
+        # now fix up the reference to verse
+        for name, entry in verse.categories.items():
+            entry.verse = verse
+        verses.append(verse)
+        
+    return verses
+
+class TopicNode(object):
+    def __init__(self, name, nodes=None, entries=None, parent=None):
+        self.name = name
+        self.nodes = nodes or []
+        self.entries = entries or []
+        self.parent = parent
+
+    def yield_walk(self, func):
+        for x in func(self): yield x
+        for n in self.nodes:
+            for x in n.yield_walk(func): yield x
+
+    def walk(self, func):
+        func(self)
+        for n in self.nodes:
+            n.walk(func)
+
+    def next_sibling(self):
+        if self.parent is None:
+            return None
+        self_index = self.parent.nodes.index(self)
+        if self_index + 1 == len(self.parent.nodes):
+            return None
+        else:
+            return self.parent.nodes[self_index + 1]
+
+    def next(self, limit_to_root=None):
+        """Returns next item in the tree, which may
+        be a child or sibling or ancestor sibling"""
+        # If limit_to_root is given, search will be limited to
+        # nodes from that root.
+
+        # Children
+        if self.nodes:
+            return self.nodes[0]
+
+        # Siblings and ancestor siblings
+        p = self
+        while p is not None and p is not limit_to_root:
+            p_sibling = p.next_sibling()
+            if p_sibling is not None:
+                return p_sibling
+            p = p.parent
+        return None
+
+    def __repr__(self):
+        return u"<TopicNode: %s>" % self.name
+
+def get_topictree():
+    rawtree = list(yaml.load(get_topictree_raw_data()))
+
+    def mk_topic_node(name, subnode_data):
+        subnodes = []
+        for n in subnode_data:
+            if isinstance(n, basestring):
+                subnodes.append(TopicNode(n))
+            elif isinstance(n, dict):
+                for k, d in n.iteritems():
+                    subnodes.append(mk_topic_node(k, d))
+            else:
+                raise TypeError("Don't know what to do with object %r at this point in tree" % n)
+        return TopicNode(name, nodes=subnodes)
+
+    tree = mk_topic_node("RootNode", rawtree)
+    # Walk the tree and add 'parent' links
+    def _add_parent_links(node):
+        for n in node.nodes:
+            n.parent = node
+    tree.walk(_add_parent_links)
+
+    return tree
+
+def find_topic_set(verses):
+    """Searches the list of verses and returns topics with
+    associated verses"""
+    # We already know that every verse has 'categories' field
+    return set(cat for v in verses for cat in v.categories.keys())
+
+class TopicEntry(object):
+    def __init__(self, topic, verse, comment, level):
+        self.topic = unicode(topic)
+        self.verse = unicode(verse)
+        self.comment = unicode(comment)
+        self.level = level
+    def __repr__(self):
+        return u'<Entry: %s || %s || level %d || %s>' % (self.verse, self.topic, self.level, self.comment)
+
+def get_topic_entries(verses):
+    """Builds a dictionary of topics with their associated verses"""
+    topics = find_topic_set(verses)
+    groups = {}
+    for t in topics:
+        entries = []
+        for v in verses:
+            entry =  v.categories.get(t)
+            if entry is not None:
+                entries.append(entry)
+        # NB: list.sort is a stable sort, so this preserves
+        # order of texts in the original YAML if they have equal level
+        entries.sort(key=lambda x: x.level) 
+        groups[t] = entries
+    return groups
+
+def populate_topic_tree(topictree, verses):
+    """Given a topic tree and the verse YAML data, adds TopicEntries,
+    and returns a dictionary of TopicEntry objects that have not been added"""
+    
+    # Traverse the tree, adding in TopicEntry objects
+    def populate_topic_node(node, topic_entries):
+        node.entries.extend(topic_entries.pop(node.name, []))
+        for subnode in node.nodes:
+            populate_topic_node(subnode, topic_entries)
+    
+    entries = get_topic_entries(verses)
+    # populate_topic_node mutates both topictree and entries
+    populate_topic_node(topictree, topic_entries=entries)
+    return entries
+
+class Scheme(object):
+    ORDER_LINEAR = 'linear'
+    ORDER_CYCLE_TOPICS = 'cycle-topics'
+    ORDERS = set([ORDER_LINEAR, ORDER_CYCLE_TOPICS])
+    def __init__(self, name=u'', description=u'', topics=None, 
+                 max=None, previous=None, order=None, levels=None,
+                 items=None, exclude=None):
+        if name == "" or name is None:
+            raise ValueError(u"Scheme does not have name")
+        self.name = unicode(name)
+        self.description = unicode(description or '')
+        self.topics = topics
+        self.max = max
+        self.previous = previous or []
+        if order not in Scheme.ORDERS:
+            raise ValueError(u"Order '%s' for scheme '%s' is unknown" % (order, name) )
+        self.order = order
+        self.levels = levels or []
+        self.items = items or []
+        self.exclude = set(exclude or [])
+
+    def entry_filter(self, topic_entry):
+        if self.levels:  # len > 0 and not None
+            # levels can be fractional values for
+            # defining order, but 1.1 should be counted
+            # as part of level 1
+            if not int(math.floor(topic_entry.level)) in self.levels:
+                return False
+
+        # for previous used verses
+        for other_scheme in self.previous:
+            if topic_entry.verse in other_scheme.verseset:
+                return False
+
+        # excluded topics
+        if topic_entry.topic in self.exclude:
+            return False
+
+        return True
+
+    def __repr__(self):
+        return u"<Scheme: %s>" % self.name
+
+def get_schemes():
+    data = list(yaml.load_all(get_schemes_raw_data()))
+    schemes = [Scheme(**d) for d in data if d is not None]
+    scheme_dict = dict((s.name, s) for s in schemes)
+    # replace 'previous' attributes with actual Scheme objects
+    for s in schemes:
+        prevlist = []
+        for name in s.previous:
+            s2 = scheme_dict.get(name)
+            if s2 is None:
+                raise ValueError(u"Scheme '%s' referred to by scheme '%s' does not exist." \
+                                     % (name, s.name))
+            prevlist.append(s2)
+        s.previous = prevlist
+    return schemes

bibleverses/read.py

-import yaml
-import sqlite3
-import math
-
-# The YAML library correctly interprets UTF8 sequences in the input
-# data, but it returns Python 'unicode' objects only if unicode
-# characters are found, 'str' objects otherwise.  This is annoying,
-# but we simply build custom objects for everything, and initialise
-# using 'foo = unicode(yaml_foo_data)', which is safe.
-
-from config import VERSEYAMLPAGENAME, TOPICYAMLPAGENAME, SCHEMEYAMLPAGENAME, DATABASE
-
-from sword import get_bible_text, correct_verse_ref
-
-def get_page(name):
-    c = sqlite3.connect(DATABASE)
-    c.row_factory = sqlite3.Row
-    data = c.execute("SELECT * FROM wiki WHERE name = ? ORDER BY version DESC;", [name]).fetchone()
-    c.close()
-    return data
-
-def get_literal_block(text):
-    # Strip off the leading {{{ and trailing }}}
-    start = text.find('{{{')
-    end = text.find('}}}')
-    return text[(start+3):end]
-
-def get_verses_raw_data():
-    text = get_page(VERSEYAMLPAGENAME)['text']
-    return get_literal_block(text)
-
-def get_topictree_raw_data():
-    return get_page(TOPICYAMLPAGENAME)['text']
-
-def get_schemes_raw_data():
-    return get_literal_block(get_page(SCHEMEYAMLPAGENAME)['text'])
-
-class Verse(object):
-    def __init__(self, verse, texts=None, categories=None):
-        self.verse = correct_verse_ref(verse)
-        if not texts:
-            texts = {}
-        if not texts.has_key('ESV'):
-            texts['ESV'] = get_bible_text(self.verse, 'ESV')
-        for k, v in texts.items():
-            texts[k] = unicode(v)
-        self.texts = texts
-        self.categories = categories or {}        
-
-    def __str__(self):
-        return self.verse
-
-    def __repr__(self):
-        return u"<Verse: %s>" % self
-
-def get_verses():
-    """Returns the verse data, in a format directly based on the source YAML (lists, strings, dicts)"""
-    text = get_verses_raw_data() 
-    verses_data = list(yaml.load_all(text))
-
-    # Check the data
-    verses = []
-    for v in filter(bool, verses_data):
-        categories_data = v.get('categories')
-        if categories_data is None:
-            raise Exception("No categories defined for %r" % v)
-        verse = v.get('bible')
-        if verse is None:
-            raise Exception("No verse defined for %r" % v)
-        texts = v.get('texts')
-        
-        categories = dict((name, TopicEntry(name, verse, entry.get('comment', ''), entry.get('level', 1)))
-                           for name, entry in categories_data.items())
-        verse = Verse(verse, texts=texts, categories=categories)
-        # now fix up the reference to verse
-        for name, entry in verse.categories.items():
-            entry.verse = verse
-        verses.append(verse)
-        
-    return verses
-
-class TopicNode(object):
-    def __init__(self, name, nodes=None, entries=None, parent=None):
-        self.name = name
-        self.nodes = nodes or []
-        self.entries = entries or []
-        self.parent = parent
-
-    def yield_walk(self, func):
-        for x in func(self): yield x
-        for n in self.nodes:
-            for x in n.yield_walk(func): yield x
-
-    def walk(self, func):
-        func(self)
-        for n in self.nodes:
-            n.walk(func)
-
-    def next_sibling(self):
-        if self.parent is None:
-            return None
-        self_index = self.parent.nodes.index(self)
-        if self_index + 1 == len(self.parent.nodes):
-            return None
-        else:
-            return self.parent.nodes[self_index + 1]
-
-    def next(self, limit_to_root=None):
-        """Returns next item in the tree, which may
-        be a child or sibling or ancestor sibling"""
-        # If limit_to_root is given, search will be limited to
-        # nodes from that root.
-
-        # Children
-        if self.nodes:
-            return self.nodes[0]
-
-        # Siblings and ancestor siblings
-        p = self
-        while p is not None and p is not limit_to_root:
-            p_sibling = p.next_sibling()
-            if p_sibling is not None:
-                return p_sibling
-            p = p.parent
-        return None
-
-    def __repr__(self):
-        return u"<TopicNode: %s>" % self.name
-
-def get_topictree():
-    rawtree = list(yaml.load(get_topictree_raw_data()))
-
-    def mk_topic_node(name, subnode_data):
-        subnodes = []
-        for n in subnode_data:
-            if isinstance(n, basestring):
-                subnodes.append(TopicNode(n))
-            elif isinstance(n, dict):
-                for k, d in n.iteritems():
-                    subnodes.append(mk_topic_node(k, d))
-            else:
-                raise TypeError("Don't know what to do with object %r at this point in tree" % n)
-        return TopicNode(name, nodes=subnodes)
-
-    tree = mk_topic_node("RootNode", rawtree)
-    # Walk the tree and add 'parent' links
-    def _add_parent_links(node):
-        for n in node.nodes:
-            n.parent = node
-    tree.walk(_add_parent_links)
-
-    return tree
-
-def find_topic_set(verses):
-    """Searches the list of verses and returns topics with
-    associated verses"""
-    # We already know that every verse has 'categories' field
-    return set(cat for v in verses for cat in v.categories.keys())
-
-class TopicEntry(object):
-    def __init__(self, topic, verse, comment, level):
-        self.topic = unicode(topic)
-        self.verse = unicode(verse)
-        self.comment = unicode(comment)
-        self.level = level
-    def __repr__(self):
-        return u'<Entry: %s || %s || level %d || %s>' % (self.verse, self.topic, self.level, self.comment)
-
-def get_topic_entries(verses):
-    """Builds a dictionary of topics with their associated verses"""
-    topics = find_topic_set(verses)
-    groups = {}
-    for t in topics:
-        entries = []
-        for v in verses:
-            entry =  v.categories.get(t)
-            if entry is not None:
-                entries.append(entry)
-        # NB: list.sort is a stable sort, so this preserves
-        # order of texts in the original YAML if they have equal level
-        entries.sort(key=lambda x: x.level) 
-        groups[t] = entries
-    return groups
-
-def populate_topic_tree(topictree, verses):
-    """Given a topic tree and the verse YAML data, adds TopicEntries,
-    and returns a dictionary of TopicEntry objects that have not been added"""
-    
-    # Traverse the tree, adding in TopicEntry objects
-    def populate_topic_node(node, topic_entries):
-        node.entries.extend(topic_entries.pop(node.name, []))
-        for subnode in node.nodes:
-            populate_topic_node(subnode, topic_entries)
-    
-    entries = get_topic_entries(verses)
-    # populate_topic_node mutates both topictree and entries
-    populate_topic_node(topictree, topic_entries=entries)
-    return entries
-
-class Scheme(object):
-    ORDER_LINEAR = 'linear'
-    ORDER_CYCLE_TOPICS = 'cycle-topics'
-    ORDERS = set([ORDER_LINEAR, ORDER_CYCLE_TOPICS])
-    def __init__(self, name=u'', description=u'', topics=None, 
-                 max=None, previous=None, order=None, levels=None,
-                 items=None, exclude=None):
-        if name == "" or name is None:
-            raise ValueError(u"Scheme does not have name")
-        self.name = unicode(name)
-        self.description = unicode(description or '')
-        self.topics = topics
-        self.max = max
-        self.previous = previous or []
-        if order not in Scheme.ORDERS:
-            raise ValueError(u"Order '%s' for scheme '%s' is unknown" % (order, name) )
-        self.order = order
-        self.levels = levels or []
-        self.items = items or []
-        self.exclude = set(exclude or [])
-
-    def entry_filter(self, topic_entry):
-        if self.levels:  # len > 0 and not None
-            # levels can be fractional values for
-            # defining order, but 1.1 should be counted
-            # as part of level 1
-            if not int(math.floor(topic_entry.level)) in self.levels:
-                return False
-
-        # for previous used verses
-        for other_scheme in self.previous:
-            if topic_entry.verse in other_scheme.verseset:
-                return False
-
-        # excluded topics
-        if topic_entry.topic in self.exclude:
-            return False
-
-        return True
-
-    def __repr__(self):
-        return u"<Scheme: %s>" % self.name
-
-def get_schemes():
-    data = list(yaml.load_all(get_schemes_raw_data()))
-    schemes = [Scheme(**d) for d in data if d is not None]
-    scheme_dict = dict((s.name, s) for s in schemes)
-    # replace 'previous' attributes with actual Scheme objects
-    for s in schemes:
-        prevlist = []
-        for name in s.previous:
-            s2 = scheme_dict.get(name)
-            if s2 is None:
-                raise ValueError(u"Scheme '%s' referred to by scheme '%s' does not exist." \
-                                     % (name, s.name))
-            prevlist.append(s2)
-        s.previous = prevlist
-    return schemes

bibleverses/update.py

 #!/usr/bin/env python
 
-from read import get_verses, get_topic_entries, populate_topic_tree, get_topictree, get_schemes
+from parse import get_verses, get_topic_entries, populate_topic_tree, get_topictree, get_schemes
 from build import build_schemes
 from writewiki import write_page
 from StringIO import StringIO