Merge branch 'omnivore-old' into omnivore-2.0

This commit is contained in:
Rob McMullen 2018-03-09 11:31:25 -08:00
commit aef3095bc6
1 changed files with 45 additions and 24 deletions

View File

@ -289,53 +289,74 @@ def get_template_path(rel_path="templates"):
return template_path
def get_template_info():
def get_template_images(partial=""):
import glob
import textwrap
fmt = " %-14s %s"
path = get_template_path()
files = glob.glob(os.path.join(path, "*"))
templates = {}
for path in files:
name = os.path.basename(path)
if name.endswith(".inf"):
continue
if partial not in name:
continue
try:
with open(path + ".inf", "r") as fh:
s = fh.read()
try:
j = json.loads(s)
except ValueError:
continue
j['name'] = name
j['path'] = path
templates[name] = j
except IOError:
continue
return templates
def get_template_info():
import textwrap
fmt = " %-14s %s"
templates = get_template_images()
lines = []
lines.append("available templates:")
for name in sorted(files):
if name.endswith(".inf"):
continue
try:
with open(name + ".inf", "r") as fh:
s = fh.read()
j = json.loads(s)
description = j["description"]
except IOError:
description = ""
d = textwrap.wrap(description, 80 - 1 - 14 - 2 - 2)
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.extend([fmt % ("", line) for line in d[1:]])
return os.linesep.join(lines) + os.linesep
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:
with open(path, "rb") as fh:
data = fh.read()
except:
raise InvalidDiskImage("Template disk image %s not found" % template)
try:
with open(path + ".inf", "r") as fh:
s = fh.read()
inf = json.loads(s)
except IOError:
inf = {"description": ""}
raise InvalidDiskImage("Failed reading template file %s" % path)
return data, inf
def create_image(template, name):
import textwrap
data, inf = get_template_data(template)
print("using %s template:\n %s" % (template, "\n ".join(textwrap.wrap(inf["description"], 77))))
try:
data, inf = get_template_data(template)
except InvalidDiskImage, e:
info = get_template_info()
print("Error: %s\n\n%s" % (e, info))
return
print("Using template %s:\n %s" % (inf['name'], "\n ".join(textwrap.wrap(inf["description"], 77))))
if not options.dry_run:
if os.path.exists(name) and not options.force:
print("skipping %s, use -f to overwrite" % (name))