Skip to content

Commit

Permalink
chore(main): rework global imports
Browse files Browse the repository at this point in the history
main uses many global imports as it is (unfortunately) mixing client-side
and service-side code: move many imports into the few methods where they
are used.

Signed-off-by: Cedric Hombourger <[email protected]>
  • Loading branch information
chombourger committed Jan 21, 2024
1 parent 3d1c6e7 commit 9e74820
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions mtda/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,19 @@

# System imports
import configparser
import gevent
import glob
import importlib
import os
import queue
import shutil
import socket
import subprocess
import sys
import threading
import tempfile
import time
import zmq

# Local imports
from mtda.console.input import ConsoleInput
from mtda.console.logger import ConsoleLogger
from mtda.console.remote import RemoteConsole, RemoteMonitor
from mtda.storage.writer import AsyncImageWriter
import mtda.constants as CONSTS
import mtda.discovery
import mtda.keyboard.controller
import mtda.power.controller
import mtda.scripts
import mtda.video.controller
import mtda.utils
from mtda import __version__
from mtda.support.usb import Composite

try:
www_support = True
Expand Down Expand Up @@ -149,6 +134,7 @@ def _composite_start(self):

result = True
storage = self.storage
from mtda.support.usb import Composite
if storage is not None and storage.variant == 'usbf':
status, _, _ = self.storage_status()
enabled = status == CONSTS.STORAGE.ON_TARGET
Expand All @@ -168,6 +154,7 @@ def _composite_stop(self):
result = None
storage = self.storage
if storage is not None and storage.variant == 'usbf':
from mtda.support.usb import Composite
Composite.remove()

self.mtda.debug(3, "main._composite_stop(): {}".format(result))
Expand Down Expand Up @@ -241,6 +228,7 @@ def console_getkey(self):
return result

def console_init(self):
from mtda.console.input import ConsoleInput
self.console_input = ConsoleInput()
self.console_input.start()

Expand Down Expand Up @@ -353,6 +341,7 @@ def console_remote(self, host, screen):
self.console_output.stop()
if host is not None:
# Create and start our remote console
from mtda.console.remote import RemoteConsole
self.console_output = RemoteConsole(host, self.conport, screen)
self.console_output.start()
else:
Expand Down Expand Up @@ -526,6 +515,7 @@ def monitor_remote(self, host, screen):
if host is not None:
# Create and start our remote console in paused
# (i.e. buffering) state
from mtda.console.remote import RemoteMonitor
self.monitor_output = RemoteMonitor(host, self.conport, screen)
self.monitor_output.pause()
self.monitor_output.start()
Expand Down Expand Up @@ -585,6 +575,7 @@ def power_locked(self, session=None):

def publish(self, topic, data):
if self.socket is not None:
import zmq
with self._socket_lock:
self.socket.send(topic, flags=zmq.SNDMORE)
self.socket.send(data)
Expand Down Expand Up @@ -874,6 +865,7 @@ def storage_write(self, data, session=None):
elif session != self._storage_owner:
raise RuntimeError('shared storage in use')

import queue
try:
if len(data) == 0:
self.mtda.debug(2, "main.storage_write(): "
Expand Down Expand Up @@ -919,6 +911,7 @@ def systemd_configure(self):
# check for changes between the target directory and the
# temporary directory to determine if systemd should be
# reloaded
import shutil
dcmp = dircmp(etcdir, newdir)
diffs = len(dcmp.left_only) # files that were removed
diffs += len(dcmp.right_only) # files that were added
Expand Down Expand Up @@ -984,6 +977,9 @@ def _power_event(self, status):
self.notify(CONSTS.EVENTS.POWER, status)

def _env_for_script(self):
import gevent
import mtda.scripts

variant = 'unknown'
if 'variant' in self.env:
variant = self.env['variant']
Expand All @@ -996,6 +992,8 @@ def _env_for_script(self):
}

def _load_device_scripts(self):
import mtda.scripts

env = self._env_for_script()
mtda.scripts.load_device_scripts(env['variant'], env)
for e in env.keys():
Expand Down Expand Up @@ -1435,6 +1433,8 @@ def load_pastebin_config(self, parser):

def post_configure_storage(self, storage, config, parser):
self.mtda.debug(3, "main.post_configure_storage()")

from mtda.storage.writer import AsyncImageWriter
self._writer = AsyncImageWriter(self, storage)

import atexit
Expand All @@ -1454,6 +1454,7 @@ def load_remote_config(self, parser):
'remote', 'host', fallback=self.remote)

# Attempt to resolve remote using Zeroconf
import mtda.discovery
watcher = mtda.discovery.Watcher(CONSTS.MDNS.TYPE)
ip = watcher.lookup(self.remote)
if ip is not None:
Expand Down Expand Up @@ -1542,6 +1543,7 @@ def notify(self, what, info):

result = None
if self.socket is not None:
import zmq
with self._socket_lock:
self.socket.send(CONSTS.CHANNEL.EVENTS, flags=zmq.SNDMORE)
self.socket.send_string("{} {}".format(what, info))
Expand Down Expand Up @@ -1573,9 +1575,13 @@ def start(self):
return False
self._storage_event(CONSTS.STORAGE.UNLOCKED)

if self.console is not None or self.monitor is not None:
from mtda.console.logger import ConsoleLogger

if self.console is not None:
# Create a publisher
if self.is_server is True:
import zmq
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % self.conport)
Expand Down Expand Up @@ -1634,8 +1640,9 @@ def start(self):
self._www.start()

if self.is_server is True:
from mtda.utils import RepeatTimer
handler = self._session_check
self._session_timer = mtda.utils.RepeatTimer(10, handler)
self._session_timer = RepeatTimer(10, handler)
self._session_timer.start()

# Start from a known state
Expand Down

0 comments on commit 9e74820

Please sign in to comment.