def extract_geometry(): input_path = get_shape_path("ne_50m_admin_0_countries") output_path = path.join(OUT_DIR, "geometry.json") features = [] with fiona.open(input_path) as source: for feat in source: del feat["properties"] geometry = feat["geometry"] feat["bbox"] = shape(geometry).bounds features.append(feat) my_layer = { "type": "FeatureCollection", "features": features } p = Popen( ['geo2topo', '-q', '1e5', 'geometry=-', '-o', output_path], stdin=PIPE, stdout=PIPE, stderr=PIPE ) errors = p.communicate(input=json.dumps(my_layer))[1] if p.returncode == 0: print c.green("Extracted data to {}".format(output_path)) else: print c.red("geo2topo exited with {}. {}".format(p.returncode, errors.decode('utf-8').strip()))
def dot_formatter_failure_summary(self, features, marker): """Output summary for failed Scenarios.""" if not self._failed_steps: return output = "\n" + cf.bold_red("Failures:") + "\n" for step in self._failed_steps: output += "{}: {}\n {}\n".format( step.path, step.parent.sentence, cf.red(step.sentence) ) if world.config.with_traceback: output += " {}\n".format( "\n ".join( [ str(cf.red(l)) for l in step.failure.traceback.split("\n")[:-2] ] ) ) output += " {}: {}\n\n".format( cf.bold_red(step.failure.name), cf.red(step.failure.reason) ) sys.stdout.write(u(output + "\n"))
def play_trailer(user_input): if args.media_type == "tv": print(colorful.red("Currently, movie torrents are only supported.")) return sp = user_input.split() if not len(sp) > 1 or not str.isdigit(sp[1]): print( colorful.red( "Incorrect usage. Enter 'trailer' and the id of the torrent to play the movie trailer." )) return torrent_info = ezflix.torrent_info(sp[1]) if torrent_info is None: print(colorful.red("Invalid selection.")) return trailer = torrent_info["trailer"] if not trailer: print(colorful.red("No trailer found.")) return geometry = "50%" subprocess.Popen([ "/bin/bash", "-c", "%s https://www.youtube.com/watch?v=%s --geometry=%s" % (media_player, trailer, geometry), ])
def load_state(self, db_file=None): """Loads application data from a db file to the application""" connection = sqlite3.connect( ":memory:") if not db_file else sqlite3.connect(db_file) cursor = connection.cursor() try: # Load Rooms cursor.execute('''SELECT * FROM room''') rooms = cursor.fetchall() for room in rooms: room_name, room_type = room[0], room[1] self.create_room(room_type, room_name) except BaseException: print( colorful.red("The application has failed to load room data, \ please contact a senior developer for help.")) try: # Load People cursor.execute('''SELECT * FROM person''') people = cursor.fetchall() for person in people: person_id, first_name, last_name, person_type,\ has_living_space, \ has_office, wants_accommodation \ = person[0], person[1], person[2], person[3],\ person[4], person[5], person[6] loaded_person = Staff( first_name, last_name, person_id, has_living_space, has_office) if person_type is "Staff" else Fellow( first_name, last_name, wants_accommodation, person_id, has_living_space, has_office) self.people.append(loaded_person) except BaseException: print( colorful.red("The application has failed to load person data, \ please contact a senior developer for help.")) try: # Load Residents cursor.execute('''SELECT * FROM room_person''') room_person_data = cursor.fetchall() for room_person in room_person_data: person_id, room_name, room_type = int( room_person[0]), room_person[1], room_person[2] related_room = find_room(self.rooms, room_name) related_person = find_person(self.people, person_id) related_room.residents.append(related_person) related_person.rooms_occupied.append({room_type: room_name}) except BaseException: print( colorful.red( "The application has failed to load relationship between person and room, \ Please contact a senior developer for help."))
class MyInteractive(cmd.Cmd): intro = colorful.green('Welcome come to my world!') + colorful.red( "Now,Let's start") + '\n' + ''' fa fafafa fafafa fa fa fa fa fafafafa fa fa fafa fa fa fa fa fa fa fa fafafa fafafaf ''' + '\n' + colorful.green('''you could use -help (get help) -quit (exit program) -search (migu video search) -mgvideo (migu video channel) ''') prompt = colorful.red('bf>') file = None @docopt_cmd def do_mgvideo(self, arg): """Usage: mgvideo""" video_channel = mgvideo.Miguvideo() video_channel.run() @docopt_cmd def do_search(self, arg): """Usage: search Options: --search=<n> """ new = search.migu_go() new.run() @docopt_cmd def do_serial(self, arg): """Usage: serial <port> [--baud=<n>] [--timeout=<seconds>] Options: --baud=<n> Baudrate [default: 9600] """ print(arg) @docopt_cmd def do_wait(self, arg): """Usage: wait <port> [--baud=<n>] [--timeout=<seconds>] Options: --baud=<n> Baudrate [default: 9600] """ print(arg) def do_quit(self, arg): """Quits out of Interactive Mode.""" print('Good Bye!') exit()
def show(self): print(f"\n \n \n" f"{self.full_product['product_name']} is a better food as it has" f" a nutriscore graded {self.full_product['nutriscore']}." f"\n You can buy it in these stores : " f"{self.full_product['store_name']}" f"\n For more information visit this url : " f"{self.full_product['url']}") print( cf.white("\n If you want to " + cf.red('save', nested=True) + " this food into your favorite, " + cf.red('press "s" ', nested=True) + "\n \n \n"))
def output_failure(step_func, errors): """ Write the given errors to stdout. """ sys.stdout.write(u(colorful.bold_red("✘"))) if step_func is not None: sys.stdout.write( u(colorful.red(" (at {0})".format(get_func_location(step_func))))) sys.stdout.write("\n") for error in errors: print(u(colorful.red(" - {0}".format(error))))
def output_failure(step_func, errors): """ Write the given errors to stdout. """ sys.stdout.write(u(colorful.bold_red(u'✘'))) if step_func is not None: sys.stdout.write( u(colorful.red(' (at {0})'.format(get_func_location(step_func))))) sys.stdout.write('\n') for error in errors: print(u(colorful.red(' - {0}'.format(error))))
def media_info(user_input): sp = user_input.split() if not len(sp) > 1 or not str.isdigit(sp[1]): print( colorful.red( "Incorrect usage. Enter 'info' and the id of the torrent to get the movie/tv show overview." )) return torrent_info = ezflix.torrent_info(sp[1]) if torrent_info is None: print(colorful.red("Invalid selection.")) return print(torrent_info["overview"])
def similar(user_input): if args.media_type == "tv": print(colorful.red("Currently, movie torrents are only supported.")) return sp = user_input.split() if not len(sp) > 1 or not str.isdigit(sp[1]): print( colorful.red( "Incorrect usage. Enter 'similar' and the id of the torrent to find similar movies." )) return torrents = ezflix.similar(sp[1]) display_table(torrents)
def output_failure(step_func, errors): """ Write the given errors to stdout. """ sys.stdout.write(u(colorful.bold_red("✘"))) if step_func is not None: sys.stdout.write( u(colorful.red(" (at {0})".format(get_func_location(step_func)))) ) sys.stdout.write("\n") for error in errors: print(u(colorful.red(" - {0}".format(error))))
def banner(header): """Prints banner. Red line accross screen with text in the middle.""" print "" header = header.upper() try: _, width = struct.unpack( 'hh', fcntl.ioctl(sys.stdin.fileno(), termios.TIOCGWINSZ, '1234')) except struct.error: width = 80 width -= 2 print colorful.red( ("[{:%s^%ss}]" % (BANNER_SEPERATOR, width)).format(header)) return
def main(): print( cf.red("IF IT IS THE FIRST TIME YOU USE THE APPLICATION, YOU MUST " "DOWNLOAD DATA AND RESET DATABASE \n")) answer = input_yes_no(sentence["data"]) if answer == 'y': rd = RequestData() rd.fetch_category() rd.fetch_products() dataclean = Cleaner() category_manager = CategoryManager() store_manager = StoreManager() data = dataclean.get_dict_data() answer = input_yes_no(sentence["erase"]) if answer == 'y': product_manager.wipe_out() category_manager.create_tables() store_manager.create_tables() product_manager.create_tables() favorite_manager.create_tables() product_manager.insert_products(data) while True: device.show() event = device.input_checker() device.on_event(event)
def get_config(filename): def recursive_merge(old, new): d = old.copy() for key in new: if key in d and isinstance(d[key], dict): d[key] = recursive_merge(d[key], new[key]) else: d[key] = new[key] return d cwd = Path.cwd() while not (cwd / filename).exists(): if cwd.parent == cwd: break # stop at the root cwd = cwd.parent try: with open(cwd / filename) as f: data = toml.load(f) if "base" in data: data = recursive_merge( get_config((cwd / filename).parent / data["base"]), data) os.chdir((cwd / filename).parent) return data except FileNotFoundError: raise click.BadOptionUsage( option_name="--config", message=colorful.red(f"Configuration file {filename} not found."), )
def show(): """ Show the modifiers and colors """ # modifiers sys.stdout.write(colorful.bold('bold') + ' ') sys.stdout.write(colorful.dimmed('dimmed') + ' ') sys.stdout.write(colorful.italic('italic') + ' ') sys.stdout.write(colorful.underlined('underlined') + ' ') sys.stdout.write(colorful.inversed('inversed') + ' ') sys.stdout.write(colorful.concealed('concealed') + ' ') sys.stdout.write(colorful.struckthrough('struckthrough') + '\n') # foreground colors sys.stdout.write(colorful.red('red') + ' ') sys.stdout.write(colorful.green('green') + ' ') sys.stdout.write(colorful.yellow('yellow') + ' ') sys.stdout.write(colorful.blue('blue') + ' ') sys.stdout.write(colorful.magenta('magenta') + ' ') sys.stdout.write(colorful.cyan('cyan') + ' ') sys.stdout.write(colorful.white('white') + '\n') # background colors sys.stdout.write(colorful.on_red('red') + ' ') sys.stdout.write(colorful.on_green('green') + ' ') sys.stdout.write(colorful.on_yellow('yellow') + ' ') sys.stdout.write(colorful.on_blue('blue') + ' ') sys.stdout.write(colorful.on_magenta('magenta') + ' ') sys.stdout.write(colorful.on_cyan('cyan') + ' ') sys.stdout.write(colorful.on_white('white') + '\n')
def extract_provinces_and_states_lines(): input_path = get_shape_path("ne_50m_admin_1_states_provinces_lines") output_path = path.join(OUT_DIR, "states-provinces-lines.json") features = [] with fiona.open(input_path) as source: for feat in source: del feat["properties"] geometry = feat["geometry"] feat["bbox"] = shape(geometry).bounds features.append(feat) my_layer = {"type": "FeatureCollection", "features": features} with Popen(['geo2topo', '-q', '5e3', 'geometry=-', '-o', output_path], stdin=PIPE, stdout=PIPE, stderr=PIPE) as subproc: errors = subproc.communicate(input=json.dumps(my_layer).encode())[1] if subproc.returncode == 0: print(c.green("Extracted data to {}".format(output_path))) else: print( c.red("geo2topo exited with {}. {}".format( subproc.returncode, errors.decode().strip())))
def run_tests(locals): outcomes = dict() def add_outcome(name, successful): if name in outcomes: raise RuntimeError("Duplicate test name: " + name) outcomes[name] = successful loc = list(locals.keys()) for l in loc: if l.startswith("test_"): print(l, end=": ") f = locals[l] try: o = f() except Exception as e: print("EXCEPTION: " + str(e)) o = False passed = ( o is None ) or o # "void" methods are successful if they don't raise an Exception print(cf.green("PASS") if passed else cf.red("FAIL")) add_outcome(l, passed) plot.set_property("Plot.Title", "See console for test results.") failed_names = [n for n, s in outcomes.items() if not s] faileds = ", ".join(failed_names) if faileds: print("FAILED TESTS: " + faileds, file=sys.stderr) if faileds: raise Exception(str(len(failed_names)) + " tests failed")
def run(cls, cmd): cls.show_cmd(cmd) try: output = subprocess.run(cmd.split(), stdout=PIPE, stderr=PIPE, universal_newlines=True) if output.returncode: cls.stdout(str(colorful.red(output.stdout))) cls.stdout(str(colorful.red(output.stderr))) return output cls.stdout(str(colorful.green(output.stderr))) cls.stdout(str(colorful.green(output.stdout))) return output except Exception as exc: cls.show_err(exc)
def upload_to_gcs(self, queried_taiga_id: str, dest_gcs_path: str) -> bool: """Upload a Taiga datafile to a specified location in Google Cloud Storage. The service account [email protected] must have storage.buckets.create access for this request. Arguments: queried_taiga_id {str} -- Taiga ID in the form dataset_permaname.dataset_version/datafile_name or dataset_permaname.dataset_version dest_gcs_path {str} -- Google Storage path to upload to, in the form bucket:path Returns: bool -- Whether the file was successfully uploaded """ self._set_token_and_initialized_api() full_taiga_id = self.get_canonical_id(queried_taiga_id) if full_taiga_id is None: return False try: self.api.upload_to_gcs(full_taiga_id, dest_gcs_path) return True except (ValueError, TaigaHttpException) as e: print(cf.red(str(e))) return False
def display_table(torrents): pt = PrettyTable() pt.field_names = [ "Id", "Torrent", "Seeds", "Peers", "Released", "Rating", "Genre" ] pt.align = "l" t = 0 for result in torrents: if (not (result["seeds"] == 0 or result["seeds"] is None) or args.no_seeds is True): pt.add_row([ result["id"], result["title"], result["seeds"], result["peers"], result["release_date"], result["rating"], result["genre"], ]) t += 1 if t > 0 or args.no_seeds: print(pt) else: print(colorful.red("No results found.")) sys.exit(0)
def merge_gettext_catalogues(existing_catalogue_file, generated_catalogue_file): if path.exists(existing_catalogue_file): args = ( existing_catalogue_file, generated_catalogue_file, "--output-file", existing_catalogue_file, # ensure that the first occurence takes precedence in merge conflict "--use-first", # sort by msgid "--sort-output", # disable wrapping long strings because crowdin does not do that "--no-wrap") (exit_code, errors) = run_program("msgcat", *args) if exit_code == 0: print( c.green("Merged {} into {}.".format(generated_catalogue_file, existing_catalogue_file))) else: print( c.red("msgcat exited with {}: {}".format( exit_code, errors.decode().strip()))) else: print( c.orange("The existing catalogue does not exist. Copying {} to {}" ).format(generated_catalogue_file, existing_catalogue_file)) shutil.copyfile(generated_catalogue_file, existing_catalogue_file)
def colored_grade(grade): if grade < 4: return colorful.red(grade) elif grade < 4.5: return colorful.yellow(grade) else: return colorful.green(grade)
def search(page=1, term=None): global ezflix ezflix = Ezflix( query=args.query if term is None else term, media_type=args.media_type, limit=int(args.limit), sort_by=args.sort_by, sort_order=args.sort_order, quality=args.quality, minimum_rating=args.minimum_rating, language=args.language, page=page, debug=debug, cli_mode=True, ) torrents = ezflix.search() if torrents is None or len(torrents) == 0: print(colorful.red("No results found.")) return False if args.latest: latest = torrents[0] file_path = ezflix.search_subtitles( latest["title"]) if args.subtitles else None print("Playing " + latest["title"]) time.sleep(2.5) peerflix(latest["link"], media_player, args.subtitles, args.remove, file_path) sys.exit() display_table(torrents) return True
def _process(self): # Prepare message msg = [] for msg_index in range(0,len(self.message)): if self.exit_on_failure is False: msg_header = '{}{}'.format(self.exception_name, colorful.bold_white(':')) if msg_index == 0 else ' '*(len(self.exception_name)+1) msg.append('\t\t{} {}'.format(colorful.bold_red(msg_header), colorful.red(self.message[msg_index]))) else: msg.append(self.message[msg_index] if msg_index == 0 else '{}{} {} {}'.format("\t"*2, ' '*(len(self.exception_name)+1), colorful.bold_white(':'), self.message[msg_index])) if self.exit_on_failure is False: for message in msg: console_write(message) if self.no_failure is False: self._fail_step(self.step_obj.id) else: self.step_obj.state = Step.State.SKIPPED return if self.no_failure is False: raise self.exception('\n'.join(msg))
def main(): p = 1 search(p) if not args.no_menu: display_menu() while True: i = input() if i == 'quit': sys.exit() elif i == 'search': refined_query = input("Enter search query: ") search(page=1, term=refined_query) continue elif i == 'next': p += 1 search(p) continue elif i == 'prev': if p > 1: p -= 1 search(p) continue elif 'info' in i: media_info(i) continue elif 'similar' in i: similar(i) continue elif 'trailer' in i: play_trailer(i) continue elif 'imdb' in i: open_imdb_page(i) continue if not str.isdigit(i): print(colorful.red('Invalid selection.')) continue magnet = ezflix.torrent_info(int(i)) if not magnet: print(colorful.red('Invalid selection.')) continue print("Playing " + magnet['title']) file_path = ezflix.search_subtitles( magnet['title']) if args.subtitles else None peerflix(magnet['link'], media_player, args.subtitles, args.remove, file_path) sys.exit()
def _get_dataframe_or_path_offline( self, id: Optional[str], name: Optional[str], version: Optional[DatasetVersion], file: Optional[str], get_dataframe: bool, ): print( cf.orange( "You are in offline mode, please be aware that you might be out of sync with the state of the dataset version (deprecation)." ) ) if id is not None: query = id else: if name is None: print(cf.red("If id is not specified, name must be specified")) return None if version is None: print(cf.red("Dataset version must be specified")) return None query = format_datafile_id(name, version, file) get_from_cache = ( self.cache.get_entry if get_dataframe else self.cache.get_raw_path ) try: df_or_path = get_from_cache(query, query) if df_or_path is not None: return df_or_path except TaigaRawTypeException as e: print( cf.red( "The file is a Raw one, please use instead `download_to_cache` with the same parameters" ) ) return None except TaigaCacheFileCorrupted as e: print(cf.red(str(e))) return None print(cf.red("The datafile you requested was not in the cache.")) return None
def extract_relay_translations(): try: response = request_relays() except Exception as e: print c.red("Failed to fetch the relays list: {}".format(e)) raise result = response.get("result") if result is not None: countries = result.get("countries") if countries is None: raise Exception("Missing the countries field.") else: raise Exception("Missing the result field.") extract_relay_locations_pot(countries) translate_relay_locations_pot(countries)
def fetch_category(self): """Request the list of category from the API""" print("Initialization - Please wait") try: response = self.req(self.cat_url) data = json.loads(response.text) self.list_cat = [i['name'] for i in data['tags']] except requests.exceptions.Timeout as t: print(cf.red("Request Timeout, please retry : ", t)) except requests.exceptions.RequestException as err: print(cf.red("Something went bad, please retry : :", err)) list_cat = constant.CATEGORIES resp = [x for x in self.list_cat if x in list_cat] self.list_cat = resp self.categories_to_json(resp)
def test_problem(problem, log=True): if log: print(f"👷 Testing {problem}...") lang = language_detector.determine_language(problem) lang_config = language_detector.get_config(lang) if log: print(f"👷 Language = {lang_config.kattis_name} {lang_config.emoji}\n") inputs = glob.glob(f"./{problem}/*.in") count = 0 failed = False for input in inputs: count += 1 if log: print(f"🔎 Test number {count}:") input_file = open(input, "rb") input_content = input_file.read() program_path = f"./{problem}/solution.{lang_config.file_extension}" output_string = lang_config.run_program(program_path, input_content) answer = input.replace('.in', '.ans') answer_file = open(answer, "r") answer_content = answer_file.read() if output_string.replace("\r\n", "\n") != answer_content.replace( "\r\n", "\n"): if log: print(cf.bold_red("❌ Failed...")) print("__________INPUT____________") print(input_content.decode('utf-8')) print("__________INPUT____________") print(cf.red("__________OUTPUT___________")) print(cf.red(output_string)) print(cf.red("__________OUTPUT___________")) print("__________EXPECTED_________") print(answer_content) print("__________EXPECTED_________") failed = True elif log: print(cf.bold_green("✅ Test succesful!\n")) return not failed
def display_generic(value: Any, seen: Set[int], show_values: bool = True, indent: int = 0, batch_dims: Optional[Tuple[Optional[int], ...]] = None) -> str: if is_dataclass(value) and not isinstance(value, type): return display_dataclass(value, seen, show_values, indent, batch_dims) return cf.red(str(value)) + "\n"
def display_token(self): if self.token: if self.token.lower() == 'b': return colorful.blue(self.edges.format(self.token)) elif self.token.lower() == 'r': return colorful.red(self.edges.format(self.token)) else: return colorful.white(self.edges.format(' '))
def solr(container): """Starts Apache Solr if it's down""" if not subprocess.getoutput("docker exec -it %s pgrep java" % (container)): print(colorful.red("!!! Apache Solr is down.") + " Starting...") subprocess.getoutput( "docker exec -it -u solr %s /opt/solr/bin/solr start" % (container) ) else: print("- Apache Solr is up")
def memcached(container): """Starts memcached if it's down""" if not subprocess.getoutput("docker exec -it %s pgrep memcached" % (container)): print(colorful.red("!!! memcached is down.") + " Starting...") subprocess.getoutput( "docker exec -it %s memcached -d -u nobody -m 64 -p 11211 127.0.0.1" % (container) ) else: print("- memcached is up")
def check(container, service, name): """Starts common services if they're down""" if not subprocess.getoutput( "docker exec -it %s pgrep %s | head -1" % (container, service) ): print(colorful.red("!!! %s is down." % (name)) + " Starting...") subprocess.getoutput( "docker exec -it %s service %s start" % (container, service) ) else: print("- %s is up" % (name))
def status(drucker): """Make sure all containers are started""" if ( subprocess.getoutput('docker ps --format="{{.Names}}" | grep -c drucker') != "5" ): print( colorful.red( "You cannot run this command when one or more containers are stopped!" ) ) start(drucker)
def phpfpm(drucker): """Starts PHP-FPM if it's down""" if not subprocess.getoutput( "docker exec -it %s pgrep php-fpm%s | head -1" % (drucker.vars.WEB_CONTAINER, drucker.vars.DEFAULT_PHP) ): print(colorful.red("!!! PHP-FPM is down.") + " Starting...") subprocess.getoutput( "docker exec -it %s service php%s-fpm start" % (drucker.vars.WEB_CONTAINER, drucker.vars.DEFAULT_PHP) ) else: print("- PHP-FPM is up")
def set_local_ssh_path(drucker): """Write the SSH public key path to the config file""" if drucker.vars.KEY_PLACEHOLDER in open(drucker.vars.DEFAULT_CONFIG).read(): pubkey = input( "Enter path to SSH public key (%s): " % (drucker.vars.DEFAULT_PUBKEY) ) if not pubkey: replace_string( drucker.vars.DEFAULT_CONFIG, drucker.vars.KEY_PLACEHOLDER, drucker.vars.DEFAULT_PUBKEY, ) elif os.path.isfile(pubkey): replace_string( drucker.vars.DEFAULT_CONFIG, drucker.vars.KEY_PLACEHOLDER, pubkey ) else: print(colorful.red("This filepath doesn't exist. Please try again.")) set_local_ssh_path(drucker)
def set_local_db_path(drucker): """Write the local DB path to the config file""" if drucker.vars.DB_PLACEHOLDER in open(drucker.vars.DEFAULT_CONFIG).read(): host_db_path = input( """ Where should we store databases locally? (%s): """ % (drucker.vars.DEFAULT_DB_PATH) ) if not host_db_path: replace_string( drucker.vars.DEFAULT_CONFIG, drucker.vars.DB_PLACEHOLDER, drucker.vars.DEFAULT_DB_PATH, ) elif os.path.isfile(host_db_path): replace_string( drucker.vars.DEFAULT_CONFIG, drucker.vars.DB_PLACEHOLDER, host_db_path ) else: print(colorful.red("This filepath doesn't exist. Please try again.")) set_local_db_path(drucker)
def console_write(self, features, marker): """ Writes the endreport for all features :param list features: all features """ stats = { "features": { "amount": 0, "passed": 0, "failed": 0, "skipped": 0, "untested": 0, "pending": 0, }, "scenarios": { "amount": 0, "passed": 0, "failed": 0, "skipped": 0, "untested": 0, "pending": 0, }, "steps": { "amount": 0, "passed": 0, "failed": 0, "skipped": 0, "untested": 0, "pending": 0, }, } pending_steps = [] duration = timedelta() for feature in features: if not feature.has_to_run(world.config.scenarios): continue stats["features"]["amount"] += 1 stats["features"][feature.state] += 1 if feature.state in [Step.State.PASSED, Step.State.FAILED]: duration += feature.duration for scenario in feature.all_scenarios: if not scenario.has_to_run(world.config.scenarios): continue if isinstance(scenario, ScenarioOutline): # skip ScenarioOutlines continue if isinstance(scenario, ScenarioLoop): # skip ScenarioLoop continue stats["scenarios"]["amount"] += 1 stats["scenarios"][scenario.state] += 1 for step in scenario.steps: stats["steps"]["amount"] += 1 stats["steps"][step.state] += 1 if step.state == Step.State.PENDING: pending_steps.append(step) colored_closing_paren = colorful.bold_white(")") colored_comma = colorful.bold_white(", ") passed_word = colorful.bold_green("{0} passed") failed_word = colorful.bold_red("{0} failed") skipped_word = colorful.cyan("{0} skipped") pending_word = colorful.bold_yellow("{0} pending") output = colorful.bold_white( "{0} features (".format(stats["features"]["amount"]) ) output += passed_word.format(stats["features"]["passed"]) if stats["features"]["failed"]: output += colored_comma + failed_word.format(stats["features"]["failed"]) if stats["features"]["skipped"]: output += colored_comma + skipped_word.format(stats["features"]["skipped"]) if stats["features"]["pending"]: output += colored_comma + pending_word.format(stats["features"]["pending"]) output += colored_closing_paren output += "\n" output += colorful.bold_white( "{} scenarios (".format(stats["scenarios"]["amount"]) ) output += passed_word.format(stats["scenarios"]["passed"]) if stats["scenarios"]["failed"]: output += colored_comma + failed_word.format(stats["scenarios"]["failed"]) if stats["scenarios"]["skipped"]: output += colored_comma + skipped_word.format(stats["scenarios"]["skipped"]) if stats["scenarios"]["pending"]: output += colored_comma + pending_word.format(stats["scenarios"]["pending"]) output += colored_closing_paren output += "\n" output += colorful.bold_white("{} steps (".format(stats["steps"]["amount"])) output += passed_word.format(stats["steps"]["passed"]) if stats["steps"]["failed"]: output += colored_comma + failed_word.format(stats["steps"]["failed"]) if stats["steps"]["skipped"]: output += colored_comma + skipped_word.format(stats["steps"]["skipped"]) if stats["steps"]["pending"]: output += colored_comma + pending_word.format(stats["steps"]["pending"]) output += colored_closing_paren if pending_steps: sr = StepRegistry() pending_step_implementations = make_unique_obj_list( pending_steps, lambda x: x.definition_func ) output += colorful.white( "\nYou have {0} pending step implementation{1} affecting {2} step{3}:\n {4}\n\nNote: this could be the reason for some failing subsequent steps".format( len(pending_step_implementations), "s" if len(pending_step_implementations) is not 1 else "", len(pending_steps), "s" if len(pending_steps) is not 1 else "", "\n ".join( [ "- '{0}' @ {1}".format( sr.get_pattern(s.definition_func), get_func_code(s.definition_func).co_filename, ) for s in pending_step_implementations ] ), ) ) output += "\n" if world.config.wip: if stats["scenarios"]["passed"] > 0: output += colorful.red( "\nThe --wip switch was used, so I didn't expect anything to pass. These scenarios passed:\n" ) has_passed_scenarios = False for feature in features: passed_scenarios = list( filter( lambda s: s.state == Step.State.PASSED, feature.all_scenarios, ) ) for scenario in passed_scenarios: output += colorful.red( "\n - {}: {}".format(feature.path, scenario.sentence) ) has_passed_scenarios = True if has_passed_scenarios: output += "\n" else: output += colorful.green( "\nThe --wip switch was used, so the failures were expected. All is good.\n" ) output += colorful.cyan( "Run {0} finished within {1}".format( marker, humanize.naturaldelta(duration) ) ) write(output)
def main(args=None): """ Entrypont to radish. Setup up configuration, loads extensions, reads feature files and runs radish """ if args is None: args = sys.argv[1:] # note: using doc string for usage, messes up Sphinx documantation usage = """ Usage: radish show <features> [--expand] [--no-ansi] radish <features>... [-b=<basedir> | --basedir=<basedir>...] [-e | --early-exit] [--debug-steps] [-t | --with-traceback] [-m=<marker> | --marker=<marker>] [-p=<profile> | --profile=<profile>] [-d | --dry-run] [-s=<scenarios> | --scenarios=<scenarios>] [--shuffle] [--tags=<tags>] [--wip] [-f=<formatter> | --formatter=<formatter>] {0} radish (-h | --help) radish (-v | --version) Arguments: features feature files to run Options: -h --help show this screen -v --version show version -e --early-exit stop the run after the first failed step --debug-steps debugs each step -t --with-traceback show the Exception traceback when a step fails -m=<marker> --marker=<marker> specify the marker for this run [default: time.time()] -p=<profile> --profile=<profile> specify the profile which can be used in the step/hook implementation -b=<basedir> --basedir=<basedir>... set base dir from where the step.py and terrain.py will be loaded. [default: $PWD/radish] You can specify -b|--basedir multiple times or split multiple paths with a colon (:) similar to $PATH. All files will be imported. -d --dry-run make dry run for the given feature files -s=<scenarios> --scenarios=<scenarios> only run the specified scenarios (comma separated list) --shuffle shuttle run order of features and scenarios --tags=<feature_tags> only run Scenarios with the given tags --wip expects all tests to fail instead of succeeding -f=<formatter> --formatter=<formatter> the output formatter which should be used. [default: gherkin] --expand expand the feature file (all preconditions) {1} (C) Copyright by Timo Furrer <*****@*****.**> """ warnings.simplefilter("always", DeprecationWarning) # load extensions load_modules(os.path.join(os.path.dirname(__file__), "extensions")) extensions = ExtensionRegistry() # add arguments from extensions to the usage usage = usage.format(extensions.get_options(), extensions.get_option_description()) sys.excepthook = catch_unhandled_exception # add version to the usage arguments = docopt( "radish {0}\n{1}".format(__VERSION__, usage), argv=args, version=__VERSION__ ) # store all arguments to configuration dict in terrain.world setup_config(arguments) # disable colors if necessary if world.config.no_ansi: colorful.disable() else: colorful.use_8_ansi_colors() # load needed extensions extensions.load(world.config) core = Core() if world.config.profile: msg = ( "Command line argument -p/--profile will be removed in a future version. Please " "use -u/--user-data instead." ) warnings.warn(msg, DeprecationWarning, stacklevel=1) feature_files = [] for given_feature in world.config.features: if not os.path.exists(given_feature): raise FeatureFileNotFoundError(given_feature) if os.path.isdir(given_feature): feature_files.extend(utils.recursive_glob(given_feature, "*.feature")) continue feature_files.append(given_feature) # parse tag expressions tag_expression = None if world.config.tags: tag_expression = tagexpressions.parse(world.config.tags) core.parse_features(feature_files, tag_expression) if not core.features or sum(len(f.scenarios) for f in core.features) == 0: utils.console_write(colorful.bold_red("Error: ") + colorful.red("No feature or no scenario specified in at least one of the given feature files") ) if tag_expression: utils.console_write(colorful.red( "You have specified a tag expression. Make sure those are valid and actually yield some Scenarios to run." ) ) return 1 argument_dispatcher = [ ((lambda: world.config.show), show_features), ((lambda: True), run_features), ] # radish command dispatching for to_run, method in argument_dispatcher: if to_run(): return method(core)
def console_writer_after_each_step(self, step): """ Writes the step to the console after it was run :param Step step: the step to write to the console """ if not isinstance(step.parent.parent, Feature): return color_func = self.get_color_func(step.state) line_jump_seq = self.get_line_jump_seq() * ( ((len(step.raw_text) + 3) if step.text else 1) + (len(step.table) + 1 if step.table_header else 0) ) output = "{0} ".format(line_jump_seq) if isinstance(step.parent, ScenarioOutline): # Highlight ScenarioOutline placeholders e.g. '<method>' output += "".join( str( colorful.white(item) if ( self._placeholder_regex.search(item) and item.strip("<>") in step.parent.examples_header ) else color_func(item) ) for item in self._placeholder_regex.split(step.sentence) ) else: output += "{0}{1}".format( self.get_id_sentence_prefix(step, colorful.bold_cyan), color_func(step.sentence), ) if step.text: id_padding = self.get_id_padding(len(step.parent.steps)) output += colorful.bold_white('\n {0}"""'.format(id_padding)) output += colorful.cyan( "".join( [ "\n {0}{1}".format(id_padding, l) for l in step.raw_text ] ) ) output += colorful.bold_white('\n {0}"""'.format(id_padding)) if step.table_header: colored_pipe = colorful.bold_white("|") col_widths = self.get_table_col_widths( [step.table_header] + step.table_data ) # output table header output += "\n {0} {1} {0}".format( colored_pipe, (" {0} ") .format(colored_pipe) .join( str(colorful.white("{1: <{0}}".format(col_widths[i], x))) for i, x in enumerate(step.table_header) ), ) # output table data for row in step.table_data: output += "\n {0} {1} {0}".format( colored_pipe, (" {0} ") .format(colored_pipe) .join( str(color_func("{1: <{0}}".format(col_widths[i], x))) for i, x in enumerate(row) ), ) if step.state == step.State.FAILED: if world.config.with_traceback: output += "\n {0}{1}".format( self.get_id_padding(len(step.parent.steps) - 2), "\n ".join( [ str(colorful.red(l)) for l in step.failure.traceback.split("\n")[:-2] ] ), ) output += "\n {0}{1}: {2}".format( self.get_id_padding(len(step.parent.steps) - 2), colorful.bold_red(step.failure.name), colorful.red(step.failure.reason), ) write(output)
def console_writer_after_each_scenario(self, scenario): """ If the scenario is a ExampleScenario it will write the Examples header :param Scenario scenario: the scenario which was ran. """ output = "" if isinstance(scenario, ScenarioOutline): output += "\n {0}:\n".format( colorful.bold_white(scenario.example_keyword) ) output += colorful.bold_white( " {0}| {1} |".format( self.get_id_padding(len(scenario.scenarios), offset=2), " | ".join( "{1: <{0}}".format(scenario.get_column_width(i), x) for i, x in enumerate(scenario.examples_header) ), ) ) elif isinstance(scenario, ScenarioLoop): output += "\n {0}: {1}".format( colorful.bold_white(scenario.iterations_keyword), colorful.cyan(scenario.iterations), ) elif isinstance(scenario.parent, ScenarioOutline): colored_pipe = colorful.bold_white("|") color_func = self.get_color_func(scenario.state) output += "{0} {1}{2} {3} {2}".format( self.get_line_jump_seq(), self.get_id_sentence_prefix( scenario, colorful.bold_cyan, len(scenario.parent.scenarios) ), colored_pipe, (" {0} ") .format(colored_pipe) .join( str( color_func( "{1: <{0}}".format(scenario.parent.get_column_width(i), x) ) ) for i, x in enumerate(scenario.example.data) ), ) if scenario.state == Step.State.FAILED: failed_step = scenario.failed_step if world.config.with_traceback: output += "\n {0}{1}".format( self.get_id_padding(len(scenario.parent.scenarios)), "\n ".join( [ str(colorful.red(l)) for l in failed_step.failure.traceback.split("\n")[:-2] ] ), ) output += "\n {0}{1}: {2}".format( self.get_id_padding(len(scenario.parent.scenarios)), colorful.bold_red(failed_step.failure.name), colorful.red(failed_step.failure.reason), ) elif isinstance(scenario.parent, ScenarioLoop): colored_pipe = colorful.bold_white("|") color_func = self.get_color_func(scenario.state) output += "{0} {1}{2} {3: <18} {2}".format( self.get_line_jump_seq(), self.get_id_sentence_prefix( scenario, colorful.bold_cyan, len(scenario.parent.scenarios) ), colored_pipe, str(color_func(scenario.iteration)), ) if scenario.state == Step.State.FAILED: failed_step = scenario.failed_step if world.config.with_traceback: output += "\n {0}{1}".format( self.get_id_padding(len(scenario.parent.scenarios)), "\n ".join( [ str(colorful.red(l)) for l in failed_step.failure.traceback.split("\n")[:-2] ] ), ) output += "\n {0}{1}: {2}".format( self.get_id_padding(len(scenario.parent.scenarios)), colorful.bold_red(failed_step.failure.name), colorful.red(failed_step.failure.reason), ) if output: write(output)