forked from springmeyer/quantumnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relativism.py
98 lines (74 loc) · 2.65 KB
/
relativism.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
# -*- coding: utf-8 -*-
import os
import sys
from xml.dom import minidom
from os.path import abspath, dirname, normcase, normpath, splitdrive
# http://code.activestate.com/recipes/302594/
def commonpath(a, b):
"""Returns the longest common to 'paths' path.
Unlike the strange os.path.commonprefix:
- this returns valid path
- accepts only two arguments
"""
a = normpath(normcase(a))
b = normpath(normcase(b))
if a == b:
return a
while len(a) > 0:
if a == b:
return a
if len(a) > len(b):
a = dirname(a)
else:
b = dirname(b)
return None
def relpath(target, base_path=os.curdir):
"""
Return a relative path to the target from either the current directory
or an optional base directory.
Base can be a directory specified either as absolute or relative
to current directory.
"""
base_path = normcase(abspath(normpath(base_path)))
target = normcase(abspath(normpath(target)))
if base_path == target:
return '.'
# On the windows platform the target may be on a different drive.
if splitdrive(base_path)[0] != splitdrive(target)[0]:
return None
common_path_len = len(commonpath(base_path, target))
# If there's no common prefix decrease common_path_len should be less by 1
base_drv, base_dir = splitdrive(base_path)
if common_path_len == len(base_drv) + 1:
common_path_len -= 1
# if base_path is root directory - no directories up
if base_dir == os.sep:
dirs_up = 0
else:
dirs_up = base_path[common_path_len:].count(os.sep)
ret = os.sep.join([os.pardir] * dirs_up)
if len(target) > common_path_len:
ret = os.path.join(ret, target[common_path_len + 1:])
return ret
def fix_file_params(name,element,base_path):
params = element.getElementsByTagName(name)
if params:
for param in params:
attr = param.getAttribute('name')
if attr == 'file':
path = param.firstChild.nodeValue
param.firstChild.nodeValue = relpath(path,base_path)
def fix_sym_file(element,base_path):
path = element.getAttribute('file')
if path:
element.setAttribute('file',relpath(path,base_path))
def fix_paths(mapfile,base_path):
doc = minidom.parse(mapfile)
datasources = doc.getElementsByTagName("Datasource")
for ds in datasources:
fix_file_params('Parameter',ds,base_path)
pnt_syms = doc.getElementsByTagName("PointSymbolizer")
for pnt in pnt_syms:
fix_sym_file(pnt,base_path)
open(mapfile,'wb').write(doc.toxml())
#def move_files(mapfile,base_path): pass