- Timestamp:
- 03/04/22 14:57:53 (2 years ago)
- Location:
- TOOLS/MOSAIX
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TOOLS/MOSAIX/CreateWeightsMask.bash
r6090 r6091 340 340 cp iodef_oce_to_atm.xml iodef.xml 341 341 342 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]' -k name -v maskutil_T 343 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="area_src"]' -k name -v area_grid_T 344 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]' -k type -v ${srcDomainType} 345 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]' -k type -v ${dstDomainType} 346 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k order -v 1 347 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k quantity -v false 348 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k renormalize -v false 349 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k use_area -v false 350 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name -v maskutil_T 351 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="area_source"]' -k name -v area_grid_T 352 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]' -k name -v dia_${OutFileName} 353 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]' -t "${SRC} mask interpolated to ${DST}" 354 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]' -t ${srcDomainType} 355 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]' -t ${dstDomainType} 356 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]' -t 1 357 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]' -k type -v ${srcDomainType} 358 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]' -k type -v ${dstDomainType} 359 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k weight_filename -v rmp_${OutFileName}.nc 360 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k order -v 1 361 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t false 362 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="quantity"]' -t false 363 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="use_area"]' -t false 364 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k renormalize -v false 365 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k quantity -v false 366 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k use_area -v false 367 342 cat << EOF > command_file_${OutFileName}.txt 343 -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]' -k name -v maskutil_T 344 -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="area_src"]' -k name -v area_grid_T 345 -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]' -k type -v ${srcDomainType} 346 -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]' -k type -v ${dstDomainType} 347 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k order -v 1 348 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k quantity -v false 349 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k renormalize -v false 350 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k use_area -v false 351 -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name -v maskutil_T 352 -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="area_source"]' -k name -v area_grid_T 353 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]' -k name -v dia_${OutFileName} 354 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]' -t "${SRC} mask interpolated to ${DST}" 355 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]' -t ${srcDomainType} 356 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]' -t ${dstDomainType} 357 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]' -t 1 358 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]' -k type -v ${srcDomainType} 359 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]' -k type -v ${dstDomainType} 360 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k weight_filename -v rmp_${OutFileName}.nc 361 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k order -v 1 362 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t false 363 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="quantity"]' -t false 364 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="use_area"]' -t false 365 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k renormalize -v false 366 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k quantity -v false 367 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k use_area -v false 368 EOF 369 370 python3 update_xml.py -i iodef.xml -c command_file_${OutFileName}.txt 368 371 cp iodef.xml iodef_${OutFileName}.xml 372 369 373 ln -fs ${OCE}_coordinates_mask.nc oce_grid.nc 370 374 ln -fs ${ATM}_grid.nc atm_grid.nc … … 488 492 #ln -fs ${ATM}_grid.nc atm_grid.nc 489 493 ln -fs ${ATM}_grid_maskFrom_${OCE}.nc atm_grid.nc 494 495 OutFileName=${srcGrid}${src}_to_${dstGrid}${dst}_${FullName} 490 496 491 497 case ${Direction} in 492 498 ( o2a ) 493 499 cp iodef_oce_to_atm.xml iodef.xml 494 495 python3 update_xml.py -i iodef.xml-n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]' -k name -v maskutil_${DSTGRID}496 python3 update_xml.py -i iodef.xml-n 'context[@id="interpol_read"]/file_definition/file[@id="file_dst"]/field[@id="mask_dst"]' -k name -v Oce2AtmMask497 498 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name -v maskutil_${SRCGRID}499 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="area_source"]' -k name -v area_grid_${SRCGRID} 500 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="mask_dest"]' -k name -v Oce2AtmMask 501 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="area_dest"]' -k name -v aire 500 cat << EOF > command_file_${OutFileName}.txt 501 -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]' -k name -v maskutil_${DSTGRID} 502 -n 'context[@id="interpol_read"]/file_definition/file[@id="file_dst"]/field[@id="mask_dst"]' -k name -v Oce2AtmMask 503 -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name -v maskutil_${SRCGRID} 504 -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="area_source"]' -k name -v area_grid_${SRCGRID} 505 -n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="mask_dest"]' -k name -v Oce2AtmMask 506 -n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="area_dest"]' -k name -v aire 507 EOF 502 508 ;; 503 509 ( a2o ) 504 510 cp iodef_atm_to_oce.xml iodef.xml 505 506 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]' -k name -v Oce2AtmMask 507 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_dst"]/field[@id="mask_dst"]' -k name -v mask_${DSTGRID} 508 509 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name -v Oce2AtmMask 510 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="area_source"]' -k name -v aire 511 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="mask_dest"]' -k name -v mask_${DSTGRID} 512 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="area_dest"]' -k name -v area_grid_${DSTGRID} 513 ;; 514 esac 515 516 OutFileName=${srcGrid}${src}_to_${dstGrid}${dst}_${FullName} 511 cat << EOF > command_file_${OutFileName}.txt 512 -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]' -k name -v Oce2AtmMask 513 -n 'context[@id="interpol_read"]/file_definition/file[@id="file_dst"]/field[@id="mask_dst"]' -k name -v mask_${DSTGRID} 514 -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name -v Oce2AtmMask 515 -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="area_source"]' -k name -v aire 516 -n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="mask_dest"]' -k name -v mask_${DSTGRID} 517 -n 'context[@id="interpol_run"]/file_definition/file[@id="file_dst"]/field[@id="area_dest"]' -k name -v area_grid_${DSTGRID} 518 EOF 519 ;; 520 esac 521 522 cat << EOF >> command_file_${OutFileName}.txt 523 -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]' -k type -v ${dstDomainType} 524 -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]' -k type -v ${srcDomainType} 525 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]' -k type -v ${srcDomainType} 526 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]' -k type -v ${dstDomainType} 527 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k weight_filename -v rmp_${OutFileName}.nc 528 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k order -v ${numOrder} 529 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k quantity -v ${Quantity} 530 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k renormalize -v ${Renormalize} 531 -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k use_area -v ${useArea} 532 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]' -k name -v dia_${OutFileName} 533 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]' -t "${SRC} mask interpolated to ${DST}" 534 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]' -t ${dstDomainType} 535 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]' -t ${srcDomainType} 536 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t ${Renormalize} 537 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]' -t ${numOrder} 538 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="quantity"]' -t ${Quantity} 539 -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="use_area"]' -t ${useArea} 540 EOF 541 542 python3 update_xml.py -i iodef.xml -c command_file_${OutFileName}.txt 543 cp iodef.xml iodef_${OutFileName}.xml 517 544 518 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_dst"]' -k type -v ${dstDomainType}519 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_read"]/domain_definition/domain[@id="domain_src"]' -k type -v ${srcDomainType}520 521 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_src"]' -k type -v ${srcDomainType}522 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]' -k type -v ${dstDomainType}523 524 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k weight_filename -v rmp_${OutFileName}.nc525 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k order -v ${numOrder}526 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k quantity -v ${Quantity}527 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k renormalize -v ${Renormalize}528 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/domain_definition/domain[@id="domain_dst"]/interpolate_domain' -k use_area -v ${useArea}529 530 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]' -k name -v dia_${OutFileName}531 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]' -t "${SRC} mask interpolated to ${DST}"532 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]' -t ${dstDomainType}533 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="source_grid"]' -t ${srcDomainType}534 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="normalization"]' -t ${Renormalize}535 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="order"]' -t ${numOrder}536 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="quantity"]' -t ${Quantity}537 python3 update_xml.py -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="use_area"]' -t ${useArea}538 539 cp iodef.xml iodef_${OutFileName}.xml540 545 ${MpiRun} ./interpol.exe --mask_src=${maskSrc} --mask_dst=${maskDst} --use_area=${useArea} 541 546 -
TOOLS/MOSAIX/update_xml.py
r5920 r6091 22 22 __HeadURL = "$HeadURL$" 23 23 24 # 24 # python update_xml.py -i ~/Unix/TOOLS/MOSAIX/iodef_atm_to_oce.xml -o essai.xml -n 'context[@id="interpol_read"]/file_definition/file[@id="file_src"]/field[@id="mask_src"]' -k name -v Bidon 25 # python update_xml.py -i ~/Unix/TOOLS/MOSAIX/iodef_atm_to_oce.xml -d -o essai.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="title"]' -t "SRC mask interpolated to DST" 26 # python update_xml.py -i ~/Unix/TOOLS/MOSAIX/iodef_atm_to_oce.xml -o essai.xml -c InFile.txt 27 25 28 # Tested with python/2.7.12 and python/3.6.4 26 29 # … … 34 37 sys.stderr.write ( "Present version is: " + str(Version[0]) + "." + str(Version[1]) + "." + str(Version[2]) + "\n" ) 35 38 sys.exit (1) 39 40 ## ============================================================================ 41 ## Needed functions 42 43 def simplify_string_list (list_str) : 44 '''Concatenate some elements of the list of strings when needed''' 45 zlist = list_str.copy () ; list_new = [] 46 while ( len (zlist) > 0 ) : 47 arg = zlist.pop (0) 48 if arg[0] == '"' : 49 for arg2 in zlist.copy () : 50 arg = arg + " " + arg2 51 zlist.pop (0) 52 if arg2[-1] == '"' : break 53 arg = arg.strip('"').strip("'") 54 list_new.append (arg) 55 return list_new 36 56 57 def UpdateNode (iodef, Node, Text, Key, Value) : 58 '''Update an xml node''' 59 # Remove whitespaces at both ends 60 Node = Node.rstrip().lstrip() 61 62 ## Find node 63 nodeList = iodef.findall (Node) 64 65 ## Check that one and only one node is found 66 if len (nodeList) == 0 : 67 print ( "Error : node not found" ) 68 print ( "Node : ", Node ) 69 sys.exit (1) 70 71 if len (nodeList) > 1 : 72 print ( "Error : " + len (nodeList)+" occurences of node found" ) 73 print ( "Node : ", Node ) 74 sys.exit (2) 75 76 ## Update element 77 elem = nodeList[0] 78 79 if Debug : 80 print ( 'Node:', Node, ' -- Key:', Key, ' -- Value:', Value , ' -- Text:', Text ) 81 82 if Text != None : 83 if Debug : 84 print ( 'Attributes of node: ' + str (elem.attrib) ) 85 print ( 'Text : ' + str (elem.text) ) 86 elem.text = Text 87 88 if Key != None : 89 # To do : check that Key exist (it is added if not : do we want that ?) 90 if Debug : 91 print ( 'Attributes of node: ' + str (elem.attrib) ) 92 elem.attrib.update ( { Key:Value } ) 93 94 return iodef 95 96 ## ============================================================================ 97 ## Main code 98 37 99 # Creating a parser to read the command line arguments 38 100 # The first step in using the argparse is creating an ArgumentParser object: 39 101 parser = argparse.ArgumentParser (description = """ 40 examples:102 Examples with the modification on the command line : 41 103 python %(prog)s -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="file_src"]/field[@id="mask_source"]' -k name -v maskutil_T 42 104 python %(prog)s -i iodef.xml -n 'context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"]' -t dstDomainType 43 python %(prog)s -i iodef.xml -e commands.txt (not implemented yet) 44 """ + "\n" + "SVN : " + __Revision__, formatter_class=argparse.RawDescriptionHelpFormatter, epilog='-------- This is the end of the help message --------') 105 106 Usage with a command file : 107 python %(prog)s -i iodef.xml -c commands.txt 108 109 Syntax in the command file : removes the quote around the node description : 110 -n context[@id="interpol_run"]/file_definition/file[@id="dia"]/variable[@name="dest_grid"] -t dstDomainType 111 112 """ + "SVN : " + __Revision__, formatter_class=argparse.RawDescriptionHelpFormatter, epilog='-------- This is the end of the help message --------') 45 113 46 114 # Adding arguments 47 group1 = parser.add_mutually_exclusive_group (required= True)48 49 parser.add_argument ( '-i', '--input' , help=" input file" , default='iodef.xml', type=str, metavar='<input_file>')50 parser.add_argument ( '-o', '--output' , help=" output file" , default=None , type=str, metavar='<output_file>')51 parser.add_argument ( '-n', '--node' , help=" xml node in Xpath syntax", default=None, required=True, type=str, metavar='<xml_node>')52 group1.add_argument ( '-k', '--key' , help=" xml key to update", default=None , type=str , metavar='<xml_key>' )53 group1.add_argument ( '-t', '--text' , help=" will replace the 'text' part of the Xpath by <text>", default=None, type=str, metavar='<text>' )54 parser.add_argument ( '-v', '--value' , help=" new value for xml key", default=None, type=str, metavar='<value>' )115 group1 = parser.add_mutually_exclusive_group (required=False) 116 117 parser.add_argument ( '-i', '--input' , help="XML input file" , default='iodef.xml', type=str, metavar='<input_file>' ) 118 parser.add_argument ( '-o', '--output' , help="XML output file" , default=None , type=str, metavar='<output_file>' ) 119 parser.add_argument ( '-n', '--node' , help="XML node in Xpath syntax", default=None, type=str, metavar='<xml_node>') 120 group1.add_argument ( '-k', '--key' , help="XML key to update" , default=None , type=str , metavar='<xml_key>' ) 121 group1.add_argument ( '-t', '--text' , help="Will replace the 'text' part of the Xpath by <text>", default=None, type=str, metavar='<text>' ) 122 parser.add_argument ( '-v', '--value' , help="New value for xml key ", default=None, type=str, metavar='<value>' ) 55 123 parser.add_argument ( '-d', '--debug' , action="store_true", default=False ) 56 124 parser.add_argument ( '-V', '--verbose', action="store_true", default=False ) 125 parser.add_argument ( '-c', '--commandfile', help="file with list of command", default=None, type=str ) 57 126 58 127 # Parse command line 59 myargs = parser.parse_args() 60 Verbose = myargs.verbose 61 62 if Verbose : print ( "Command line arguments : " , myargs ) 63 64 FileIn = myargs.input 65 FileOut = myargs.output 66 Node = myargs.node 67 Key = myargs.key 68 Text = myargs.text 69 Value = myargs.value 70 Debug = myargs.debug 71 72 # Error handling not dealed by argparse 73 if Key != None and Value == None : 74 print ( "Error. When -k|--key=<key> is specified, you must specify -v|--value=<xml_value>" ) 75 sys.exit (-1) 76 128 myargs = parser.parse_args () 129 Verbose = myargs.verbose 130 Debug = myargs.debug 131 132 if Debug : print ( "Command line arguments : ", myargs ) 133 134 FileCommand = myargs.commandfile 135 FileIn = myargs.input 136 FileOut = myargs.output 137 Node = myargs.node 138 Key = myargs.key 139 Text = myargs.text 140 Value = myargs.value 141 142 if FileCommand != None : 143 if ( Node != None or Key != None or Text != None or Value != None ) : 144 print ('Error : when a command file is specified, options -k|--key, -n|--node and -v|--value a unused' ) 145 exit (-2) 146 77 147 if FileOut == None : FileOut = FileIn 78 79 # Remove whitespaces at both ends 80 Node = Node.rstrip().lstrip() 81 148 82 149 ## Get XML tree from input file 83 150 iodef = xml.etree.ElementTree.parse ( FileIn ) 84 151 85 ## Find node 86 nodeList = iodef.findall ( Node ) 87 88 ## Check that one and only one node is found 89 if len (nodeList) == 0 : 90 print ( "Error : node not found" ) 91 print ( "Node : " + Node ) 92 sys.exit (1) 93 94 if len (nodeList) > 1 : 95 print ( "Error : " + len (nodeList)+" occurences of node found" ) 96 print ( "Node : " + Node ) 97 sys.exit (2) 98 99 ## Update element 100 elem = nodeList[0] 101 102 if Debug : 103 print ( 'Node : ' + Node ) 104 print ( 'Key : ' + Key ) 105 print ( 'Value : ' + Value ) 106 107 if Text != None : 108 if Debug : 109 print ( 'Attributes of node: ' + str (elem.attrib) ) 110 print ( 'Text : ' + str (elem.text) ) 111 elem.text = Text 112 113 if Key != None : 114 # To do : check that Key exist (it is added if not : do we want that ?) 115 if Debug : 116 print ( 'Attributes of node: ' + str (elem.attrib) ) 117 elem.attrib.update ( { Key:Value } ) 152 if FileCommand == None : 153 ## Only one node to modify 154 155 # Error handling not dealed by argparse 156 if Key != None and Value == None : 157 print ( "Error. When -k|--key=<key> is specified, you must specify -v|--value=<xml_value>" ) 158 sys.exit (-1) 159 160 iodef = UpdateNode ( iodef, Node, Text, Key, Value) 161 162 else : 163 ## Read a list of modification commands in a command file 164 fic = open (FileCommand, 'r') 165 lignes = fic.readlines() 166 167 for nn, ligne in enumerate (lignes) : 168 169 ligne = ligne.strip() 170 if ligne == '' : break # Skips blank lines 171 if ligne[0] == '#' : break # Skips comment lines 172 173 ligne = ligne.split('#')[0] # Remove trailing comments 174 175 list_args = ligne.split() 176 list_args = simplify_string_list ( list_args ) 177 178 if Debug : 179 print ( '{:3d} : '.format(nn+1), end='') 180 print ( list_args ) 181 myargs_ligne = parser.parse_args ( list_args ) 182 183 Node = myargs_ligne.node 184 Key = myargs_ligne.key 185 Text = myargs_ligne.text 186 Value = myargs_ligne.value 187 188 if Key != None and Value == None : 189 print ( "Error. When -k|--key=<key> is specified, you must specify -v|--value=<xml_value>" ) 190 sys.exit (-1) 191 UpdateNode ( iodef, Node, Text, Key, Value) 118 192 119 193 ## Writes XML tree to file
Note: See TracChangeset
for help on using the changeset viewer.