def _attempt_build(self): try: users_layouts = build_from_file( self._input_path, auto_format_and_overwrite=False) except LayoutError as e: if 'Cannot read this file' in str(e): self._log.setText(MultilineString.shift_left(""" The builder says it cannot access your input file, but that is probably because your editor had it locked at the moment it tried. It will carry on as normal the next time you save a change. """)) else: self._log.setText(str(e)) return top_item = users_layouts.first_top_level_item() # If the top level item in the tree is a widget, we just show it. if isinstance(top_item, QWidget): self._show_built_content(top_item) # Whereas, if it is a layout we wrap it in a widget so we can show it. elif isinstance(top_item, QLayout): wrapper = QWidget(top_item) self._show_built_content(wrapper) self._log.setText('Build successful')
def test_shift_left(self): # Normal usage str_input = """ foo bar baz """ result = MultilineString.shift_left(str_input) lines = result.split('\n') self.assertEqual(lines[0], 'foo') self.assertEqual(lines[1], ' bar') self.assertEqual(lines[2], ' baz')
def test_reformatted_file_gets_written_to_file_specified(self): tmp_dir = tempfile.mkdtemp() reformat_location = path.join(tmp_dir, 're-formatted.txt') str_input = """ top_widget QWidget layout QVBoxLayout """ build_from_multi_line_string( str_input, auto_format_and_write_to=reformat_location) with open(reformat_location, 'r') as input_file: contents = MultilineString.shift_left(input_file.read()) print contents self.assertEqual( contents, MultilineString.shift_left(""" top_widget QWidget layout QVBoxLayout """)) shutil.rmtree(tmp_dir)
def test_at_api_level(self): # Make a file that we will then overwrite. orig_fd = tempfile.NamedTemporaryFile(suffix='.txt', delete=False) orig_file_path = orig_fd.name content = MultilineString.shift_left(""" layout QHBoxLayout widget QWidget """) orig_fd.write(content) orig_fd.close() # Mandate the overwrite OriginalFileReWriter.overwrite_original(orig_file_path, 'new content') # Check for both the presence of the new content, and the # backup message. with open(orig_file_path, 'r') as input_file: content = input_file.read() self.assertTrue('new content' in content) self.assertTrue('has been' in content)