Przeglądaj źródła

Split to module and add logger

Cixo 1 rok temu
rodzic
commit
9a2b41940e

+ 4 - 67
core.py

@@ -1,72 +1,9 @@
-import gi
-gi.require_version("Gtk", "3.0")
-
-from gi.repository import Gtk
-
-class main_window(Gtk.Window):
-    def __init__(self):
-        super().__init__(title = "CxNewsletter JSON creator")
-        
-        self.connect("destroy", Gtk.main_quit)
-
-        self.content = Gtk.Grid()
-        self.content.set_column_spacing(4)
-        self.content.set_row_spacing(4)
-        self.content.set_column_homogeneous(False)
-        self.add(self.content)
-        
-        self.input = Gtk.FileChooserButton()
-        self.input.set_width_chars(20)
-        self.input.set_title("Select XLSX file")
-        self.content.attach(self.input, 0, 0, 2, 1)
-
-        input_label = Gtk.Label()
-        input_label.set_label("Select XLSX")
-        self.content.attach(input_label, 0, 1, 2, 1)
-
-        self.output = Gtk.FileChooserButton()
-        self.output.set_width_chars(20)
-        self.input.set_title("Select output JSON")
-        self.content.attach(self.output, 2, 0, 2, 1)
-
-        output_label = Gtk.Label()
-        output_label.set_label("Select JSON")
-        self.content.attach(output_label, 2, 1, 2, 1)
-
-        self.convert = Gtk.Button()
-        self.convert.set_label("Convert")
-        self.content.attach(self.convert, 0, 2, 4, 1)    
-
-        name_column = Gtk.Label()
-        name_column.set_width_chars(40)
-        name_column.set_xalign(1)
-        name_column.set_label("Select column with names and surnames")
-        self.content.attach(name_column, 6, 0, 1, 1)
-
-        self.name_column = Gtk.ComboBoxText()
-        self.content.attach(self.name_column, 7, 0, 1, 1)
-
-        email_column = Gtk.Label()
-        email_column.set_width_chars(40)
-        email_column.set_xalign(1)
-        email_column.set_label("Select column with e-mails")
-        self.content.attach(email_column, 6, 1, 1, 1)
-
-        self.email_column = Gtk.ComboBoxText()
-        self.email_column.set_popup_fixed_width(200)
-        self.content.attach(self.email_column, 7, 1, 1, 1)
-
-        phone_column = Gtk.Label()
-        phone_column.set_width_chars(40)
-        phone_column.set_xalign(1)
-        phone_column.set_label("Select column with phone numbers")
-        self.content.attach(phone_column, 6, 2, 1, 1)
-        
-        self.phone_column = Gtk.ComboBoxText()
-        self.content.attach(self.phone_column, 7, 2, 1, 1)
+from cx_newsletter_importer import ui
+from cx_newsletter_importer.gtk import Gtk
+from cx_newsletter_importer import logger
 
 if __name__ == "__main__":
-    app = main_window()
+    app = ui.app(logger.stdio_logger())
     app.show_all()
 
     Gtk.main()

+ 9 - 0
cx_newsletter_importer/__init__.py

@@ -0,0 +1,9 @@
+__all__ = [
+    "ui",
+    "gtk",
+    "logger"
+]
+
+from . import ui
+from . import gtk
+from . import logger

+ 4 - 0
cx_newsletter_importer/gtk.py

@@ -0,0 +1,4 @@
+import gi
+gi.require_version("Gtk", "3.0")
+
+from gi.repository import Gtk

+ 69 - 0
cx_newsletter_importer/logger.py

@@ -0,0 +1,69 @@
+from abc import ABC, abstractmethod
+
+class logger(ABC):
+    def __init__(self):
+        pass
+    
+    @abstractmethod
+    def info(self, content):    
+        pass
+
+    @abstractmethod
+    def warning(self, content):
+        pass
+
+    @abstractmethod
+    def error(self, content):
+        pass
+
+class stdio_logger(logger): 
+    def info(self, content):
+        print("[INFO]: " + str(content))
+
+    def warning(self, content):
+        print("[WARNING]: " + str(content))
+
+    def error(self, content):
+        print("[ERROR]: " + str(content))
+
+class dummy_logger(logger):
+    def info(self, content):
+        pass
+
+    def warning(self, content):
+        pass
+
+    def error(self, content):
+        pass
+
+class file_logger(logger):
+    def __init__(self, file):
+        try:
+            self.file = open(file, "w")
+        except:
+            print("Can not open log file: \"" + file + "\"")
+            exit(-1)
+
+    def __del__(self):
+        self.file.close()
+
+    def info(self, content):
+        try:
+            self.file.write("[INFO]: " + str(content) + "\n")
+        except:
+            print("Can not write to log file!")
+            exit(-1)
+
+    def warning(self, content): 
+        try:
+            self.file.write("[WARNING]: " + str(content) + "\n")
+        except:
+            print("Can not write to log file!")
+            exit(-1)
+
+    def error(self, content):   
+        try:
+            self.file.write("[ERROR]: " + str(content) + "\n")
+        except:
+            print("Can not write to log file!")
+            exit(-1)

+ 73 - 0
cx_newsletter_importer/ui.py

@@ -0,0 +1,73 @@
+from .gtk import Gtk
+
+class app(Gtk.Window):
+    def __init__(self, logger):
+        self.logger = logger
+        super().__init__(title = "CxNewsletter JSON creator")
+        
+        self.connect("destroy", Gtk.main_quit)
+
+        self.content = Gtk.Grid()
+        self.content.set_column_spacing(4)
+        self.content.set_row_spacing(4)
+        self.content.set_column_homogeneous(False)
+        self.add(self.content)
+        
+        self.input = Gtk.FileChooserButton()
+        self.input.set_width_chars(20)
+        self.input.set_title("Select XLSX file")
+        self.content.attach(self.input, 0, 0, 2, 1)
+
+        input_label = Gtk.Label()
+        input_label.set_label("Select XLSX")
+        self.content.attach(input_label, 0, 1, 2, 1)
+
+        self.output = Gtk.FileChooserButton()
+        self.output.set_width_chars(20)
+        self.input.set_title("Select output JSON")
+        self.content.attach(self.output, 2, 0, 2, 1)
+
+        output_label = Gtk.Label()
+        output_label.set_label("Select JSON")
+        self.content.attach(output_label, 2, 1, 2, 1)
+
+        self.convert = Gtk.Button()
+        self.convert.set_label("Convert")
+        self.convert.connect("clicked", self.convert_clicked)
+        self.content.attach(self.convert, 0, 2, 4, 1)    
+
+        name_column = Gtk.Label()
+        name_column.set_width_chars(40)
+        name_column.set_xalign(1)
+        name_column.set_label("Select column with names and surnames")
+        self.content.attach(name_column, 6, 0, 1, 1)
+
+        self.name_column = Gtk.ComboBoxText()
+        self.content.attach(self.name_column, 7, 0, 1, 1)
+
+        email_column = Gtk.Label()
+        email_column.set_width_chars(40)
+        email_column.set_xalign(1)
+        email_column.set_label("Select column with e-mails")
+        self.content.attach(email_column, 6, 1, 1, 1)
+
+        self.email_column = Gtk.ComboBoxText()
+        self.email_column.set_popup_fixed_width(200)
+        self.content.attach(self.email_column, 7, 1, 1, 1)
+
+        phone_column = Gtk.Label()
+        phone_column.set_width_chars(40)
+        phone_column.set_xalign(1)
+        phone_column.set_label("Select column with phone numbers")
+        self.content.attach(phone_column, 6, 2, 1, 1)
+        
+        self.phone_column = Gtk.ComboBoxText()
+        self.content.attach(self.phone_column, 7, 2, 1, 1)
+        
+        self.logger.info("App window created.")
+
+        
+    def convert_clicked(self, trigger):
+        self.logger.info("Convert button clicked.")
+
+