Skip to content

Commit

Permalink
cline.py -- tried to reduce chances of corrupting default files
Browse files Browse the repository at this point in the history
trap ctrl-C as default files are written, give a better warning if they somehow still are.
  • Loading branch information
trmrsh committed Mar 16, 2021
1 parent d7f5de9 commit 1f710e4
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions hipercam/cline.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import sys
import pickle
import warnings
import signal
from collections import OrderedDict

# next two lines allow tab completion of file names
Expand Down Expand Up @@ -245,9 +246,11 @@ def __init__(self, direnv, defdir, cname, args):
self._lpars = {}
except (EOFError, pickle.UnpicklingError):
warnings.warn(
"failed to read local defaults file "
+ self._lname
+ "; possible corrupted file.\n",
f"failed to read local defaults file {self._lname}; " +
"possible corrupted file. Defaults local to this " +
"command will be reset.\n" +
"Re-run it with 'prompt' in case hidden " +
"parameters have changed values.",
ClineWarning,
)
self._lpars = {}
Expand All @@ -260,9 +263,11 @@ def __init__(self, direnv, defdir, cname, args):
self._gpars = {}
except (EOFError, pickle.UnpicklingError):
warnings.warn(
"failed to read global defaults file "
+ self._gname
+ "; possible corrupted file.\n",
f"failed to read global defaults file {self._gname}; " +
"possible corrupted file. Global defaults will be reset.\n" +
"This comand and others may have with altered " +
"'hidden' parameter values. Use 'prompt' on the " +
"command line if you encounter odd behaviour or problems",
ClineWarning,
)
self._gpars = {}
Expand Down Expand Up @@ -345,6 +350,10 @@ def save(self):
ClineWarning,
)

# ignore ctrl-C during writing of default files to reduce
# chance of corruption
signal.signal(signal.SIGINT, signal.SIG_IGN)

# save local defaults
try:
with open(self._lname, "wb") as flocal:
Expand Down Expand Up @@ -381,6 +390,9 @@ def save(self):
ClineWarning,
)

# return to default behaviour
signal.signal(signal.SIGINT, signal.SIG_DFL)

def prompt_state(self):
"""Says whether prompting is being forced or not. Note the propting state does
not change once an Cline is initialized, being fixed by the presence
Expand Down

0 comments on commit 1f710e4

Please sign in to comment.