Force UTF-8 encoding when r/w'ing text files

This commit is contained in:
Dan 2018-01-21 16:56:50 +01:00
parent 564725a68e
commit c7d2de3ee9
5 changed files with 28 additions and 28 deletions

View File

@ -70,15 +70,15 @@ def start():
shutil.rmtree("{}/types".format(DESTINATION), ignore_errors=True) shutil.rmtree("{}/types".format(DESTINATION), ignore_errors=True)
shutil.rmtree("{}/functions".format(DESTINATION), ignore_errors=True) shutil.rmtree("{}/functions".format(DESTINATION), ignore_errors=True)
with open("{}/source/auth_key.tl".format(HOME)) as auth, \ with open("{}/source/auth_key.tl".format(HOME), encoding="utf-8") as auth, \
open("{}/source/sys_msgs.tl".format(HOME)) as system, \ open("{}/source/sys_msgs.tl".format(HOME), encoding="utf-8") as system, \
open("{}/source/main_api.tl".format(HOME)) as api: open("{}/source/main_api.tl".format(HOME), encoding="utf-8") as api:
schema = (auth.read() + system.read() + api.read()).splitlines() schema = (auth.read() + system.read() + api.read()).splitlines()
with open("{}/template/class.txt".format(HOME)) as f: with open("{}/template/class.txt".format(HOME), encoding="utf-8") as f:
template = f.read() template = f.read()
with open(notice_path) as f: with open(notice_path, encoding="utf-8") as f:
notice = [] notice = []
for line in f.readlines(): for line in f.readlines():
@ -162,10 +162,10 @@ def start():
init = "{}/__init__.py".format(path) init = "{}/__init__.py".format(path)
if not os.path.exists(init): if not os.path.exists(init):
with open(init, "w") as f: with open(init, "w", encoding="utf-8") as f:
f.write(notice + "\n\n") f.write(notice + "\n\n")
with open(init, "a") as f: with open(init, "a", encoding="utf-8") as f:
f.write("from .{} import {}\n".format(snek(c.name), capit(c.name))) f.write("from .{} import {}\n".format(snek(c.name), capit(c.name)))
sorted_args = sort_args(c.args) sorted_args = sort_args(c.args)
@ -397,7 +397,7 @@ def start():
read_types += "\n " read_types += "\n "
read_types += "{} = Object.read(b)\n ".format(arg_name) read_types += "{} = Object.read(b)\n ".format(arg_name)
with open("{}/{}.py".format(path, snek(c.name)), "w") as f: with open("{}/{}.py".format(path, snek(c.name)), "w", encoding="utf-8") as f:
f.write( f.write(
template.format( template.format(
notice=notice, notice=notice,
@ -414,7 +414,7 @@ def start():
) )
) )
with open("{}/all.py".format(DESTINATION), "w") as f: with open("{}/all.py".format(DESTINATION), "w", encoding="utf-8") as f:
f.write(notice + "\n\n") f.write(notice + "\n\n")
f.write("layer = {}\n\n".format(layer)) f.write("layer = {}\n\n".format(layer))
f.write("objects = {") f.write("objects = {")
@ -436,7 +436,7 @@ def start():
f.write("\n}\n") f.write("\n}\n")
for k, v in namespaces.items(): for k, v in namespaces.items():
with open("{}/{}/__init__.py".format(DESTINATION, k), "a") as f: with open("{}/{}/__init__.py".format(DESTINATION, k), "a", encoding="utf-8") as f:
f.write("from . import {}\n".format(", ".join([i for i in v])) if v else "") f.write("from . import {}\n".format(", ".join([i for i in v])) if v else "")

View File

@ -44,7 +44,7 @@ def generate(source_path, base):
if not i.startswith("__"): if not i.startswith("__"):
build("/".join([path, i]), level=level + 1) build("/".join([path, i]), level=level + 1)
except NotADirectoryError: except NotADirectoryError:
with open(path + "/" + i) as f: with open(path + "/" + i, encoding="utf-8") as f:
p = ast.parse(f.read()) p = ast.parse(f.read())
for node in ast.walk(p): for node in ast.walk(p):
@ -59,7 +59,7 @@ def generate(source_path, base):
os.makedirs(os.path.dirname(destination + "/" + full_path), exist_ok=True) os.makedirs(os.path.dirname(destination + "/" + full_path), exist_ok=True)
with open(destination + "/" + full_path, "w") as f: with open(destination + "/" + full_path, "w", encoding="utf-8") as f:
f.write( f.write(
page_template.format( page_template.format(
title=name, title=name,
@ -94,7 +94,7 @@ def generate(source_path, base):
inner_path = base + "/index" + ".rst" inner_path = base + "/index" + ".rst"
module = "pyrogram.api.{}".format(base) module = "pyrogram.api.{}".format(base)
with open(destination + "/" + inner_path, "w") as f: with open(destination + "/" + inner_path, "w", encoding="utf-8") as f:
f.write( f.write(
toctree.format( toctree.format(
title=k.title(), title=k.title(),
@ -111,10 +111,10 @@ def start():
global page_template global page_template
global toctree global toctree
with open(home + "/template/page.txt") as f: with open(home + "/template/page.txt", encoding="utf-8") as f:
page_template = f.read() page_template = f.read()
with open(home + "/template/toctree.txt") as f: with open(home + "/template/toctree.txt", encoding="utf-8") as f:
toctree = f.read() toctree = f.read()
generate(types_path, types_base) generate(types_path, types_base)

View File

@ -43,7 +43,7 @@ def start():
files = [i for i in os.listdir("{}/source".format(home))] files = [i for i in os.listdir("{}/source".format(home))]
with open(notice_path) as f: with open(notice_path, encoding="utf-8") as f:
notice = [] notice = []
for line in f.readlines(): for line in f.readlines():
@ -51,7 +51,7 @@ def start():
notice = "\n".join(notice) notice = "\n".join(notice)
with open("{}/all.py".format(dest), "w") as f_all: with open("{}/all.py".format(dest), "w", encoding="utf-8") as f_all:
f_all.write(notice + "\n\n") f_all.write(notice + "\n\n")
f_all.write("count = {count}\n\n") f_all.write("count = {count}\n\n")
f_all.write("exceptions = {\n") f_all.write("exceptions = {\n")
@ -66,14 +66,14 @@ def start():
init = "{}/__init__.py".format(dest) init = "{}/__init__.py".format(dest)
if not os.path.exists(init): if not os.path.exists(init):
with open(init, "w") as f_init: with open(init, "w", encoding="utf-8") as f_init:
f_init.write(notice + "\n\n") f_init.write(notice + "\n\n")
with open(init, "a") as f_init: with open(init, "a", encoding="utf-8") as f_init:
f_init.write("from .{}_{} import *\n".format(name.lower(), code)) f_init.write("from .{}_{} import *\n".format(name.lower(), code))
with open("{}/source/{}".format(home, i)) as f_csv, \ with open("{}/source/{}".format(home, i), encoding="utf-8") as f_csv, \
open("{}/{}_{}.py".format(dest, name.lower(), code), "w") as f_class: open("{}/{}_{}.py".format(dest, name.lower(), code), "w", encoding="utf-8") as f_class:
reader = csv.reader(f_csv, delimiter="\t") reader = csv.reader(f_csv, delimiter="\t")
super_class = caml(name) super_class = caml(name)
@ -98,10 +98,10 @@ def start():
sub_classes.append((sub_class, id, message)) sub_classes.append((sub_class, id, message))
with open("{}/template/class.txt".format(home), "r") as f_class_template: with open("{}/template/class.txt".format(home), "r", encoding="utf-8") as f_class_template:
class_template = f_class_template.read() class_template = f_class_template.read()
with open("{}/template/sub_class.txt".format(home), "r") as f_sub_class_template: with open("{}/template/sub_class.txt".format(home), "r", encoding="utf-8") as f_sub_class_template:
sub_class_template = f_sub_class_template.read() sub_class_template = f_sub_class_template.read()
class_template = class_template.format( class_template = class_template.format(
@ -123,10 +123,10 @@ def start():
f_all.write("}\n") f_all.write("}\n")
with open("{}/all.py".format(dest)) as f: with open("{}/all.py".format(dest), encoding="utf-8") as f:
content = f.read() content = f.read()
with open("{}/all.py".format(dest), "w") as f: with open("{}/all.py".format(dest), "w", encoding="utf-8") as f:
f.write(re.sub("{count}", str(count), content)) f.write(re.sub("{count}", str(count), content))
print("Compiling Errors: [100%]") print("Compiling Errors: [100%]")

View File

@ -42,7 +42,7 @@ class Error(Exception):
# TODO: Proper log unknown errors # TODO: Proper log unknown errors
if self.CODE == 520: if self.CODE == 520:
with open("unknown_errors.txt", "a") as f: with open("unknown_errors.txt", "a", encoding="utf-8") as f:
f.write("{}\t{}\t{}\n".format(x.error_code, x.error_message, query_type)) f.write("{}\t{}\t{}\n".format(x.error_code, x.error_message, query_type))
@staticmethod @staticmethod

View File

@ -311,7 +311,7 @@ class Client:
def load_session(self, session_name): def load_session(self, session_name):
try: try:
with open("{}.session".format(session_name)) as f: with open("{}.session".format(session_name), encoding="utf-8") as f:
s = json.load(f) s = json.load(f)
except FileNotFoundError: except FileNotFoundError:
self.dc_id = 1 self.dc_id = 1
@ -326,7 +326,7 @@ class Client:
auth_key = base64.b64encode(self.auth_key).decode() auth_key = base64.b64encode(self.auth_key).decode()
auth_key = [auth_key[i: i + 43] for i in range(0, len(auth_key), 43)] auth_key = [auth_key[i: i + 43] for i in range(0, len(auth_key), 43)]
with open("{}.session".format(self.session_name), "w") as f: with open("{}.session".format(self.session_name), "w", encoding="utf-8") as f:
json.dump( json.dump(
dict( dict(
dc_id=self.dc_id, dc_id=self.dc_id,