github.com/openshift/installer@v1.4.17/docs/user/aws/images/relink-dia.py (about)

     1  #!/usr/bin/env python3
     2  #
     3  # Replace file:///... references with relative links and add the targets to Git.
     4  
     5  import base64
     6  import os
     7  import sys
     8  import urllib.parse
     9  import xml.etree.ElementTree
    10  
    11  
    12  def slug(uri):
    13      return os.path.basename(uri)
    14  
    15  
    16  def get_def(path):
    17      tree = xml.etree.ElementTree.parse(path)
    18      root = tree.getroot()
    19      root.set('id', slug(path))
    20      return root
    21  
    22  
    23  def relink(path):
    24      images = set()
    25      tree = xml.etree.ElementTree.parse(path)
    26      root = tree.getroot()
    27      parents = {c: p for p in tree.getiterator() for c in p}
    28      defs = xml.etree.ElementTree.Element('{http://www.w3.org/2000/svg}defs')
    29      root.insert(0, defs)
    30      for image in list(root.findall('{http://www.w3.org/2000/svg}image')):
    31          uri = urllib.parse.unquote(image.get('{http://www.w3.org/1999/xlink}href')).split('/./', 1)[-1]
    32          if uri not in images:
    33              defs.append(get_def(path=uri))
    34              images.add(uri)
    35          parent = parents[image]
    36          parent_index = list(parent).index(image)
    37          parent.remove(image)
    38          attrib = dict(image.items())
    39          attrib['{http://www.w3.org/1999/xlink}href'] = '#' + slug(uri)
    40          parent.insert(
    41              parent_index,
    42              xml.etree.ElementTree.Element('{http://www.w3.org/2000/svg}use', attrib=attrib))
    43      tree.write(path)
    44  
    45  
    46  for path in sys.argv[1:]:
    47      relink(path=path)