Skip to content
Snippets Groups Projects
Commit 378d7190 authored by ferrari's avatar ferrari
Browse files

Add kwargs arguments

parent b56c1f82
No related branches found
No related tags found
No related merge requests found
......@@ -32,6 +32,7 @@ The json file created will be:
```json
{"script": "/home/xxx/Documents/Python/auto_git_info/test_auto_git.py",
"commit": "7ce86099e62e7ffdfe77fbbcafbe48b862c269b9",
"branch": "master",
"parameters": {"input": "rnd_input",
"output": null},
"time": "2023-06-23T18:04:30.894271"}
......@@ -45,6 +46,8 @@ Instead of passing the `Namespace` object from the `ArgumentParser` parser, you
Alternatively, you can pass a dictionary or any serializable object.
You can also use `None` if you don't have any arguments.
Additional kwargs can be passed. The kwargs names will be the keys in the json, and their values will be the associated values.
#### path
If you don't want the default path you can use the `path` named argument to specify the path to save the info.
......
from setuptools import setup, find_packages
setup(name='auto_git_info',
version='1.0',
version='1.0.1',
description='Automatically log your command line arguments and commit sha',
author='Maxence Ferrari',
py_modules=['auto_git_info'],
......
__version__ = 1.0
\ No newline at end of file
__version__ = "1.0.1"
......@@ -7,13 +7,14 @@ import warnings
import __main__
def log_info(args, *, path=None):
def log_info(args, *, path=None, **kwargs):
"""
Create a json file with script, commit, parameters, and time keys.
The commit is the commit sha and script is the main script executed
args: An argparse object, None or any serializable objet
path: Path to the json file. default to 'run_log/current_time.json'
**kwargs: Add serializable objet to the json with its kwargs as a key
"""
time = datetime.datetime.now().isoformat()
......@@ -31,13 +32,17 @@ def log_info(args, *, path=None):
with open(path, 'w') as logfile:
repo = git.Repo(os.path.dirname(os.path.abspath(__main__.__file__)), search_parent_directories=True)
sha = repo.head.object.hexsha
branch = repo.head.ref.name
changes = repo.index.diff(None)
if len(changes):
warnings.simplefilter('always', DeprecationWarning)
warnings.warn(f'{[c.a_path for c in changes]} have differences with current commit',
DeprecationWarning, stacklevel=2)
warnings.simplefilter('default', DeprecationWarning)
json.dump({'script': __main__.__file__,
out = {'script': __main__.__file__,
'commit': sha,
'branch': branch,
'parameters': args,
'time': time}, logfile)
'time': time}
out.update({k: v for k, v in kwargs.items() if k not in out})
json.dump(out, logfile)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment