Chromium Code Reviews| Index: recipe_modules/json/api.py |
| diff --git a/recipe_modules/json/api.py b/recipe_modules/json/api.py |
| index a6e5e5473fe4c316b0ce602e01ae2f382a61ec2d..4b319d527d2d0749b77e90184ca83d9ff15c38e1 100644 |
| --- a/recipe_modules/json/api.py |
| +++ b/recipe_modules/json/api.py |
| @@ -14,21 +14,24 @@ from recipe_engine import config_types |
| class JsonOutputPlaceholder(recipe_util.OutputPlaceholder): |
| """JsonOutputPlaceholder is meant to be a placeholder object which, when added |
| - to a step's cmd list, will be replaced by annotated_run with the path to a |
| + to a step's cmd list, will be replaced by the recipe engine with the path to a |
| temporary file (e.g. /tmp/tmp4lp1qM) which will exist only for the duration of |
| - the step. If the script requires a flag (e.g. --output-json /path/to/file), |
| - you must supply that flag yourself in the cmd list. |
| + the step. Create a JsonOutputPlaceholder by calling the 'output()' method of |
| + the JsonApi. |
| - This placeholder can be optionally added when you use the Steps.step() |
| - method in this module. |
| + The step is expected to write JSON data to this file, and when the step is |
| + finished, the file will be read and the JSON parsed back into the recipe, and |
| + will be available as part of the step result. |
| - FIXME |
| - After the termination of the step, this file is expected to contain a valid |
| - JSON document, which will be set as the json.output for that step in the |
| - step_history OrderedDict passed to your recipe generator. |
| + Example: |
| + result = api.step('step name', |
| + ['write_json_to_file.sh', api.json.output()]) |
| + # `result.json.output` is the parsed JSON value. |
| + |
| + See the example recipe (./example.py) for some more uses. |
| """ |
| - def __init__(self, api, add_json_log, name=None): |
| - self.raw = api.m.raw_io.output_text('.json') |
| + def __init__(self, api, add_json_log, name=None, leak_to=None): |
| + self.raw = api.m.raw_io.output_text('.json', leak_to=leak_to) |
| self.add_json_log = add_json_log |
| super(JsonOutputPlaceholder, self).__init__(name=name) |
| @@ -54,10 +57,12 @@ class JsonOutputPlaceholder(recipe_util.OutputPlaceholder): |
| pass |
| if self.add_json_log: |
| - key = self.label + ('' if valid else ' (invalid)') |
| - with contextlib.closing(recipe_util.StringListIO()) as listio: |
| - json.dump(ret, listio, indent=2, sort_keys=True) |
| - presentation.logs[key] = listio.lines |
| + if valid: |
| + with contextlib.closing(recipe_util.StringListIO()) as listio: |
| + json.dump(ret, listio, indent=2, sort_keys=True) |
| + presentation.logs[self.label] = listio.lines |
| + else: |
| + presentation.logs[self.label + ' (invalid)'] = raw_data.splitlines() |
|
Paweł Hajdan Jr.
2017/03/14 12:56:14
Consider also capturing what the exception was and
iannucci
2017/03/14 17:34:53
Done.
|
| return ret |
| @@ -104,9 +109,14 @@ class JsonApi(recipe_api.RecipeApi): |
| return self.m.raw_io.input_text(self.dumps(data), '.json') |
| @recipe_util.returns_placeholder |
| - def output(self, add_json_log=True, name=None): |
| - """A placeholder which will expand to '/tmp/file'.""" |
| - return JsonOutputPlaceholder(self, add_json_log, name=name) |
| + def output(self, add_json_log=True, name=None, leak_to=None): |
| + """A placeholder which will expand to '/tmp/file'. |
| + |
| + If leak_to is provided, it must be a Path object. This path will be used in |
| + place of a random temporary file, and the file will not be deleted at the |
| + end of the step. |
| + """ |
| + return JsonOutputPlaceholder(self, add_json_log, name=name, leak_to=leak_to) |
| # TODO(you): This method should be in the `file` recipe_module |
| def read(self, name, path, add_json_log=True, output_name=None, **kwargs): |