def withlines(linenum, poentry_text): """Returns text with line numbers""" start = linenum new_text = [] lines_with_nums = zip(range(start, start + 100), poentry_text.splitlines()) for line_no, line in lines_with_nums: new_text.append(textclass(line_no) + textclass(':') + textclass(line)) return textclass('\n').join(new_text)
def parse_pofile(fn_or_string): """Parses a po file and attaches original poentry blocks When polib parses a pofile, it captures the line number of the start of the block, but doesn't capture the original string for the block. When you call str()/unicode() on the poentry, it "reassembles" the block with textwrapped lines, so it returns something substantially different than the original block. This is problematic if we want to print out the block with the line numbers--one for each line. So this wrapper captures the line numbers and original text for each block and attaches that to the parsed poentries in an attribute named "original" thus allowing us to print the original text with line numbers. """ from polib import _is_file, detect_encoding, io, pofile # This parses the pofile parsed_pofile = pofile(fn_or_string) # Now we need to build a linenumber -> block hash so that we can # accurately print out what was in the pofile because polib will # reassembled what it parsed, but it's not the same. if _is_file(fn_or_string): enc = detect_encoding(fn_or_string, 'pofile') fp = io.open(fn_or_string, 'rt', encoding=enc) else: fp = fn_or_string.splitlines(True) fp = list(fp) entries = list(parsed_pofile) for i, poentry in enumerate(entries): # Grab the lines that make up the poentry. # Note: linenum is 1-based, so we convert it to 0-based. try: lines = fp[poentry.linenum - 1:entries[i + 1].linenum - 1] except IndexError: lines = fp[poentry.linenum - 1:] # Nix blank lines at the end. while lines and not lines[-1].strip(): lines.pop() # Join them and voila! poentry.original = textclass('').join(lines) return parsed_pofile
def lint(ctx, quiet, color, varformat, rules, reporter, errorsonly, path): """ Lints .po/.pot files for issues You can ignore rules on a string-by-string basis by adding an extracted comment "dennis-ignore: <comma-separated-rules>". See documentation for details. """ global TERM if not quiet: out('dennis version {version}'.format(version=__version__)) if not color: TERM = FauxTerminal() linter = Linter(varformat.split(','), rules.split(',')) templatelinter = TemplateLinter(varformat.split(','), rules.split(',')) po_files = [] for item in path: if os.path.isdir(item): for root, dirs, files in os.walk(item): po_files.extend( [os.path.join(root, fn) for fn in files if fn.endswith(('.po', '.pot'))]) else: po_files.append(item) po_files = [os.path.abspath(fn) for fn in po_files if fn.endswith(('.po', '.pot'))] if not po_files: err('Nothing to work on. Use --help for help.') ctx.exit(1) files_to_errors = {} total_error_count = 0 total_warning_count = 0 total_files_with_errors = 0 for fn in po_files: try: if not os.path.exists(fn): raise IOError('File "{fn}" does not exist.'.format(fn=fn)) if fn.endswith('.po'): results = linter.verify_file(fn) else: results = templatelinter.verify_file(fn) except IOError as ioe: # This is not a valid .po file. So mark it as an error. err('>>> Problem opening file: {fn}'.format(fn=fn)) err(repr(ioe)) out('') # FIXME - should we track this separately as an invalid # file? files_to_errors[fn] = (1, 0) total_error_count += 1 continue if errorsonly: # Go through and nix all the non-error LintMessages results = [res for res in results if res.kind == 'err'] # We don't want to print output for files that are fine, so we # update the bookkeeping and move on. if not results: files_to_errors[fn] = (0, 0) continue if not quiet and not reporter: out(TERM.bold_green, '>>> Working on: {fn}'.format(fn=fn), TERM.normal) error_results = [res for res in results if res.kind == 'err'] warning_results = [res for res in results if res.kind == 'warn'] error_count = len(error_results) total_error_count += error_count warning_count = len(warning_results) total_warning_count += warning_count if not quiet: for msg in error_results: if reporter == 'line': out(fn, ':', textclass(msg.poentry.linenum), ':', '0', ':', msg.code, ':', msg.msg) else: out(TERM.bold_red, msg.code, ': ', msg.msg, TERM.normal) out(withlines(msg.poentry.linenum, msg.poentry.original)) out('') if not quiet and not errorsonly: for msg in warning_results: if reporter == 'line': out(fn, ':', textclass(msg.poentry.linenum), ':', '0', ':', msg.code, ':', msg.msg) else: out(TERM.bold_yellow, msg.code, ': ', msg.msg, TERM.normal) out(withlines(msg.poentry.linenum, msg.poentry.original)) out('') files_to_errors[fn] = (error_count, warning_count) if error_count > 0: total_files_with_errors += 1 if not quiet and reporter != 'line': out('Totals') if not errorsonly: out(' Warnings: {warnings:5}'.format(warnings=warning_count)) out(' Errors: {errors:5}\n'.format(errors=error_count)) if len(po_files) > 1 and not quiet and reporter != 'line': out('Final totals') out(' Number of files examined: {count:5}'.format( count=len(po_files))) out(' Total number of files with errors: {count:5}'.format( count=total_files_with_errors)) if not errorsonly: out(' Total number of warnings: {count:5}'.format( count=total_warning_count)) out(' Total number of errors: {count:5}'.format( count=total_error_count)) out('') file_counts = [ (counts[0], counts[1], fn.split(os.sep)[-3], fn.split(os.sep)[-1]) for (fn, counts) in files_to_errors.items() ] # If we're showing errors only, then don't talk about warnings. if errorsonly: header = 'Errors Filename' line = ' {errors:5} {locale} ({fn})' else: header = 'Warnings Errors Filename' line = ' {warnings:5} {errors:5} {locale} ({fn})' file_counts = list(reversed(sorted(file_counts))) printed_header = False for error_count, warning_count, locale, fn in file_counts: if not error_count and not warning_count: continue if not printed_header: out(header) printed_header = True out(line.format( warnings=warning_count, errors=error_count, fn=fn, locale=locale)) # Return 0 if everything was fine or 1 if there were errors. ctx.exit(code=1 if total_error_count else 0)