Created
October 21, 2016 19:07
-
-
Save tswast/fa8bd1cbdf84e0777109d217c2271622 to your computer and use it in GitHub Desktop.
Remove None and NoneType from parameter types in docstrings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2016 Google Inc. | |
# | |
# 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. | |
# Run this with: | |
# ag -l 'type ([^:]+): [^\s]+\s*,?\s*(or\s*)?(``None``|``NoneType``|None|NoneType|:class:`NoneType`)' | xargs python ~/Desktop/cleannone.py | |
import codecs | |
import re | |
import shutil | |
import sys | |
def insert_optional_at_first_non_space(line): | |
line_chars = [] | |
for i, c in enumerate(line): | |
if c == ' ': | |
line_chars.append(c) | |
continue | |
end = line[i:] | |
if not end.lower().startswith('(optional') and not end.lower().startswith('optional'): | |
line_chars.extend("(Optional) ") | |
line_chars.extend(end) | |
break | |
return ''.join(line_chars) | |
def main(paths): | |
for path in paths: | |
# Move to a backup location to edit path in-place. | |
shutil.move(path, path + '.bak') | |
with codecs.open(path + '.bak', 'r', 'utf-8') as f, codecs.open(path, 'w', 'utf-8') as outf: | |
lastlinematched = False | |
insertoptional = False | |
for line in f: | |
if insertoptional: | |
line = insert_optional_at_first_non_space(line) | |
insertoptional = False | |
if lastlinematched: | |
# Insert (Optional) in front of param, if not already there. | |
indent, param, end = line.split(':', 2) | |
if len(end.strip()) == 0: | |
# Description starts on the next line, so insert the (Optional) there, instead. | |
insertoptional = True | |
elif not end.lower().startswith(' (optional') and not end.lower().startswith(' optional'): | |
line = indent + ':' + param + ': (Optional)' + end | |
lastlinematched = re.search('type ([^:]+): [^\s]+\s*,?\s*(or\s*)?(``None``|``NoneType``|None|:class:`NoneType`)(Type)?', line) | |
if lastlinematched: | |
# Delete the "or None" part of the line. | |
line = re.sub(',?\s*(or\s*)?(``None``|``NoneType``|None|:class:`NoneType`)(Type)?', '', line) | |
outf.write(line) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment