def run_cmd(cmd): try: process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) if is_py2: for line in iter(process.stdout.readline, ""): sys.stdout.write(line) sys.stdout.flush() else: for line in iter(process.stdout.readline, b""): line = line.decode("utf-8") sys.stdout.write(line) sys.stdout.flush() process.wait() except subprocess.CalledProcessError as e: raise RuntimeError("dump failed with code {} and output: {}".format( e.returncode, e.output)) except OSError as e: if e.errno == 2: echo.err( "dump is not installed, you need to run `pip install dump`") raise
def run_cmd(cmd): try: process = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) if is_py2: for line in iter(process.stdout.readline, ""): sys.stdout.write(line) sys.stdout.flush() else: for line in iter(process.stdout.readline, b""): line = line.decode("utf-8") sys.stdout.write(line) sys.stdout.flush() process.wait() except subprocess.CalledProcessError as e: raise RuntimeError("dump failed with code {} and output: {}".format(e.returncode, e.output)) except OSError as e: if e.errno == 2: echo.err("dump is not installed, you need to run `pip install dump`") raise
def main(name, dry_run): """go through and check wishlist against previous entries""" echo.out( "{}. Starting on wishlist {}", datetime.datetime.utcnow(), name, ) # Let's flush out any problems connecting to the DB before getting into the loop WatchlistItem.interface.connect() name = name[0] email = Email(name) item_count = 1 try: for item_count, we in enumerate(Wishlist(name), item_count): try: echo.out("{}. (p{}) {}", item_count, we.page, we.title) item = Item( uuid=we.uuid, body=we.jsonable(), price=we.price, element=we, ) add_item(email, item, dry_run) except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() email.errors.append((e, (exc_type, exc_value, exc_traceback))) echo.err("{}. Failed!", item_count) echo.exception(e) # bail if we've had a lot of errors or the first N items # have all resulted in an error total_errors = len(email.errors) if total_errors > 25 or (total_errors > 10 and total_errors == item_count): break echo.out( "{}. Done with wishlist, {} total items, {} changes", datetime.datetime.utcnow(), item_count, len(email), ) except Exception as e: exc_type, exc_value, exc_traceback = sys.exc_info() email.errors.append((e, (exc_type, exc_value, exc_traceback))) echo.exception(e) if not dry_run: email.send(item_count=item_count)
def main(path): '''scan path directory and any subdirectories for valid captain scripts''' basepath = os.path.abspath(os.path.expanduser(str(path))) echo.h2("Available scripts in {}".format(basepath)) echo.br() for root_dir, dirs, files in os.walk(basepath, topdown=True): for f in fnmatch.filter(files, '*.py'): try: filepath = os.path.join(root_dir, f) # super edge case, this makes sure the python script won't start # an interactive console session which would cause the session # to start and not allow the for loop to complete with open(filepath, encoding="UTF-8") as fp: body = fp.read() is_console = "InteractiveConsole" in body is_console = is_console or "code" in body is_console = is_console and "interact(" in body if is_console: continue s = captain.Script(filepath) if s.can_run_from_cli(): rel_filepath = s.call_path(basepath) p = s.parser echo.h3(rel_filepath) desc = p.description if desc: echo.indent(desc, indent=(" " * 4)) subcommands = s.subcommands if subcommands: echo.br() echo.indent("Subcommands:", indent=(" " * 4)) for sc in subcommands.keys(): echo.indent(sc, indent=(" " * 6)) echo.br() except captain.ParseError: pass except Exception as e: #echo.exception(e) #echo.err("Failed to parse {} because {}", f, e.message) echo.err("Failed to parse {}", f) echo.verbose(e.message) echo.br()
def main(path): '''scan path directory and any subdirectories for valid captain scripts''' basepath = os.path.abspath(os.path.expanduser(str(path))) echo.h2("Available scripts in {}".format(basepath)) echo.br() for root_dir, dirs, files in os.walk(basepath, topdown=True): for f in fnmatch.filter(files, '*.py'): try: filepath = os.path.join(root_dir, f) # super edge case, this makes sure the python script won't start # an interactive console session which would cause the session # to start and not allow the for loop to complete with open(filepath, encoding="UTF-8") as fp: body = fp.read() is_console = "InteractiveConsole" in body is_console = is_console or "code" in body is_console = is_console and "interact(" in body if is_console: continue s = captain.Script(filepath) if s.can_run_from_cli(): rel_filepath = s.call_path(basepath) p = s.parser echo.h3(rel_filepath) desc = p.description if desc: echo.indent(desc, indent=(" " * 4)) subcommands = s.subcommands if subcommands: echo.br() echo.indent("Subcommands:", indent=(" " * 4)) for sc in subcommands.keys(): echo.indent(sc, indent=(" " * 6)) echo.br() except captain.ParseError: pass except Exception as e: #echo.exception(e) #echo.err("Failed to parse {} because {}", f, e.message) echo.err("Failed to parse {}", f) echo.verbose(String(e)) echo.br()
def main_dump(name, **kwargs): """This is really here just to test that I can parse a wishlist completely and to demonstrate (by looking at the code) how to iterate through a list""" name = name[0] #pout.v(name, start_page, stop_page, kwargs) #pout.x() w = Wishlist(name) i = 1 for i, item in enumerate(w, 1): try: item_json = item.jsonable() echo.out("{}. {} is ${:.2f}", i, item_json["title"], item_json["price"]) except RobotError: raise except ParseError as e: echo.err("{}. Failed!", i) echo.err(e.body) echo.exception(e) echo.out("Done with wishlist, {} total items", i)
def main_dump(name, start_page, stop_page, **kwargs): """This is really here just to test that I can parse a wishlist completely and to demonstrate (by looking at the code) how to iterate through a list""" name = name[0] #pout.v(name, start_page, stop_page, kwargs) #pout.x() pages = set() current_url = "" w = Wishlist() for i, item in enumerate(w.get(name, start_page, stop_page), 1): new_current_url = w.current_url if new_current_url != current_url: current_url = new_current_url echo.h3(current_url) try: item_json = item.jsonable() echo.out("{}. {} is ${:.2f}", i, item_json["title"], item_json["price"]) echo.indent(item_json["url"]) except RobotError: raise except ParseError as e: echo.err("{}. Failed!", i) echo.err(e.body) echo.exception(e) except KeyboardInterrupt: break except Exception as e: echo.err("{}. Failed!", i) echo.exception(e) finally: pages.add(w.current_page) echo.out("Done with wishlist, {} total pages parsed (from {} to {})", len(pages), start_page, stop_page)