forked from astropy/astropy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.py
More file actions
74 lines (62 loc) · 2.06 KB
/
Copy pathcheck.py
File metadata and controls
74 lines (62 loc) · 2.06 KB
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
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
A collection of functions for checking various XML-related strings for
standards compliance.
"""
import re
import urlparse
def check_id(ID):
"""
Returns `True` if *ID* is a valid XML ID.
"""
return re.match(r"^[A-Za-z_][A-Za-z0-9_\.\-]*$", ID) is not None
def fix_id(ID):
"""
Given an arbitrary string, create one that can be used as an xml
id. This is rather simplistic at the moment, since it just
replaces non-valid characters with underscores.
"""
if re.match(r"^[A-Za-z_][A-Za-z0-9_\.\-]*$", ID):
return ID
if len(ID):
corrected = ID
if not len(corrected) or re.match('^[^A-Za-z_]$', corrected[0]):
corrected = '_' + corrected
corrected = (re.sub(r"[^A-Za-z_]", '_', corrected[0]) +
re.sub(r"[^A-Za-z0-9_\.\-]", "_", corrected[1:]))
return corrected
return ''
_token_regex = r"(?![\r\l\t ])[^\r\l\t]*(?![\r\l\t ])"
def check_token(token):
"""
Returns `True` if *token* is a valid XML token, as defined by XML
Schema Part 2.
"""
return (token == '' or
re.match(
"[^\r\n\t ]?([^\r\n\t ]| [^\r\n\t ])*[^\r\n\t ]?$", token)
is not None)
def check_mime_content_type(content_type):
"""
Returns `True` if *content_type* is a valid MIME content type
(syntactically at least), as defined by RFC 2045.
"""
ctrls = ''.join(chr(x) for x in xrange(0, 0x20))
token_regex = '[^()<>@,;:\\\"/[\]?= %s\x7f]+' % ctrls
return re.match(
r'(?P<type>%s)/(?P<subtype>%s)$' % (token_regex, token_regex),
content_type) is not None
def check_anyuri(uri):
"""
Returns `True` if *uri* is a valid URI as defined in RFC 2396.
"""
if (re.match(
(r"(([a-zA-Z][0-9a-zA-Z+\-\.]*:)?/{0,2}[0-9a-zA-Z;" +
r"/?:@&=+$\.\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\.\-_!~*'()%]+)?"),
uri) is None):
return False
try:
urlparse.urlparse(uri)
except:
return False
return True