def convert_file(filename, _from, _to): py_file = join(get_path('py'), f'{filename}.py') lua_file = join(get_path('lua'), f'{filename}.lua') in_file = py_file if _from == 'py' else lua_file out_file = py_file if _to == 'py' else lua_file content = read(in_file) content = content.replace(before[_from].strip(), '') for pattern, replace in regexps_before[_from]: content = re.sub(pattern, replace, content, flags=re.DOTALL) for pattern, replace in regexps_DOTALL[_from]: content = re.sub(pattern, replace, content, flags=re.DOTALL) for pattern, replace in regexps_MULTILINE[_from]: content = re.sub(pattern, replace, content, flags=re.MULTILINE) for i in range(5): for pattern, replace in regexps_DOTALL[_from]: content = re.sub(pattern, replace, content, flags=re.DOTALL) for pattern, replace in regexps_after[_from]: content = re.sub(pattern, replace, content, flags=re.DOTALL) content = f"{before[_to]}\n\n{content.strip()}\n".lstrip() write(out_file, content)
def convert_file(dev, file, _from, _to, out): dev_str = 'dev' if dev else 'prod' py_file = get_path(dev, 'py', file, out=out) lua_file = get_path(dev, 'lua', file, out=out) in_file = py_file if _from == 'py' else lua_file out_file = py_file if _to == 'py' else lua_file content = read(in_file) content = \ content.replace(before[_from].replace('{dev}', dev_str).strip(), '') for pattern, replace in regexps_before[_from]: content = re.sub(pattern, replace, content, flags=re.DOTALL) for pattern, replace in regexps_DOTALL[_from]: content = re.sub(pattern, replace, content, flags=re.DOTALL) for pattern, replace in regexps_MULTILINE[_from]: pattern = pattern.replace('{dev}', dev_str) replace = replace.replace('{dev}', dev_str) content = re.sub(pattern, replace, content, flags=re.MULTILINE) for i in range(5): for pattern, replace in regexps_DOTALL[_from]: content = re.sub(pattern, replace, content, flags=re.DOTALL) for pattern, replace in regexps_after[_from]: content = re.sub(pattern, replace, content, flags=re.DOTALL) content = \ f"{before[_to].replace('{dev}', dev_str)}\n\n" \ f"{content.strip()}\n".lstrip() write(out_file, content)
def download_tpls(dev): path = get_path(dev, 'tpl', '', root=True) print(f'Скачиваю шаблоны в папку:\n {path}\nШаблоны:') for file in tpl_files: title = get_tpl_title(file, dev) filename = get_path(dev, 'tpl', file, '') download_page(title, filename)
def download_docs(dev): path = get_path(dev, 'docs', '', root=True) print(f'Скачиваю документацию в папку:\n {path}\nШаблоны:') files = declension_files + testcases_files for file in files: title = get_docs_title(file, dev) filename = get_path(dev, 'docs', file, '') download_page(title, filename)
def clear_out_dir(dev, lang): for file in (declension_files + testcases_files): # Копирование результата в `ru.out`: out_file = get_path(dev, lang, file, out=True) ensure_parent_dir(out_file) copy(get_path(dev, lang, file, out=False), out_file) if not compare_dir(dev, lang): print(f'Ошибка: папки `{lang}` не синхронизированы.') return
def download_lua(dev, testcases=False): if not compare_dir(dev, 'lua'): print('Ошибка: папки `lua` не синхронизированы.') return path = get_path(dev, 'lua', '', root=True) print(f'Скачиваю lua-модули в папку:\n {path}\nМодули:') files = testcases_files if testcases else declension_files for file in files: title = get_module_title(file, dev) filename = get_path(dev, 'lua', file, '') download_page(title, filename)
def compare_dir(lang): path = get_path(lang) if not compare(f'{path}/noun.{lang}', f'{path}/noun.out.{lang}'): return False for file in listdir(f'{path}/libs'): if file in ['__pycache__']: continue if not compare(f'{path}/libs/{file}', f'{path}/libs.out/{file}'): return False return True
def download(dev=True): if not compare_dir('lua'): print('Ошибка: папки `lua` не синхронизированы.') return path = get_path('lua') print(f'Скачиваю lua-модули в папку:\n {path}\nМодули:') for file in files: title = get_module_title(file, dev) download_module(title, f'{path}/{file}.lua')
def convert_dir(dev, _from, _to): if not compare_dir(dev, _to): print(f'Ошибка: папки `{_to}` не синхронизированы до конвертации.') return for file in declension_files: # Прямое преобразование: convert_file(dev, file, _from, _to, out=False) # Копирование результата в `ru.out`: in_file = get_path(dev, _to, file, out=False) out_file = get_path(dev, _to, file, out=True) write(out_file, '') # чтобы создать папки, если их нет copy(in_file, out_file) # Обратное преобразование для .out: convert_file(dev, file, _to, _from, out=True) if not compare_dir(dev, _from): print( f'Ошибка: папки `{_from}` не синхронизированы после конвертации.') return
def clear_out_dir(lang): py_path = get_path(lang) for module in files: active = module.replace('[.out]', '') out = module.replace('[.out]', '.out') # Копирование результата в `libs.out`: copy(join(py_path, f'{active}.{lang}'), join(py_path, f'{out}.{lang}')) if not compare_dir(lang): print(f'Ошибка: папки `{lang}` не синхронизированы.') return
def upload(dev, ru_noun_version, inflection_version, desc): if not compare_dir('lua'): print('Ошибка: папки `lua` не синхронизированы.') return desc = process_desc(dev, desc) path = get_path('lua') print(f'Загружаю lua-модули в ВС:') for file in files: title = get_module_title(file, dev) print(f'- {title} - ', end='') file = file.replace('[.out]', '.out') if save_page(title, read_file(dev, f'{path}/{file}.lua'), f'v{ru_noun_version}: {desc}'): print('OK') dev_prefix = 'User:Vitalik/' if dev else '' title = f'Module:{dev_prefix}inflection/tools' path = get_path('lua', noun=False) save_page(title, read_file(dev, f'{path}/tools.lua'), f'v{inflection_version}: {desc}')
def upload_docs(version, desc, dev): desc = process_desc(dev, version, desc) print(f'Загружаю документацию в ВС:') files = declension_files + testcases_files for file in files: title = get_docs_title(file, dev) print(f'- {title} - ', end='') path = get_path(dev, 'docs', file, out=False) if not exists(path): print('Not exist') continue if save_page(title, read_file(dev, path), f'v{version}: {desc}'): print('OK')
def upload_lua(version, desc, dev, testcases=False): if not compare_dir(dev, 'lua'): print('Ошибка: папки `lua` не синхронизированы.') return desc = process_desc(dev, version, desc) print(f'Загружаю lua-модули в ВС:') files = testcases_files if testcases else declension_files for file in files: title = get_module_title(file, dev) print(f'- {title} - ', end='') path = get_path(dev, 'lua', file, out=True) if save_page(title, read_file(dev, path), f'v{version}: {desc}'): print('OK')
def convert_dir(_from, _to): if not compare_dir(_to): print(f'Ошибка: папки `{_to}` не синхронизированы.') return py_path = get_path(_to) for module in files: active = module.replace('[.out]', '') out = module.replace('[.out]', '.out') # Прямое преобразование: convert_file(active, _from, _to) # Копирование результата в `libs.out`: copy(join(py_path, f'{active}.{_to}'), join(py_path, f'{out}.{_to}')) # Обратное преобразование для .out: convert_file(out, _to, _from) if not compare_dir(_from): print(f'Ошибка: папки `{_from}` не синхронизированы.') return
def compare_dir(dev, lang): for file in declension_files: if not compare(get_path(dev, lang, file, out=False), get_path(dev, lang, file, out=True)): return False return True