def handle_error(msg): """Handle an error.""" lg().disabled = False lg().error(msg) from jomiel_kore.app import exit_error exit_error()
def init(): """Initiates the application subsystems.""" from yomiel.subsys import log log.init() from yomiel.log import lg lg().info("all subsystems initiated")
def main_loop(): """This is the main loop for the application.""" input_uri = read_input(opts.uri) auth_opts = determine_auth_opts() from jomiel_comm.inquiry import ( connect, inquire, InquireError, ) try: lg().info( "connect to %s (timeout=%d)", opts.router_endpoint, opts.connect_timeout, ) sck = connect( opts.router_endpoint, auth=auth_opts, logger=lg(), ) for uri in input_uri: lg().info("inquire <%s>", uri) resp = inquire( sck, uri, timeout=opts.connect_timeout, ) dump_metadata(resp) except KeyboardInterrupt: sigint() except OSError as msg: handle_error(msg) except InquireError as msg: handle_error(msg) exit_normal()
def dump_metadata(response): """Print the metadata to standard output.""" def has_stream(): """Has stream data.""" return response.media.stream def has_image(): """Has image data.""" return response.media.image if has_stream(): lg().debug("has video") elif has_image(): lg().debug("has image") else: handle_error("unexpected response (empty media lists)") from sys import stdout if "raw" in opts.output_format: stdout.write(str(response.media)) else: if "json" in opts.output_format: from jomiel_comm.formatter import to_json to_json( response.media, minified=opts.debug_minify_json, stream=stdout, ) elif "yaml" in opts.output_format: from jomiel_comm.formatter import to_yaml to_yaml(response.media, stream=stdout) elif "terse" in opts.output_format: dump_terse_response(stdout, response.media) else: handle_error( "unexpected --output-format value (%s)" % opts.output_format, )
def init(): """Initiates the logging subsystem.""" from yomiel.cache import logger_paths, opts from jomiel_kore.log import log_init (logger_file, logger_idents) = log_init(logger_paths) from yomiel.log import lg lg().debug( "subsys/log: configuration file loaded from '%s'", logger_file, ) if opts.logger_idents: from jomiel_kore.app import dump_logger_identities dump_logger_identities( logger_idents, opts.logger_idents_verbose, ) lg().info("log subsystem initiated")
def sigint(): """Handle SIGINT.""" lg().error("signal interrupt") exit_normal()
def run(self): """Application entry point; executes the app.""" from configargparse import get_parser parser = get_parser() parser.add( "uri", metavar="<uri>", nargs="*", help="the URIs to parse", ) parser.add( "-o", "--output-format", help="Print messages in the specified data serialization format", choices=["raw", "json", "yaml", "terse"], metavar="[raw|json|yaml|terse]", default="raw", ) def jomiel_group(): """Add the jomiel options group.""" grp = parser.add_argument_group("jomiel") grp.add( "-r", "--router-endpoint", help="jomiel router endpoint address to connect to", default="tcp://*****:*****@server:port", ) grp.add( "--ssh-key-file", help="Path to the key file to use", metavar="FILE", ) grp.add( "--ssh-password", help="Password to the SSH server", metavar="PASSWD", ) grp.add( "--ssh-timeout", help="""Time (in seconds) after which no activity will result in the tunnel closing""", default=60, type=check_if_positive_integer, metavar="TIME", ) grp.add( "--ssh-paramiko", help="Use paramiko instead of pexpect", action="store_true", ) ssh_group() def dump_terse_response(stdout, media_response): """Dump a terse response Args: stdout (obj): the std output stream to dump the object to media_response (obj): the media response object to dump """ stdout.write("---\ntitle: " + media_response.title + "\n") stdout.write("quality:\n") def get_terse_quality_string(): """Return terse string for a stream quality.""" return " profile: {}\n width: {}\n height: {}\n".format( stream_quality.profile, stream_quality.width, stream_quality.height, ) for stream in media_response.stream: stream_quality = stream.quality quality_string = get_terse_quality_string() stdout.write(quality_string) def dump_metadata(response): """Print the metadata to standard output.""" def has_stream(): """Has stream data.""" return response.media.stream def has_image(): """Has image data.""" return response.media.image if has_stream(): lg().debug("has video") elif has_image(): lg().debug("has image") else: handle_error("unexpected response (empty media lists)") from sys import stdout if "raw" in opts.output_format: stdout.write(str(response.media)) else: if "json" in opts.output_format: from jomiel_comm.formatter import to_json to_json( response.media, minified=opts.debug_minify_json, stream=stdout, ) elif "yaml" in opts.output_format: from jomiel_comm.formatter import to_yaml to_yaml(response.media, stream=stdout) elif "terse" in opts.output_format: dump_terse_response(stdout, response.media) else: handle_error( "unexpected --output-format value (%s)" % opts.output_format, ) def determine_auth_opts(): """Determine whether auth should be used.""" from jomiel_comm.auth import auth_opts_new rval = None if "curve" in opts.auth_mode: from jomiel_comm.auth import curve_opts_new curve_opts = curve_opts_new( opts.curve_server_public_key_file, opts.curve_client_key_file, ) rval = auth_opts_new(curve=curve_opts) elif "ssh" in opts.auth_mode: if not opts.ssh_server: handle_error( "argument --ssh-server: conflicting option " "string (None) when used with --auth-mode=ssh", ) from jomiel_comm.auth import ssh_opts_new ssh_opts = ssh_opts_new( opts.ssh_server, opts.ssh_key_file, opts.ssh_password, opts.ssh_timeout, opts.ssh_paramiko, ) rval = auth_opts_new(ssh=ssh_opts) return rval def main_loop(): """This is the main loop for the application.""" input_uri = read_input(opts.uri) auth_opts = determine_auth_opts() from jomiel_comm.inquiry import ( connect, inquire, InquireError, ) try: lg().info( "connect to %s (timeout=%d)", opts.router_endpoint, opts.connect_timeout, ) sck = connect( opts.router_endpoint, auth=auth_opts, logger=lg(), ) for uri in input_uri: lg().info("inquire <%s>", uri) resp = inquire( sck, uri, timeout=opts.connect_timeout, ) dump_metadata(resp) except KeyboardInterrupt: sigint() except OSError as msg: handle_error(msg) except InquireError as msg: handle_error(msg) exit_normal() opts = super().parse_opts(parser) lg().disabled = "terse" in opts.output_format from yomiel.subsys import init init() main_loop()