-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow users to provide copying of optional files without failing. #41
Changes from all commits
b78b9ba
a8b1fb1
86c6f75
ab5b171
3806ecb
63d23ee
cd5fe76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python Debugger: Current File", | ||
"type": "debugpy", | ||
"request": "launch", | ||
"program": "${file}", | ||
"console": "integratedTerminal" | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true, | ||
"python.testing.autoTestDiscoverOnSaveEnabled": true, | ||
"python.testing.promptToConfigure": true, | ||
"python.testing.pytestArgs": ["-v", "--color=yes", "--no-cov"], | ||
"python.testing.cwd": "./tests", | ||
"pre-commit-helper.config": "${workspaceFolder}/.pre-commit-config.yaml", | ||
"pre-commit-helper.runOnSave": "all hooks", | ||
"pre-commit-helper.runOnSaveRegex": "^(.*)\\.(py|c|h)$" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import os | ||
from logging import getLogger | ||
|
||
from .fsutils import cp, mkdir | ||
|
@@ -17,13 +18,20 @@ | |
|
||
NOTE | ||
---- | ||
"action" can be one of mkdir", "copy", etc. | ||
"action" can be one of "mkdir", "copy", "copy_req", "copy_opt", etc. | ||
Corresponding "act" would be ['dir1', 'dir2'], [['src1', 'dest1'], ['src2', 'dest2']] | ||
"copy_req" will raise an error if the source file does not exist | ||
"copy_opt" will not raise an error if the source file does not exist but will present a warning | ||
|
||
Attributes | ||
---------- | ||
config : dict | ||
Dictionary of files to manipulate | ||
|
||
NOTE | ||
---- | ||
`copy` will be deprecated in the future in favor of `copy_req` and `copy_opt` | ||
Users are encouraged to use `copy_req` and `copy_opt` instead of `copy` | ||
""" | ||
|
||
def __init__(self, config): | ||
|
@@ -35,15 +43,25 @@ | |
Method to execute bulk actions on files described in the configuration | ||
""" | ||
sync_factory = { | ||
'copy': self._copy_files, | ||
'copy': self.copy_req, | ||
'copy_req': self.copy_req, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept the distinction for clarity. If it were my choice, I would deprecate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case, I would suggest comments to indicate that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. If that is the consensus from the team, we can make that happen. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done @DavidNew-NOAA |
||
'copy_opt': self.copy_opt, | ||
'mkdir': self._make_dirs, | ||
} | ||
# loop through the configuration keys | ||
for action, files in self.config.items(): | ||
sync_factory[action](files) | ||
|
||
@staticmethod | ||
def _copy_files(filelist): | ||
def copy_req(filelist): | ||
FileHandler._copy_files(filelist, required=True) | ||
|
||
@staticmethod | ||
def copy_opt(filelist): | ||
FileHandler._copy_files(filelist, required=False) | ||
|
||
@staticmethod | ||
def _copy_files(filelist, required=True): | ||
"""Function to copy all files specified in the list | ||
|
||
`filelist` should be in the form: | ||
|
@@ -53,15 +71,28 @@ | |
---------- | ||
filelist : list | ||
List of lists of [src, dest] | ||
required : bool, optional | ||
Flag to indicate if the src file is required to exist. Default is True | ||
""" | ||
for sublist in filelist: | ||
if len(sublist) != 2: | ||
raise Exception( | ||
raise IndexError( | ||
f"List must be of the form ['src', 'dest'], not {sublist}") | ||
src = sublist[0] | ||
dest = sublist[1] | ||
cp(src, dest) | ||
logger.info(f'Copied {src} to {dest}') | ||
if os.path.exists(src): | ||
try: | ||
cp(src, dest) | ||
logger.info(f'Copied {src} to {dest}') | ||
except Exception as ee: | ||
logger.exception(f"Error copying {src} to {dest}") | ||
raise ee(f"Error copying {src} to {dest}") | ||
else: | ||
if required: | ||
logger.exception(f"Source file '{src}' does not exist and is required, ABORT!") | ||
raise FileNotFoundError(f"Source file '{src}' does not exist") | ||
else: | ||
logger.warning(f"Source file '{src}' does not exist, skipping!") | ||
|
||
@staticmethod | ||
def _make_dirs(dirlist): | ||
|
@@ -73,5 +104,9 @@ | |
List of directories to create | ||
""" | ||
for dd in dirlist: | ||
mkdir(dd) | ||
logger.info(f'Created {dd}') | ||
try: | ||
mkdir(dd) | ||
logger.info(f'Created {dd}') | ||
except Exception as ee: | ||
logger.exception(f"Error creating directory {dd}") | ||
raise ee(f"Error creating directory {dd}") | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import os | ||
import sys | ||
|
||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src'))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this file needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. It tells the package where the src is in relation to the tests. Some packages do not keep code under Another use for this file is to provide test fixtures that are/could be used across tests. Eg. if |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are these vscode files supposed to be committed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They should be. There is nothing here that is user specific.