Source code for bokeh.command.subcommands.file_output
#-----------------------------------------------------------------------------# Copyright (c) Anaconda, Inc., and Bokeh Contributors.# All rights reserved.## The full license is in the file LICENSE.txt, distributed with this software.#-----------------------------------------------------------------------------''' Abstract base class for subcommands that output to a file (or stdout).'''#-----------------------------------------------------------------------------# Boilerplate#-----------------------------------------------------------------------------from__future__importannotationsimportlogging# isort:skiplog=logging.getLogger(__name__)#-----------------------------------------------------------------------------# Imports#-----------------------------------------------------------------------------# Standard library importsimportargparseimportsysfromabcimportabstractmethodfromos.pathimportsplitext# Bokeh importsfrom...documentimportDocumentfrom..subcommandimport(Arg,Args,Argument,Subcommand,)from..utilimportbuild_single_handler_applications,die#-----------------------------------------------------------------------------# Globals and constants#-----------------------------------------------------------------------------__all__=('FileOutputSubcommand',)#-----------------------------------------------------------------------------# General API#-----------------------------------------------------------------------------#-----------------------------------------------------------------------------# Dev API#-----------------------------------------------------------------------------
[docs]classFileOutputSubcommand(Subcommand):''' Abstract subcommand to output applications as some type of file. '''# subtype must set this instance attribute to file extensionextension:str
[docs]@classmethoddeffiles_arg(cls,output_type_name:str)->Arg:''' Returns a positional arg for ``files`` to specify file inputs to the command. Subclasses should include this to their class ``args``. Example: .. code-block:: python class Foo(FileOutputSubcommand): args = ( FileOutputSubcommand.files_arg("FOO"), # more args for Foo ) + FileOutputSubcommand.other_args() '''return('files',Argument(metavar='DIRECTORY-OR-SCRIPT',nargs='+',help=(f"The app directories or scripts to generate {output_type_name} for"),default=None,))
[docs]@classmethoddefother_args(cls)->Args:''' Return args for ``-o`` / ``--output`` to specify where output should be written, and for a ``--args`` to pass on any additional command line args to the subcommand. Subclasses should append these to their class ``args``. Example: .. code-block:: python class Foo(FileOutputSubcommand): args = ( FileOutputSubcommand.files_arg("FOO"), # more args for Foo ) + FileOutputSubcommand.other_args() '''return((('-o','--output'),Argument(metavar='FILENAME',action='append',type=str,help="Name of the output file or - for standard output.",)),('--args',Argument(metavar='COMMAND-LINE-ARGS',nargs="...",help="Any command line arguments remaining are passed on to the application handler",)),)
[docs]definvoke(self,args:argparse.Namespace)->None:''' '''argvs={f:args.argsforfinargs.files}applications=build_single_handler_applications(args.files,argvs)ifargs.outputisNone:outputs:list[str]=[]else:outputs=list(args.output)# copy so we can pop from itiflen(outputs)>len(applications):die("--output/-o was given too many times (%d times for %d applications)"%(len(outputs),len(applications)))for(route,app)inapplications.items():doc=app.create_document()iflen(outputs)>0:filename=outputs.pop(0)else:filename=self.filename_from_route(route,self.extension)self.write_file(args,filename,doc)
[docs]@abstractmethoddeffile_contents(self,args:argparse.Namespace,doc:Document)->str|bytes|list[str]|list[bytes]:''' Subclasses must override this method to return the contents of the output file for the given doc. subclassed methods return different types: str: html, json bytes: SVG, png Raises: NotImplementedError '''raiseNotImplementedError()