Selaa lähdekoodia

Moved format recognition to exporter_factory

Vsevolod Levitan 1 vuosi sitten
vanhempi
commit
35b9eecadb
2 muutettua tiedostoa jossa 24 lisäystä ja 9 poistoa
  1. 5 9
      main.py
  2. 19 0
      src/export/exporter_factory.py

+ 5 - 9
main.py

@@ -1,5 +1,6 @@
 import os
 from flask import Flask
+from src.errors.argument_exception import argument_exception
 from src.export.exporter_factory import exporter_factory
 
 from src.settings.settings_manager import settings_manager
@@ -18,15 +19,10 @@ app = Flask(__name__)
 def get_export_format(storage_key: str, format: str):
     global setman, start, strg
 
-    exp = None
-    format = format.lower().strip()
-    fac = exporter_factory()
-    if format == "csv":
-        exp = fac.make_csv()
-    elif format == "json":
-        exp = fac.make_json()
-    elif format == "markdown":
-        exp = fac.make_markdown()
+    try:
+        exp = exporter_factory().create(format)
+    except argument_exception as e:
+        return "Wrong format: " + e.error.error_text, 400
 
     if storage_key not in strg.data:
         return "Key not found", 404

+ 19 - 0
src/export/exporter_factory.py

@@ -1,3 +1,5 @@
+from src.errors.argument_exception import argument_exception
+from src.export.strategies.export import export
 from src.export.exporter import exporter
 from src.export.strategies.csv_export import csv_export
 from src.export.strategies.markdown_export import markdown_export
@@ -5,6 +7,23 @@ from src.export.strategies.json_export import json_export
 
 
 class exporter_factory:
+    __formats = {"csv": csv_export, "markdown": markdown_export, "json": json_export}
+
+    def create(self, format: str):
+        """Создать экспортер заданного формата"""
+
+        format = format.lower().strip()
+
+        if format not in self.__formats:
+            raise argument_exception("Wrong format name")
+
+        return self.make(self.__formats[format])
+
+    def make(self, strat: export):
+        """Создать экспортер с переданной стратегией"""
+
+        return exporter(strat())
+
     def make_csv(self):
         """Создать экспортер формата CSV"""