def cli_render(argv, builder=None, builder_name='mpe'): """ Accept invocations to render documents from source to dest or stdout. Subsequent invocations should be separated by '--'. The initial group of arguments TODO: see cli_process. Setup publisher chain from builder class, and run them converting document(s) from source to dest. """ if not builder: Builder = comp.get_builder_class(builder_name, class_name='Builder') builder = Builder() #argvs = split_argv(argv) builder.prepare_initial_components() # replace settings for initial components builder.process_command_line(argv=argv)#s.next()) #pub.set_components(reader_name, parser_name, writer_name) #pub.process_programmatic_settings( # settings_spec, settings_overrides, config_section) #pub.set_source(source, source_path) #pub.set_destination(destination, destination_path) #output = pub.publish(enable_exit_status=enable_exit_status) assert builder.settings print builder.render(None, builder.settings._source) return print builder.document.parse_messages print builder.document.transform_messages
def status(self, sa=None, *paths): """ Work in progress: run over documents and store extractor results. """ # XXX print frontend.cli_process(source, builder_name='htdocs') B = comp.get_builder_class('dotmpe.du.builder.htdocs') #B = comp.get_builder_class('dotmpe.du.builder.dotmpe_v5') B.extractor_spec = [ ( 'dotmpe.du.ext.extractor.reference.Extractor', 'dotmpe.du.ext.extractor.TransientStorage' ), ( 'dotmpe.du.ext.extractor.htdocs.HtdocsExtractor', 'dotmpe.du.ext.extractor.TransientStorage' ) ] store_params = { #'HtdocsStorage': ((), dict(dbref=)), 'dotmpe.du.ext.extractor.TransientStorage': (( {}, 'results', ), dict()), } b = B() b.prepare_extractors(**store_params) sources = [] for path in paths: if os.path.isdir(path): walk_opts = res.fs.Dir.walk_opts.copy() walk_opts.update(dict(recurse=True, files=True)) sources_ = list( res.fs.Dir.walk(path, confparse.Values(walk_opts))) [ sources.append(s) for s in sources_ if s.endswith('.rst') ] elif os.path.isfile(path): if path.endswith('.rst'): sources.append(path) for source in sources: document = None print 'source', source try: document = b.build(source=open(source).read(), source_id=source) except: #print 'error building %s' % source traceback.print_exc() log.err('building %s', source) continue try: b.process(document) except: #print 'error processing', source traceback.print_exc() log.err('processing %s', source) continue ts = b.extractors[0][1] print 'ts.results', ts.results, self
def cli_run(argv, stdin=None, builder=None, builder_name='mpe'): """ Enter extended batch mode; read commands from stdin or readline. Using argv, this resolve the initial argument to a callable method of the builder and delegates further (argv/stdin) handling to it. This defaults to 'interactive', if no arguments are given and the stdin is connected to a terminal. For no-terminal without arguments it will try to run 'read_script'. """ if not stdin: stdin = sys.stdin if not builder: Builder = comp.get_builder_class(builder_name, class_name='Builder') builder = Builder() # connected to TTY session or redirected descriptor? argvs = None if argv: argvs = split_argv(argv) elif not stdin.isatty(): builder.read_script() # run commands from argv for argv in argvs: # XXX very naively.. if argv: cmd = argv.pop(0) else: cmd = 'interactive' try: getattr(builder, cmd)(argv) except Exception, e: logger.error( "Error in command handler %r: %s" % (cmd, e) ) traceback.print_exc(sys.stderr) if hasattr(e, 'info'): traceback.print_exception(*e.info)
def cli_process(argv, builder=None, builder_name='mpe', description=''): """ - Load builder for given name or use provided instance. Make one or more invocations to process for given source files, process will run all extractors of the given builder. - CLI arguments for subsequent calls are separated by '--'. TODO: Extractors should be initialized only once (ie. using initial options only). Settings are updated from additional options if provided. Ofcourse all other components should be reusable and/or reset appropiately. FIXME: does not merge settings between invokations """ if not builder: Builder = comp.get_builder_class(builder_name, class_name='Builder') builder = Builder() # raises exception if no argv argvs = split_argv(argv) builder.prepare_initial_components() #print 'components', builder.components # replace settings for initial components builder.process_command_line(argv=argvs.next()) # Rest deals with argv handling and defers to run_process (tmp) for argv in argvs: # replace settings for initial components builder.process_command_line(argv=argv) builder._do_process() else: # No further args (or break but not using that) builder._do_process()
def __expose_options(self, buildername): """ Add a section to the document containing an optiongroup with options provided by the builder/reader/parser combo. """ p = buildername.rfind('.') assert p>-1, "Illegal build-line: %s" % buildername package_name, class_name = buildername[:p], buildername[p+1:] builder_class = get_builder_class( package_name, class_name ) prsr = frontend.OptionParser(components=( builder_class.Reader, builder_class.Parser, #builder_class.Writer )) specnode = nodes.section('',nodes.title('','Docutils Options')) specnode += self.__add_optiongroup(prsr) for optiongroup in prsr.option_groups: specnode += nodes.title('', optiongroup.title) specnode += self.__add_optiongroup(optiongroup) self.document += specnode
def get_builder(buildername, allowed_builders=[]): """ Parse string-name and load class component. """ # Determine package and class p = buildername.rfind('.') assert p>-1, "Illegal build-line: %s" % buildername package_name, class_name = buildername[:p], buildername[p+1:] # XXX in-doc override for admin #if self.user and self.user.admin: # package_name, class_name = read_buildline(source, # default_package=package_name, # default_class=class_name) # Look for allowed packages and get module path if allowed_builders: assert package_name in allowed_builders, "Builder %s is not "\ "allowed for this server. " % package_name # Import try: builder_class = get_builder_class( package_name, class_name ) except ImportError, e: print "No such builder: ", e raise e