Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
17e3747
rm api from cli.py
cadenmyers13 Oct 20, 2025
82f7394
add copy_examples api to packsmanager
cadenmyers13 Oct 20, 2025
d973c83
add tests for copy_examples
cadenmyers13 Oct 20, 2025
375606c
add additional dirs to temp structure
cadenmyers13 Oct 20, 2025
711c24e
rm old tests
cadenmyers13 Oct 20, 2025
987c3e3
add news
cadenmyers13 Oct 20, 2025
9e65a27
glob only files and not dirs
cadenmyers13 Oct 20, 2025
5354831
fix target dir location to be under cwd and change name for new test …
cadenmyers13 Oct 22, 2025
47d87fb
Add additional test case, fix how copy location is tested
cadenmyers13 Oct 22, 2025
0a490b9
copy_examples function
cadenmyers13 Oct 22, 2025
6e2695d
rm error message function
cadenmyers13 Oct 23, 2025
e96988f
add test for forced overwrite of copied files
cadenmyers13 Oct 23, 2025
abfba68
Add 'force' param to copy_examples
cadenmyers13 Oct 23, 2025
d4cddf5
fix copy_tree to handle warnings and errors properly
cadenmyers13 Oct 27, 2025
38881b8
Merge branch 'main' of github.com:diffpy/diffpy.cmi into copy-func-test
cadenmyers13 Oct 28, 2025
f4fe605
pin workflow to python 3.13
cadenmyers13 Oct 30, 2025
07cbcd7
add test for force and non-force to make sure files are copied correctly
cadenmyers13 Oct 31, 2025
078b13c
update copy_tree_to_target to handle different copying cases
cadenmyers13 Oct 31, 2025
e7444d7
capture warning output in print statement
cadenmyers13 Oct 31, 2025
c792c0b
add rich to conda.txt
cadenmyers13 Oct 31, 2025
7d7a2a3
add UC comments
cadenmyers13 Oct 31, 2025
52a6752
use ANSI color code for now
cadenmyers13 Oct 31, 2025
611bdf5
rm rich from reqs
cadenmyers13 Oct 31, 2025
d6f34e0
Rm colors from print
cadenmyers13 Oct 31, 2025
087851b
[pre-commit.ci] auto fixes from pre-commit hooks
pre-commit-ci[bot] Oct 31, 2025
87c1f43
rm color from test
cadenmyers13 Nov 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
copy_examples function
  • Loading branch information
cadenmyers13 committed Oct 22, 2025
commit 0a490b92e66faa4edd11db3bbb5543860a2fac2c
65 changes: 64 additions & 1 deletion src/diffpy/cmi/packsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See LICENSE.rst for license information.
#
##############################################################################

import shutil
from importlib.resources import as_file
from pathlib import Path
from typing import List, Union
Expand Down Expand Up @@ -149,8 +149,71 @@ def copy_examples(
Target directory to copy examples into. Defaults to current
working directory.
"""
self._target_dir = target_dir.resolve() if target_dir else Path.cwd()

if "all" in examples_to_copy:
self._copy_all()
return

for item in examples_to_copy:
if item in self.available_examples():
self._copy_pack(item)
elif self._is_example_name(item):
self._copy_example(item)
else:
self._not_found_error(item)
del self._target_dir
return

def _copy_all(self):
"""Copy all packs and examples."""
for pack_name in self.available_examples():
self._copy_pack(pack_name)

def _copy_pack(self, pack_name):
"""Copy all examples in a single pack."""
examples = self.available_examples().get(pack_name, [])
for ex_name, ex_path in examples:
self._copy_tree_to_target(pack_name, ex_name, ex_path)

def _copy_example(self, example_name):
Comment thread
cadenmyers13 marked this conversation as resolved.
"""Copy a single example by its name."""
example_found = False
for pack_name, examples in self.available_examples().items():
for ex_name, ex_path in examples:
if ex_name == example_name:
self._copy_tree_to_target(pack_name, ex_name, ex_path)
example_found = True
if not example_found:
self._not_found_error(example_name)

def _is_example_name(self, name):
"""Return True if the given name matches any known example."""
for pack_name, examples in self.available_examples().items():
for example_name, _ in examples:
if example_name == name:
return True
return False

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main update on recent push is the content below here.

I've also found this package called rich that allows you to print console outputs in color which is nice. This will be good for pretty printing for cmi print info because that will output a lot of text. Color will help separate the content.

def _copy_tree_to_target(self, pack_name, example_name, src_path):
"""Helper to handle the actual filesystem copy."""
dest_dir = self._target_dir / pack_name / example_name
if dest_dir.exists():
plog.warning(
f"Example directory(ies): '{dest_dir.stem}' already exist. "
" Current versions of existing files have "
"been left unchanged. To overwrite, please rerun "
"and specify --force."
)
return
dest_dir.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(src_path, dest_dir)

def _not_found_error(self, name):
raise FileNotFoundError(
f"No examples or packs found for input: '{name}'"
)

def _resolve_pack_file(self, identifier: Union[str, Path]) -> Path:
"""Resolve a pack identifier to an absolute .txt path.

Expand Down
Loading