From 71a8f56b35fe04350931047a019eb6b43ec72920 Mon Sep 17 00:00:00 2001 From: Daniel Knuettel Date: Fri, 6 Oct 2023 13:43:08 +0200 Subject: [PATCH 1/8] improvements on error messages when file access is impossible --- lib/gpt/core/io/load.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/gpt/core/io/load.py b/lib/gpt/core/io/load.py index dcf133b2..e8b34cb4 100644 --- a/lib/gpt/core/io/load.py +++ b/lib/gpt/core/io/load.py @@ -18,6 +18,8 @@ # import gpt, cgpt from gpt.params import params_convention +import os +import warnings # load through cgpt backend (openQCD, ...) @@ -55,6 +57,20 @@ def load(fn, **p): gpt.core.io.qlat_io, ] + if(not os.path.exists(fn)): + raise FileNotFoundError(f"[Errno 2] No such file or directory: '{fn}'") + + if(os.path.isfile(fn)): + if(not os.access(fn, os.R_OK)): + raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") + if(os.path.isdir(fn)): + if(not os.access(fn, os.X_OK)): + raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") + if(not os.access(fn, os.R_OK)): + warnings.warn(f"Permission denied: '{fn}'. File access may be possible.", ResourceWarning) + g.message(f"Permission denied: '{fn}'. File access may be possible.") + + for fmt in supported: try: return fmt.load(fn, p) From a93cfc7292071c4f4d8e6588cb3d34151043b892 Mon Sep 17 00:00:00 2001 From: Daniel Knuettel Date: Fri, 6 Oct 2023 14:07:46 +0200 Subject: [PATCH 2/8] internally, gpt is used as gpt --- lib/gpt/core/io/load.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gpt/core/io/load.py b/lib/gpt/core/io/load.py index e8b34cb4..d526feeb 100644 --- a/lib/gpt/core/io/load.py +++ b/lib/gpt/core/io/load.py @@ -68,7 +68,7 @@ def load(fn, **p): raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") if(not os.access(fn, os.R_OK)): warnings.warn(f"Permission denied: '{fn}'. File access may be possible.", ResourceWarning) - g.message(f"Permission denied: '{fn}'. File access may be possible.") + gpt.message(f"Permission denied: '{fn}'. File access may be possible.") for fmt in supported: From 732acc2508209a455c2e2d9288689bcd94219b00 Mon Sep 17 00:00:00 2001 From: Daniel Knuettel Date: Fri, 6 Oct 2023 14:25:47 +0200 Subject: [PATCH 3/8] some reasoning on error messages --- lib/gpt/core/io/load.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/gpt/core/io/load.py b/lib/gpt/core/io/load.py index d526feeb..5af00504 100644 --- a/lib/gpt/core/io/load.py +++ b/lib/gpt/core/io/load.py @@ -64,11 +64,17 @@ def load(fn, **p): if(not os.access(fn, os.R_OK)): raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") if(os.path.isdir(fn)): + # This part is for gpt's own format which uses directories + # instead of files. if(not os.access(fn, os.X_OK)): raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") if(not os.access(fn, os.R_OK)): - warnings.warn(f"Permission denied: '{fn}'. File access may be possible.", ResourceWarning) - gpt.message(f"Permission denied: '{fn}'. File access may be possible.") + # Here, handling the access is a bit tricky: + # It is not necessary to have directory read access. + # But it is very likely that the individual files will lack + # read access. + warnings.warn(f"Permission denied: '{fn}' (directory). File access may be possible.", ResourceWarning) + gpt.message(f"Permission denied: '{fn} (directory)'. File access may be possible.") for fmt in supported: From 4099dbc81c2322b764dbdc6c3b24cd86c6947615 Mon Sep 17 00:00:00 2001 From: Daniel Knuettel Date: Fri, 6 Oct 2023 14:26:51 +0200 Subject: [PATCH 4/8] linting --- lib/gpt/core/io/load.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/gpt/core/io/load.py b/lib/gpt/core/io/load.py index 5af00504..f94bb022 100644 --- a/lib/gpt/core/io/load.py +++ b/lib/gpt/core/io/load.py @@ -57,18 +57,18 @@ def load(fn, **p): gpt.core.io.qlat_io, ] - if(not os.path.exists(fn)): + if not os.path.exists(fn): raise FileNotFoundError(f"[Errno 2] No such file or directory: '{fn}'") - if(os.path.isfile(fn)): - if(not os.access(fn, os.R_OK)): + if os.path.isfile(fn): + if not os.access(fn, os.R_OK): raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") - if(os.path.isdir(fn)): + if os.path.isdir(fn): # This part is for gpt's own format which uses directories # instead of files. - if(not os.access(fn, os.X_OK)): + if not os.access(fn, os.X_OK): raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") - if(not os.access(fn, os.R_OK)): + if not os.access(fn, os.R_OK): # Here, handling the access is a bit tricky: # It is not necessary to have directory read access. # But it is very likely that the individual files will lack From f909c05557df3e0bb1ecbb2c10d5c13bdb0eab0f Mon Sep 17 00:00:00 2001 From: Daniel Knuettel Date: Fri, 6 Oct 2023 14:51:18 +0200 Subject: [PATCH 5/8] linting --- lib/gpt/core/io/load.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/gpt/core/io/load.py b/lib/gpt/core/io/load.py index f94bb022..3101ed3d 100644 --- a/lib/gpt/core/io/load.py +++ b/lib/gpt/core/io/load.py @@ -70,13 +70,12 @@ def load(fn, **p): raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") if not os.access(fn, os.R_OK): # Here, handling the access is a bit tricky: - # It is not necessary to have directory read access. + # It is not necessary to have directory read access. # But it is very likely that the individual files will lack # read access. warnings.warn(f"Permission denied: '{fn}' (directory). File access may be possible.", ResourceWarning) gpt.message(f"Permission denied: '{fn} (directory)'. File access may be possible.") - for fmt in supported: try: return fmt.load(fn, p) From 47785fa6abf831a95ebe1e8ad0c064de73344fb0 Mon Sep 17 00:00:00 2001 From: Daniel Knuettel Date: Fri, 6 Oct 2023 15:18:44 +0200 Subject: [PATCH 6/8] adapted error messages to flexibility of gpt.load --- lib/gpt/core/io/load.py | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/lib/gpt/core/io/load.py b/lib/gpt/core/io/load.py index 3101ed3d..efe94526 100644 --- a/lib/gpt/core/io/load.py +++ b/lib/gpt/core/io/load.py @@ -27,7 +27,7 @@ def load_cgpt(*a): result = [] r, metadata = cgpt.load(*a, gpt.default.is_verbose("io")) if r is None: - raise gpt.LoadError() + raise gpt.LoadError(f"file: {a[0]}") for gr in r: grid = gpt.grid(gr[1], eval("gpt." + gr[2]), eval("gpt." + gr[3]), gr[0]) result_grid = [] @@ -57,25 +57,6 @@ def load(fn, **p): gpt.core.io.qlat_io, ] - if not os.path.exists(fn): - raise FileNotFoundError(f"[Errno 2] No such file or directory: '{fn}'") - - if os.path.isfile(fn): - if not os.access(fn, os.R_OK): - raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") - if os.path.isdir(fn): - # This part is for gpt's own format which uses directories - # instead of files. - if not os.access(fn, os.X_OK): - raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") - if not os.access(fn, os.R_OK): - # Here, handling the access is a bit tricky: - # It is not necessary to have directory read access. - # But it is very likely that the individual files will lack - # read access. - warnings.warn(f"Permission denied: '{fn}' (directory). File access may be possible.", ResourceWarning) - gpt.message(f"Permission denied: '{fn} (directory)'. File access may be possible.") - for fmt in supported: try: return fmt.load(fn, p) @@ -86,6 +67,16 @@ def load(fn, **p): # rules this one out as well pass + if not os.path.exists(fn): + # XXX: when implementing non-file URLs, change this error message. + raise FileNotFoundError(f"[Errno 2] No such file or directory: '{fn}'") + + if os.path.isfile(fn): + if not os.access(fn, os.R_OK): + raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") + if os.path.isdir(fn): + raise LoadError(f"Error: '{fn}' is a directory and gpt_io failed to process it (permissions[+x] ok? wrong path?).") + a = [fn] if len(p) > 0: a.append(p) From b53c993f3c1f9ec3e71dd8459bb045bd51b1d075 Mon Sep 17 00:00:00 2001 From: Daniel Knuettel Date: Fri, 6 Oct 2023 15:21:44 +0200 Subject: [PATCH 7/8] LoadError -> gpt.LoadError --- lib/gpt/core/io/load.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gpt/core/io/load.py b/lib/gpt/core/io/load.py index efe94526..fa3f784e 100644 --- a/lib/gpt/core/io/load.py +++ b/lib/gpt/core/io/load.py @@ -75,7 +75,7 @@ def load(fn, **p): if not os.access(fn, os.R_OK): raise PermissionError(f"[Errno 13] Permission denied: '{fn}'") if os.path.isdir(fn): - raise LoadError(f"Error: '{fn}' is a directory and gpt_io failed to process it (permissions[+x] ok? wrong path?).") + raise gpt.LoadError(f"Error: '{fn}' is a directory and gpt_io failed to process it (permissions[+x] ok? wrong path?).") a = [fn] if len(p) > 0: From 893bbd7543bc39829310611f868a79c903a1256f Mon Sep 17 00:00:00 2001 From: Daniel Knuettel Date: Wed, 25 Oct 2023 12:21:55 +0200 Subject: [PATCH 8/8] removed unused import --- lib/gpt/core/io/load.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/gpt/core/io/load.py b/lib/gpt/core/io/load.py index fa3f784e..2eb8f99a 100644 --- a/lib/gpt/core/io/load.py +++ b/lib/gpt/core/io/load.py @@ -19,7 +19,6 @@ import gpt, cgpt from gpt.params import params_convention import os -import warnings # load through cgpt backend (openQCD, ...)