From 92b70c22ac79283a22b2ce5b47fe0cca214444c2 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 1 Jun 2016 19:11:05 -0400 Subject: [PATCH] Add more v1 -> v2 migrators --- cli-v1-to-v2 | 144 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 114 insertions(+), 30 deletions(-) diff --git a/cli-v1-to-v2 b/cli-v1-to-v2 index 08435f7..e6b0e9a 100755 --- a/cli-v1-to-v2 +++ b/cli-v1-to-v2 @@ -2,6 +2,7 @@ from __future__ import print_function, unicode_literals import argparse +import io import logging import os import re @@ -52,7 +53,7 @@ def main(sysargs=sys.argv[:]): if args.write: logging.info('Updating %s', filepath) - with open(filepath, 'w') as outfile: + with io.open(filepath, 'w', encoding='utf-8') as outfile: outfile.write(updated_source) else: logging.info('// Updated %s:', filepath) @@ -84,7 +85,7 @@ def _find_candidate_files(paths): def _update_filepath(filepath): - with open(filepath) as infile: + with io.open(filepath, encoding='utf-8') as infile: return _update_source(infile.read()) @@ -96,39 +97,39 @@ def _update_source(source): def _migrator(func): - _MIGRATORS[func.__name__.replace('_migrate_', '')] = func + _MIGRATORS[func.__name__.strip('_')] = func return func @_migrator -def _migrate_command_slice(source): +def _slice_pointer_types(source): return re.sub( - '(?P\\[\\])cli\\.Command{', - _command_slice_repl, source + '(?P\\[\\])cli\\.(?PCommand|Author){', + _slice_pointer_types_repl, source, flags=re.UNICODE ) -def _command_slice_repl(match): - return '{prefix}*cli.Command{{'.format(**match.groupdict()) +def _slice_pointer_types_repl(match): + return '{prefix}*cli.{type}{{'.format(**match.groupdict()) @_migrator -def _migrate_command_literal(source): +def _pointer_type_literal(source): return re.sub( - '(?P\\s+)cli\\.Command{', - _command_literal_repl, source + '(?P\\s+)cli\\.(?PCommand|Author){', + _pointer_type_literal_repl, source, flags=re.UNICODE ) -def _command_literal_repl(match): - return '{prefix}&cli.Command{{'.format(**match.groupdict()) +def _pointer_type_literal_repl(match): + return '{prefix}&cli.{type}{{'.format(**match.groupdict()) @_migrator -def _migrate_slice_types(source): +def _slice_types(source): return re.sub( '&cli\\.(?PIntSlice|StringSlice){(?P[^}]*)}', - _slice_type_repl, source, flags=re.DOTALL + _slice_type_repl, source, flags=re.DOTALL|re.UNICODE ) @@ -137,10 +138,10 @@ def _slice_type_repl(match): @_migrator -def _migrate_flag_literals(source): +def _flag_literals(source): return re.sub( '(?P\\s+)cli\\.(?P\\w+)Flag{', - _flag_literal_repl, source + _flag_literal_repl, source, flags=re.UNICODE ) @@ -149,23 +150,23 @@ def _flag_literal_repl(match): @_migrator -def _migrate_v1_imports(source): +def _v1_imports(source): return re.sub( '"(?:github\\.com|gopkg\\.in)/(?:codegangsta|urfave)/cli(?:\\.v1|)"', - _V2_IMPORT, source + _V2_IMPORT, source, flags=re.UNICODE ) @_migrator -def _migrate_new_exit_error(source): - return re.sub('cli\\.NewExitError', 'cli.Exit', source) +def _new_exit_error(source): + return re.sub('cli\\.NewExitError', 'cli.Exit', source, flags=re.UNICODE) @_migrator -def _migrate_bool_t_flag(source): +def _bool_t_flag(source): return re.sub( 'cli\\.BoolTFlag{(?P[^}]*)}', - _bool_t_flag_repl, source, flags=re.DOTALL + _bool_t_flag_repl, source, flags=re.DOTALL|re.UNICODE ) @@ -174,10 +175,10 @@ def _bool_t_flag_repl(match): @_migrator -def _migrate_context_args_index(source): +def _context_args_index(source): return re.sub( '\\.Args\\(\\)\\[(?P\\d+)\\]', - _context_args_index_repl, source + _context_args_index_repl, source, flags=re.UNICODE ) @@ -186,10 +187,10 @@ def _context_args_index_repl(match): @_migrator -def _migrate_envvar_string(source): +def _envvar_string(source): return re.sub( 'EnvVar:(?P\\s+)"(?P[^"]+)"', - _envvar_string_repl, source + _envvar_string_repl, source, flags=re.UNICODE ) @@ -197,17 +198,20 @@ def _envvar_string_repl(match): return 'EnvVars:{ws}[]string{{{value}}}'.format( value=', '.join([ '"{}"'.format(s) for s in - re.split('\\s*,\\s*', match.groupdict()['string']) + re.split( + '\\s*,\\s*', match.groupdict()['string'], + flags=re.UNICODE + ) ]), **match.groupdict() ) @_migrator -def _migrate_flag_name_stringly(source): +def _flag_name_stringly(source): return re.sub( '(?P\\s+)Name:(?P\\s+)"(?P[^"]+)"', - _flag_name_stringly_repl, source + _flag_name_stringly_repl, source, flags=re.UNICODE ) @@ -237,6 +241,86 @@ def _flag_name_stringly_repl(match): ) +@_migrator +def _commands_opaque_type(source): + return re.sub('cli\\.Commands', '[]*cli.Command', source, flags=re.UNICODE) + + +@_migrator +def _flag_names(source): + return re.sub('\\.GetName\\(\\)', '.Names()[0]', source, flags=re.UNICODE) + + +@_migrator +def _app_categories(source): + return re.sub( + '\\.App\\.Categories\\(\\)', '.App.Categories', + source, flags=re.UNICODE + ) + + +@_migrator +def _range_categories(source): + # XXX: brittle + return re.sub( + '(?P:=\\s+range\\s+)categories(?P[^\\.])', + _range_categories_repl, source, flags=re.UNICODE|re.IGNORECASE + ) + + +def _range_categories_repl(match): + return '{prefix}categories.Categories(){suffix}'.format( + **match.groupdict() + ) + + +@_migrator +def _command_category_commands(source): + # XXX: brittle + return re.sub( + '(?P\\s+category\\.)Commands(?P[^(])', + _command_category_commands_repl, source, flags=re.UNICODE + ) + + +def _command_category_commands_repl(match): + return '{prefix}VisibleCommands(){suffix}'.format(**match.groupdict()) + + +@_migrator +def _context_bool_t(source): + # XXX: probably brittle + return re.sub( + '(?P\\S+)(?:Global|)BoolT\\(', + _context_bool_t_repl, source, flags=re.UNICODE + ) + + +def _context_bool_t_repl(match): + return '!{prefix}Bool('.format(**match.groupdict()) + + +@_migrator +def _context_global_methods(source): + return re.sub( + '\\.Global(?P' \ + 'Bool|Duration|Float64|Generic|Int|IntSlice|String|StringSlice|' \ + 'FlagNames|IsSet|Set' \ + ')\\(', + _context_global_methods_repl, source, flags=re.UNICODE + ) + + +def _context_global_methods_repl(match): + return '.{method}('.format(**match.groupdict()) + + +@_migrator +def _context_parent(source): + # XXX: brittle + return re.sub('\\.Parent\\(\\)', '.Lineage()[1]', source, flags=re.UNICODE) + + def test_migrators(): import difflib