def update_preview_text(self): """ Creates the html text and updates the html of *self.document*. """ html_data = self._add_element('html') self._add_element('head', parent=html_data) self._add_element('title', self.title_line_edit.text(), html_data.head) css_path = AppLocation.get_data_path() / 'serviceprint' / 'service_print.css' custom_css = get_text_file_string(css_path) if not custom_css: custom_css = DEFAULT_CSS self._add_element('style', custom_css, html_data.head, attribute=('type', 'text/css')) self._add_element('body', parent=html_data) self._add_element('h1', html.escape(self.title_line_edit.text()), html_data.body, class_id='serviceTitle') for index, item in enumerate(self.service_manager.service_items): self._add_preview_item(html_data.body, item['service_item'], index) if not self.show_chords_check_box.isChecked(): # Remove chord row and spacing span elements when not printing chords for chord_row in html_data.find_class('chordrow'): chord_row.drop_tree() for spacing_span in html_data.find_class('chordspacing'): spacing_span.drop_tree() # Add the custom service notes: if self.footer_text_edit.toPlainText(): div = self._add_element('div', parent=html_data.body, class_id='customNotes') self._add_element( 'span', translate('OpenLP.ServiceManager', 'Service Notes: '), div, class_id='customNotesTitle') self._add_element('span', html.escape(self.footer_text_edit.toPlainText()), div, class_id='customNotesText') self.document.setHtml(lxml.html.tostring(html_data).decode()) self.preview_widget.updatePreview()
def __init__(self): """ Initialise the theme object. """ # basic theme object with defaults json_path = AppLocation.get_directory( AppLocation.AppDir) / 'core' / 'lib' / 'json' / 'theme.json' jsn = get_text_file_string(json_path) self.load_theme(jsn) self.background_filename = None
def __init__(self): """ Initialise the theme object. """ # basic theme object with defaults json_dir = os.path.join(AppLocation.get_directory(AppLocation.AppDir), 'core', 'lib', 'json') json_file = os.path.join(json_dir, 'theme.json') jsn = get_text_file_string(json_file) jsn = json.loads(jsn) self.expand_json(jsn)
def __init__(self): """ Initialise the theme object. """ # basic theme object with defaults json_dir = os.path.join(AppLocation.get_directory(AppLocation.AppDir), 'core', 'lib', 'json') json_file = os.path.join(json_dir, 'theme.json') jsn = get_text_file_string(json_file) jsn = json.loads(jsn) self.expand_json(jsn) self.background_filename = None
def upgrade_themes(self): """ Upgrade the xml files to json. :rtype: None """ xml_file_paths = AppLocation.get_section_data_path('themes').glob('*/*.xml') for xml_file_path in xml_file_paths: theme_data = get_text_file_string(xml_file_path) theme = self._create_theme_from_xml(theme_data, self.theme_path) self._write_theme(theme) xml_file_path.unlink()
def test_get_text_file_string_no_file(self): """ Test the get_text_file_string() function when a file does not exist """ # GIVEN: A patched is_file which returns False, and a file path with patch.object(Path, 'is_file', return_value=False): file_path = Path('testfile.txt') # WHEN: get_text_file_string is called result = get_text_file_string(file_path) # THEN: The result should be False file_path.is_file.assert_called_with() assert result is False, 'False should be returned if no file exists'
def get_theme_data(self, theme_name): """ Returns a theme object from an XML file :param theme_name: Name of the theme to load from file :return: The theme object. """ self.log_debug('get theme data for theme {name}'.format(name=theme_name)) xml_file = os.path.join(self.path, str(theme_name), str(theme_name) + '.xml') xml = get_text_file_string(xml_file) if not xml: self.log_debug('No theme data - using default theme') return ThemeXML() else: return self._create_theme_from_xml(xml, self.path)
def get_theme_data(self, theme_name): """ Returns a theme object from a JSON file :param str theme_name: Name of the theme to load from file :return: The theme object. :rtype: Theme """ theme_name = str(theme_name) theme_file_path = self.theme_path / theme_name / '{file_name}.json'.format(file_name=theme_name) theme_data = get_text_file_string(theme_file_path) if not theme_data: self.log_debug('No theme data - using default theme') return Theme() return self._create_theme_from_json(theme_data, self.theme_path)
def test_get_text_file_string_no_file(self): """ Test the get_text_file_string() function when a file does not exist """ with patch('openlp.core.lib.os.path.isfile') as mocked_isfile: # GIVEN: A mocked out isfile which returns true, and a text file name filename = 'testfile.txt' mocked_isfile.return_value = False # WHEN: get_text_file_string is called result = get_text_file_string(filename) # THEN: The result should be False mocked_isfile.assert_called_with(filename) self.assertFalse(result, 'False should be returned if no file exists')
def get_text_file_string_no_file_test(self): """ Test the get_text_file_string() function when a file does not exist """ with patch('openlp.core.lib.os.path.isfile') as mocked_isfile: # GIVEN: A mocked out isfile which returns true, and a text file name filename = 'testfile.txt' mocked_isfile.return_value = False # WHEN: get_text_file_string is called result = get_text_file_string(filename) # THEN: The result should be False mocked_isfile.assert_called_with(filename) self.assertFalse(result, 'False should be returned if no file exists')
def upgrade_themes(self): """ Upgrade the xml files to json. :rtype: None """ # Wait for 2 seconds to allow some other things to start processing first wait_for(lambda: False, timeout=1) xml_file_paths = AppLocation.get_section_data_path('themes').glob( '*/*.xml') for xml_file_path in xml_file_paths: theme_data = get_text_file_string(xml_file_path) theme = self._create_theme_from_xml(theme_data, self.theme_path) self.save_theme(theme) xml_file_path.unlink()
def get_theme_data(self, theme_name): """ Returns a theme object from an XML file :param theme_name: Name of the theme to load from file :return: The theme object. """ self.log_debug('get theme data for theme %s' % theme_name) xml_file = os.path.join(self.path, str(theme_name), str(theme_name) + '.xml') xml = get_text_file_string(xml_file) if not xml: self.log_debug('No theme data - using default theme') return ThemeXML() else: return self._create_theme_from_xml(xml, self.path)
def get_text_file_string_read_error_test(self): """ Test the get_text_file_string() method when a read error happens """ with patch('openlp.core.lib.os.path.isfile') as mocked_isfile, patch('openlp.core.lib.open', create=True) as mocked_open: # GIVEN: A mocked-out open() which raises an exception and isfile returns True filename = 'testfile.txt' mocked_isfile.return_value = True mocked_open.side_effect = IOError() # WHEN: get_text_file_string is called result = get_text_file_string(filename) # THEN: None should be returned mocked_isfile.assert_called_with(filename) mocked_open.assert_called_with(filename, 'r') assert result is None, 'None should be returned if the file cannot be opened'
def test_get_text_file_string_read_error(self): """ Test the get_text_file_string() method when a read error happens """ # GIVEN: A patched open which raises an exception and is_file which returns True with patch.object(Path, 'is_file'), \ patch.object(Path, 'open'): file_path = Path('testfile.txt') file_path.is_file.return_value = True file_path.open.side_effect = OSError() # WHEN: get_text_file_string is called result = get_text_file_string(file_path) # THEN: None should be returned file_path.is_file.assert_called_once_with() file_path.open.assert_called_once_with('r', encoding='utf-8') assert result is None, 'None should be returned if the file cannot be opened'
def get_text_file_string_read_error_test(self): """ Test the get_text_file_string() method when a read error happens """ with patch('openlp.core.lib.os.path.isfile') as mocked_isfile, \ patch('openlp.core.lib.open', create=True) as mocked_open: # GIVEN: A mocked-out open() which raises an exception and isfile returns True filename = 'testfile.txt' mocked_isfile.return_value = True mocked_open.side_effect = IOError() # WHEN: get_text_file_string is called result = get_text_file_string(filename) # THEN: None should be returned mocked_isfile.assert_called_with(filename) mocked_open.assert_called_with(filename, 'r', encoding='utf-8') self.assertIsNone(result, 'None should be returned if the file cannot be opened')
def update_preview_text(self): """ Creates the html text and updates the html of *self.document*. """ html_data = self._add_element('html') self._add_element('head', parent=html_data) self._add_element('title', self.title_line_edit.text(), html_data.head) css_path = os.path.join(AppLocation.get_data_path(), 'service_print.css') custom_css = get_text_file_string(css_path) if not custom_css: custom_css = DEFAULT_CSS self._add_element('style', custom_css, html_data.head, attribute=('type', 'text/css')) self._add_element('body', parent=html_data) self._add_element('h1', html.escape(self.title_line_edit.text()), html_data.body, classId='serviceTitle') for index, item in enumerate(self.service_manager.service_items): self._add_preview_item(html_data.body, item['service_item'], index) # Add the custom service notes: if self.footer_text_edit.toPlainText(): div = self._add_element('div', parent=html_data.body, classId='customNotes') self._add_element('span', translate('OpenLP.ServiceManager', 'Custom Service Notes: '), div, classId='customNotesTitle') self._add_element('span', html.escape(self.footer_text_edit.toPlainText()), div, classId='customNotesText') self.document.setHtml(lxml.html.tostring(html_data).decode()) self.preview_widget.updatePreview()
def update_preview_text(self): """ Creates the html text and updates the html of *self.document*. """ html_data = self._add_element('html') self._add_element('head', parent=html_data) self._add_element('title', self.title_line_edit.text(), html_data.head) css_path = os.path.join(AppLocation.get_data_path(), 'service_print.css') custom_css = get_text_file_string(css_path) if not custom_css: custom_css = DEFAULT_CSS self._add_element('style', custom_css, html_data.head, attribute=('type', 'text/css')) self._add_element('body', parent=html_data) self._add_element('h1', html.escape(self.title_line_edit.text()), html_data.body, classId='serviceTitle') for index, item in enumerate(self.service_manager.service_items): self._add_preview_item(html_data.body, item['service_item'], index) # Add the custom service notes: if self.footer_text_edit.toPlainText(): div = self._add_element('div', parent=html_data.body, classId='customNotes') self._add_element( 'span', translate('OpenLP.ServiceManager', 'Custom Service Notes: '), div, classId='customNotesTitle') self._add_element('span', html.escape(self.footer_text_edit.toPlainText()), div, classId='customNotesText') self.document.setHtml(lxml.html.tostring(html_data).decode()) self.preview_widget.updatePreview()