[Python-checkins] r50653 - sandbox/trunk/seealso/convert-python-faqs.py

andrew.kuchling python-checkins at python.org
Fri Jul 14 22:27:42 CEST 2006


Author: andrew.kuchling
Date: Fri Jul 14 22:27:41 2006
New Revision: 50653

Added:
   sandbox/trunk/seealso/convert-python-faqs.py   (contents, props changed)
Log:
Skeleton for infogami-to-pyramid conversion script

Added: sandbox/trunk/seealso/convert-python-faqs.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/seealso/convert-python-faqs.py	Fri Jul 14 22:27:41 2006
@@ -0,0 +1,110 @@
+#!/usr/bin/python
+# Read copy of the pyfaq infogami site (assumed to be in the pyfaq/
+# subdirectory) and write out a pyramid-formatting tree to pyramid-faq/
+#
+# Warning: erases the existing pyramid-faq/ directory on starting!
+
+import os, shutil, glob
+import htmlload
+
+ET = htmlload.ET
+
+DESTDIR = 'pyramid-faq'
+
+def get_setting (root, setting_name, default=None):
+    """Look for a paragraph containing a setting, like 'CATEGORY: general',
+    and returns the result of the setting.
+    """
+    setting_start = setting_name.upper() + ':'
+    for para in root.findall('*/p'):
+        if para.text is not None and para.text.startswith(setting_start):
+            value = para.text[len(setting_start):]
+            value = value.strip()
+            return value
+    else:
+        return default
+
+def convert_question (filename):
+    base = os.path.basename(filename).replace('.xml', '')
+    # root is the 'html' element
+    root = htmlload.load(filename)
+    title = root.findtext('head/title') 
+
+    # Find category and name
+    category = get_setting(root, 'category')
+    if category is None:
+        ##print 'Filename without category:', filename
+        return
+    
+    name = get_setting(root, 'name', base)
+
+    qdir = os.path.join(DESTDIR, category, name)
+    os.makedirs(qdir)
+
+    # Write body of question
+    f = open(os.path.join(qdir, 'question.html'), 'w')
+    body = root.find('body')
+    for child in body.getchildren():
+        s = ET.tostring(child, 'utf-8')
+        f.write(s)
+
+    # XXX should add a link to the wiki page for this question
+    f.close()
+
+    # Write .yml files
+    f = open(os.path.join(qdir, 'index.yml'), 'w')
+    f.write("""--- !fragment
+template: index.html
+# The data to pass to the template
+local:
+  title: %s
+  content: !fragment content.yml
+    """ % title)
+    f.close()
+
+    f = open(os.path.join(qdir, 'content.yml'), 'w')
+    f.write("""--- !fragment
+# Type of template to use
+template: content.html
+
+# The data to pass to the template
+local:
+  content:
+    breadcrumb: !breadcrumb nav.yml nav
+    text: !htmlfile question.html""")
+    f.close()
+
+             
+    
+def convert_index (filename):
+    root = htmlload.load(filename)
+
+def write_master_index (index_files):
+    pass
+
+
+if os.path.exists(DESTDIR):
+    shutil.rmtree(DESTDIR)
+os.mkdir(DESTDIR)
+
+# List the files to be converted
+files = set(glob.glob('pyfaq/*.xml'))
+
+for fn in list(files):
+    if fn.startswith('pyfaq/_'):
+        files.discard(fn)
+
+index_files = [fn for fn in files if fn.endswith('-index.xml')]
+for fn in index_files:
+    files.discard(fn)
+
+# Convert questions
+for fn in list(files):
+    convert_question(fn)
+
+# Convert indexes
+for fn in index_files:
+    convert_index(fn)
+
+write_master_index(index_files)
+


More information about the Python-checkins mailing list