def read(self, path): """ Reads video setting from file. @param path: path to file with video setting. """ ctx, section = resource_helper.getRoot(path, safe=True) if section is None: LOG_WARNING('File with video settings not found. Uses default values', path) return subCtx, subSection = resource_helper.getSubSection(ctx, section, 'audio', safe=True) if subSection: self.__setting['audio'] = self.__readTracks(subCtx, subSection) subCtx, subSection = resource_helper.getSubSection(ctx, section, 'subtitles', safe=True) if subSection: self.__setting['subtitles'] = self.__readTracks(subCtx, subSection, offset=1) return self
def __readChapters(ctx, root, filterFunction): ctx, section = resource_helper.getSubSection(ctx, root, 'chapters') chapters = [] index = 0 for chapterCtx, chapterSection in resource_helper.getIterator(ctx, section): filePath = __getCustomSectionValue(chapterCtx, chapterSection, 'file-path') title = __getCustomSectionValue(chapterCtx, chapterSection, 'title') background = __getCustomSectionValue(chapterCtx, chapterSection, 'background') attributes = __getChapterAttributes(filePath, filterFunction) ids = attributes.get('ids', []) if len(ids) != len(set(ids)): _logger.warning('chapter %s has duplicate page ids', title) chapter = {'filePath': filePath, 'pageIDs': ids, 'newPageIDs': attributes.get('newIds', []), 'uiData': {'index': int(index), 'label': translation(title), 'image': background, 'tooltip': makeTooltip(translation(title), '\n'.join(attributes.get('chaptersTitles', [])))}} if any((ids in chapter['pageIDs'] for chapter in chapters)): _logger.warning('chapter %s has duplicate page ids from another chapters', title) _logger.debug('ManualXMLDataReader: Read chapters. Chapter: %s', chapter) chapters.append(chapter) index += 1 return chapters
def __getCustomSectionValue(ctx, section, name, safe=False): valueCtx, valueSection = resource_helper.getSubSection(ctx, section, name, safe) result = None if valueSection is not None: item = resource_helper.readItem(valueCtx, valueSection, name) result = item.value return result
def _readSettings(ctx, root): ctx, section = resource_helper.getSubSection(ctx, root, 'settings') settings = _getDefaultSettings() for ctx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(ctx, subSection, 'setting') settings[item.name] = item.value return _MessagesSettings(**settings)
def _readSettings(ctx, root): ctx, section = resource_helper.getSubSection(ctx, root, "settings") settings = _getDefaultSettings() for ctx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(ctx, subSection, "setting") settings[item.name] = item.value return settings
def _readSettings(ctx, root): ctx, section = resource_helper.getSubSection(ctx, root, 'settings') settings = _getDefaultSettings() for xmlCtx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(xmlCtx, subSection, 'setting') settings[item.name] = item.value return settings
def _readConfigItem(key, ctx, section): ctx, sub = resource_helper.getSubSection(ctx, section, key, safe=True) config = {} if sub is not None: for itemCtx, item in resource_helper.getIterator(ctx, sub): source = resource_helper.readDict(itemCtx, item) config[source.name] = source.value return config
def _readMessages(ctx, root): ctx, section = resource_helper.getSubSection(ctx, root, 'messages') messages = {} for ctx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(ctx, subSection, 'message') text, aliases = item.value aliases = aliases.split(',', 1) if len(aliases) == 1: aliases *= 2 messages[item.name] = (html.translation(text), tuple(aliases)) return messages
def _readMacros(xmlCtx, section, valueName='value'): result = {} name = resource_helper.readItemName(xmlCtx, section) macros = _readItemMacros(xmlCtx, section) subCtx, subSection = resource_helper.getSubSection(xmlCtx, section, valueName) for nextCtx, nextSection in resource_helper.getIterator(subCtx, subSection): item = resource_helper.readItem(nextCtx, nextSection) if not item.name: raise resource_helper.ResourceError(nextCtx, '{0}: name is required in each item'.format(name)) result[item.name] = item.value return resource_helper.ResourceItem('macros', name, _MacrosValue(macros, result))
def _readMacros(xmlCtx, section, valueName = 'value'): result = {} name = resource_helper.readItemName(xmlCtx, section) macros = _readItemMacros(xmlCtx, section) subCtx, subSection = resource_helper.getSubSection(xmlCtx, section, valueName) for nextCtx, nextSection in resource_helper.getIterator(subCtx, subSection): item = resource_helper.readItem(nextCtx, nextSection) if not item.name: raise resource_helper.ResourceError(nextCtx, '{0}: name is required in each item'.format(name)) result[item.name] = item.value return resource_helper.ResourceItem('macros', name, _MacrosValue(macros, result))
def _readMessages(ctx, root): ctx, section = resource_helper.getSubSection(ctx, root, 'messages') messages = {} for xmlCtx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(xmlCtx, subSection, 'message') text, aliases = item.value aliases = aliases.split(',', 1) if len(aliases) == 1: aliases *= 2 messages[item.name] = (html.translation(text), tuple(aliases)) return messages
def __readChapter(ctx, root, isBootcampEnabled, bootcampRunCount): pages = [] details = [] index = 0 ctx, section = resource_helper.getSubSection(ctx, root, 'lessons') for lessonCtx, lessonSection in resource_helper.getIterator(ctx, section): template = __getCustomSectionValue(lessonCtx, lessonSection, 'template') title = translation( __getCustomSectionValue(lessonCtx, lessonSection, 'title')) background = __getCustomSectionValue(lessonCtx, lessonSection, 'background') description = __getCustomSectionValue(lessonCtx, lessonSection, 'description', safe=True) if description is None: description = '' else: description = translation(description) contentRendererLinkage = '' if template is _BOOTCAMP_PAGE: if not isBootcampEnabled: continue contentRendererData = __getBootcampRendererData(bootcampRunCount) contentRendererLinkage = _MANUAL_LESSON_TEMPLATES.get(template) else: contentRendererData, hintsCount = __getHintsRendererData( lessonCtx, lessonSection) if hintsCount > 0: contentRendererLinkage = _MANUAL_LESSON_TEMPLATES.get(template) pages.append({ 'buttonsGroup': 'ManualChapterGroup', 'pageIndex': int(index), 'selected': False, 'label': str(int(index) + 1), 'tooltip': { 'tooltip': makeTooltip(title) } }) details.append({ 'title': title, 'description': description, 'background': background, 'contentRendererLinkage': contentRendererLinkage, 'contentRendererData': contentRendererData }) index += 1 chapterData = {'pages': pages, 'details': details} _logger.debug('ManualXMLDataReader: Read chapter: %s', chapterData) return chapterData
def _readStyles(ctx, root): ctx, section = resource_helper.getSubSection(ctx, root, 'styles', safe=True) styles = {} if section is not None: for xmlCtx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(xmlCtx, subSection, 'style') expectedKeys = _EXPECTED_STYLES[item.name] for key in expectedKeys: pass styles[item.name] = item.value return styles
def __getHintsRendererData(lessonCtx, lessonSection): hints = [] contentRendererData = None hintsCtx, hintsSection = resource_helper.getSubSection(lessonCtx, lessonSection, 'hints', safe=True) if hintsSection is not None: for hintCtx, hintSection in resource_helper.getIterator(hintsCtx, hintsSection): hintText = translation(__getCustomSectionValue(hintCtx, hintSection, 'text')) hintIcon = __getCustomSectionValue(hintCtx, hintSection, 'icon') hints.append({'text': hintText, 'icon': hintIcon}) contentRendererData = {'hints': hints} return (contentRendererData, len(hints))
def __getChaptersTitlesList(chapterFileName, isBootcampEnabled): chaptersTitles = [] chapterPath = _CHAPTERS_DATA_PATH + chapterFileName with resource_helper.root_generator(chapterPath) as ctx, root: ctx, section = resource_helper.getSubSection(ctx, root, 'lessons') for lessonCtx, lessonSection in resource_helper.getIterator(ctx, section): title = translation(__getCustomSectionValue(lessonCtx, lessonSection, 'title')) template = __getCustomSectionValue(lessonCtx, lessonSection, 'template') if template is _BOOTCAMP_PAGE and not isBootcampEnabled: continue chaptersTitles.append(title) return chaptersTitles
def __readChapter(ctx, root, filterFunction, bootcampRunCount, chapterTitle=''): pages = [] details = [] index = 0 ctx, section = resource_helper.getSubSection(ctx, root, 'lessons') for lessonCtx, lessonSection in resource_helper.getIterator(ctx, section): template = __getCustomSectionValue(lessonCtx, lessonSection, 'template') if not filterFunction(template): continue title = translation(__getCustomSectionValue(lessonCtx, lessonSection, 'title')) background = __getCustomSectionValue(lessonCtx, lessonSection, 'background') description = __getCustomSectionValue(lessonCtx, lessonSection, 'description', safe=True) pageId = __getCustomSectionValue(lessonCtx, lessonSection, 'id') if description is None: description = '' else: description = translation(description) contentRendererLinkage = '' if template == ManualPageTypes.MAPS_TRAINING_PAGE: contentRendererData = {'text': backport.text(R.strings.maps_training.manualPage.button())} contentRendererLinkage = _MANUAL_LESSON_TEMPLATES.get(template) elif template == ManualPageTypes.BOOTCAMP_PAGE: contentRendererData = __getBootcampRendererData(bootcampRunCount) contentRendererLinkage = _MANUAL_LESSON_TEMPLATES.get(template) elif template == ManualPageTypes.VIDEO_PAGE: contentRendererData = __getVideoRendererData(lessonCtx, lessonSection) contentRendererLinkage = _MANUAL_LESSON_TEMPLATES.get(template) else: contentRendererData, hintsCount = __getHintsRendererData(lessonCtx, lessonSection) if hintsCount > 0: contentRendererLinkage = _MANUAL_LESSON_TEMPLATES.get(template) pages.append({'buttonsGroup': 'ManualChapterGroup', 'pageIndex': int(index), 'selected': False, 'hasNewContent': __isNew(lessonCtx, lessonSection), 'label': str(int(index) + 1), 'tooltip': {'tooltip': makeTooltip(title)}}) details.append({'title': title, 'chapterTitle': chapterTitle, 'description': description, 'background': background, 'contentRendererLinkage': contentRendererLinkage, 'contentRendererData': contentRendererData, 'id': pageId, 'pageType': template}) index += 1 chapterData = {'pages': pages, 'details': details} _logger.debug('ManualXMLDataReader: Read chapter: %s', chapterData) return chapterData
def _readConfig(ctx, root, parentTag, childTag, itemClass): ctx, section = resource_helper.getSubSection(ctx, root, parentTag, safe=True) if not section: return {} config = {} for xmlCtx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(xmlCtx, subSection, childTag) if not item.type == _ITEM_TYPE.DICT: raise AssertionError('Type of value should be dict') name = item.name raise name in config and resource_helper.ResourceError(xmlCtx, 'Item {0} is duplicated.'.format(name)) config[name] = itemClass(**item.value) return config
def _readConfig(ctx, root, parentTag, childTag, itemClass): ctx, section = resource_helper.getSubSection(ctx, root, parentTag, safe=True) if not section: return {} config = {} for ctx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(ctx, subSection, childTag) assert item.type == _ITEM_TYPE.DICT, "Type of value should be dict" name = item.name if name in config: raise resource_helper.ResourceError(ctx, "Item {0} is duplicated.".format(name)) config[name] = itemClass(**item.value) return config
def _readStyles(ctx, root): ctx, section = resource_helper.getSubSection(ctx, root, 'styles', safe=True) styles = {} if section is not None: for xmlCtx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(xmlCtx, subSection, 'style') raise item.name in _EXPECTED_STYLES or AssertionError('Style section %s is not expected!' % item.name) expectedKeys = _EXPECTED_STYLES[item.name] for key in expectedKeys: raise key in item.value or AssertionError('Style option %s is expected in section %s!' % (key, item.name)) styles[item.name] = item.value return styles
def _readStyles(ctx, root): ctx, section = resource_helper.getSubSection(ctx, root, "styles", safe=True) styles = {} if section is not None: for ctx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(ctx, subSection, "style") raise item.name in _EXPECTED_STYLES or AssertionError("Style section %s is not expected!" % item.name) expectedKeys = _EXPECTED_STYLES[item.name] for key in expectedKeys: raise key in item.value or AssertionError( "Style option %s is expected in section %s!" % (key, item.name) ) styles[item.name] = item.value return styles
def readXMLConfig(filename): config = {} ctx, root = resource_helper.getRoot(filename, safe=False) if root is None: return config else: sectionCtx, section = resource_helper.getSubSection(ctx, root, 'common', safe=False) if section is not None: config.update(resource_helper.readDict(sectionCtx, section).value) for key in ('filters', 'formatters', 'handlers', 'loggers'): config[key] = _readConfigItem(key, ctx, root) resource_helper.purgeResource(filename) return config
def _readConfig(ctx, root, parentTag, childTag, itemClass): ctx, section = resource_helper.getSubSection(ctx, root, parentTag, safe=True) if not section: return {} config = {} for xmlCtx, subSection in resource_helper.getIterator(ctx, section): item = resource_helper.readItem(xmlCtx, subSection, childTag) name = item.name if name in config: raise resource_helper.ResourceError( xmlCtx, 'Item {0} is duplicated.'.format(name)) config[name] = itemClass(**item.value) return config
def __readChapters(ctx, root, isBootcampEnabled): ctx, section = resource_helper.getSubSection(ctx, root, 'chapters') chapters = [] index = 0 for chapterCtx, chapterSection in resource_helper.getIterator(ctx, section): filePath = __getCustomSectionValue(chapterCtx, chapterSection, 'file-path') title = __getCustomSectionValue(chapterCtx, chapterSection, 'title') background = __getCustomSectionValue(chapterCtx, chapterSection, 'background') lessonsTitlesList = __getChaptersTitlesList(filePath, isBootcampEnabled) chapter = {'filePath': filePath, 'uiData': {'index': int(index), 'label': translation(title), 'image': background, 'tooltip': makeTooltip(translation(title), '\n'.join(lessonsTitlesList))}} _logger.debug('ManualXMLDataReader: Read chapters. Chapter: %s', chapter) chapters.append(chapter) index += 1 return chapters
def __getChapterAttributes(chapterFileName, filterFunction): chaptersTitles = [] ids = [] newIds = [] chapterPath = _CHAPTERS_DATA_PATH + chapterFileName with resource_helper.root_generator(chapterPath) as ctx, root: ctx, section = resource_helper.getSubSection(ctx, root, 'lessons') for lessonCtx, lessonSection in resource_helper.getIterator(ctx, section): template = __getCustomSectionValue(lessonCtx, lessonSection, 'template') if not filterFunction(template): continue lessonId = int(__getCustomSectionValue(lessonCtx, lessonSection, 'id')) ids.append(lessonId) if __getCustomSectionValue(lessonCtx, lessonSection, 'new', safe=True): newIds.append(lessonId) chaptersTitles.append(translation(__getCustomSectionValue(lessonCtx, lessonSection, 'title'))) return {'ids': ids, 'newIds': newIds, 'chaptersTitles': chaptersTitles}
def readXMLConfig(filename): """ Reads XML file to load the logging configuration as dictionary. Note: Here is no any validation, validation will be applied if trying configure logging package. :param filename: string containing relative path to configuration file that is started from "res" directory. :return: dictionary containing logging configuration. """ config = {} ctx, root = resource_helper.getRoot(filename, safe=False) if root is None: return config else: sectionCtx, section = resource_helper.getSubSection(ctx, root, 'common', safe=False) if section is not None: config.update(resource_helper.readDict(sectionCtx, section).value) for key in ('filters', 'formatters', 'handlers', 'loggers'): config[key] = _readConfigItem(key, ctx, root) resource_helper.purgeResource(filename) return config