MCPcopy Create free account
hub / github.com/AcademySoftwareFoundation/MaterialX / main

Function main

python/Scripts/mxformat.py:20–87  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

18 error = str(e)
19
20def main():
21 parser = argparse.ArgumentParser(description="Reformat a folder of MaterialX documents in place.")
22 parser.add_argument('-y', '--yes', dest='yes', action="store_true", help="Proceed without asking for confirmation from the user.")
23 parser.add_argument('-u', '--upgrade', dest='upgrade', action="store_true", help='Upgrade documents to the latest version of the standard.')
24 parser.add_argument('-v', '--validate', dest='validate', action="store_true", help='Perform MaterialX validation on documents after reformatting.')
25 parser.add_argument('-x', '--xml_syntax', dest='xml_syntax', action="store_true", help='Check XML syntax after reformatting.')
26 parser.add_argument(dest="inputFolder", help="An input folder to scan for MaterialX documents.")
27 opts = parser.parse_args()
28
29 validDocs = dict()
30 for root, dirs, files in os.walk(opts.inputFolder):
31 for filename in files:
32 fullpath = os.path.join(root, filename)
33 if fullpath.endswith('.mtlx'):
34 doc = mx.createDocument()
35 try:
36 readOptions = mx.XmlReadOptions()
37 readOptions.readComments = True
38 readOptions.readNewlines = True
39 readOptions.upgradeVersion = opts.upgrade
40 try:
41 mx.readFromXmlFile(doc, fullpath, mx.FileSearchPath(), readOptions)
42 except Exception as err:
43 print('Skipping "' + filename + '" due to exception: ' + str(err))
44 continue
45 validDocs[fullpath] = doc
46 except mx.Exception:
47 pass
48
49 if not validDocs:
50 print('No MaterialX documents were found in "%s"' % (opts.inputFolder))
51 return
52
53 print('Found %s MaterialX files in "%s"' % (len(validDocs), opts.inputFolder))
54
55 mxVersion = mx.getVersionIntegers()
56
57 if not opts.yes:
58 if opts.upgrade:
59 question = 'Would you like to upgrade all %i documents to MaterialX v%i.%i in place (y/n)?' % (len(validDocs), mxVersion[0], mxVersion[1])
60 else:
61 question = 'Would you like to reformat all %i documents in place (y/n)?' % len(validDocs)
62 answer = input(question)
63 if answer != 'y' and answer != 'Y':
64 return
65
66 validate = opts.validate
67 if validate:
68 print(f'- Validate documents')
69 xml_syntax = opts.xml_syntax
70 if xml_syntax:
71 print(f'- Check XML syntax')
72 for (filename, doc) in validDocs.items():
73 if xml_syntax:
74 xml_string = mx.writeToXmlString(doc)
75 errors = is_well_formed(xml_string)
76 if errors:
77 print(f'- Warning: Document {filename} is not well-formed XML: {errors}')

Callers 1

mxformat.pyFile · 0.70

Calls 8

dictClass · 0.85
printFunction · 0.85
strClass · 0.85
lenFunction · 0.85
is_well_formedFunction · 0.85
XmlReadOptionsMethod · 0.80
getVersionIntegersMethod · 0.45
validateMethod · 0.45

Tested by

no test coverage detected