{{철도역 정보}} 틀을 {{새 철도역 정보}} 틀로 일괄 치환하는 용도로 사용할 수 있는 스크립트입니다. 철도역이 1개 노선이 있을 때만 스크립트가 반응합니다. 일단은 일일이 질의하게 스크립트를 지정했습니다.
사용하실 때에는 아래의 스크립트를 Pywikibot 디렉토리/scripts/userscripts 디렉토리에서 "station_templates_replace.py"로 저장하신 뒤에
Pywikibot이 있는 디렉토리에서 다음과 같은 코드를 실행시키면 됩니다.
> python pwb.py station_templates_replace
import re
import pywikibot
site = pywikibot.Site('ko', 'libre')
temp_work = pywikibot.Page(site, 'Template:철도역 정보/시작' )
lix = temp_work.getReferences()
for j in lix:
txt = j.text
pat_total = r'\{\{철도역 정보/시작((.|\n)*)\{\{철도역 정보/끝((.|\n)*?)\}\}'
pat_init = r'\{\{철도역 정보/시작((.|\n)*)\{\{철도역 정보/일반'
pat_mid = r'\{\{철도역 정보/일반((.|\n)*)\{\{철도역 정보/끝'
pat_end = r'\{\{철도역 정보/끝((.|\n)*?)\}\}'
pat_link = r'\[\[(.*?)\|(.*?)\]\]'
if re.search(pat_init, txt) and len([x for x in re.finditer(pat_mid, txt)])==1 :
# 철도역 정보/시작에서 추출
ini_mat = re.search(pat_init, txt).group(1)
# 철도역 정보/일반에서 추출.
mid_mat = re.search(pat_mid, txt).group(1)
#변수명 추출
temp_dict = {}
if re.search(r'\|배경색(\s*)=(\s*?)(.*)', ini_mat):
temp_dict['background'] = re.search(r'\|배경색(\s*)=(\s*)(.*)', ini_mat).group(3).replace('\n','')
if re.search(r'\|글자색(\s*)=(\s*)(.*)', ini_mat):
temp_dict['color'] = re.search(r'\|글자색(\s*)=(\s*)(.*)', ini_mat).group(3).replace('\n','')
if re.search(r'\|관할(\s*)=(\s*)(.*)', ini_mat):
temp_dict['manage'] = re.search(r'\|관할(\s*)=(\s*)(.*)', ini_mat).group(3).replace('\n','')
if re.search(r'\|개업일(\s*)=(\s*)(.*)', ini_mat):
temp_dict['open'] = re.search(r'\|개업일(\s*)=(\s*)(.*)', ini_mat).group(3).replace('\n','')
if re.search(r'\|폐지일(\s*)=(\s*)(.*)', ini_mat):
temp_dict['closed'] = re.search(r'\|폐지일(\s*)=(\s*)(.*)', ini_mat).group(3).replace('\n','')
if re.search(r'\|주소(\s*)=(\s*)(.*)', ini_mat):
temp_dict['address'] = re.search(r'\|주소(\s*)=(\s*)(.*)', ini_mat).group(3).replace('\n','')
if re.search(r'\|소재지(\s*)=(\s*)(.*)', ini_mat):
temp_dict['address'] = re.search(r'\|소재지(\s*)=(\s*)(.*)', ini_mat).group(3).replace('\n','')
if re.search(r'\|그림(\s*)=(\s*)(.*)', mid_mat):
temp_dict['picture'] = re.search(r'\|그림(\s*)=(\s*)(.*)', ini_mat).group(3).replace('\n','')
if re.search(r'\|역명(\s*)=(\s*)(.*)', mid_mat):
temp_dict['station'] = re.search(r'\|역명(\s*)=(\s*)(.*)', mid_mat).group(3).replace('\n','')
if re.search(r'\|역번호(\s*)=(\s*)(.*)', mid_mat):
temp_dict['number'] = re.search(r'\|역번호(\s*)=(\s*)(.*)', mid_mat).group(3).replace('\n','')
if re.search(r'\|노선(\s*)=(\s*)(.*)', mid_mat):
temp_dict['line'] = re.search(r'\|노선(\s*)=(\s*)(.*)', mid_mat).group(3).replace('\n','')
# 틀에서 부차 텍스트가 단축형이면 단축 텍스트를 표시명에서 제거함.
if re.search(pat_link, temp_dict['line']):
txt1 = re.search(pat_link, temp_dict['line']).group(1)
txt2 = re.search(pat_link, temp_dict['line']).group(2)
if txt2 in txt1:
temp_dict['line'] = re.sub(pat_link, r'[[\1]]', temp_dict['line'])
#새 틀이 적용됨.
new_text = f'''{{{{새 철도역 정보
|상태 =
|배경색 = {temp_dict.get('background') and temp_dict['background'][0]!='|' and temp_dict['background'] }
|글자색 = {temp_dict.get('color') and temp_dict['color'][0]!='|' and temp_dict['color']}
|관할약칭 =
|역명 = {temp_dict.get('station') and temp_dict['station'][0]!='|' and temp_dict['station']}
|그림 = {temp_dict.get('picture') and temp_dict['picture'][0]!='|' and temp_dict['picture']}
|역번호 = {temp_dict.get('number') and temp_dict['number'][0]!='|' and temp_dict['number']}
|역코드 =
|주소 = {temp_dict.get('address') and (temp_dict['address'][0] not in ['|', '{']) and temp_dict['address']}
|개업일 = {temp_dict.get('open') and temp_dict['open'][0]!='|' and temp_dict['open']}
|폐지일 = {temp_dict.get('closed') and temp_dict['closed'][0]!='|' and temp_dict['closed']}
|관할 = {temp_dict.get('manage') and temp_dict['manage'][0]!='|' and temp_dict['manage']}
|승강장 =
|운임구역 =
|노선 = {temp_dict.get('line') and temp_dict['line'][0]!='|' and temp_dict['line']}
|관련역 =
}}}}'''
new_text = new_text.replace('False', '').replace('None', '')
# 틀 바꾸기 전에 바뀌는 틀 내용 보여주기
print('Old Template : ')
print(re.search(pat_total, txt).group())
print('New Template : ')
print(new_text)
# 탬플릿을 바꿀 지 안 바꿀 지 질의합니다.
ans = input('Change Template to new one? (y to change, q to quit program) ')
if ans == 'y':
txt = re.sub(pat_total, new_text, txt)
j.text = txt
j.save('새 철도역 템플릿 일괄적용.')
elif ans == 'q':
exit()
else:
pass