def parse_project(self, project_config): self.project = dgen_project.dgenProject() if not isinstance(project_config, dict): dgen_utils.log_err('parse_project: project_config is not dict') known = ['document', 'template', 'template_conf', 'templates_root', 'revealjs_dir', 'filename', 'metadata'] self.no_unknown_in_conf(known, project_config) mandatory = ['document', 'template', 'templates_root', 'filename'] self.all_mandatory_in_conf(mandatory, project_config) if 'templates_root' in project_config: self.project.templates_root = self.parse_templates_root(project_config['templates_root']) if 'template' in project_config: self.project.template = self.parse_template(project_config['template']) if self.project.template_refresh_required(): self.project.refresh_template() if 'template_conf' in project_config: self.project.template_conf = self.parse_template_conf(project_config['template_conf']) if 'document' in project_config: self.project.document = self.parse_document(project_config['document']) if 'metadata' in project_config: self.project.metadata = self.parse_metadata(project_config['metadata']) if 'revealjs_dir' in project_config: self.project.revealjs_dir = self.parse_revealjs(project_config['revealjs_dir']) if 'filename' in project_config: self.project.filename = self.parse_filename(project_config['filename']) return self.project
def add_contents_file(self, value): if isinstance(value, str): self.__contents = self.__contents + [value] elif isinstance(value, list): self.__contents = self.__contents + value else: dgen_utils.log_err('value is not str or list: ' + value)
def sort_files(self, unsorted): sorted_files = unsorted try: sorted_files = self.imported_module.sort_files(unsorted) except NotImplementedError, e: dgen_utils.log_err('error running %s.sort_files: %s' % (self.imported_module, e))
def contents(self, value): if isinstance(value, str): self.__contents = [value] elif isinstance(value, list): self.__contents = value else: dgen_utils.log_err('value is not str or list: ' + value)
def generate_page(self, document, to_format): files = self.symbol_processor.replace_symbols_in_collection( self.project.template_conf.metadata + document.template_conf.metadata + document.contents) # Build the document contents contents = u'\n' symbols = dict(self.symbol_processor.symbols) for path in files: for expanded_path in dgen_utils.expand_path_with_glob( path, self.project.file_sorter): if os.path.isfile(expanded_path): symbols['file'] = os.path.basename(expanded_path).split( '.')[0] with io.open(expanded_path, 'r', encoding='utf-8') as f: filecontents = self.symbol_processor.replace_symbols_in_string( f.read(), symbols) contents = contents + filecontents + u'\n\n' else: dgen_utils.log_err('file does not exist:', expanded_path) self.print_markdown_contents(contents) output_file = os.path.join(self.project.html_dir, document.html_filename) pandoc_options = self.project.template_conf.pandoc_options pandoc_options = document.template_conf.pandoc_options + pandoc_options pandoc_options = self.symbol_processor.replace_symbols_in_collection( pandoc_options) pandoc_options = [ '--output=' + dgen_utils.expand_path(output_file), '--from=markdown', '--to=' + to_format ] + pandoc_options output = dgen_utils.run_cmd_with_io('pandoc', pandoc_options, stdindata=contents) assert output == ''
def template_dir(self): result = os.path.join(self.templates_root, self.template) if dgen_utils.is_git_url(result) is True: return result result = dgen_utils.expand_path(result) if os.path.isdir(result) is False: dgen_utils.log_err('template_dir %s does not exist!' % (result)) return result
def parse_list(self, list_conf): if isinstance(list_conf, str): return [self.parse_string(list_conf)] elif isinstance(list_conf, list): result = [] for string in list_conf: result = result + [self.parse_string(string)] return result else: dgen_utils.log_err("expected string or list, got:", list_conf)
def import_source_code(self): try: source_code_folder = os.path.dirname(self.source_code_path) if source_code_folder not in sys.path: sys.path.append(source_code_folder) module_name = os.path.basename(self.source_code_path).split('.')[0] self.__imported_module = importlib.import_module(module_name) except ImportError, e: dgen_utils.log_err('cannot import %s: %s' % (self.source_code_path, e))
def parse_section(self, section_conf, section_type): ''' parse section config and return the result ''' if not isinstance(section_conf, dict): dgen_utils.log_err("section_conf is not a dict", section_conf) known = ['name', 'contents', 'template_conf'] self.no_unknown_in_conf(known, section_conf) section = dgen_section.dgenSection() section.section_type = section_type if 'name' in section_conf: section.name = self.parse_name(section_conf['name']) if 'contents' in section_conf: section.contents = self.parse_contents(section_conf['contents']) if 'template_conf' in section_conf: section.template_conf = self.parse_template_conf(section_conf['template_conf']) return section
def parse_project(self, project_config): self.project = dgen_project.dgenProject() if not isinstance(project_config, dict): dgen_utils.log_err('parse_project: project_config is not dict') known = [ 'document', 'template', 'template_conf', 'templates_root', 'pathspec', 'revealjs_dir', 'filename', 'classification', 'metadata', 'file_sorter' ] self.no_unknown_in_conf(known, project_config) mandatory = ['document', 'template', 'filename'] self.all_mandatory_in_conf(mandatory, project_config) if 'templates_root' in project_config: self.project.templates_root = self.parse_templates_root( os.getenv('templates_root', project_config['templates_root'])) if 'pathspec' in project_config: self.project.pathspec = self.parse_pathspec( project_config['pathspec']) if 'template' in project_config: self.project.template = self.parse_template( os.getenv('template', project_config['template'])) if self.project.template_refresh_required(): self.project.refresh_template() if 'template_conf' in project_config: self.project.template_conf = self.parse_template_conf( os.getenv('template_conf', project_config['template_conf'])) if 'document' in project_config: self.project.document = self.parse_document( project_config['document']) if 'metadata' in project_config: self.project.metadata = self.parse_metadata( os.getenv('metadata', project_config['metadata'])) if 'revealjs_dir' in project_config: self.project.revealjs_dir = self.parse_revealjs( os.getenv('metadata', project_config['revealjs_dir'])) if 'filename' in project_config: self.project.filename = self.parse_filename( os.getenv('filename', project_config['filename'])) if 'classification' in project_config: self.project.classification = self.parse_classification( os.getenv('classification', project_config['classification'])) if 'file_sorter' in project_config: self.project.file_sorter = self.parse_file_sorter( os.getenv('file_sorter', project_config['file_sorter'])) return self.project
def parse_document(self, document_conf): ''' parse document config and return the result ''' if not isinstance(document_conf, list): dgen_utils.log_err("document_conf is not a list:", document_conf) document = dgen_document.dgenDocument() known = ['cover', 'toc', 'page'] for item in document_conf: self.no_unknown_in_conf(known, item) # TODO: adding the doc_type after is dogey. may need to refactor. if 'cover' in item: cover = self.parse_section(item['cover'], 'cover') document.add_section(cover) if 'toc' in item: toc = self.parse_section(item['toc'], 'toc') document.add_section(toc) if 'page' in item: section = self.parse_section(item['page'], 'page') document.add_section(section) return document
def html_filename(self): ext = 'html' if self.name == '': dgen_utils.log_err('name not set') return '.'.join([self.name, ext])
def no_unknown_in_conf(self, known, conf): dgen_utils.log_dbg('known fields:', known) for key in conf: if not key in known: dgen_utils.log_err('unknown attribute:', key)
def all_mandatory_in_conf(self, mandatory, conf): dgen_utils.log_dbg('mandatory fields:', mandatory) for key in mandatory: if not key in conf: dgen_utils.log_err('attribute missing:', conf)
def parse_string(self, string): if not isinstance(string, (str, int, float)): dgen_utils.log_err("could not parse as string or number:", string) return str(string)
def pdf_filename(self): if self.filename == '': dgen_utils.log_err('filename not set') return '.'.join([self.filename, 'pdf'])
def revealjs_dir(self, value): self.__revealjs_dir = dgen_utils.expand_path(value) if not os.path.isdir(self.__revealjs_dir): dgen_utils.log_err('revealjs_dir %s does not exist!' % (value))
def copy_reveal(self): if self.project.revealjs_dir is '': dgen_utils.log_err('config item revealjs_dir not set for project') dgen_utils.copy_files(self.project.html_dir, self.project.revealjs_dir)
def template_dir(self): result = os.path.join(self.templates_root, self.template) result = dgen_utils.expand_paths(result) if not os.path.isdir(result): dgen_utils.log_err('template_dir %s does not exist!' % (result)) return result