/usr/share/mypaint/lib/xml.py is in mypaint 1.2.0-4.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # -*- encoding: utf-8 -*-
# This file is part of MyPaint.
# Copyright (C) 2015 by Andrew Chadwick <a.t.chadwick@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
"""Helpers, and constants for the XML dialects MyPaint uses."""
## Imports
from __future__ import absolute_import
import xml.etree.ElementTree as _ET
## Consts for XML dialects
# Namespaces are registered by importing this module.
OPENRASTER_MEDIA_TYPE = "image/openraster"
OPENRASTER_VERSION = u"0.0.5"
OPENRASTER_MYPAINT_NS = "http://mypaint.org/ns/openraster"
_OPENRASTER_NAMESPACES = {
"mypaint": OPENRASTER_MYPAINT_NS,
}
for prefix, uri in _OPENRASTER_NAMESPACES.items():
_ET.register_namespace(prefix, uri)
## Helper functions
def indent_etree(elem, level=0):
"""Indent an XML etree.
This does not seem to come with python?
Source: http://effbot.org/zone/element-lib.htm#prettyprint
"""
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent_etree(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
def xsd2bool(arg):
"""Converts an XSD boolean datatype string from XML to a Python bool
>>> xsd2bool("true")
True
>>> xsd2bool("false")
False
>>> xsd2bool(1)
True
>>> xsd2bool(0)
False
Ref: http://www.w3.org/TR/xmlschema-2/#boolean
"""
return str(arg).lower() in ("true", "1")
def escape(u, quot=False, apos=False):
"""Escapes a Unicode string for use in XML/HTML.
>>> u = u'<foo> & "bar"'
>>> escape(u)
'<foo> & "bar"'
>>> escape(u, quot=True)
'<foo> & "bar"'
>>> escape(None) is None
True
Works like ``cgi.escape()``, but adds character ref encoding for
characters which lie outside the ASCII range.
The returned string is ASCII.
"""
if u is None:
return None
u = u.replace("&", "&")
u = u.replace("<", "<")
u = u.replace(">", ">")
if apos:
u = u.replace("'", "'")
if quot:
u = u.replace('"', """)
return u.encode("ascii", "xmlcharrefreplace")
## Module testing
def _test(self):
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
|