forked from nlplab/brat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
svg.py
125 lines (97 loc) · 3.52 KB
/
svg.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; coding: utf-8; -*-
# vim:set ft=python ts=4 sw=4 sts=4 autoindent:
'''
SVG saving and storage functionality.
Author: Pontus Stenetorp <pontus is s u-tokyo ac jp>
Author: Goran Topic <goran is s u-tokyo ac jp>
Version: 2011-04-22
'''
# TODO: Can we verify somehow that what we are getting is actually an svg?
# TODO: Limits to size? Or inherent from HTTP?
from __future__ import with_statement
from os.path import join as path_join
from os.path import isfile, exists
from os import mkdir
from common import ProtocolError, NoPrintJSONError
from config import BASE_DIR, WORK_DIR
from message import display_message
from session import get_session
### Constants
# TODO: We really need a work directory
SVG_DIR = path_join(WORK_DIR, 'svg')
# TODO: These constants most likely don't belong here
CSS_PATH = path_join(BASE_DIR, 'style.css')
GRAYSCALE_CSS_PATH = path_join(BASE_DIR, 'style_grayscale.css')
###
class UnknownSVGVersionError(ProtocolError):
def __init__(self, unknown_version):
self.unknown_version = unknown_version
def json(self, json_dic):
json_dic['exception'] = 'unknownSVGVersion'
display_message('Version "%s" is not a valid version'
% self.unknown_version, 'error')
return json_dic
class NoSVGError(ProtocolError):
def __init__(self, version):
self.version = version
def json(self, json_dic):
json_dic['exception'] = 'noSVG'
directory('SVG with version "%s" does not exist "%s"'
% (self.version, ), 'error')
return json_dic
class CorruptSVGError(ProtocolError):
def __init__(self):
pass
def json(self, json_dic):
json_dic['exception'] = 'corruptSVG'
return json_dic
def _save_svg(svg, colour=True):
if colour:
css_path = CSS_PATH
else:
css_path = GRAYSCALE_CSS_PATH
svg_path = _svg_path(colour=colour)
with open(svg_path, 'w') as svg_file:
svg_file.write('<?xml version="1.0" standalone="no"?>'
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" '
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">')
defs = svg.find('</defs>')
with open(css_path, 'r') as css_file:
css = css_file.read()
if defs != -1:
css = '<style type="text/css"><![CDATA[' + css + ']]></style>'
svg = svg[:defs] + css + svg[defs:]
svg_file.write(svg)
else:
# TODO: @amadanmath: When does this actually happen?
raise CorruptSVGError
def _svg_path(colour=True):
# Create the SVG_DIR if necessary
if not exists(SVG_DIR):
mkdir(SVG_DIR)
base_path = path_join(SVG_DIR, get_session().sid)
if colour:
return base_path + '_colour.svg'
else:
return base_path + '_greyscale.svg'
def store_svg(svg):
_save_svg(svg)
_save_svg(svg, colour=False)
return {}
def retrieve_svg(document, version):
if version == 'colour':
svg_path = _svg_path()
elif version == 'greyscale':
svg_path = _svg_path(colour=False)
else:
raise UnknownSVGVersionError(version)
if not isfile(svg_path):
raise NoSVGError(version)
print 'Content-Type: image/svg+xml'
print 'Content-Disposition: inline; filename=' + document + '.svg\n'
with open(svg_path, 'r') as svg_file:
print svg_file.read()
print
# Bail out with a hack since we violated the protocol
raise NoPrintJSONError