-
Notifications
You must be signed in to change notification settings - Fork 33
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
Refactors amclient.py #48
Conversation
1cef7ce
to
80d320b
Compare
@jrwdunham @jraddaoui this should be a little bit better now. @jrwdunham I hadn't anticipated the effect of renaming the branch, and so this is a new pull request. I have addressed the majority of the concerns in #46 Specifically:
What I haven't been able to address is primarily:
I had a look at using getattr to understand if there was a behaviour I was missing, but this seems to be used for exceptions i.e. when an attribute isn't there. Whereas the change I've implemented is to provide a way for the script to return a message to the user on the command over returning As far as I can tell, the output is equivalent. Maybe I'm missing something though? Idk, What do you think? Additional Changes Given @sevein's comments here, and time pressures, are you happy I create a new issue to add setuptools functionality at a later date? I think it would be a worthwhile addition. |
@ross-spencer The behaviour that is being missed is that automation-tools/transfers/amclient.py Line 152 in 80d320b
In current master, you can compare this pretty-printed Python output:
to this easily machine-readable JSON output:
I would like to keep the old API including the Another issue is that your changes are checking whether the return value is a
Using the Why not keep your improved error-handling/printing logic but move it to the
And then restore the end of
|
I believe you want @ross-spencer rather than me 😀 |
transfers/transfer.py
Outdated
parser = argparse.ArgumentParser(description=__doc__, | ||
formatter_class=rawformatter) | ||
parser.add_argument('-u', '--user', metavar='USERNAME', required=True, | ||
help=('Username of the Archivematica dashboard user ' |
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.
Just FYI, all these parens are unneeded because the kwarg=value
expressions are all already in the method's parens. So this could just be
parser.add_argument('-u', '--user', metavar='USERNAME', required=True,
help='Username of the Archivematica dashboard user '
'to authenticate as.')
80d320b
to
451ca2f
Compare
Hi @jrwdunham I have added your snippet and it looks good (and makes sense). I'm afraid it's a case of RTFM for me here. i hadn't seen that |
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.
Hi @ross-spencer. This is looking good. I have a few remaining comments.
transfers/amclient.py
Outdated
LOGGER.warning('%s Request to %s returned %s %s', method, url, | ||
response.status_code, response.reason) | ||
LOGGER.debug('Response: %s', response.text) | ||
return errors.ERR_INVALID_RESPONSE |
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.
Since we're no longer returning None
from this func, I think you should change the :returns:
line of the func docstring to reflect what can actually be returned.
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.
Also, as you probably noticed, this _call_url_json
is defined almost identically in both amclient.py and transfer.py. Do you think you could move your revised version into a new utils module that both of the public modules import?
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.
Good spot on the docstring.
I like the utils.py
idea as well. I have to change the error_lookup to be a little more robust - a function call rather than a naive dictionary lookup but I think that will work well across both scripts.
transfers/amclient.py
Outdated
# from any failed calls to the AM or SS servers. | ||
def stdout(self, stuff): | ||
"""Print to stdout, either as JSON or pretty-printed Python.""" | ||
if self.output_mode == 'json': |
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.
This isn't your responsibility, but could you call lower()
on self.stdout
before the comparison here? This would allow the user to pass --output-mode=JSON
and have it work as expected.
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.
For sure!
transfers/amclientargs.py
Outdated
|
||
# Allow execution as an executable and the script to be run at package level | ||
# by ensuring that it can see itself. | ||
sys.path.append('../') |
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.
This smells a bit to me but I understand why you did it. I think maybe you should replace this with sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
in this file and in amclient.py and transfer.py. If you do, then we should be able to call these files in all of these ways:
$ pwd
automation-tools
$ ./transfers/amclient.py aips
$ python transfers/amclient.py aips
$ python -m transfers.amclient aips
$ cd transfers
$ ./amclient.py aips
$ python amclient.py aips
$ python -m amclient aips
As this PR is right now, you cannot call ./transfers/amclient.py aips
or python transfers/amclient.py aips
. What do you think?
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.
Yup! Can confirm 👍
e4051bf
to
43c988c
Compare
* Creates new logging configuration script to reduce redundancy * Separates responsibility of amclient, defaults, and cli args * Cleans up argparse output using metavar * Adds Conformity to PEP8 to amclient.py and transfers.py * Returns user friendly string via getattr if function call fails in main * Improved error return from JSON request function in amclient.py * Adds .db and .lck files to .gitignore * Works at module and executable level * E402 flake8 warning ignored in Tox to enable module/exe to work * Captures connection errors in transfers.py * Adds a utils module to handle json requests across scripts * Initial changes provides scope for future refactoring as required * Addresses #47
43c988c
to
699b018
Compare
@jrwdunham changes made! What do you think - looking better? |
Follows up #46 with more appropriate naming conventions for the branch.