1

Is there a way to keep Python Toolboxes metadata formatted nicely for version control?

I use git to maintain my Python Toolboxes and when I make edits to the toolbox the metadata is updated and put all on one line. For example if I make a change to the tool alias I only want git to record the difference on that line.

Currently metadata looks like this

toolbox.tool.pyt.xml

<?xml version="1.0" encoding="UTF-8"?>
<metadata xml:lang="en"><Esri><CreaDate>20180824</CreaDate><CreaTime>10320700</CreaTime><ArcGISFormat>1.0</ArcGISFormat><SyncOnce>TRUE</SyncOnce><ModDate>20180828</ModDate><ModTime>111340</ModTime></Esri><toolbox name="partition" alias="partition"><arcToolboxHelpPath>c:\program files (x86)\arcgis\desktop10.3\Help\gp</arcToolboxHelpPath><toolsets/></toolbox><dataIdInfo><idCitation><resTitle>partition</resTitle></idCitation></dataIdInfo><distInfo><distributor><distorFormat><formatName>ArcToolbox Toolbox</formatName></distorFormat></distributor></distInfo></metadata>

I want this

toolbox.tool.pyt.xml

<?xml version="1.0" encoding="UTF-8"?>
<metadata xml:lang="en">
  <Esri>
    <CreaDate>20180824</CreaDate>
    <CreaTime>10320700</CreaTime>
    <ArcGISFormat>1.0</ArcGISFormat>
    <SyncOnce>TRUE</SyncOnce>
    <ModDate>20180828</ModDate>
    <ModTime>111340</ModTime>
  </Esri>
  <toolbox name="partition" alias="partition">
    <arcToolboxHelpPath>c:\program files (x86)\arcgis\desktop10.3\Help\gp</arcToolboxHelpPath>
    <toolsets/>
  </toolbox>
  <dataIdInfo>
    <idCitation>
      <resTitle>partition</resTitle>
    </idCitation>
  </dataIdInfo>
  <distInfo>
    <distributor>
      <distorFormat>
        <formatName>ArcToolbox Toolbox</formatName>
      </distorFormat>
    </distributor>
  </distInfo>
</metadata>
  • I use notepad++ and the xml tools plugin to reformat it as you wish. – Hornbydd Aug 28 '18 at 19:48
1

ArcGIS will always save the XML with the newlines and whitespace stripped.

However, you can use a few things, such as python or powershell, to pretty print your XML.

Here's a simple command-line powershell one liner that you can run from a cmd.exe prompt:

powershell -Command "& {([xml](gc 'yourfile.xml')).Save('yourfile.xml')}"

To do this automatically during a commit, in your git repo, create a file called pre-commit in the .git/hooks folder with the following content:

#!/bin/sh

if git rev-parse --verify HEAD >/dev/null 2>&1
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

git diff --cached --name-only --diff-filter=AM $against | grep '\.xml$' | \
xargs -I % powershell -Command "& {([xml](gc '%')).Save('%')}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.