From f6f0af4e60bfa55b0445ad5011f81411e52535d8 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Sun, 28 Jun 2020 22:18:29 +0200
Subject: [PATCH] Linting and grouping.

---
 engine-api.py                              |  3 +-
 engine-core.py                             |  6 +--
 modules/base/mail.py                       |  2 +-
 modules/base/models.py                     | 11 ++--
 modules/base/utils.py                      |  3 +-
 modules/cli/padavan.py                     |  2 +-
 modules/cli/redis/adapter.py               |  2 +-
 modules/cli/redis/channels.py              | 39 ++++++++++++++
 modules/cli/redis/messenger.py             |  2 +-
 modules/{base/enum.py => core/channels.py} | 59 +++-------------------
 modules/core/engine.py                     | 11 ++--
 modules/core/events.py                     |  3 +-
 modules/core/liquidsoap/playerclient.py    | 19 +++----
 modules/scheduling/calendar.py             |  2 +-
 modules/scheduling/fallback_manager.py     |  4 +-
 modules/scheduling/scheduler.py            | 19 +++----
 modules/scheduling/types.py                | 56 ++++++++++++++++++++
 17 files changed, 146 insertions(+), 97 deletions(-)
 create mode 100644 modules/cli/redis/channels.py
 rename modules/{base/enum.py => core/channels.py} (65%)
 create mode 100644 modules/scheduling/types.py

diff --git a/engine-api.py b/engine-api.py
index acf5961e..67eaeda5 100644
--- a/engine-api.py
+++ b/engine-api.py
@@ -36,12 +36,11 @@ from flask_restful                  import Api, Resource, abort
 from apispec                        import APISpec
 from apispec.ext.marshmallow        import MarshmallowPlugin
 from apispec_webframeworks.flask    import FlaskPlugin
-# import werkzeug
 from werkzeug.exceptions            import HTTPException, default_exceptions, Aborter
 
 from modules.base.logger            import AuraLogger
 from modules.base.config            import AuraConfig
-from modules.base.models            import AuraDatabaseModel, Schedule, Playlist, PlaylistEntry, PlaylistEntryMetaData, TrackService
+from modules.base.models            import AuraDatabaseModel, Schedule, Playlist, PlaylistEntry, PlaylistEntryMetaData
 
 
 
diff --git a/engine-core.py b/engine-core.py
index 7ce9df6d..c9c636eb 100755
--- a/engine-core.py
+++ b/engine-core.py
@@ -181,7 +181,7 @@ class AuraEngine:
 if __name__ == "__main__":        
 
     engine = AuraEngine()
-    lqs_startup = True
+    start_lqs = True
     lqs_cmd = False
 
     signal.signal(signal.SIGINT, engine.exit_gracefully)
@@ -189,7 +189,7 @@ if __name__ == "__main__":
 
     if len(sys.argv) >= 2:
         if "--without-lqs" in sys.argv:
-            lqs_startup = False
+            start_lqs = False
         if "--get-lqs-command" in sys.argv:
             lqs_cmd = True
         if "--use-test-data" in sys.argv:
@@ -200,4 +200,4 @@ if __name__ == "__main__":
     if lqs_cmd:
         print(engine.get_lqs_cmd(True, True))
     else:
-        engine.startup(lqs_startup)
+        engine.startup(start_lqs)
diff --git a/modules/base/mail.py b/modules/base/mail.py
index d5829fe3..e32c9090 100644
--- a/modules/base/mail.py
+++ b/modules/base/mail.py
@@ -26,7 +26,7 @@ class MailingException(Exception):
     """
     Thrown when some mail cannot be sent.
     """
-    pass
+
 
 
 
diff --git a/modules/base/models.py b/modules/base/models.py
index ccb20f6a..5939a4cc 100644
--- a/modules/base/models.py
+++ b/modules/base/models.py
@@ -25,19 +25,18 @@ import datetime
 import sqlalchemy as sa
 
 from sqlalchemy.ext.declarative         import declarative_base
-from sqlalchemy                         import orm, func
-from sqlalchemy                         import BigInteger, Boolean, Column, DateTime, Integer, String, ForeignKey, ForeignKeyConstraint
+from sqlalchemy                         import orm
+from sqlalchemy                         import BigInteger, Boolean, Column, DateTime, Integer, String, ForeignKey
 from sqlalchemy.orm                     import relationship
-from sqlalchemy.sql.expression          import false, true
-from sqlalchemy.ext.hybrid              import hybrid_property, hybrid_method
+from sqlalchemy.ext.hybrid              import hybrid_property
 
-from sqlalchemy                         import create_engine
 
-from modules.base.enum                  import Channel, ChannelType, PlaylistType
+from modules.scheduling.types           import PlaylistType
 from modules.base.config                import AuraConfig
 from modules.base.utils                 import SimpleUtil, EngineUtil
 
 
+
 # Init Config
 config = AuraConfig()
 
diff --git a/modules/base/utils.py b/modules/base/utils.py
index 873eaec7..34e7133d 100644
--- a/modules/base/utils.py
+++ b/modules/base/utils.py
@@ -21,7 +21,8 @@ import datetime
 import time
 
 from enum import Enum
-from modules.base.enum import Channel, ChannelType, PlaylistType
+from modules.core.channels import ChannelType
+from modules.scheduling.types import PlaylistType
 
 
 
diff --git a/modules/cli/padavan.py b/modules/cli/padavan.py
index c27a267c..737cdb8b 100644
--- a/modules/cli/padavan.py
+++ b/modules/cli/padavan.py
@@ -19,7 +19,7 @@
 
 import json
 
-from modules.base.enum import RedisChannel
+from modules.cli.redis.channels import RedisChannel
 from modules.base.utils import TerminalColors
 from modules.cli.redis.adapter import ClientRedisAdapter, ServerRedisAdapter
 from modules.base.models import AuraDatabaseModel
diff --git a/modules/cli/redis/adapter.py b/modules/cli/redis/adapter.py
index 4c663275..39b14af4 100644
--- a/modules/cli/redis/adapter.py
+++ b/modules/cli/redis/adapter.py
@@ -31,7 +31,7 @@ from modules.cli.redis.messenger import RedisMessenger
 from modules.cli.redis.statestore import RedisStateStore
 # from modules.communication.connection_tester import ConnectionTester
 from modules.base.exceptions import RedisConnectionException
-from modules.base.enum import RedisChannel
+from modules.cli.redis.channels import RedisChannel
 from modules.base.utils import TerminalColors
 
 
diff --git a/modules/cli/redis/channels.py b/modules/cli/redis/channels.py
new file mode 100644
index 00000000..3dba2f4d
--- /dev/null
+++ b/modules/cli/redis/channels.py
@@ -0,0 +1,39 @@
+#
+# Aura Engine (https://gitlab.servus.at/aura/engine)
+#
+# Copyright (C) 2017-2020 - The Aura Engine Team.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+from enum import Enum
+
+
+
+class RedisChannel(Enum):
+    STANDARD = "aura"
+
+    DPE_REPLY = "delete_playlist_entry_reply"
+    FNP_REPLY = "fetch_new_programme_reply"
+    GAP_REPLY = "get_act_programme_reply"
+    GS_REPLY  = "get_status_reply"
+    GCS_REPLY = "get_connection_status_reply"
+    GNF_REPLY = "get_next_file_reply"
+    IPE_REPLY = "insert_playlist_entry_reply"
+    IP_REPLY  = "init_player_reply"
+    TS_REPLY  = "track_service_reply"
+    MPE_REPLY = "move_playlist_entry_reply"
+    PMQ_REPLY = "print_message_queue_reply"
+    RDB_REPLY = "recreate_database_reply"
+    SNF_REPLY = "get_next_file_reply"
diff --git a/modules/cli/redis/messenger.py b/modules/cli/redis/messenger.py
index bbbe7e5d..b6821832 100644
--- a/modules/cli/redis/messenger.py
+++ b/modules/cli/redis/messenger.py
@@ -22,7 +22,7 @@ import logging
 
 
 from modules.cli.redis.statestore import RedisStateStore
-from modules.base.enum import RedisChannel
+from modules.cli.redis.channels import RedisChannel
 
 
 """
diff --git a/modules/base/enum.py b/modules/core/channels.py
similarity index 65%
rename from modules/base/enum.py
rename to modules/core/channels.py
index 2a655aee..dce672c3 100644
--- a/modules/base/enum.py
+++ b/modules/core/channels.py
@@ -21,24 +21,14 @@ from enum import Enum
 
 
 
-class RedisChannel(Enum):
-    STANDARD = "aura"
-
-    DPE_REPLY = "delete_playlist_entry_reply"
-    FNP_REPLY = "fetch_new_programme_reply"
-    GAP_REPLY = "get_act_programme_reply"
-    GS_REPLY  = "get_status_reply"
-    GCS_REPLY = "get_connection_status_reply"
-    GNF_REPLY = "get_next_file_reply"
-    IPE_REPLY = "insert_playlist_entry_reply"
-    IP_REPLY  = "init_player_reply"
-    TS_REPLY  = "track_service_reply"
-    MPE_REPLY = "move_playlist_entry_reply"
-    PMQ_REPLY = "print_message_queue_reply"
-    RDB_REPLY = "recreate_database_reply"
-    SNF_REPLY = "get_next_file_reply"
-
+class TransitionType(Enum):
+    """
+    Types of fade-in and fade-out transition.
+    """
+    INSTANT = "instant"
+    FADE = "fade"
 
+    
 class Channel(Enum):
     """
     Channel name mappings to the Liqidsoap channel IDs
@@ -94,39 +84,6 @@ class ChannelType(Enum):
         return str(self.value["id"])
 
 
-class TransitionType(Enum):
-    """
-    Types of fade-in and fade-out transition.
-    """
-    INSTANT = "instant"
-    FADE = "fade"
-
-
-class PlaylistType(Enum):
-    DEFAULT     = { "id": 0, "name": "default" }    # Default play mode
-    SHOW        = { "id": 1, "name": "show" }       # The first played when some default playlist fails
-    TIMESLOT    = { "id": 2, "name": "timeslot" }   # The second played when the timeslot fallback fails
-    STATION     = { "id": 3, "name": "station" }    # The last played when everything else fails
-
-    @property
-    def id(self):
-        return self.value["id"]
-
-    def __str__(self):
-        return str(self.value["name"])
-
-
-class TimerType(Enum):
-    SWITCH = "switch"
-    FADEIN = "fadein"
-    FADEOUT = "fadeout"
-
-
-class EntryQueueState(Enum):
-    OKAY = "ok"
-    CUT = "cut"
-    OUT_OF_SCHEDULE = "oos"
-
 class EntryPlayState(Enum):
     UNKNOWN = "unknown"
     LOADING = "loading"
@@ -139,4 +96,4 @@ class LiquidsoapResponse(Enum):
     STREAM_STATUS_POLLING = "polling"
     STREAM_STATUS_STOPPED = "stopped"
     STREAM_STATUS_CONNECTED = "connected"
-    
\ No newline at end of file
+    
diff --git a/modules/core/engine.py b/modules/core/engine.py
index 0fa5a229..dee253d3 100644
--- a/modules/core/engine.py
+++ b/modules/core/engine.py
@@ -20,21 +20,18 @@
 import time
 import logging
 
-from urllib.parse               import urlparse, ParseResult
 from contextlib                 import suppress
 from threading                  import Thread
 
 import meta
 
-from modules.base.enum          import ChannelType, Channel, TransitionType, LiquidsoapResponse, EntryPlayState
 from modules.base.utils         import TerminalColors, SimpleUtil as SU, EngineUtil
-from modules.base.exceptions    import LQConnectionError, InvalidChannelException, NoActiveEntryException, LQStreamException, LoadSourceException
-from modules.core.liquidsoap.playerclient import LiquidSoapPlayerClient
-# from modules.core.liquidsoap.recorderclient import LiquidSoapRecorderClient
+from modules.base.exceptions    import LQConnectionError, InvalidChannelException, LQStreamException, LoadSourceException
+from modules.core.channels      import ChannelType, Channel, TransitionType, LiquidsoapResponse, EntryPlayState
 from modules.core.startup       import StartupThread
-from modules.core.state         import PlayerStateService
 from modules.core.events        import EngineEventDispatcher
-
+from modules.core.liquidsoap.playerclient import LiquidSoapPlayerClient
+# from modules.core.liquidsoap.recorderclient import LiquidSoapRecorderClient
 
 
 class SoundSystem():
diff --git a/modules/core/events.py b/modules/core/events.py
index 8fff9894..8c05f027 100644
--- a/modules/core/events.py
+++ b/modules/core/events.py
@@ -21,7 +21,6 @@ import logging
 
 from modules.base.utils         import SimpleUtil as SU
 from modules.base.exceptions    import NoActiveEntryException
-# from modules.base.exceptions    import LQConnectionError, InvalidChannelException, NoActiveEntryException, EngineMalfunctionException, LQStreamException, LoadSourceException
 from modules.base.mail          import AuraMailer
 from modules.plugins.monitor    import AuraMonitor
 from modules.core.state         import PlayerStateService
@@ -203,7 +202,7 @@ class EngineEventDispatcher():
         Callend when no entry is playing
         """
         self.logger.debug("on_idle(..)")
-        self.logger.warn(SU.red("Currently there's nothing playing!"))
+        self.logger.error(SU.red("Currently there's nothing playing!"))
         self.call_event("on_idle", None)
 
 
diff --git a/modules/core/liquidsoap/playerclient.py b/modules/core/liquidsoap/playerclient.py
index f0e3c12d..1f9150e2 100644
--- a/modules/core/liquidsoap/playerclient.py
+++ b/modules/core/liquidsoap/playerclient.py
@@ -17,7 +17,8 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-from modules.base.enum import Channel
+# FIXME Refactor to avoid use of Channel Class here
+from modules.core.channels import Channel
 from modules.core.liquidsoap.client import LiquidSoapClient
 
 
@@ -257,17 +258,17 @@ class LiquidSoapPlayerClient(LiquidSoapClient):
 
 
     # ------------------------------------------------------------------------------------------ #
-    def recorder(self, num, command, *args):
-        if command == "status":
-            return self.recorderstatus(num)
+    # def recorder(self, num, command, *args):
+    #     if command == "status":
+    #         return self.recorderstatus(num)
 
-        if command == "start":
-            return self.recorderstart(num)
+    #     if command == "start":
+    #         return self.recorderstart(num)
 
-        if command == "stop":
-            return self.recorderstop(num)
+    #     if command == "stop":
+    #         return self.recorderstop(num)
 
-        return "LiquidSoapPlayerClient does not understand mixer." + command + str(args)
+    #     return "LiquidSoapPlayerClient does not understand mixer." + command + str(args)
 
 
     # ------------------------------------------------------------------------------------------ #
diff --git a/modules/scheduling/calendar.py b/modules/scheduling/calendar.py
index 1c229206..410d80a2 100644
--- a/modules/scheduling/calendar.py
+++ b/modules/scheduling/calendar.py
@@ -26,7 +26,7 @@ import logging
 
 from datetime import datetime
 
-from modules.base.enum import PlaylistType
+from modules.scheduling.types import PlaylistType
 from modules.base.utils import SimpleUtil
 from modules.base.models import Schedule, Playlist, PlaylistEntry, PlaylistEntryMetaData
 from modules.scheduling.calender_fetcher import CalendarFetcher
diff --git a/modules/scheduling/fallback_manager.py b/modules/scheduling/fallback_manager.py
index 8b9d37de..1e67bd8d 100644
--- a/modules/scheduling/fallback_manager.py
+++ b/modules/scheduling/fallback_manager.py
@@ -27,10 +27,10 @@ import random
 import librosa
 
 from accessify                  import private, protected
-from modules.base.enum          import PlaylistType
+from modules.scheduling.types   import PlaylistType
 from modules.base.utils         import SimpleUtil, EngineUtil
 from modules.base.mail          import AuraMailer
-from modules.base.enum          import ChannelType
+from modules.core.channels      import ChannelType
 
 
 class FallbackManager:
diff --git a/modules/scheduling/scheduler.py b/modules/scheduling/scheduler.py
index 78f10fd4..9088817e 100644
--- a/modules/scheduling/scheduler.py
+++ b/modules/scheduling/scheduler.py
@@ -18,25 +18,26 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-
+import logging
+import threading
 import time
 import json
 import decimal
 import traceback
 import sqlalchemy
-import logging
-import threading
 
 from operator import attrgetter
 from datetime import datetime, timedelta
 
-from modules.base.models import AuraDatabaseModel, Schedule, Playlist, PlaylistEntry, PlaylistEntryMetaData
-from modules.base.exceptions import NoActiveScheduleException, NoActiveEntryException, LoadSourceException
-from modules.base.enum import Channel, ChannelType, TimerType, TransitionType, EntryQueueState, EntryPlayState
-from modules.base.utils import SimpleUtil, TerminalColors, EngineUtil
 from modules.cli.redis.messenger import RedisMessenger
-from modules.scheduling.calendar import AuraCalendarService
-from modules.scheduling.fallback_manager import FallbackManager
+
+from modules.base.utils                     import SimpleUtil, EngineUtil
+from modules.base.models                    import AuraDatabaseModel, Schedule, Playlist
+from modules.base.exceptions                import NoActiveScheduleException, LoadSourceException
+from modules.core.channels                  import ChannelType, TransitionType, EntryPlayState
+from modules.scheduling.types               import EntryQueueState
+from modules.scheduling.calendar            import AuraCalendarService
+from modules.scheduling.fallback_manager    import FallbackManager
 
 
 # FIXME this is probably not needed?
diff --git a/modules/scheduling/types.py b/modules/scheduling/types.py
new file mode 100644
index 00000000..3ba1d8e0
--- /dev/null
+++ b/modules/scheduling/types.py
@@ -0,0 +1,56 @@
+#
+# Aura Engine (https://gitlab.servus.at/aura/engine)
+#
+# Copyright (C) 2017-2020 - The Aura Engine Team.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+from enum import Enum
+
+
+class PlaylistType(Enum):
+    """
+    Types of playlists.
+    """
+    DEFAULT     = { "id": 0, "name": "default" }    # Default play mode
+    SHOW        = { "id": 1, "name": "show" }       # The first played when some default playlist fails
+    TIMESLOT    = { "id": 2, "name": "timeslot" }   # The second played when the timeslot fallback fails
+    STATION     = { "id": 3, "name": "station" }    # The last played when everything else fails
+
+    @property
+    def id(self):
+        return self.value["id"]
+
+    def __str__(self):
+        return str(self.value["name"])
+
+
+# class TimerType(Enum):
+#     """
+#     Types of queue timers.
+#     """
+#     SWITCH = "switch"
+#     FADEIN = "fadein"
+#     FADEOUT = "fadeout"
+
+
+class EntryQueueState(Enum):
+    """
+    Types of playlist entrie behaviours.
+    """
+    OKAY = "ok"
+    CUT = "cut"
+    OUT_OF_SCHEDULE = "oos"
+
-- 
GitLab