def list_formatters(stream): """ Writes a list of the available formatters and their description to stream. :param stream: Output stream to use. """ formatter_names = sorted(formatters) column_size = compute_words_maxsize(formatter_names) schema = u" %-"+ str(column_size) +"s %s\n" for name in formatter_names: stream.write(schema % (name, formatters[name].description))
def report_tag_counts(self): # -- PREPARE REPORT: ordered_tags = sorted(list(self.tag_counts.keys())) tag_maxsize = compute_words_maxsize(ordered_tags) schema = " @%-" + _text(tag_maxsize) + "s %4d (used for %s)\n" # -- EMIT REPORT: self.stream.write("TAG COUNTS (alphabetically sorted):\n") for tag in ordered_tags: tag_data = self.tag_counts[tag] counts = len(tag_data) details = self.get_tag_count_details(tag_data) self.stream.write(schema % (tag, counts, details)) self.stream.write("\n")
def report_steps_by_type(self): """Show an overview of the existing step implementations per step type. """ # pylint: disable=too-many-branches assert set(self.step_types) == set(self.step_registry.steps.keys()) language = self.config.lang or "en" language_keywords = i18n.languages[language] for step_type in self.step_types: steps = list(self.step_registry.steps[step_type]) if step_type != "step": steps.extend(self.step_registry.steps["step"]) if not steps: continue # -- PREPARE REPORT: For a step-type. step_type_name = step_type.upper() if step_type == "step": step_keyword = "* " step_type_name = "GENERIC" else: # step_keyword = step_type.capitalize() keywords = language_keywords[step_type] if keywords[0] == u"* " or keywords[0] == u"*": # -- CASE: Skip over generic-step keyword and # use next keyword (as default) for this step_type. assert len(keywords) > 1 step_keyword = keywords[1] else: step_keyword = keywords[0] steps_text = [u"%s%s" % (step_keyword, step.pattern) for step in steps] if self.shows_location: max_size = compute_words_maxsize(steps_text) if max_size < self.min_location_column: max_size = self.min_location_column schema = u" %-" + _text(max_size) + "s # %s\n" else: schema = u" %s\n" # -- REPORT: message = "%s STEP DEFINITIONS[%s]:\n" self.stream.write(message % (step_type_name, len(steps))) for step, step_text in zip(steps, steps_text): if self.shows_location: self.stream.write(schema % (step_text, step.location)) else: self.stream.write(schema % step_text) self.stream.write("\n")
def report_tag_counts(self): # -- PREPARE REPORT: ordered_tags = sorted(list(self.tag_counts.keys())) tag_maxsize = compute_words_maxsize(ordered_tags) schema = " @%-" + str(tag_maxsize) + "s %4d (used for %s)\n" # -- EMIT REPORT: self.stream.write("TAG COUNTS (alphabetically sorted):\n") for tag in ordered_tags: tag_data = self.tag_counts[tag] counts = len(tag_data) details = self.get_tag_count_details(tag_data) self.stream.write(schema % (tag, counts, details)) self.stream.write("\n")
def report_tag_counts_by_usage(self): # -- PREPARE REPORT: compare_tag_counts_size = lambda x: len(self.tag_counts[x]) ordered_tags = sorted(list(self.tag_counts.keys()), key=compare_tag_counts_size) tag_maxsize = compute_words_maxsize(ordered_tags) schema = " @%-" + _text(tag_maxsize) + "s %4d (used for %s)\n" # -- EMIT REPORT: self.stream.write("TAG COUNTS (most often used first):\n") for tag in ordered_tags: tag_data = self.tag_counts[tag] counts = len(tag_data) details = self.get_tag_count_details(tag_data) self.stream.write(schema % (tag, counts, details)) self.stream.write("\n")
def report_tag_counts_by_usage(self): # -- PREPARE REPORT: compare = lambda x, y: cmp(len(self.tag_counts[y]), len(self.tag_counts[x])) ordered_tags = sorted(list(self.tag_counts.keys()), compare) tag_maxsize = compute_words_maxsize(ordered_tags) schema = " @%-" + str(tag_maxsize) + "s %4d (used for %s)\n" # -- EMIT REPORT: self.stream.write("TAG COUNTS (most often used first):\n") for tag in ordered_tags: tag_data = self.tag_counts[tag] counts = len(tag_data) details = self.get_tag_count_details(tag_data) self.stream.write(schema % (tag, counts, details)) self.stream.write("\n")
def report_tags_by_locations(self): # -- PREPARE REPORT: locations = set() for tag_elements in self.tag_counts.values(): locations.update([six.text_type(x.location) for x in tag_elements]) location_column_size = compute_words_maxsize(locations) schema = u" %-" + _text(location_column_size) + "s %s\n" # -- EMIT REPORT: self.stream.write("TAG LOCATIONS (alphabetically ordered):\n") for tag in sorted(self.tag_counts.keys()): self.stream.write(" @%s:\n" % tag) for element in self.tag_counts[tag]: info = u"%s: %s" % (element.keyword, element.name) self.stream.write(schema % (element.location, info)) self.stream.write("\n") self.stream.write("\n")
def report_tags_by_locations(self): # -- PREPARE REPORT: locations = set() for tag_elements in list(self.tag_counts.values()): locations.update([str(x.location) for x in tag_elements]) location_column_size = compute_words_maxsize(locations) schema = " %-" + str(location_column_size) + "s %s\n" # -- EMIT REPORT: self.stream.write("TAG LOCATIONS (alphabetically ordered):\n") for tag in sorted(self.tag_counts.keys()): self.stream.write(" @%s:\n" % tag) for element in self.tag_counts[tag]: info = "%s: %s" % (element.keyword, element.name) self.stream.write(schema % (element.location, info)) self.stream.write("\n") self.stream.write("\n")
def report_undefined_steps(self): if not self.undefined_steps: return # -- STEP: Undefined steps. undefined_steps = sorted(self.undefined_steps, key=attrgetter("location")) steps_text = [u" %s %s" % (step.keyword, step.name) for step in undefined_steps] max_size = compute_words_maxsize(steps_text) if max_size < self.min_location_column: max_size = self.min_location_column self.stream.write("\nUNDEFINED STEPS[%d]:\n" % len(steps_text)) schema = u"%-" + _text(max_size) + "s # %s\n" for step, step_text in zip(undefined_steps, steps_text): self.stream.write(schema % (step_text, step.location))
def report_undefined_steps(self): if not self.undefined_steps: return # -- STEP: Undefined steps. compare = lambda x, y: cmp(x.location, y.location) undefined_steps = sorted(self.undefined_steps, compare) steps_text = [" %s %s" % (step.keyword, step.name) for step in undefined_steps] max_size = compute_words_maxsize(steps_text) if max_size < self.min_location_column: max_size = self.min_location_column self.stream.write("\nUNDEFINED STEPS[%d]:\n" % len(steps_text)) schema = "%-" + str(max_size) + "s # %s\n" for step, step_text in zip(undefined_steps, steps_text): self.stream.write(schema % (step_text, step.location))
def write_table(self, table): """ Write a ReST simple table. EXAMPLE: ========================================= ===== ===== ===== ===== Step Definition Given When Then Step ========================================= ===== ===== ===== ===== Given a file named "{filename}" contains Then the file "{filename}" should ... ========================================= ===== ===== ===== ===== :param table: Table to render (as `behave.model.Table`) .. todo:: Column alignments """ assert self.stream # -- STEP: Determine table layout dimensions cols_size = [] separator_parts = [] row_schema_parts = [] for col_index, heading in enumerate(table.headings): column = [str(row[col_index]) for row in table.rows] column.append(heading) column_size = compute_words_maxsize(column) cols_size.append(column_size) separator_parts.append("=" * column_size) row_schema_parts.append("%-" + str(column_size) + "s") separator = " ".join(separator_parts) + "\n" row_schema = " ".join(row_schema_parts) + "\n" self.stream.write("\n") # -- ENSURE: Empty line before table start. self.stream.write(separator) self.stream.write(row_schema % tuple(table.headings)) self.stream.write(separator) for row in table.rows: self.stream.write(row_schema % tuple(row)) self.stream.write(separator) self.stream.write("\n") # -- ENSURE: Empty line after table end.
def write_table(self, table): """ Write a ReST simple table. EXAMPLE: ========================================= ===== ===== ===== ===== Step Definition Given When Then Step ========================================= ===== ===== ===== ===== Given a file named "{filename}" contains Then the file "{filename}" should ... ========================================= ===== ===== ===== ===== :param table: Table to render (as `behave.model.Table`) .. todo:: Column alignments """ assert self.stream # -- STEP: Determine table layout dimensions cols_size = [] separator_parts = [] row_schema_parts = [] for col_index, heading in enumerate(table.headings): column = [six.text_type(row[col_index]) for row in table.rows] column.append(heading) column_size = compute_words_maxsize(column) cols_size.append(column_size) separator_parts.append("=" * column_size) row_schema_parts.append("%-" + _text(column_size) + "s") separator = " ".join(separator_parts) + "\n" row_schema = " ".join(row_schema_parts) + "\n" self.stream.write("\n") # -- ENSURE: Empty line before table start. self.stream.write(separator) self.stream.write(row_schema % tuple(table.headings)) self.stream.write(separator) for row in table.rows: self.stream.write(row_schema % tuple(row)) self.stream.write(separator) self.stream.write("\n") # -- ENSURE: Empty line after table end.
def print_formatters(title=None, stream=None): """Prints the list of available formatters and their description. :param title: Optional title (as string). :param stream: Optional, output stream to use (default: sys.stdout). """ from behave.formatter._registry import format_items from operator import itemgetter if stream is None: stream = sys.stdout if title: stream.write(u"%s\n" % title) format_items = sorted(format_items(resolved=True), key=itemgetter(0)) format_names = [item[0] for item in format_items] column_size = compute_words_maxsize(format_names) schema = u" %-"+ _text(column_size) +"s %s\n" for name, formatter_class in format_items: formatter_description = getattr(formatter_class, "description", "") stream.write(schema % (name, formatter_description))
def report_unused_step_definitions(self): unused_step_definitions = self.select_unused_step_definitions() if not unused_step_definitions: return # -- STEP: Prepare report for unused step definitions. # ORDERING: Sort step definitions by file location. get_location = lambda x: x.location step_definitions = sorted(unused_step_definitions, key=get_location) step_texts = [self.describe_step_definition(step_definition) for step_definition in step_definitions] max_size = compute_words_maxsize(step_texts) if max_size < self.min_location_column-2: max_size = self.min_location_column-2 # -- STEP: Write report. schema = u" %-" + _text(max_size) + "s # %s\n" self.stream.write("UNUSED STEP DEFINITIONS[%d]:\n" % len(step_texts)) for step_definition, step_text in zip(step_definitions, step_texts): self.stream.write(schema % (step_text, step_definition.location))
def print_formatters(title=None, stream=None): """Prints the list of available formatters and their description. :param title: Optional title (as string). :param stream: Optional, output stream to use (default: sys.stdout). """ from behave.formatter._registry import format_items from operator import itemgetter if stream is None: stream = sys.stdout if title: stream.write(u"%s\n" % title) format_items = sorted(format_items(resolved=True), key=itemgetter(0)) format_names = [item[0] for item in format_items] column_size = compute_words_maxsize(format_names) schema = u" %-" + _text(column_size) + "s %s\n" for name, formatter_class in format_items: formatter_description = getattr(formatter_class, "description", "") stream.write(schema % (name, formatter_description))
def report_used_step_definitions(self): # -- STEP: Used step definitions. # ORDERING: Sort step definitions by file location. get_location = lambda x: x[0].location step_definition_items = self.step_usage_database.items() step_definition_items = sorted(step_definition_items, key=get_location) for step_definition, steps in step_definition_items: stepdef_text = self.describe_step_definition(step_definition) steps_text = [u" %s %s" % (step.keyword, step.name) for step in steps] steps_text.append(stepdef_text) max_size = compute_words_maxsize(steps_text) if max_size < self.min_location_column: max_size = self.min_location_column schema = u"%-" + _text(max_size) + "s # %s\n" self.stream.write(schema % (stepdef_text, step_definition.location)) schema = u"%-" + _text(max_size) + "s # %s\n" for step, step_text in zip(steps, steps_text): self.stream.write(schema % (step_text, step.location)) self.stream.write("\n")