* Pedantic edits to website/create_code_samples. Make it use plac for interface, remove unnecessary regex, ensure unicode is handled correctly under Python 2.

This commit is contained in:
Matthew Honnibal 2015-10-19 12:56:00 +11:00
parent 726bb648da
commit d51579ffe6

View File

@ -1,8 +1,11 @@
#!/usr/bin/env python
import sys
import re
from __future__ import unicode_literals
import os
import ast
import io
import plac
# cgi.escape is deprecated since py32
try:
@ -11,18 +14,20 @@ except ImportError:
from cgi import escape
src_dirname = sys.argv[1]
dst_dirname = sys.argv[2]
# e.g. python website/create_code_samples tests/website/ website/src/
def main(src_dirname, dst_dirname):
prefix = "test_"
for filename in os.listdir(src_dirname):
match = re.match(re.escape(prefix) + r"(.+)\.py$", filename)
if not match:
if not filename.startswith('test_'):
continue
if not filename.endswith('.py'):
continue
name = match.group(1)
source = open(os.path.join(src_dirname, filename)).readlines()
# Remove test_ prefix and .py suffix
name = filename[6:-3]
with io.open(os.path.join(src_dirname, filename), 'r', encoding='utf8') as file_:
source = file_.readlines()
tree = ast.parse("".join(source))
for root in tree.body:
@ -61,5 +66,10 @@ for filename in os.listdir(src_dirname):
code_filename = "%s.%s" % (name, root.name[len(prefix):])
with open(os.path.join(dst_dirname, code_filename), "w") as f:
with io.open(os.path.join(dst_dirname, code_filename),
"w", encoding='utf8') as f:
f.write(escape("".join(body)))
if __name__ == '__main__':
plac.call(main)