File: demo.py

package info (click to toggle)
python-aafigure 0.6-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 428 kB
  • sloc: python: 1,942; makefile: 112
file content (52 lines) | stat: -rwxr-xr-x 1,408 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python

"""\
Demonstration code for aafigure use as python module.
"""

import sys
import aafigure
import aafigure.aa

ascii_art = u"""\
    ---> | ^|   |
    <--- | || --+--
    <--> | |V   |
 __             __
|  |__  +---+  |__|
        |box|   ..
        +---+  Xenophon
"""

# Show what we're parsing.
print(u" input ".center(78, '='))
print(ascii_art)
# Parse the image.
aaimg = aafigure.AsciiArtImage(ascii_art)
aaimg.recognize()

# For fun, output the ASCII version in the console.
print(u" output ".center(78, '='))
aav = aafigure.aa.AsciiOutputVisitor({'file_like': sys.stdout, 'scale': 2})
aav.visit_image(aaimg)
print(u"="*78)

# Writing an SVG file would be possible in a similar way, but there is the much
# easier render function for that.

# A stringIO object is returned for the output when the output parameter is not
# given. If it were, the output would be directly written to that object.
visitor, output = aafigure.render(ascii_art, options={'format': 'svg'})

# The process method can be used for a lower level access. The visitor class
# has to be specified by the user in this case.  To get output, a file like
# object has to be passed in the options:
# {'file_like' = open("somefile.svg", "wb")}
import aafigure.svg
from io import StringIO
fl = StringIO()
visitor = aafigure.process(
    ascii_art,
    aafigure.svg.SVGOutputVisitor,
    options={'file_like': fl}
)