# -*- coding: utf-8 -*-
"""r364 回填：8 条 Waterloo（计算数学/纯数学/物理 MSc/计算数学 co-op/经济 MA co-op/ECE PhD/CS PhD/统计 PhD）"""
import io, re, shutil, os

HTML = 'index_main.html'

RESEARCH = ('2026-27 秋季国际生费率（Finance 费率表）：研究型硕士 $7,696.00/学期、杂费 $1,014.11/学期（含 HST；'
            'Chemical Engineering $1,022.59、Mechanical and Mechatronic Engineering $1,031.06）；兼读 $3,848.00 + $693.99；'
            'Co-op Fee $836.00；研究型硕士常见最低资助 $12,000/年，国际生另可能获 IMAE；'
            '按学期计费、费率每年由 Board of Governors 批准；项目打包总额官网未公布，不估算')
RESEARCH_COOP1 = RESEARCH + '；本项目含 1 个四个月带薪 co-op 工作学期，工作学期计入注册并收 Co-op Fee'
RESEARCH_COOP2 = RESEARCH + '；本项目含两个带薪工作学期（政府或金融业），工作学期计入注册并收 Co-op Fee'
PHD = ('2026-27 秋季国际生费率（Finance 费率表）：博士研究型 $7,396.00/学期、杂费 $1,014.11/学期（含 HST）；'
       '兼读 $3,698.00 + $693.99；PhD 最低资助 $29,145/年（2026-05-01 起、最多 4 年），PGS 至 $10,000；'
       '按学期计费、费率每年由 Board of Governors 批准；项目打包总额官网未公布，不估算')

LANG_MATH = 'TOEFL iBT 新制 4.5（写作 5、口语 5）；IELTS Academic 7.0（写作 6.5、口语 6.5）'
LANG_ECE = 'TOEFL iBT 新制 4.5（写作 4.5、口语 4、阅读 4、听力 4）；IELTS Academic 6.5（写作 6.0、口语 6.0）'
LANG_CS = ('TOEFL iBT 新制 5（写作 4.5、口语 4）；IELTS Academic 6.5（写作 6.0、口语 6.5）'
           '——Cheriton 单独口径，与数学学院基准（TOEFL 4.5 / IELTS 7.0）不同，照录不换算')

FIELDS = {
 'uw-compmath-mmath': {
   'ddl': 'Fall（次年 9 月）入学，全年仅此一轮：1 月 15 日截止（表原文未标年份；2027 申请季对应 2027-01-15）',
   'tui': RESEARCH,
   'test': 'notreq',
 },
 'uw-puremath-mmath': {
   'ddl': 'Fall（次年 9 月）入学，全年仅此一轮：1 月 7 日截止（表原文未标年份；2027 申请季对应 2027-01-07）'
          '——早于同院计算数学（1 月 15 日）与应用数学（1 月 15 日）',
   'tui': RESEARCH,
   'lang': LANG_MATH,
   'test': 'notreq',
 },
 'uw-phys-msc': {
   'ddl': 'Fall（次年 9 月）1 月 9 日、Winter（次年 1 月）6 月 1 日、Spring（次年 5 月）10 月 1 日'
          '（表原文未标年份；2027 申请季对应 2027-01-09 / 2027-06-01 / 2027-10-01）',
   'tui': RESEARCH,
   'lang': LANG_MATH,
   'test': 'notreq',
 },
 'uw-compmath-mmath-coop': {
   'ddl': 'Fall（次年 9 月）入学，全年仅此一轮：1 月 15 日截止（表原文未标年份；2027 申请季对应 2027-01-15）',
   'tui': RESEARCH_COOP1,
   'dur': '20 个月（含 1 个四个月带薪 co-op 工作学期）· 仅全日制 · 研究论文（Coursework 通道停招）· 仅秋季入学',
   'lang': LANG_MATH,
   'test': 'notreq',
 },
 'uw-econ-ma-coop': {
   'ddl': 'Fall（9 月）入学，全年仅此一轮：2 月 1 日截止（表原文未标年份；2027 申请季对应 2027-02-01）',
   'tui': RESEARCH_COOP2,
   'dur': '20 个月（含两个带薪工作学期）· 仅全日制 · Coursework（可转 Research Paper）· 仅秋季入学',
   'lang': LANG_MATH,
   'test': 'rec',
 },
 'uw-ece-phd': {
   'ddl': 'Fall（9 月）2 月 1 日、Winter（次年 1 月）6 月 1 日、Spring（次年 5 月）10 月 1 日'
          '（表原文未标年份；2027 申请季对应 2027-02-01 / 2027-06-01 / 2027-10-01）',
   'tui': PHD,
   'lang': LANG_ECE,
   'dur': '48 个月全日制 · 三季入学（Fall / Winter / Spring）· Thesis 研究型 · 可兼读 · 录取时须已有导师',
   'test': 'notreq',
 },
 'uw-cs-phd': {
   'ddl': 'Fall（次年 9 月）12 月 1 日、Winter（次年 1 月）6 月 1 日、Spring（次年 5 月）10 月 1 日'
          '（表原文未标年份；2027 申请季对应 2026-12-01 / 2027-06-01 / 2027-10-01）'
          '——秋季截止在前一年 12 月，早于多数院系',
   'tui': PHD,
   'lang': LANG_CS,
   'dur': '48 个月全日制 · 三季入学 · Thesis 研究型 · 可兼读 · 可转 PhD Internship 版',
   'test': 'notreq',
 },
 'uw-statistics-phd': {
   'ddl': 'Fall（次年 9 月）入学，全年仅此一轮：12 月 1 日截止（表原文未标年份；2027 申请季对应 2026-12-01）'
          '——原记录写 1 月 15 日，与本轮官网页不符，已按官网更正',
   'tui': PHD,
   'lang': LANG_MATH,
   'dur': '48 个月全日制 · 仅秋季入学 · Thesis 研究型 · 可兼读 · 可能要求面试',
   'test': 'notreq',
 },
}

html = io.open(HTML, encoding='utf-8').read()
if not os.path.exists(HTML + '.bak_r364'):
    shutil.copy(HTML, HTML + '.bak_r364')

start = html.index('const PROGRAMS=[')
end = html.index('const CATD=[')
seg = html[start:end]

parts = seg.split('{id:"')
out = [parts[0]]
cnt = 0
for p in parts[1:]:
    pid = p.split('"', 1)[0]
    if pid in FIELDS:
        for k, v in FIELDS[pid].items():
            if k == 'test':
                tpat = re.compile(r'test:\{cls:"((?:[^"\\]|\\.)*)"\}')
                tms = list(tpat.finditer(p))
                assert len(tms) == 1, 'test in %s found %d' % (pid, len(tms))
                p = tpat.sub(lambda m: 'test:{cls:"' + v + '"}', p, count=1)
                cnt += 1
                continue
            pat = re.compile(r'(?<![A-Za-z])' + k + r':"((?:[^"\\]|\\.)*)"')
            ms = list(pat.finditer(p))
            assert len(ms) == 1, 'field %s in %s found %d' % (k, pid, len(ms))
            p = pat.sub(lambda m: k + ':"' + v.replace('"', '\\"') + '"', p, count=1)
            cnt += 1
    out.append(p)

new_seg = '{id:"'.join(out)
new_html = html[:start] + new_seg + html[end:]
assert new_html != html
io.open(HTML, 'w', encoding='utf-8', newline='').write(new_html)
print('fields', cnt, 'bytes', len(new_html.encode('utf-8')))
