Skip to content

Commit 890bfd7

Browse files
committed
[cmd/file_upgrade] is now part of nixio package
1 parent ac96cc1 commit 890bfd7

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

nixio/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@
3434
# version
3535
from .info import VERSION
3636

37+
# cmd
38+
from .cmd.upgrade import file_upgrade
39+
3740
__all__ = ("File", "Block", "Group", "DataArray", "DataFrame", "Tag",
3841
"MultiTag", "Source", "Section", "S", "Feature", "Property",
3942
"OdmlType", "SampledDimension", "RangeDimension", "SetDimension",
4043
"FileMode", "DataSliceMode", "DataType", "DimensionType",
41-
"LinkType", "Compression", "SliceMode", "IndexMode", "validator")
44+
"LinkType", "Compression", "SliceMode", "IndexMode", "validator")
4245
__author__ = ('Christian Kellner, Adrian Stoewer, Andrey Sobolev, Jan Grewe, '
4346
'Balint Morvai, Achilleas Koutsou')
4447
__version__ = VERSION

nixio/cmd/upgrade.py

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,15 @@ def update_ver():
218218

219219

220220
def collect_tasks(fname):
221-
print(f"{fname}:")
222-
221+
tasks = list()
223222
file_ver = get_file_version(fname)
224223
file_verstr = ".".join(str(v) for v in file_ver)
225224
lib_verstr = ".".join(str(v) for v in nix.file.HDF_FF_VERSION)
226225
if file_ver >= nix.file.HDF_FF_VERSION:
227-
print(f" Up to date ({file_verstr})")
228-
return
226+
return tasks, file_verstr, lib_verstr
229227

230228
# even if the version string indicates the file is old, check format
231229
# details before scheduling tasks
232-
tasks = list()
233230
id_task = add_file_id(fname)
234231
if id_task:
235232
tasks.append(id_task)
@@ -245,11 +242,7 @@ def collect_tasks(fname):
245242
# always update the format last
246243
tasks.append(update_format_version(fname))
247244

248-
# print task list
249-
print(f" {file_verstr} -> {lib_verstr}")
250-
print(" - " + "\n - ".join(t.__doc__ for t in tasks) + "\n")
251-
252-
return tasks
245+
return tasks, file_verstr, lib_verstr
253246

254247

255248
def create_subcmd_parser(parser):
@@ -260,12 +253,54 @@ def create_subcmd_parser(parser):
260253
return parser
261254

262255

256+
def print_tasks(fname, tasklist, fileversion, libversion):
257+
if len(tasklist) == 0:
258+
print(f"File {fname} is up to date ({fileversion})")
259+
return
260+
print(f"{fname}: {fileversion} -> {libversion}")
261+
print(" - " + "\n - ".join(t.__doc__ for t in tasklist) + "\n")
262+
263+
264+
def process_tasks(fname, tasklist, quiet=True):
265+
if not quiet:
266+
print(f"Processing {fname} ", end="", flush=True)
267+
for task in tasklist:
268+
task()
269+
if not quiet:
270+
print("done")
271+
272+
273+
def file_upgrade(fname, quiet=True):
274+
"""
275+
Upgrades a file from an old format version to the current version.
276+
277+
:param fname: The fully qualified filename.
278+
:type fname: str
279+
:param quiet: Whether or not the upgrade tool should give feedback on the command line. Defaults to True, no output.
280+
:type quiet: bool
281+
282+
:returns: True if the conversion succeeded, False otherwise, it the file does not need upgrading True is returned.
283+
:rtype : bool
284+
"""
285+
try:
286+
tasklist, fileversion, libversion = collect_tasks(fname)
287+
if not quiet:
288+
print_tasks(fname, tasklist, fileversion, libversion)
289+
290+
process_tasks(fname, tasklist, quiet=quiet)
291+
except Exception as e:
292+
print(f"An Exception occurred while upgrading file {fname}. Error is {e}")
293+
return False
294+
return True
295+
296+
263297
def main(args):
264298
filenames = args.file
265299

266300
tasks = dict()
267301
for fname in filenames:
268-
tasklist = collect_tasks(fname)
302+
tasklist, fileversion, libversion = collect_tasks(fname)
303+
print_tasks(fname, tasklist, fileversion, libversion)
269304
if not tasklist:
270305
continue
271306

@@ -296,7 +331,4 @@ def main(args):
296331

297332
if conf in ("y", "yes"):
298333
for fname, tasklist in tasks.items():
299-
print(f"Processing {fname} ", end="", flush=True)
300-
for task in tasklist:
301-
task()
302-
print("done")
334+
process_tasks(fname, tasklist, quiet=False)

0 commit comments

Comments
 (0)