Use pathlib.Path in setup.py

This commit is contained in:
Lonami Exo 2018-12-15 13:26:18 +01:00
parent da5c171346
commit e71c556ca7

View File

@ -12,9 +12,10 @@ Extra supported commands are:
import itertools import itertools
import json import json
import os
import re import re
import shutil import shutil
from os import chdir
from pathlib import Path
from sys import argv from sys import argv
from setuptools import find_packages, setup from setuptools import find_packages, setup
@ -28,29 +29,28 @@ class TempWorkDir:
self.original = None self.original = None
def __enter__(self): def __enter__(self):
self.original = os.path.abspath(os.path.curdir) self.original = Path('.')
os.chdir(os.path.abspath(os.path.dirname(__file__))) chdir(Path(__file__).parent)
return self return self
def __exit__(self, *args): def __exit__(self, *args):
os.chdir(self.original) chdir(self.original)
GENERATOR_DIR = 'telethon_generator' GENERATOR_DIR = Path('telethon_generator')
LIBRARY_DIR = 'telethon' LIBRARY_DIR = Path('telethon')
ERRORS_IN = os.path.join(GENERATOR_DIR, 'data', 'errors.csv') ERRORS_IN = GENERATOR_DIR / 'data/errors.csv'
ERRORS_OUT = os.path.join(LIBRARY_DIR, 'errors', 'rpcerrorlist.py') ERRORS_OUT = LIBRARY_DIR / 'errors/rpcerrorlist.py'
METHODS_IN = os.path.join(GENERATOR_DIR, 'data', 'methods.csv') METHODS_IN = GENERATOR_DIR / 'data/methods.csv'
TLOBJECT_IN_CORE_TL = os.path.join(GENERATOR_DIR, 'data', 'mtproto_api.tl') TLOBJECT_IN_TLS = [Path(x) for x in GENERATOR_DIR.glob('data/*.tl')]
TLOBJECT_IN_TL = os.path.join(GENERATOR_DIR, 'data', 'telegram_api.tl') TLOBJECT_OUT = LIBRARY_DIR / 'tl'
TLOBJECT_OUT = os.path.join(LIBRARY_DIR, 'tl')
IMPORT_DEPTH = 2 IMPORT_DEPTH = 2
DOCS_IN_RES = os.path.join(GENERATOR_DIR, 'data', 'html') DOCS_IN_RES = GENERATOR_DIR / 'data/html'
DOCS_OUT = 'docs' DOCS_OUT = Path('docs')
def generate(which): def generate(which):
@ -60,13 +60,12 @@ def generate(which):
from telethon_generator.generators import\ from telethon_generator.generators import\
generate_errors, generate_tlobjects, generate_docs, clean_tlobjects generate_errors, generate_tlobjects, generate_docs, clean_tlobjects
layer = find_layer(TLOBJECT_IN_TL) layer = next(filter(None, map(find_layer, TLOBJECT_IN_TLS)))
errors = list(parse_errors(ERRORS_IN)) errors = list(parse_errors(ERRORS_IN))
methods = list(parse_methods(METHODS_IN, {e.str_code: e for e in errors})) methods = list(parse_methods(METHODS_IN, {e.str_code: e for e in errors}))
tlobjects = list(itertools.chain( tlobjects = list(itertools.chain(*(
parse_tl(TLOBJECT_IN_CORE_TL, layer, methods), parse_tl(file, layer, methods) for file in TLOBJECT_IN_TLS)))
parse_tl(TLOBJECT_IN_TL, layer, methods)))
if not which: if not which:
which.extend(('tl', 'errors')) which.extend(('tl', 'errors'))
@ -94,8 +93,8 @@ def generate(which):
which.remove('errors') which.remove('errors')
print(action, 'RPCErrors...') print(action, 'RPCErrors...')
if clean: if clean:
if os.path.isfile(ERRORS_OUT): if ERRORS_OUT.is_file():
os.remove(ERRORS_OUT) ERRORS_OUT.unlink()
else: else:
with open(ERRORS_OUT, 'w', encoding='utf-8') as file: with open(ERRORS_OUT, 'w', encoding='utf-8') as file:
generate_errors(errors, file) generate_errors(errors, file)
@ -104,7 +103,7 @@ def generate(which):
which.remove('docs') which.remove('docs')
print(action, 'documentation...') print(action, 'documentation...')
if clean: if clean:
if os.path.isdir(DOCS_OUT): if DOCS_OUT.is_dir():
shutil.rmtree(DOCS_OUT) shutil.rmtree(DOCS_OUT)
else: else:
generate_docs(tlobjects, methods, layer, DOCS_IN_RES, DOCS_OUT) generate_docs(tlobjects, methods, layer, DOCS_IN_RES, DOCS_OUT)
@ -112,12 +111,11 @@ def generate(which):
if 'json' in which: if 'json' in which:
which.remove('json') which.remove('json')
print(action, 'JSON schema...') print(action, 'JSON schema...')
mtproto = 'mtproto_api.json' json_files = [x.with_suffix('.json') for x in TLOBJECT_IN_TLS]
telegram = 'telegram_api.json'
if clean: if clean:
for x in (mtproto, telegram): for file in json_files:
if os.path.isfile(x): if file.is_file():
os.remove(x) file.unlink()
else: else:
def gen_json(fin, fout): def gen_json(fin, fout):
methods = [] methods = []
@ -131,8 +129,8 @@ def generate(which):
with open(fout, 'w') as f: with open(fout, 'w') as f:
json.dump(what, f, indent=2) json.dump(what, f, indent=2)
gen_json(TLOBJECT_IN_CORE_TL, mtproto) for fin, fout in zip(TLOBJECT_IN_TLS, json_files):
gen_json(TLOBJECT_IN_TL, telegram) gen_json(fin, fout)
if which: if which:
print('The following items were not understood:', which) print('The following items were not understood:', which)
@ -171,7 +169,7 @@ def main():
else: else:
# e.g. install from GitHub # e.g. install from GitHub
if os.path.isdir(GENERATOR_DIR): if GENERATOR_DIR.is_dir():
generate(['tl', 'errors']) generate(['tl', 'errors'])
# Get the long description from the README file # Get the long description from the README file