mirror of
https://github.com/robmcmullen/atrcopy.git
synced 2025-01-16 18:32:35 +00:00
Fixed #8: added partial template name matching
This commit is contained in:
parent
55b368414e
commit
cf1fe5344b
@ -289,50 +289,61 @@ def get_template_path(rel_path="templates"):
|
|||||||
return template_path
|
return template_path
|
||||||
|
|
||||||
|
|
||||||
def get_template_info():
|
def get_template_images(partial=""):
|
||||||
import glob
|
import glob
|
||||||
import textwrap
|
|
||||||
fmt = " %-14s %s"
|
|
||||||
|
|
||||||
path = get_template_path()
|
path = get_template_path()
|
||||||
files = glob.glob(os.path.join(path, "*"))
|
files = glob.glob(os.path.join(path, "*"))
|
||||||
|
templates = {}
|
||||||
lines = []
|
for path in files:
|
||||||
lines.append("available templates:")
|
name = os.path.basename(path)
|
||||||
for name in sorted(files):
|
|
||||||
if name.endswith(".inf"):
|
if name.endswith(".inf"):
|
||||||
continue
|
continue
|
||||||
|
if partial not in name:
|
||||||
|
continue
|
||||||
try:
|
try:
|
||||||
with open(name + ".inf", "r") as fh:
|
with open(path + ".inf", "r") as fh:
|
||||||
s = fh.read()
|
s = fh.read()
|
||||||
try:
|
try:
|
||||||
j = json.loads(s)
|
j = json.loads(s)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
continue
|
continue
|
||||||
description = j["description"]
|
j['name'] = name
|
||||||
|
j['path'] = path
|
||||||
|
templates[name] = j
|
||||||
except IOError:
|
except IOError:
|
||||||
description = ""
|
|
||||||
if not description:
|
|
||||||
continue
|
continue
|
||||||
d = textwrap.wrap(description, 80 - 1 - 14 - 2 - 2)
|
return templates
|
||||||
|
|
||||||
|
|
||||||
|
def get_template_info():
|
||||||
|
import textwrap
|
||||||
|
fmt = " %-14s %s"
|
||||||
|
|
||||||
|
templates = get_template_images()
|
||||||
|
|
||||||
|
lines = []
|
||||||
|
lines.append("available templates:")
|
||||||
|
for name in sorted(templates.keys()):
|
||||||
|
d = textwrap.wrap(templates[name]["description"], 80 - 1 - 14 - 2 - 2)
|
||||||
lines.append(fmt % (os.path.basename(name), d[0]))
|
lines.append(fmt % (os.path.basename(name), d[0]))
|
||||||
lines.extend([fmt % ("", line) for line in d[1:]])
|
lines.extend([fmt % ("", line) for line in d[1:]])
|
||||||
return os.linesep.join(lines) + os.linesep
|
return os.linesep.join(lines) + os.linesep
|
||||||
|
|
||||||
|
|
||||||
def get_template_data(template):
|
def get_template_data(template):
|
||||||
path = os.path.join(get_template_path(), template)
|
possibilities = get_template_images(template)
|
||||||
|
if not possibilities:
|
||||||
|
raise InvalidDiskImage("Unknown template disk image %s" % template)
|
||||||
|
if len(possibilities) > 1:
|
||||||
|
raise InvalidDiskImage("Name %s is ambiguous (%d matches: %s)" % (template, len(possibilities), ", ".join(sorted(possibilities.keys()))))
|
||||||
|
name, inf = possibilities.popitem()
|
||||||
|
path = inf['path']
|
||||||
try:
|
try:
|
||||||
with open(path, "rb") as fh:
|
with open(path, "rb") as fh:
|
||||||
data = fh.read()
|
data = fh.read()
|
||||||
except:
|
|
||||||
raise InvalidDiskImage("Unknown template disk image %s" % template)
|
|
||||||
try:
|
|
||||||
with open(path + ".inf", "r") as fh:
|
|
||||||
s = fh.read()
|
|
||||||
inf = json.loads(s)
|
|
||||||
except IOError:
|
except IOError:
|
||||||
inf = {"description": ""}
|
raise InvalidDiskImage("Failed reading template file %s" % path)
|
||||||
return data, inf
|
return data, inf
|
||||||
|
|
||||||
|
|
||||||
@ -345,7 +356,7 @@ def create_image(template, name):
|
|||||||
info = get_template_info()
|
info = get_template_info()
|
||||||
print("Error: %s\n\n%s" % (e, info))
|
print("Error: %s\n\n%s" % (e, info))
|
||||||
return
|
return
|
||||||
print("using %s template:\n %s" % (template, "\n ".join(textwrap.wrap(inf["description"], 77))))
|
print("Using template %s:\n %s" % (inf['name'], "\n ".join(textwrap.wrap(inf["description"], 77))))
|
||||||
if not options.dry_run:
|
if not options.dry_run:
|
||||||
if os.path.exists(name) and not options.force:
|
if os.path.exists(name) and not options.force:
|
||||||
print("skipping %s, use -f to overwrite" % (name))
|
print("skipping %s, use -f to overwrite" % (name))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user