Commits

Jonathan Fine  committed 6421195 Merge

$ hg merge populate # into default

  • Participants
  • Parent commits c60afc7, 6227436

Comments (0)

Files changed (5)

 disk and read it from disk.  Basically, it is a dictionary of data.
 In memory we might use a different representation, for example as in
 JSON.
+
+
+populate branch
+
+This is for populatine a aos with rendered objects.

File adaptable-object-store.py

-# File adaptable-object-store.py
-
-'''
-Module adaptable-object-store
-
->>> 'hi'
-'hi'
-
-
-'''
-
-
-
-if __name__ == '__main__':
-
-    import doctest
-
-    print doctest.testmod(optionflags=doctest.ELLIPSIS)
 
 
 # TODO: Inherit from a metaclass here.
+# TODO: repr AmsMath is "anon(tex='lots of stuff ...', etc)"
 class AmsMath(
-    collections.namedtuple('anon', 'tex dvi png'),
+    collections.namedtuple('anon', 'tex dvi png svg'),
     _Base
     ):
 
 
 if __name__ == '__main__':
 
-    import shutil
-
     # Create a clean local aos.
     # TODO: Find the true home for this code.
+    # TODO: Appears in both ao.py and populate.py
+    import shutil
     try:
         shutil.rmtree(os.path.join('local', 'aos'))
         print 'ok'

File aos/populate.py

+import os
+import tempfile
+import shutil
+import subprocess
+from aos.ao import AmsMath      # TODO: Remove this ugly dependency.
+
+_AMSMATH_TEMPLATE = r'''
+\documentclass{article}
+\usepackage{amsmath}
+\begin{document}
+
+\shipout\hbox{%%
+\(
+%s
+\)%%
+}
+
+\end{document}
+'''
+
+def create_ao(source):
+
+    # TODO: Refactor this code - similar code in python-hvector.
+    dirname = tempfile.mkdtemp()
+    try:
+        dvi = do_amsmath(dirname, source)
+        png = do_dvipng(dirname)
+        svg = do_dvisvgm(dirname)
+        ao = AmsMath(tex=source, dvi=dvi, png=png, svg=svg)
+
+    finally:
+        shutil.rmtree(dirname)
+
+    return ao
+
+
+def do_amsmath(dirname, source):
+
+    # TODO: Refactor this code - similar code in python-hvector.
+    args = [
+        'latex',
+        '--interaction=nonstopmode',
+        '--output-comment', 'Created by aos.populate.do_amsmath',
+        'o.tex',
+        ]
+
+    # Create the wrapped source file 'o.tex'.
+    wrapped_source = _AMSMATH_TEMPLATE % source
+    tex_filename = os.path.join(dirname, 'o.tex')
+    dvi_filename = os.path.join(dirname, 'o.dvi')
+    with open(tex_filename, 'wb') as f:
+        f.write(wrapped_source)
+
+    # Run latex on the wrapped source file.
+    process = subprocess.Popen(
+        args,
+        cwd=dirname,
+        stdin=subprocess.PIPE,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+        )
+    stdout, stderr = process.communicate()
+
+    # Pick up the resulting dvi file.
+    with open(dvi_filename, 'rb') as f:
+        dvi_bytes = f.read()
+
+    return dvi_bytes
+
+
+def _generic_do(dirname, args, short_filename):
+
+    long_filename = os.path.join(dirname, short_filename)
+    process = subprocess.Popen(
+        args,
+        cwd=dirname,
+        stdin=subprocess.PIPE,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+        )
+    stdout, stderr = process.communicate()
+
+    # Pick up the resulting dvi file.
+    with open(long_filename, 'rb') as f:
+        field_value = f.read()
+
+    return field_value
+
+
+def do_dvipng(dirname):
+
+    args = [
+        'dvipng',
+        '-o', 'o.png',
+        'o.dvi',
+        ]
+    short_filename = 'o.png'
+
+    return _generic_do(dirname, args, short_filename)
+
+
+def do_dvisvgm(dirname):
+
+    args = [
+        'dvisvgm',
+        'o.dvi',
+        ]
+    short_filename = 'o.svg'
+
+    return _generic_do(dirname, args, short_filename)
+
+
+def add_formulas_to_aos(aos_path, formula_path_pairs):
+
+    for formula, path in formula_path_pairs:
+
+        ao = create_ao(formula)
+        ao.write(aos_path, path)
+
+
+def md5_key_formulas(formulas):
+    '''Convert formulas into key-value pairs.'''
+
+    # TODO: Key to come first?
+    import hashlib
+    formula_path_pairs = tuple(
+        (formula, hashlib.md5(formula).hexdigest())
+        for formula in formulas
+        )
+    return formula_path_pairs
+
+
+# TODO: Introduce object to make all this clearer (and better).
+def amsmath_populate(aos_path, formulas):
+
+    formula_path_pairs = md5_key_formulas(formulas)
+    add_formulas_to_aos('./local/aos/-1/c/amsmath', formula_path_pairs)
+
+
+if __name__ == '__main__':
+
+    ao = create_ao('2 + 2 = 4')
+    print repr(ao)
+
+    formulas = (
+        '1 + 1 = 2',
+        'x^2 + y^2 = z^2',
+        'E = mc^2',
+        )
+
+    # Create a clean local aos.
+    # TODO: Find the true home for this code.
+    # TODO: Appears in both ao.py and populate.py
+    import shutil
+    try:
+        shutil.rmtree(os.path.join('local', 'aos'))
+        print 'ok'
+    except OSError:
+        pass
+    os.makedirs(os.path.join('local', 'aos/-1/'))
+
+    # Populate the local aos store.
+    amsmath_populate('./local/aos/-1/c/amsmath', formulas)
+
+

File test_aos/runtests.py

 import aos
 import aos.patterns
 import aos.ao
+import aos.populate
 
 if __name__ == '__main__':
 
         aos,
         aos.patterns,
         aos.ao,
+        aos.populate,
         ):
 
         tmp =  doctest.testmod(module, optionflags=doctest.ELLIPSIS)