Skip to content

Instantly share code, notes, and snippets.

@elibixby
Last active August 30, 2016 20:08
Show Gist options
  • Save elibixby/188248643bc8b8cf5ff91b89e35c8df4 to your computer and use it in GitHub Desktop.
Save elibixby/188248643bc8b8cf5ff91b89e35c8df4 to your computer and use it in GitHub Desktop.
Convert files using tf.flags to files using argparse
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import re
import textwrap
FLAGS_TYPES = {
'integer': 'int',
'string': 'str',
'float': 'float',
'boolean': 'bool'
}
flags_regex = re.compile(
'flags.DEFINE_(?P<type>[a-z]*)\(\s*\'(?P<name>.*?)\',\s*(?P<default>.*?),(?P<help>(?:\s*\'(.*?)\'\s*)*)\)'
)
flags_def = re.compile('flags = tf.app.flags')
flags_assign = re.compile('FLAGS = flags.FLAGS')
ARGPARSE_STRING = """parser.add_argument(
\'--{name}\',
type={type},
default={default},
help=\"\"\"{help}\"\"\"
)"""
def argparse_repl(match):
groupdict = match.groupdict()
groupdict['help'] = '\n'.join(textwrap.wrap(' '.join([
help_str.strip().strip('\'').strip('\"')
for help_str in groupdict['help'].split('\n')
])))
groupdict['type'] = FLAGS_TYPES[groupdict['type']]
return ARGPARSE_STRING.format(**groupdict)
def convert(infile, outfile):
with open(infile, 'r') as old, open(outfile, 'w') as new:
old_str = old.read()
new_string = flags_regex.sub(
argparse_repl,
flags_def.sub(
'parser = argparse.ArgumentParser()\n',
flags_assign.sub('FLAGS = parser.parse_args()', old_str)
)
)
new.write(new_string)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--files', nargs='+', type=str)
parser.add_argument('--outdir', default='noflags', type=str)
args = parser.parse_args()
if not os.path.exists(args.outdir):
os.makedirs(args.outdir)
for filename in args.files:
convert(
filename, os.path.join(args.outdir, os.path.split(filename)[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment