Last active
October 18, 2020 02:15
-
-
Save lum1narie/35b6c75735ce70b44185b88dcdd7add6 to your computer and use it in GitHub Desktop.
k1low/tblsが生成したmarkdownからテーブル/カラムの一覧を取得し、.tbls.yamlのcommentの雛形を作るスクリプト
This file contains hidden or 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
import glob | |
import os.path | |
import sys | |
from collections import OrderedDict | |
""" | |
k1low/tblsが生成したmarkdownファイルから、 | |
.tbls.yamlのcommentの雛形を出力する | |
Parameters | |
---------- | |
path : string | |
tblsによって出力されたmarkdownファイルを格納するディレクトリ | |
Returns | |
------- | |
yaml_sentence : string | |
.tbls.yamlに書くべきcomments設定の雛形 | |
""" | |
def generate(path): | |
mds = glob.glob(os.path.join(path, "*.md")) | |
tables = OrderedDict() | |
for md in mds: | |
tablename = os.path.splitext(os.path.basename(md))[0] | |
if tablename == "README": | |
continue | |
tables[tablename] = [] | |
lines = "" | |
with open(md, "r") as f: | |
lines = f.read().split("\n") | |
i = 0 | |
is_in_table = False | |
while (i < len(lines)): | |
s = lines[i] | |
if not is_in_table: | |
if s == "## Columns": | |
is_in_table = True | |
i += (4 - 1) | |
else: | |
if len(s) == 0: | |
break | |
if s[0] == "|": | |
columnname = s.split("|")[1].strip(" ") | |
tables[tablename].append(columnname) | |
i += 1 | |
YAML_TABLE_TEMPLATE = ( | |
" -\n" \ | |
" table: {tablename}\n" \ | |
" tableComment: # TODO:\n" \ | |
" columnComments:\n" \ | |
) | |
YAML_COLUMN_TEMPLATE = " {columnname}: # TODO:" | |
yaml_sentence = ( | |
"comments:\n" + | |
"\n".join( | |
[YAML_TABLE_TEMPLATE.format(tablename=t) + | |
"\n".join( | |
[YAML_COLUMN_TEMPLATE.format(columnname=c) for c in l]) | |
for t, l in tables.items()] | |
) | |
) | |
return yaml_sentence | |
if __name__ == "__main__": | |
md_dir = os.path.dirname(sys.argv[1]) | |
write_file = os.path.join(os.path.dirname(__file__), "base_comment.yaml") | |
with open(write_file, "w") as f: | |
f.write(generate(md_dir)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment