[cvs-annodex] commit (/annodex): +cmmlwiki/trunk/cmmlwiki-import
conrad
nobody at lists.annodex.net
Mon Apr 11 23:36:36 EST 2005
Update of /annodex (new revision 1236)
Added files:
cmmlwiki/trunk/cmmlwiki-import
Log Message:
add cmmlwiki-import to import a cmml file into the database
Added: cmmlwiki/trunk/cmmlwiki-import
===================================================================
--- cmmlwiki/trunk/cmmlwiki-import 2005-04-11 13:36:06 UTC (rev 1235)
+++ cmmlwiki/trunk/cmmlwiki-import 2005-04-11 13:36:35 UTC (rev 1236)
@@ -0,0 +1,142 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2005 CSIRO Australia
+#
+# Based on sample code from the Python/XML HOWTO:
+# http://pyxml.sourceforge.net/topics/howto/xml-howto.html
+#
+# You may find this script to be a useful example of processing CMML in
+# python. Improvements and bugfixes welcome :-)
+#
+# Conrad Parker <conrad at annodex.net>, Mar 3 2005
+
+import sys
+from time import strptime, strftime
+
+import sqlite
+
+from xml.sax import saxutils
+from xml.sax import make_parser
+from xml.sax.handler import feature_namespaces
+
+db = "/var/local/lib/cmmlwiki/cmmlwiki.db"
+
+def clock2timestamp(str):
+ t = strptime(str, "clock:%Y%m%dT%H%M%SZ")
+ return strftime ("%H:%M:%S", t)
+
+class cmmlHandler(saxutils.DefaultHandler):
+ def __init__(self, cur, ixp):
+ self.inDesc = False
+ self.cur = cur
+ self.ixp = ixp
+
+ def startElement(self, name, attrs):
+ if name == 'clip':
+ self.speaker = ""
+ self.inDesc = False
+ self.vals = {}
+ self.vals['ixp'] = self.ixp
+ self.vals['desc'] = ""
+ self.vals['img_src'] = ""
+ self.vals['a_href'] = ""
+ self.vals['id'] = attrs.get('id')
+ self.vals['start'] = attrs.get('start')
+ self.vals['end'] = attrs.get('end')
+ #self.start_time = clock2timestamp(attrs.get('start', None))
+
+ if name == 'meta':
+ if (attrs.get('name') == 'DC.Contributor'):
+ self.speaker = attrs.get('content')
+
+ if name == 'a':
+ self.vals['a_href'] = attrs.get('href')
+
+ if name == 'img':
+ self.vals['img_src'] = attrs.get('src')
+
+ if name == 'desc':
+ self.inDesc = True
+
+ def characters(self, ch):
+ if self.inDesc:
+ self.vals['desc'] = self.vals['desc'] + ch
+
+ def endElement(self, name):
+ if name == 'clip':
+ sql = """INSERT INTO clips (ixp, start_time, id, img_src, a_href, desc)
+ VALUES (%(ixp)s,%(start)s,%(id)s,%(img_src)s,%(a_href)s,%(desc)s) """
+ #print sql
+ cur.execute(sql, self.vals)
+# print """<div id="%(id)s" class="clip">
+# <img src="%(imgsrc)s" align="left"/>
+# <h3>%(id)s</h3>
+# <table>
+# <tr>
+# <th>Start:</th>
+# <td><input type="text" name="%(id)s:start" value="%(start)s"/></td>
+# <th>End:</th>
+# <td><input type="text" name="%(id)s:end" value="%(end)s"/></td>
+# </tr>
+# <tr>
+# <th>Link:</th>
+# <td colspan="3"><input type="text" size="80" name="%(id)s:href" value="%(a_href)s"/><td>
+# </tr>
+# <tr>
+# <th>Desc:</th>
+# <td colspan="3"><textarea rows="8" cols="80" name="%(id)s:desc">%(text)s</textarea></td>
+# </tr>
+# </table>
+# </div>
+# """ % self.vals
+
+ if name == 'desc':
+ self.inDesc = False
+
+def get_ixp(cur, project_name):
+ sql = 'SELECT * FROM projects WHERE name="%s"' % project_name
+ cur.execute(sql)
+ try:
+ row = cur.next()
+ ixp = row['ixp']
+ except:
+ ixp = -1
+
+ return ixp
+
+if __name__=='__main__':
+
+ try:
+ project_name = sys.argv[1]
+ cmml_file = sys.argv[2]
+ except IndexError:
+ print "Usage: cmmlwiki-import ProjectName file.cmml"
+ sys.exit(1)
+
+ # Connect to the database
+ con = sqlite.connect(db, encoding='utf-8', autocommit=1)
+ cur = con.cursor()
+
+ # Get the project index
+ ixp = get_ixp (cur, project_name)
+
+ if (ixp == -1):
+ print "cmmlwiki-import: Unknown project %s" % project_name
+ sys.exit(1)
+
+ # Create a parser
+ parser = make_parser()
+
+ # Tell the parser we are not interested in XML namespaces
+ parser.setFeature(feature_namespaces, 0)
+
+ # Create the handler
+ dh = cmmlHandler(cur, ixp)
+
+ # Tell the parser to use the handler
+ parser.setContentHandler(dh)
+
+ # Parse the input
+ parser.parse(cmml_file)
+ #except ValueError:
+ # print "cmmlwiki-import: Error opening URL %s" % cmml_file
Property changes on: cmmlwiki/trunk/cmmlwiki-import
___________________________________________________________________
Name: svn:executable
+ *
--
conrad
More information about the cvs-annodex
mailing list