You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
5 years ago
|
#! /usr/bin/python3
|
||
|
|
||
|
import sys
|
||
|
import re
|
||
|
from geo import Geo
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
if len(sys.argv) < 4:
|
||
|
print("Usage:")
|
||
|
print(" %s <infile.geo> <outfile.geo> <operation> [<operation options> ...]" % (sys.argv[0], ))
|
||
|
print("Operations:")
|
||
|
print(" del_model <reg_ex_filter> Delete models with the name matching regular expression.")
|
||
|
print(" geo_name <geo_name> Rename the name of the .geo .")
|
||
|
exit()
|
||
|
|
||
|
fn_in = sys.argv[1]
|
||
|
fn_out = sys.argv[2]
|
||
|
if fn_in == fn_out:
|
||
|
print("Input and output filenames are identical. Refusing to run, to avoid accidental lose.")
|
||
|
|
||
|
print("Reading '%s'..." % (fn_in, ))
|
||
|
fh_in = open(fn_in, "rb")
|
||
|
geo = Geo()
|
||
|
geo.loadFromFile(fh_in)
|
||
|
fh_in.close()
|
||
|
print("Done.")
|
||
|
|
||
|
arg_i = 3
|
||
|
while arg_i < len(sys.argv):
|
||
|
operation = sys.argv[arg_i]
|
||
|
arg_i += 1
|
||
|
if operation == "del_model":
|
||
|
reg_exp_str = sys.argv[arg_i]
|
||
|
arg_i += 1
|
||
|
reg_exp = re.compile(reg_exp_str)
|
||
|
for i in range(len(geo.models) - 1, -1, -1):
|
||
|
name = geo.models[i].name.decode("utf-8")
|
||
|
if reg_exp.search(name) is not None:
|
||
|
print("Remove: %s" % (name, ))
|
||
|
del geo.models[i]
|
||
|
else:
|
||
|
print("Keep : %s" % (name, ))
|
||
|
elif operation == "geo_name":
|
||
|
name = sys.argv[arg_i]
|
||
|
arg_i += 1
|
||
|
print("Rename GEO from '%s' to '%s'" % (geo.header_modelheader_name.decode("utf-8"), name))
|
||
|
geo.header_modelheader_name = bytes(name, "utf-8")
|
||
|
else:
|
||
|
print("Unknown operation: '%s'" % (operation, ))
|
||
|
exit()
|
||
|
|
||
|
print("Writing '%s'..." % (fn_out, ))
|
||
|
fh_out = open(fn_out, "wb")
|
||
|
geo.saveToFile(fh_out)
|
||
|
fh_out.close()
|
||
|
print("Done.")
|