summaryrefslogtreecommitdiffstats
path: root/third_party/python/cookiecutter/cookiecutter/replay.py
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/python/cookiecutter/cookiecutter/replay.py')
-rw-r--r--third_party/python/cookiecutter/cookiecutter/replay.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/third_party/python/cookiecutter/cookiecutter/replay.py b/third_party/python/cookiecutter/cookiecutter/replay.py
new file mode 100644
index 0000000000..9730e84da8
--- /dev/null
+++ b/third_party/python/cookiecutter/cookiecutter/replay.py
@@ -0,0 +1,52 @@
+"""
+cookiecutter.replay.
+
+-------------------
+"""
+import json
+import os
+
+from cookiecutter.utils import make_sure_path_exists
+
+
+def get_file_name(replay_dir, template_name):
+ """Get the name of file."""
+ suffix = '.json' if not template_name.endswith('.json') else ''
+ file_name = f'{template_name}{suffix}'
+ return os.path.join(replay_dir, file_name)
+
+
+def dump(replay_dir, template_name, context):
+ """Write json data to file."""
+ if not make_sure_path_exists(replay_dir):
+ raise OSError(f'Unable to create replay dir at {replay_dir}')
+
+ if not isinstance(template_name, str):
+ raise TypeError('Template name is required to be of type str')
+
+ if not isinstance(context, dict):
+ raise TypeError('Context is required to be of type dict')
+
+ if 'cookiecutter' not in context:
+ raise ValueError('Context is required to contain a cookiecutter key')
+
+ replay_file = get_file_name(replay_dir, template_name)
+
+ with open(replay_file, 'w') as outfile:
+ json.dump(context, outfile, indent=2)
+
+
+def load(replay_dir, template_name):
+ """Read json data from file."""
+ if not isinstance(template_name, str):
+ raise TypeError('Template name is required to be of type str')
+
+ replay_file = get_file_name(replay_dir, template_name)
+
+ with open(replay_file) as infile:
+ context = json.load(infile)
+
+ if 'cookiecutter' not in context:
+ raise ValueError('Context is required to contain a cookiecutter key')
+
+ return context