forked from chris-little/WorldWeatherSymbols
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_uri.py
111 lines (83 loc) · 3.11 KB
/
set_uri.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
# -*- coding: iso-8859-1 -*-
#!/usr/bin/python
"""
Process all *.svg files in the repository and update the rdf:about property
using the supplied base_uri and the relative path.
Args:
* base_uri (string): The base URI to be prepended to the file paths
Example usage:
python set_uri.py https://raw.github.com/OGCMetOceanDWG/WorldWeatherSymbols/master
.. note::
The following XML tag will be updated <cc:Work rdf:about="">
"""
import os
import sys
import warnings
def svg_file_list():
"""
Returns a list of all svg files with their relative paths
"""
svg_list = []
for root, dirs, files in os.walk('.'):
# Exclude the git database folder
if root.find('.git') < 0:
# Remove the initial ./ from the root path
root = root[2:]
files = [file_ for file_ in files if file_.endswith(".svg")]
for svg_file in files:
svg_list.append(os.path.join(root, svg_file))
return svg_list
def modify_uri(base_uri, svg_file):
"""
Read the entire contents of svg_file, find the row containing the
rdf:about tag and update with the full URI
Args:
* base_uri (string): The base URI to be prepended to the file paths
* svg_file (string): Relative path to the SVG file
Returns boolen to indicate success
"""
success = False
with open(svg_file, 'r') as svg_fh:
contents = svg_fh.readlines()
for line_num, line in enumerate(contents):
tag_start = line.find("<cc:Work")
if tag_start >= 0:
# Check for the end bracket of the tag '>', if it exists then
# store the remaining chars to the appended to the end of the
# modified line. Note, if the original tag was split across
# multiple lines then we assume it was only on two lines.
tag_end = line.find(">", tag_start)
if tag_end < 0:
warn_msg = ('The URI tag was split across multiple lines' +
' in ' + svg_file + '\n')
warnings.warn(warn_msg)
# Attempting to fix by removing the second line.
del contents[line_num + 1]
ending = ''
else:
ending = line[tag_end+1:]
# Modify the line
beginning = line[:tag_start]
contents[line_num] = (
beginning + '<cc:Work rdf:about="' +
os.path.join(base_uri, svg_file) +
'">' + ending + '\n')
success = True
break
if success:
with open(svg_file, 'w') as svg_fh:
svg_fh.writelines(contents)
return success
if __name__ == "__main__":
base_uri = sys.argv[1]
successes = 0
failures = 0
for svg_file in svg_file_list():
if modify_uri(base_uri, svg_file):
successes += 1
else:
failures += 1
total = successes + failures
print str(total), "files processed."
print str(successes), "files sucessfully modified."
print str(failures), "files failed to be updated."