From f5775049ddcb940f04b63dbc115af904e736f048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E6=A3=AE=E4=B8=AD?= Date: Mon, 22 Jun 2020 11:18:52 +0800 Subject: [PATCH] updata:File operations use utf8 encoding --- aerich/cli.py | 8 ++++---- aerich/migrate.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/aerich/cli.py b/aerich/cli.py index f279c34..ef6669b 100644 --- a/aerich/cli.py +++ b/aerich/cli.py @@ -96,7 +96,7 @@ async def upgrade(ctx: Context): if not exists: async with in_transaction(get_app_connection_name(config, app)) as conn: file_path = os.path.join(Migrate.migrate_location, version) - with open(file_path, "r") as f: + with open(file_path, "r", encoding="utf-8") as f: content = json.load(f) upgrade_query_list = content.get("upgrade") for upgrade_query in upgrade_query_list: @@ -119,7 +119,7 @@ async def downgrade(ctx: Context): file = last_version.version async with in_transaction(get_app_connection_name(config, app)) as conn: file_path = os.path.join(Migrate.migrate_location, file) - with open(file_path, "r") as f: + with open(file_path, "r", encoding="utf-8") as f: content = json.load(f) downgrade_query_list = content.get("downgrade") if not downgrade_query_list: @@ -177,7 +177,7 @@ async def init( parser.set(name, "tortoise_orm", tortoise_orm) parser.set(name, "location", location) - with open(config_file, "w") as f: + with open(config_file, "w", encoding="utf-8") as f: parser.write(f) if not os.path.isdir(location): @@ -218,7 +218,7 @@ async def init_db(ctx: Context, safe): version = await Migrate.generate_version() await Aerich.create(version=version, app=app) - with open(os.path.join(dirname, version), "w") as f: + with open(os.path.join(dirname, version), "w", encoding="utf-8") as f: content = { "upgrade": [schema], } diff --git a/aerich/migrate.py b/aerich/migrate.py index 0cb77dc..b973bbe 100644 --- a/aerich/migrate.py +++ b/aerich/migrate.py @@ -96,7 +96,7 @@ class Migrate: "upgrade": cls.upgrade_operators, "downgrade": cls.downgrade_operators, } - with open(os.path.join(cls.migrate_location, version), "w") as f: + with open(os.path.join(cls.migrate_location, version), "w", encoding="utf-8") as f: json.dump(content, f, indent=2, ensure_ascii=False) return version @@ -154,11 +154,11 @@ class Migrate: """ pattern = rf"(\n)?('|\")({app})(.\w+)('|\")" for i, model_file in enumerate(model_files): - with open(model_file, "r") as f: + with open(model_file, "r", encoding="utf-8") as f: content = f.read() ret = re.sub(pattern, rf"\2{cls.diff_app}\4\5", content) mode = "w" if i == 0 else "a" - with open(old_model_file, mode) as f: + with open(old_model_file, mode, encoding="utf-8") as f: f.write(ret) @classmethod