Skip to content

Commit

Permalink
[setuptools packaging] access modules via imports (HelloZeroNet#1969)
Browse files Browse the repository at this point in the history
  • Loading branch information
radfish authored and HelloZeroNet committed Apr 15, 2019
1 parent 6d8f55c commit f6e3a74
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 80 deletions.
9 changes: 6 additions & 3 deletions plugins/Chart/ChartCollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def setInitialLastValues(self, sites):

def getCollectors(self):
collectors = {}
file_server = sys.modules["main"].file_server
import main
file_server = main.file_server
sites = file_server.sites
if not sites:
return collectors
Expand Down Expand Up @@ -100,7 +101,8 @@ def getSiteCollectors(self):
return site_collectors

def getUniquePeers(self):
sites = sys.modules["main"].file_server.sites
import main
sites = main.file_server.sites
return set(itertools.chain.from_iterable(
[site.peers.keys() for site in sites.values()]
))
Expand Down Expand Up @@ -168,7 +170,8 @@ def collectSites(self, sites, collectors, last_values):
def collector(self):
collectors = self.getCollectors()
site_collectors = self.getSiteCollectors()
sites = sys.modules["main"].file_server.sites
import main
sites = main.file_server.sites
i = 0
while 1:
self.collectGlobal(collectors, self.last_values)
Expand Down
2 changes: 1 addition & 1 deletion plugins/Chart/ChartDb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from Config import config
from Db import Db
from Db.Db import Db
import time


Expand Down
2 changes: 1 addition & 1 deletion plugins/Newsfeed/NewsfeedPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re

from Plugin import PluginManager
from Db import DbQuery
from Db.DbQuery import DbQuery
from Debug import Debug


Expand Down
16 changes: 8 additions & 8 deletions plugins/Stats/StatsPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from Config import config
from util import helper
from Debug import Debug
from Db import Db


@PluginManager.registerTo("UiRequest")
Expand Down Expand Up @@ -40,8 +41,8 @@ def actionStats(self):
import gc
import sys
from Ui import UiRequest
from Db import Db
from Crypt import CryptConnection
import main


hpy = None
Expand All @@ -58,7 +59,6 @@ def actionStats(self):
return

s = time.time()
main = sys.modules["main"]

# Style
yield """
Expand Down Expand Up @@ -142,7 +142,8 @@ def actionStats(self):
# Trackers
yield "<br><br><b>Trackers:</b><br>"
yield "<table class='trackers'><tr> <th>address</th> <th>request</th> <th>successive errors</th> <th>last_request</th></tr>"
for tracker_address, tracker_stat in sorted(sys.modules["Site.SiteAnnouncer"].global_stats.items()):
from Site import SiteAnnouncer # importing at the top of the file breaks plugins
for tracker_address, tracker_stat in sorted(SiteAnnouncer.global_stats.items()):
yield self.formatTableRow([
("%s", tracker_address),
("%s", tracker_stat["num_request"]),
Expand Down Expand Up @@ -173,7 +174,7 @@ def actionStats(self):

# Db
yield "<br><br><b>Db</b>:<br>"
for db in sys.modules["Db.Db"].opened_dbs:
for db in Db.opened_dbs:
tables = [row["name"] for row in db.execute("SELECT name FROM sqlite_master WHERE type = 'table'").fetchall()]
table_rows = {}
for table in tables:
Expand Down Expand Up @@ -341,7 +342,7 @@ def actionStats(self):
for obj in objs:
yield " - %.1fkb: %s<br>" % (self.getObjSize(obj, hpy), html.escape(repr(obj)))

from Site import Site
from Site.Site import Site
objs = [obj for obj in gc.get_objects() if isinstance(obj, Site)]
yield "<br>Sites (%s):<br>" % len(objs)
for obj in objs:
Expand Down Expand Up @@ -636,7 +637,6 @@ def benchmark(name, standard):
assert data == data_unpacked, "%s != %s" % (data_unpacked, data)

# Db
from Db import Db
import sqlite3
yield "<br>Db: (version: %s, API: %s)<br>" % (sqlite3.sqlite_version, sqlite3.version)

Expand Down Expand Up @@ -668,12 +668,12 @@ def benchmark(name, standard):

with benchmark("Open x 10", 0.13):
for i in range(10):
db = Db(schema, "%s/benchmark.db" % config.data_dir)
db = Db.Db(schema, "%s/benchmark.db" % config.data_dir)
db.checkTables()
db.close()
yield "."

db = Db(schema, "%s/benchmark.db" % config.data_dir)
db = Db.Db(schema, "%s/benchmark.db" % config.data_dir)
db.checkTables()
import json

Expand Down
3 changes: 2 additions & 1 deletion plugins/Trayicon/TrayiconPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def main(self):
global notificationicon, winfolders
from .lib import notificationicon, winfolders
import gevent.threadpool
import main

self.main = sys.modules["main"]
self.main = main

fs_encoding = sys.getfilesystemencoding()

Expand Down
7 changes: 4 additions & 3 deletions plugins/disabled-Multiuser/MultiuserPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from Config import config
from Plugin import PluginManager
from Crypt import CryptBitcoin
from User import UserManager
from . import UserPlugin

try:
Expand All @@ -16,7 +17,7 @@
@PluginManager.registerTo("UiRequest")
class UiRequestPlugin(object):
def __init__(self, *args, **kwargs):
self.user_manager = sys.modules["User.UserManager"].user_manager
self.user_manager = UserManager.user_manager
super(UiRequestPlugin, self).__init__(*args, **kwargs)

# Create new user and inject user welcome message if necessary
Expand Down Expand Up @@ -135,7 +136,7 @@ def actionUserLogout(self, to):
script += "$('#button_notification').on('click', function() { zeroframe.cmd(\"userLoginForm\", []); });"
self.cmd("injectScript", script)
# Delete from user_manager
user_manager = sys.modules["User.UserManager"].user_manager
user_manager = UserManager.user_manager
if self.user.master_address in user_manager.users:
if not config.multiuser_local:
del user_manager.users[self.user.master_address]
Expand All @@ -149,7 +150,7 @@ def actionUserLoginForm(self, to):

# Login form submit
def responseUserLogin(self, master_seed):
user_manager = sys.modules["User.UserManager"].user_manager
user_manager = UserManager.user_manager
user = user_manager.get(CryptBitcoin.privatekeyToAddress(master_seed))
if not user:
user = user_manager.create(master_seed=master_seed)
Expand Down
7 changes: 4 additions & 3 deletions src/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ def saveValue(self, key, value):

def getServerInfo(self):
from Plugin import PluginManager
import main

info = {
"platform": sys.platform,
Expand All @@ -520,9 +521,9 @@ def getServerInfo(self):
}

try:
info["ip_external"] = sys.modules["main"].file_server.port_opened
info["tor_enabled"] = sys.modules["main"].file_server.tor_manager.enabled
info["tor_status"] = sys.modules["main"].file_server.tor_manager.status
info["ip_external"] = main.file_server.port_opened
info["tor_enabled"] = main.file_server.tor_manager.enabled
info["tor_status"] = main.file_server.tor_manager.status
except:
pass

Expand Down
2 changes: 1 addition & 1 deletion src/Content/ContentDb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
import os

from Db import Db
from Db.Db import Db
from Config import config
from Plugin import PluginManager
from Debug import Debug
Expand Down
3 changes: 0 additions & 3 deletions src/Db/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from .Db import Db
from .DbQuery import DbQuery
from .DbCursor import DbCursor
11 changes: 6 additions & 5 deletions src/Debug/DebugHook.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

def shutdown(reason="Unknown"):
logging.info("Shutting down (reason: %s)..." % reason)
if "file_server" in dir(sys.modules["main"]) and sys.modules["main"].file_server.running:
import main
if "file_server" in dir(main) and main.file_server.running:
try:
if "file_server" in dir(sys.modules["main"]):
gevent.spawn(sys.modules["main"].file_server.stop)
if "ui_server" in dir(sys.modules["main"]):
gevent.spawn(sys.modules["main"].ui_server.stop)
if "file_server" in dir(main):
gevent.spawn(main.file_server.stop)
if "ui_server" in dir(main):
gevent.spawn(main.ui_server.stop)
except Exception as err:
print("Proper shutdown error: %s" % err)
sys.exit(0)
Expand Down
3 changes: 2 additions & 1 deletion src/Peer/Peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def connect(self, connection=None):
elif self.site:
connection_server = self.site.connection_server
else:
connection_server = sys.modules["main"].file_server
import main
connection_server = main.file_server
self.connection = connection_server.getConnection(self.ip, self.port, site=self.site, is_tracker_connection=self.is_tracker_connection)
self.reputation += 1
self.connection.sites += 1
Expand Down
15 changes: 8 additions & 7 deletions src/Site/Site.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ def __init__(self, address, allow_create=True, settings=None):
self.storage = SiteStorage(self, allow_create=allow_create) # Save and load site files
self.content_manager = ContentManager(self)
self.content_manager.loadContents() # Load content.json files
if "main" in sys.modules and "file_server" in dir(sys.modules["main"]): # Use global file server by default if possible
self.connection_server = sys.modules["main"].file_server
else:
if "main" in sys.modules:
sys.modules["main"].file_server = FileServer()
self.connection_server = sys.modules["main"].file_server
if "main" in sys.modules: # import main has side-effects, breaks tests
import main
if "file_server" in dir(main): # Use global file server by default if possible
self.connection_server = main.file_server
else:
self.connection_server = FileServer()
main.file_server = FileServer()
self.connection_server = main.file_server
else:
self.connection_server = FileServer()

self.announcer = SiteAnnouncer(self) # Announce and get peer list from other nodes

Expand Down
2 changes: 1 addition & 1 deletion src/Site/SiteStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import util
from util import SafeRe
from Db import Db
from Db.Db import Db
from Debug import Debug
from Config import config
from util import helper
Expand Down
3 changes: 0 additions & 3 deletions src/Site/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from .Site import Site
from .SiteStorage import SiteStorage
from .SiteAnnouncer import SiteAnnouncer
2 changes: 1 addition & 1 deletion src/Test/TestDbQuery.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from Db import DbQuery
from Db.DbQuery import DbQuery


class TestDbQuery:
Expand Down
2 changes: 1 addition & 1 deletion src/Test/TestSiteDownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from Config import config
from File import FileRequest
from File import FileServer
from Site import Site
from Site.Site import Site
from . import Spy


Expand Down
6 changes: 3 additions & 3 deletions src/Test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def filter(self, record):
config.data_dir = TEST_DATA_PATH # Use test data for unittests
config.initLogging()

from Site import Site
from Site.Site import Site
from Site import SiteManager
from User import UserManager
from File import FileServer
Expand All @@ -104,7 +104,7 @@ def filter(self, record):


def cleanup():
sys.modules["Db.Db"].dbCloseAll()
Db.dbCloseAll()
for dir_path in [config.data_dir, config.data_dir + "-temp"]:
for file_name in os.listdir(dir_path):
ext = file_name.rsplit(".", 1)[-1]
Expand Down Expand Up @@ -388,7 +388,7 @@ def db(request):

if os.path.isfile(db_path):
os.unlink(db_path)
db = Db(schema, db_path)
db = Db.Db(schema, db_path)
db.checkTables()

def stop():
Expand Down
6 changes: 4 additions & 2 deletions src/Tor/TorManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ def start(self):

def setStatus(self, status):
self.status = status
if "ui_server" in dir(sys.modules.get("main", {})):
sys.modules["main"].ui_server.updateWebsocket()
if "main" in sys.modules: # import main has side-effects, breaks tests
import main
if "ui_server" in dir(main):
main.ui_server.updateWebsocket()

def startTor(self):
if sys.platform.startswith("win"):
Expand Down
4 changes: 2 additions & 2 deletions src/Ui/UiRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,8 @@ def actionWebsocket(self):
# Debug last error
def actionDebug(self):
# Raise last error from DebugHook
import sys
last_error = sys.modules["main"].DebugHook.last_error
import main
last_error = main.DebugHook.last_error
if last_error:
raise last_error[0](last_error[1]).with_traceback(last_error[2])
else:
Expand Down
11 changes: 6 additions & 5 deletions src/Ui/UiServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ def run_application(self):
except Exception as err:
logging.error("UiWSGIHandler websocket error: %s" % Debug.formatException(err))
if config.debug: # Allow websocket errors to appear on /Debug
import sys
sys.modules["main"].DebugHook.handleError()
import main
main.DebugHook.handleError()
else: # Standard HTTP request
try:
super(UiWSGIHandler, self).run_application()
except Exception as err:
logging.error("UiWSGIHandler error: %s" % Debug.formatException(err))
if config.debug: # Allow websocket errors to appear on /Debug
import sys
sys.modules["main"].DebugHook.handleError()
import main
main.DebugHook.handleError()

def handle(self):
# Save socket to be able to close them properly on exit
Expand Down Expand Up @@ -160,7 +160,8 @@ def start(self):
self.server.serve_forever()
except Exception as err:
self.log.error("Web interface bind error, must be running already, exiting.... %s" % err)
sys.modules["main"].file_server.stop()
import main
main.file_server.stop()
self.log.debug("Stopped.")

def stop(self):
Expand Down
Loading

0 comments on commit f6e3a74

Please sign in to comment.