Skip to content

Commit

Permalink
Merge pull request #24 from molssi-seamm/dev
Browse files Browse the repository at this point in the history
Improved naming of loop subdirectories
  • Loading branch information
seamm committed Jul 29, 2024
2 parents 2d48a4a + 05b5623 commit 93ae014
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 14 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ on:
pull_request:
branches:
- "main"
schedule:
# Run on master by default Sunday morning at 3:30:
# Scheduled workflows run on the latest commit on the default or base branch.
# (from https://help.github.com/en/actions/reference/events-that-trigger-workflows#scheduled-events-schedule)
- cron: "30 3 * * 0"

jobs:
ci:
Expand Down
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
=======
History
=======
2024.7.28 -- Improved naming of loop subdirectories
* The subdirectories now start at 1, not 0, to make counting more normal
* When looping over systems, now have the option to name the directories
after the system or configuration name, not the iteration number.

2023.11.9 -- Bugfix: "For" loops could crash
* For loops could crash writing to write final_structure.mmcif before the directory
had been made.
Expand Down
42 changes: 35 additions & 7 deletions loop_step/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, flowchart=None, extension=None):
self._loop_value = None
self._loop_length = None
self._file_handler = None
self._custom_directory_name = None

super().__init__(
flowchart=flowchart, title="Loop", extension=extension, logger=logger
Expand Down Expand Up @@ -66,7 +67,11 @@ def git_revision(self):

@property
def working_path(self):
return Path(self.directory) / f"iter_{self._loop_value:{self.iter_format}}"
if self._custom_directory_name is not None:
tmp = Path(self.directory) / self._custom_directory_name
else:
tmp = Path(self.directory) / f"iter_{self._loop_value+1:{self.iter_format}}"
return tmp

def describe(self):
"""Write out information about what this node will do"""
Expand Down Expand Up @@ -145,6 +150,9 @@ def run(self):
context=seamm.flowchart_variables._data
)

# Reset variables to initial state.
self._custom_directory_name = None

# Print out header to the main output
printer.important(__(self.description_text(P), indent=self.indent))

Expand Down Expand Up @@ -551,6 +559,7 @@ def run(self):
if self._loop_value >= self._loop_length:
self._loop_value = None
self._loop_length = None
self._custom_directory_name = None

# Revert the loop index variables to the next outer loop
# if there is one, or remove them.
Expand All @@ -573,6 +582,15 @@ def run(self):
system_db.system = configuration.system
system.configuration = configuration

if P["directory name"] == "system name":
self._custom_directory_name = self.safe_filename(system.name)
elif P["directory name"] == "configuration name":
self._custom_directory_name = self.safe_filename(
configuration.name
)
else:
self._custom_directory_name = None

# Set up the index variables
tmp = self.get_variable("_loop_indices")
self.set_variable(
Expand Down Expand Up @@ -605,9 +623,8 @@ def run(self):
# Add the iteration to the ids so the directory structure is
# reasonable
self.flowchart.reset_visited()
self.set_subids(
(*self._id, f"iter_{self._loop_value:{self.iter_format}}")
)
tmp = self.working_path.name
self.set_subids((*self._id, tmp))

# Run through the steps in the loop body
try:
Expand All @@ -617,9 +634,8 @@ def run(self):
traceback.print_exc(file=sys.stderr)
traceback.print_exc(file=sys.stdout)
except Exception as e:
printer.job(
f"Caught exception in loop iteration {self._loop_value}: {str(e)}"
)
tmp = self.working_path.name
printer.job(f"Caught exception in loop iteration {tmp}: {str(e)}")
with open(iter_dir / "stderr.out", "a") as fd:
traceback.print_exc(file=fd)
if "continue" in P["errors"]:
Expand Down Expand Up @@ -783,3 +799,15 @@ def loop_node(self):
# There is no body of the loop!
self.logger.debug("There is no loop body")
return None

def safe_filename(self, filename):
clean = re.sub(r"[/\\?%*:|\"<>\x7F\x00-\x1F]", "-", filename)

# Check for duplicates...
path = Path(self.directory) / clean
count = 1
while path.exists():
count += 1
path = Path(self.directory) / f"{clean}_{count}"

return path.name
13 changes: 13 additions & 0 deletions loop_step/loop_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ class LoopParameters(seamm.Parameters):
"description": "",
"help_text": "The filter for the configuration name",
},
"directory name": {
"default": "loop iteration",
"kind": "string",
"default_units": "",
"enumeration": (
"loop iteration",
"system name",
"configuration name",
),
"format_string": "s",
"description": "Directory names:",
"help_text": "The directory name for the loop iteration.",
},
"errors": {
"default": "continue to next iteration",
"kind": "string",
Expand Down
2 changes: 2 additions & 0 deletions loop_step/tk_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ def reset_dialog(self, widget=None):
row=row, column=2, columnspan=2, sticky=tk.EW
)
frame.columnconfigure(3, weight=1)
row += 1
self["directory name"].grid(row=row, column=0, columnspan=3, sticky=tk.W)
else:
raise RuntimeError("Don't recognize the loop_type {}".format(loop_type))
row += 1
Expand Down
4 changes: 2 additions & 2 deletions versioneer.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ def get_config_from_root(root):
# configparser.NoOptionError (if it lacks "VCS="). See the docstring at
# the top of versioneer.py for instructions on writing your setup.cfg .
setup_cfg = os.path.join(root, "setup.cfg")
parser = configparser.SafeConfigParser()
parser = configparser.ConfigParser()
with open(setup_cfg, "r") as f:
parser.readfp(f)
parser.read_file(f)
VCS = parser.get("versioneer", "VCS") # mandatory

def get(parser, name):
Expand Down

0 comments on commit 93ae014

Please sign in to comment.