From 97b877c943b5f2b2ab4d13bbc0e421480e5dd0d8 Mon Sep 17 00:00:00 2001 From: Egor fureunoir Gorbunov Date: Fri, 16 May 2025 01:53:19 +0300 Subject: [PATCH] Features: 1) Add support for parsing files directly from zip-encoded streams. Fixes: 1) Correct parsing logic to ensure consistent handling of file objects regardless of source type. Extra: Refactor indentation and reduce duplicate logic for file parsing. --- geo/management/commands/cities.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/geo/management/commands/cities.py b/geo/management/commands/cities.py index ac2e9c6a..7793ea7a 100644 --- a/geo/management/commands/cities.py +++ b/geo/management/commands/cities.py @@ -215,20 +215,31 @@ class Command(BaseCommand): with zipfile.ZipFile(str(filepath)).open(name + ".txt", "r") as f: zip_member = f file_obj = io.TextIOWrapper(zip_member, encoding="utf-8") + + for row in file_obj: + if not row.startswith("#"): + yield dict( + list( + zip( + settings.files[filekey]["fields"], + row.rstrip("\n").split("\t"), + ) + ) + ) else: with open(os.path.join(self.data_dir, filename), encoding="utf-8") as f: file_obj = f - for row in file_obj: - if not row.startswith("#"): - yield dict( - list( - zip( - settings.files[filekey]["fields"], - row.rstrip("\n").split("\t"), + for row in file_obj: + if not row.startswith("#"): + yield dict( + list( + zip( + settings.files[filekey]["fields"], + row.rstrip("\n").split("\t"), + ) + ) ) - ) - ) def parse(self, data): for line in data: