Commits

Anonymous committed 6606f9b

Changes to data for endpoints, parse_test, various templates have had changes mostly timeline to ge tthe page working.

  • Participants
  • Parent commits 10f4a99

Comments (0)

Files changed (7)

openletters/controllers/data.py

 import urllib
 
 from openletters.lib.base import BaseController, render
+
+from openletters import model
+
 from openletters.transform.transform_rdf import rdf_transform
+from openletters.transform.transform_json import json_transform
+from openletters.transform.transform_xml import xml_transform
 
 log = logging.getLogger(__name__)
 
 class DataController(BaseController):
-    
-    def endpoint (self):
-        response.headers['Content-Type'] = 'application/rdf+xml; charset=utf-8'
-        rdf = rdf_transform()
+    '''
+       Return an endpoint in which ever format is requested
+    '''
+    def endpoint (self, author = '', correspondent = ''):
         
-        return rdf.create_rdf_end()
+        if author == "rdf":
+            response.headers['Content-Type'] = 'application/rdf+xml; charset=utf-8'
+            rdf = rdf_transform()
+            return rdf.create_rdf_end()
+        
+        elif author == "json":
+            response.headers['Content-Type'] = 'application/json'
+            json = json_transform()
+            return json.to_end_dict()
+        
+        elif author == "xml":
+            xml = xml_transform()
+            response.headers['Content-Type'] = 'application/xml'
+            if correspondent == "simile":
+                return xml.endpoint_xml("simile")
+            else:
+                return xml.endpoint_xml()
 
     def book (self):
         #response.headers['Content-Type'] = 'application/json'
             if req in c:
                 b = '<li>%s</li>' % c
         
-        return b    
+        return b    
+    
+        

openletters/parse/parse_text.py

 '''
 def parse_balanced_quotes (text):
     ret_quotes = []
-    m_quote = ''
+    
     bq = re.findall('"([^\\"]+)"', text)
-    bq = str(bq).replace("[", "").replace("u'", "").replace("]", "").replace("'", "").strip()
+    #is there a better way of removing the punctuation - common theme for project
+    bq = str(bq).replace("[", "").replace("u'", "").replace("]", "").replace("'", "").replace('"', "").strip()
 
     if "," in str(bq):
         for a in str(bq).split(","): 
-            if str(a[0:1]).isspace():
+            if str(a[:1]).isspace():
                 str(a).strip() 
-            if str(a[0:1]) is "u":
+            if str(a[:1]) is "u":
                 str(a).replace("u", "")
-
-            if str(a[0:1]).isupper() and "!" not in a and len(str(a)) < 40:
-                ret_quotes.append(a)  
+                
+            print "str", str(a[:1])
+            if str(a[:1]).isupper():
+                print "str", str(a[:1])
+                if "!" not in a and len(str(a)) < 40:
+                    ret_quotes.append(a)  
             else:
                 pass     
     else:
-        if str(bq[0:1]).isspace():
-                str(bq[0:1]).strip()
-        if str(bq[0:1]) is "u":
-                str(bq[0:1]).replace("u", "")
-                           
-        if str(bq[0:1]).isupper and "!" not in bq and len(str(bq)) < 40:
-            ret_quotes.append(bq)         
+        if str(bq[:1]).isspace():
+                str(bq[:1]).strip()
+        if str(bq[:1]) is "u":
+                str(bq[:1]).replace("u", "")
+        
+        if str(bq[:1]).isupper():
+            if  "!" not in bq and len(str(bq)) < 40:
+                ret_quotes.append(bq)         
         else:
             pass
         

openletters/templates/layout.html

     ${optional_head()}
   </py:if>
 </head>
-<body>
+<!-- remove if timeline doesn't work -->
+<body onload="onLoad();" onresize="onResize();" >
 <div id="airlock">
   <!--[if IE]>
     <hr class="holder" />

openletters/templates/timeline/index.html

   <py:def function="optional_head">
     <script src="http://static.simile.mit.edu/timeline/api-2.3.0/timeline-api.js" type="text/javascript"></script>
   <script type="text/javascript">
-    jQuery(document).ready(function() {
+      var tl;
+      function onLoad() {
+  
       var eventSource = new Timeline.DefaultEventSource();
-      evts = [];
-   
-//      var elem = jQuery(elem);
-//      var dateEvent = new Date();
-//      dateEvent.setISO8601(elem.find('.published')[0].getAttribute('title'));
-//      var evt = new Timeline.DefaultEventSource.Event(
-//         dateEvent, //start
-//         dateEvent, //end
-//         dateEvent, //latestStart
-//         dateEvent, //earliestEnd
-//         true, //instant
-//         elem.find('.content a:first').text(), //text
-//         elem.find('.entry-content').text() //description
-//      );
-//      eventSource.add(evt);
 
       var bandInfos = [
         Timeline.createBandInfo({
-          width:          "100%", 
-          intervalUnit:   Timeline.DateTime.MINUTE, 
-          intervalPixels: 10,
-          eventSource: eventSource,
+          eventSource:     eventSource,
+          date:           "Jun 28 1836 00:00:00 GMT",
+          width:          "70%", 
+          intervalUnit:   Timeline.DateTime.MONTH, 
+          intervalPixels: 100,
         }),
+        Timeline.createBandInfo({
+          overview:       true,
+          eventSource: 	  eventSource,
+          date:           "Jun 28 1836 00:00:00 GMT",
+          width:          "30%", 
+          intervalUnit:   Timeline.DateTime.YEAR, 
+          intervalPixels: 200,
+        })
       ];
-      tl = Timeline.create($("#timeline"), bandInfos);
-    });
+      
+     bandInfos[1].syncWith = 0;
+     bandInfos[1].highlight = true;
+      
+      tl = Timeline.create(document.getElementById("timeline"), bandInfos);
+      Timeline.loadXML("http://localhost:5000/data/endpoint/xml/simile", function(xml, url) { eventSource.loadXML(xml, url); });
+    };
+    
+    var resizeTimerID = null;
+ function onResize() {
+     if (resizeTimerID == null) {
+         resizeTimerID = window.setTimeout(function() {
+             resizeTimerID = null;
+             tl.layout();
+         }, 500);
+     }
+ }
+    
+    
   </script>
 
   </py:def>

openletters/transform/transform_json.py

     import json
 except ImportError:
     import simplejson as json
+    
+from openletters.model import dbase
+from openletters.parse import parse_text
+
 
 class json_transform:
   
     '''
     Function to return the text as json
     '''
-    def to_dict (self, letterobj, type):
+    def to_dict (self, letterobj, type = ''):
 
         dict = '{'
         
         return self.jsonify(dict)
     
     '''
+       Function to return json endpoint
+    '''
+    def to_end_dict (self):
+
+        letter = dbase.get_endpoint_rdf()
+        
+        letter_items = letter.items()
+        letter_items.sort()
+        
+        dict = '{'
+        
+        dict += "index: {"
+         
+        for url, text in letter_items:
+            
+            dict +=  str(url) + ': [ {'
+            dict += '"author": " Charles Dickens",'
+            dict += '"correspondent: "' + str(text[1])
+            dict += '", "date": "' + str(text[3])+'T00:00:00'
+            
+            letter_quotes = parse_text.parse_balanced_quotes(text[2])
+            for quote in letter_quotes:
+                dict += '", "text": "' + parse_text.stripPunc(quote)
+            
+            dict += '"}],'
+        
+        #remove the last comma
+        dict = dict[0: -1]
+        
+        dict += '} }'
+            
+        return self.jsonify(dict)
+    
+    '''
     Function to return the text as json
     '''
     def corr_json (self, author, letterobj):

openletters/transform/transform_rdf.py

 from openletters.model import dbase
 from openletters.parse import parse_text
 
-import urllib
+try:
+    from sets import Set
+except ImportException:
+    from set import Set
+    
+import urllib, rdflib
 
-import rdflib
 from rdflib.Graph import ConjunctiveGraph as Graph
 from rdflib.store import Store, NO_STORE, VALID_STORE
 from rdflib import Namespace, Literal, URIRef, RDF, RDFS, plugin
         letter_rdf = self.g.serialize(format="pretty-xml", max_depth=3)
         return letter_rdf
     
-    ''' function to create an endpoint '''
+    ''' function to create an endpoint in rdf/xml '''
     def create_rdf_end (self):
 
         correspondence = base_uri 
             
             #for name in letter_name:
             #    letter_rdf += "<letter:personReferred>%s</letter:personReferred>" %(name)
-                               
+           # works = Set(["Copperfield", "David Copperfield"])                
             letter_quotes = parse_text.parse_balanced_quotes(text[2])
             for quote in letter_quotes:
                 #the length is to remove anything really long
-                if str(quote[0:1]).isupper and "!" not in quote and len(str(quote)) < 40:
-                    self.add_text(correspondence, parse_text.stripPunc(quote))
+                #if str(quote[0:1]).isupper and "!" not in quote and len(str(quote)) < 40:
+                self.add_text(correspondence, parse_text.stripPunc(quote))
+                #if quote in works:
+                #    self.add_author_text(correspondence, parse_text.stripPunc(quote))
+                #else:
+                #    self.add_text(correspondence, parse_text.stripPunc(quote))
 
         letter_rdf = self.g.serialize(format="pretty-xml", max_depth=3)
         return letter_rdf
         #self.g.add((book, Letter["textReferred"], Literal(textname)))       
         return book
     
+    ''' function to add author referred text to the graph'''
+    def add_author_text (self, correspondence, textname):
+
+        textid = urllib.quote(textname)
+        book = URIRef(base_uri + 'book/%s' % textid)
+        self.g.add((correspondence, letter_ns['textAuthorReferred'], Literal(textname)))  
+        #self.g.add((book, Letter["textReferred"], Literal(textname)))       
+        return book
+    
     ''' function to add time '''
     def add_time(self, correspondence, time):
         

openletters/transform/transform_xml.py

 import xml.etree.ElementTree as ET
+import urllib
+
+from openletters.model import dbase
 
 class xml_transform:
  
-    '''create an XML of letters for a defined author'''
+    '''create XML of letters for a defined author'''
     def index_xml (self, letters):
     
         root = ET.Element("opencorrespondence")
             id = ET.SubElement(letter, "id")
             id.text = unicode(str(l.id))
             
-            if l.letter_text:
-                
-                l_text = ET.SubElement(letter, "text")
-                l_text.text = unicode(self.xml_encode(l.letter_text))
+            #if l.letter_text:
+            #    
+            #    l_text = ET.SubElement(letter, "text")
+            #    l_text.text = unicode(self.xml_encode(l.letter_text))
    
         doc = ET.tostring(root, "UTF-8")
     
         doc = ET.tostring(root, "UTF-8")
         
         return doc
+    
+    '''
+        Function to create xml for Simile timeline
+    '''
+    def endpoint_xml (self, type = ''):
+        
+        root = ET.Element("data", {'date-time-format': 'iso8601'})
+        
+        letter = {}  
+        letter = dbase.get_endpoint_rdf()
+    
+        letter_items = letter.items()
+        letter_items.sort()
+          
+        for url, utext in letter_items:
+            if type == "simile":
+                event = ET.SubElement(root, "event", {"start": str(utext[3])+"T00:00:00", "title": utext[1] , "link": 'http://www.opencorrespondence.org/letters/view/dickens/' + urllib.quote(utext[1]) + '/' + str(url)  })
+                event.text = unicode("Letter to " + utext[1])
+            else:
+                letter = ET.SubElement(root, "letter")
+                aurl = ET.SubElement(letter, "author")
+                aurl.text = unicode("Charles Dickens")
+                author = ET.SubElement(letter, "correspondent")
+                author.text = unicode(utext[1])
+                date = ET.SubElement(letter, "date")
+                date.text = unicode(utext[3])
+                id = ET.SubElement(letter, "id")
+                id.text = unicode('http://www.opencorrespondence.org/letters/view/dickens/' + urllib.quote(utext[1]) + '/' + str(url))
+        
+        doc  = ET.tostring(root, "UTF-8")
+        return doc
 
     def xml_encode (self, text):