def translate_cmd(scriptname, command, argv): """Translate a single string or .po file of strings.""" if '-' not in argv: # Don't print version stuff if we're reading from stdin. print '%s version %s' % (scriptname, __version__) parser = build_parser( 'usage: %prog tramslate ' '[- | -s STRING <STRING> ... | FILENAME <FILENAME> ...]', description='Translates a string or a .po file into Pirate.', epilog='Note: Translating files is done in-place replacing ' 'the original file.', sections=[ (format_types(), True), (format_pipeline_parts(), True), ]) # FIXME: move printing of available types to epilog. also rename # types to something more accurate like "variable formats". parser.add_option( '-t', '--types', dest='types', help='Comma-separated list of variable types. See Available Types.', metavar='TYPES', default='python') parser.add_option( '-p', '--pipeline', dest='pipeline', help='Translate pipeline. See Available Pipeline Parts.', metavar='PIPELINE', default='html,pirate') parser.add_option( '-s', '--string', action='store_true', dest='strings', help='translates specified string args') (options, args) = parser.parse_args(argv) if not args: parser.print_help() return 1 translator = Translator( options.types.split(','), options.pipeline.split(',')) if options.strings: # Args are strings to be translated for arg in args: print_utf8(translator.translate_string(arg)) elif len(args) == 1 and args[0] == '-': # Read everything from stdin, then translate it print_utf8(translator.translate_string(sys.stdin.read())) else: # Args are filenames for arg in args: translator.translate_file(arg) return 0
def translate(ctx, varformat, pipeline, strings, path): """ Translate a single string or .po file of strings. If you want to pull the string from stdin, use "-". Note: Translating files is done in-place replacing the original file. """ if not (len(path) == 1 and path[0] == '-'): out('dennis version {version}'.format(version=__version__)) if not path: err('Nothing to work on. Use --help for help.') ctx.exit(1) translator = Translator( varformat.split(','), pipeline.split(',')) if strings: # Args are strings to be translated for arg in path: data = translator.translate_string(arg) if PY2: data = data.encode('utf-8') out(data) elif len(path) == 1 and path[0] == '-': # Read everything from stdin, then translate it data = click.get_binary_stream('stdin').read() data = translator.translate_string(data) out(data) else: # Args are filenames for arg in path: translator.translate_file(arg) ctx.exit(0)
def translate(ctx, varformat, pipeline, strings, path): """ Translate a single string or .po file of strings. If you want to pull the string from stdin, use "-". Note: Translating files is done in-place replacing the original file. """ if not (path and path[0] == '-'): # We don't want to print this if they're piping to stdin out('dennis version {version}'.format(version=__version__)) if not path: raise click.UsageError('nothing to work on. Use --help for help.') try: translator = Translator(varformat.split(','), pipeline.split(',')) except InvalidPipeline as ipe: raise click.UsageError(ipe.args[0]) if strings: # Args are strings to be translated for arg in path: data = translator.translate_string(arg) if PY2: data = data.encode('utf-8') out(data) elif path[0] == '-': # Read everything from stdin, then translate it data = click.get_binary_stream('stdin').read() data = translator.translate_string(data) out(data) else: # Check all the paths first for arg in path: if not os.path.exists(arg): raise click.UsageError(u'File {fn} does not exist.'.format( fn=click.format_filename(arg))) for arg in path: click.echo(translator.translate_file(arg)) ctx.exit(0)
def translate(ctx, varformat, pipeline, strings, path): """ Translate a single string or .po file of strings. If you want to pull the string from stdin, use "-". Note: Translating files is done in-place replacing the original file. """ if not (path and path[0] == '-'): # We don't want to print this if they're piping to stdin out('dennis version {version}'.format(version=__version__)) if not path: raise click.UsageError('nothing to work on. Use --help for help.') try: translator = Translator(varformat.split(','), pipeline.split(',')) except InvalidPipeline as ipe: raise click.UsageError(ipe.args[0]) if strings: # Args are strings to be translated for arg in path: data = translator.translate_string(arg) out(data) elif path[0] == '-': # Read everything from stdin, then translate it data = click.get_binary_stream('stdin').read() data = translator.translate_string(data) out(data) else: # Check all the paths first for arg in path: if not os.path.exists(arg): raise click.UsageError('File {fn} does not exist.'.format( fn=click.format_filename(arg))) for arg in path: click.echo(translator.translate_file(arg)) ctx.exit(0)
def translate_cmd(scriptname, command, argv): """Translate a single string or .po file of strings.""" if '-' not in argv: # Don't print version stuff if we're reading from stdin. out('{0} version {1}'.format(scriptname, __version__)) parser = build_parser( 'usage: %prog tramslate ' '[- | -s STRING <STRING> ... | FILENAME <FILENAME> ...]', description='Translates a string or a .po file into Pirate.', epilog='Note: Translating files is done in-place replacing ' 'the original file.', sections=[ (format_vars(), True), (format_pipeline_parts(), True), ]) parser.add_option( '--vars', dest='vars', help=('Comma-separated list of variable types. See Available Variable ' 'Formats.'), metavar='VARS', default='pysprintf,pyformat') parser.add_option( '-p', '--pipeline', dest='pipeline', help='Translate pipeline. See Available Pipeline Parts.', metavar='PIPELINE', default='html,pirate') parser.add_option( '-s', '--string', action='store_true', dest='strings', help='translates specified string args') (options, args) = parser.parse_args(argv) if not args: parser.print_help() return 1 translator = Translator( options.vars.split(','), options.pipeline.split(',')) if options.strings: # Args are strings to be translated for arg in args: data = translator.translate_string(arg) if PY2: data = data.encode('utf-8') print(data) elif len(args) == 1 and args[0] == '-': # Read everything from stdin, then translate it data = translator.translate_string(sys.stdin.read()) if PY2: data = data.encode('utf-8') print(data) else: # Args are filenames for arg in args: translator.translate_file(arg) return 0