From 5ea0b5fd3add5725a788104c32eaf385b550af1a Mon Sep 17 00:00:00 2001 From: Dan Palmer Date: Mon, 1 Mar 2021 16:27:48 +0000 Subject: [PATCH] Lint/format --- src/devdata/engine.py | 10 ++++-- src/devdata/exporting.py | 35 +++++++++++-------- .../commands/devdata_exporter_process.py | 6 ++-- src/devdata/strategies.py | 28 ++++++++++++--- 4 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/devdata/engine.py b/src/devdata/engine.py index fbaef8e..c7218d6 100644 --- a/src/devdata/engine.py +++ b/src/devdata/engine.py @@ -83,10 +83,14 @@ def export_data(django_dbname, only=None, no_update=False): continue model = to_model(app_model_label) - bar.set_description('{} ({})'.format(app_model_label, strategy.name)) + bar.set_description( + "{} ({})".format(app_model_label, strategy.name) + ) if isinstance(strategy, Exportable): - strategy.export_data(django_dbname, model, exporter, no_update, log=bar.write) + strategy.export_data( + django_dbname, model, exporter, no_update, log=bar.write + ) def import_schema(django_dbname): @@ -142,7 +146,7 @@ def import_data(django_dbname): bar = progress(model_strategies) for app_model_label, strategy in bar: model = to_model(app_model_label) - bar.set_description('{} ({})'.format(app_model_label, strategy.name)) + bar.set_description("{} ({})".format(app_model_label, strategy.name)) strategy.import_data(django_dbname, model) diff --git a/src/devdata/exporting.py b/src/devdata/exporting.py index f39a1c0..122164d 100644 --- a/src/devdata/exporting.py +++ b/src/devdata/exporting.py @@ -7,15 +7,18 @@ from tempfile import NamedTemporaryFile, TemporaryDirectory, TemporaryFile from typing import Any, Dict, Iterator, NamedTuple -SEPARATOR_BYTE = b'\x00' +SEPARATOR_BYTE = b"\x00" BLOCK_SIZE = 4096 -ExportRequest = NamedTuple('ExportRequest', ( - ('app_model_label', str), - ('strategy_class', str), - ('strategy_kwargs', Dict[str, Any]), - ('database', str), -)) +ExportRequest = NamedTuple( + "ExportRequest", + ( + ("app_model_label", str), + ("strategy_class", str), + ("strategy_kwargs", Dict[str, Any]), + ("database", str), + ), +) def read_requests(fh: io.BytesIO) -> Iterator[ExportRequest]: @@ -25,7 +28,7 @@ def read_requests(fh: io.BytesIO) -> Iterator[ExportRequest]: left, separator, right = buffer.partition(SEPARATOR_BYTE) if separator: - command_dict = json.loads(left.decode('utf-8')) + command_dict = json.loads(left.decode("utf-8")) yield ExportRequest(**command_dict) buffer = right @@ -41,7 +44,7 @@ def response_writer(fh: io.BytesIO) -> Iterator[io.BytesIO]: class ExportFailed(Exception): def __init__(self, exitcode: int): self.exitcode = exitcode - + def __repr__(self): return "ExportFailed(exitcode={})".format(self.exitcode) @@ -51,8 +54,8 @@ def __init__(self, command: str): self.command = command self._process = None self.buffer = bytes() - - def __enter__(self) -> 'Exporter': + + def __enter__(self) -> "Exporter": self._process = subprocess.Popen( self.command.split(), stdin=subprocess.PIPE, @@ -60,7 +63,7 @@ def __enter__(self) -> 'Exporter': ) self._process.__enter__() return self - + def __exit__(self, *args, **kwargs): self._process.__exit__(*args, **kwargs) @@ -83,7 +86,7 @@ def export( written = 0 with TemporaryDirectory() as tempdir: - request_data = json.dumps(export_request._asdict()).encode('utf-8') + request_data = json.dumps(export_request._asdict()).encode("utf-8") self._process.stdin.write(request_data) self._process.stdin.write(SEPARATOR_BYTE) self._process.stdin.flush() @@ -95,12 +98,14 @@ def export( raise ExportFailed(self._process.returncode) self.buffer += self._process.stdout.read1(BLOCK_SIZE) - data, separator, rest = self.buffer.partition(SEPARATOR_BYTE) + data, separator, rest = self.buffer.partition( + SEPARATOR_BYTE + ) written += output.write(data) self.buffer = rest if separator: break - + temp_file_path.rename(output_path) return written diff --git a/src/devdata/management/commands/devdata_exporter_process.py b/src/devdata/management/commands/devdata_exporter_process.py index bff4406..a21b618 100644 --- a/src/devdata/management/commands/devdata_exporter_process.py +++ b/src/devdata/management/commands/devdata_exporter_process.py @@ -10,7 +10,7 @@ class Command(BaseCommand): help = "Dump the data generated by a given strategy." - + def handle(self, **options): for request in read_requests(sys.stdin.buffer): with response_writer(sys.stdout.buffer) as output: @@ -21,7 +21,9 @@ def handle_request(self, request, output): app_config = apps.get_app_config(app_name) model = app_config.get_model(model_name) - strategy = import_string(request.strategy_class)(**request.strategy_kwargs) + strategy = import_string(request.strategy_class)( + **request.strategy_kwargs + ) if not isinstance(strategy, Exportable): raise CommandError("Strategy not locally exportable") diff --git a/src/devdata/strategies.py b/src/devdata/strategies.py index c08eefc..262bdd4 100644 --- a/src/devdata/strategies.py +++ b/src/devdata/strategies.py @@ -40,7 +40,14 @@ def __init__(self, *args, name, **kwargs): self.name = name - def export_data(self, django_dbname, model, exporter, no_update=False, log=lambda x: None): + def export_data( + self, + django_dbname, + model, + exporter, + no_update=False, + log=lambda x: None, + ): """ Export the data to a directory on disk. `no_update` indicates not to update if there is any data already existing locally. @@ -139,7 +146,7 @@ def export_local(self, django_dbname, model, output): else serializers.get_serializer("json") ) - stream = codecs.getwriter('utf-8')(output) + stream = codecs.getwriter("utf-8")(output) serializer.serialize( queryset.iterator(), @@ -149,7 +156,14 @@ def export_local(self, django_dbname, model, output): stream=stream, ) - def export_data(self, django_dbname, model, exporter, no_update=False, log=lambda x: None): + def export_data( + self, + django_dbname, + model, + exporter, + no_update=False, + log=lambda x: None, + ): app_model_label = to_app_model_label(model) data_file = self.data_file(app_model_label) @@ -157,7 +171,9 @@ def export_data(self, django_dbname, model, exporter, no_update=False, log=lambd return kwargs = self.get_kwargs(model) - klass = "{}.{}".format(self.__class__.__module__, self.__class__.__name__) + klass = "{}.{}".format( + self.__class__.__module__, self.__class__.__name__ + ) self.ensure_dir_exists(app_model_label) @@ -191,7 +207,9 @@ def import_data(self, django_dbname, model): ) self.import_objects(django_dbname, model, objects) except Exception: - print("Failed to import {} ({})".format(app_model_label, self.name)) + print( + "Failed to import {} ({})".format(app_model_label, self.name) + ) raise def import_objects(self, django_dbname, model, objects):