def test_to_json(self): self.assertEqual( '[5, "hi"]', json_util.to_json( tuple_util.dict_to_named_tuple('MyClass4', { 'a': 5, 'b': 'hi' })))
def _test_grep(self, content, text, node_type, word_boundary=False): tmp_dir = self._make_temp_content(content) options = refactor_options(word_boundary=word_boundary) real_result = refactor_ast.grep([tmp_dir], text, node_type, options=options) result = [] for item in real_result: filename = file_util.remove_head(item.filename, tmp_dir + os.sep) t = (filename, item.snippet, item.snippet_lines) result.append(t) json = json_util.to_json(result, indent=2) return self._test_grep_result(tmp_dir, real_result, json)
def test_read_save(self): self.assertEqual( '[5, "hi"]', json_util.to_json( tuple_util.dict_to_named_tuple('MyClass4', { 'a': 5, 'b': 'hi' }))) expected_object = {'a': 5, 'b': 'hi'} tmp_dir = tempfile.mkdtemp(prefix='tead_save', suffix='.json') tmp = path.join(tmp_dir, 'foo.json') json_util.save_file(tmp, expected_object, indent=2) actual_object = json_util.read_file(tmp) self.assertEqual(expected_object, actual_object) filesystem.remove_directory(tmp_dir)
def vm_copy(self, vm_id, new_vm_id): check.check_string(vm_id) check.check_string(new_vm_id) url = self._make_url('vms') data = { 'name': new_vm_id, 'parentId': vm_id, } json = json_util.to_json(data, indent = 2, sort_keys = True) response = self._make_request('post', url, data = json) response_data = response.json() if response.status_code == 409: if response_data['Code'] == 107: raise vmware_error('{} - {}'.format(response_data['Code'], response_data['Message'])) else: raise vmware_error('vm already exists: "{}"'.format(new_vm_id)) elif response.status_code != 201: raise vmware_error('Error querying: "{}": {}'.format(url, response.status_code)) response_data = response.json() self._log.log_d('vm_copy: response_data={}'.format(pprint.pformat(response_data))) return response_data
def to_json(self): l = self.to_list() return json_util.to_json(l, indent=2, sort_keys=True)
def to_json(self): return json_util.to_json(self.to_dict(), indent=2, sort_keys=True)
def _sql_encode_string_list(clazz, l): check.check_string_seq(l) s = json_util.to_json(l) return s if s is not None else 'null'
def _output_table_to_stream(clazz, data, stream, options): if options.style == data_output_style.BRIEF: for item in data: stream.write(str(item[options.brief_column])) stream.write(line_break.DEFAULT_LINE_BREAK) elif options.style == data_output_style.JSON: data = clazz._normalize_table_for_structured_data(data) stream.write(json_util.to_json(data, indent=2, sort_keys=True)) elif options.style == data_output_style.CSV: for item in data: item = clazz._normalize_table_item(item) stream.write(options.csv_delimiter.join(item)) stream.write(line_break.DEFAULT_LINE_BREAK) elif options.style in [ data_output_style.TABLE, data_output_style.PLAIN_TABLE ]: data = clazz._normalize_data(data) table_data = table(data=data, column_names=options.column_names) if table_data.empty: return is_plain = options.style == data_output_style.PLAIN_TABLE if is_plain: table_style = text_table_style(spacing=1, box=text_box_space()) else: #table_style = text_table_style(spacing = 1, box = text_box_unicode()) table_style = text_table_style(spacing=1, box=text_box_ascii()) column_names = None if options.column_names: column_names = list(options.column_names) for column in sorted(options.remove_columns or [], reverse=True): table_data.remove_column(column) if column_names: column_names.remove(column) if column_names: column_names = tuple(column_names) tt = text_table(data=table_data, style=table_style) if column_names: if not is_plain: tt.set_labels(column_names) tt.set_column_names(column_names) if options.table_cell_renderers: for column_name, renderer in options.table_cell_renderers.items( ): tt.set_col_renderer(column_name, renderer) if options.table_title and not is_plain: tt.set_title(options.table_title) text = str(tt) if options.table_flexible_column: flexible_index = table_data.resolve_x( options.table_flexible_column) lines = text.split(line_break.DEFAULT_LINE_BREAK) render_width = len(lines[0]) try: terminal_width = console.terminal_width() except Exception as ex: terminal_width = None if terminal_width != None: max_column_width = tt._max_column_width(flexible_index) if terminal_width < render_width: overflow = render_width - terminal_width + 1 new_column_width = max(max_column_width - overflow, 0) else: new_column_width = max_column_width tt.set_col_renderer( options.table_flexible_column, text_cell_renderer(width=new_column_width)) text = str(tt) stream.write(text) stream.write(line_break.DEFAULT_LINE_BREAK) else: raise RuntimeError('Unhandled data output style: {}'.format(style))
def to_json(self, short_hash=False): d = self.to_dict(short_hash=short_hash) return json_util.to_json(d, indent=2, sort_keys=True)