From 481d70eff857a09da381ecd6f6efe0cb38cc417e Mon Sep 17 00:00:00 2001 From: James Lu Date: Mon, 20 Jun 2016 18:18:54 -0700 Subject: [PATCH] New import paths for properly installed pylinkirc --- classes.py | 6 ++---- conf.py | 2 +- coreplugin.py | 7 ++----- log.py | 4 ++-- plugins/bots.py | 9 ++------- plugins/changehost.py | 11 ++--------- plugins/commands.py | 8 ++------ plugins/ctcp.py | 8 ++------ plugins/example.py | 10 ++-------- plugins/exec.py | 9 ++------- plugins/fantasy.py | 9 ++------- plugins/games.py | 9 ++------- plugins/networks.py | 10 ++-------- plugins/opercmds.py | 9 ++------- plugins/relay.py | 9 ++------- plugins/servprotect.py | 8 ++------ protocols/hybrid.py | 11 ++++------- protocols/inspircd.py | 12 ++++-------- protocols/nefarious.py | 10 +++------- protocols/ts6.py | 12 ++++-------- protocols/ts6_common.py | 11 +++-------- protocols/unreal.py | 12 ++++-------- pylink | 13 +++---------- setup.py | 4 +++- utils.py | 5 ++--- 25 files changed, 61 insertions(+), 157 deletions(-) diff --git a/classes.py b/classes.py index 2aea6a1..870006e 100644 --- a/classes.py +++ b/classes.py @@ -17,10 +17,8 @@ import hashlib from copy import deepcopy import inspect -from log import * -import world -import utils -import structures +from . import world, utils, structures +from .log import * ### Exceptions diff --git a/conf.py b/conf.py index c464950..4487123 100644 --- a/conf.py +++ b/conf.py @@ -17,7 +17,7 @@ import yaml import sys from collections import defaultdict -import world +from . import world global testconf testconf = {'bot': diff --git a/coreplugin.py b/coreplugin.py index 90614b6..1578fcc 100644 --- a/coreplugin.py +++ b/coreplugin.py @@ -7,11 +7,8 @@ import sys import signal import os -import utils -import conf -import classes -from log import log -import world +from . import utils, conf, classes, world +from .log import log def _shutdown(irc=None): """Shuts down the Pylink daemon.""" diff --git a/log.py b/log.py index c1dbd72..a4b86dc 100644 --- a/log.py +++ b/log.py @@ -9,9 +9,9 @@ access the global logger object by importing "log" from this module import logging import sys import os -import world -from conf import conf, confname +from . import world +from .conf import conf, confname stdout_level = conf['logging'].get('stdout') or 'INFO' diff --git a/plugins/bots.py b/plugins/bots.py index 0f74ea7..1dc1dde 100644 --- a/plugins/bots.py +++ b/plugins/bots.py @@ -2,13 +2,8 @@ bots.py: Spawn virtual users/bots on a PyLink server and make them interact with things. """ - -import sys -import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - -import utils -from log import log +from pylinkirc import utils +from pylinkirc.log import log @utils.add_cmd def spawnclient(irc, source, args): diff --git a/plugins/changehost.py b/plugins/changehost.py index fbeb07f..d080993 100644 --- a/plugins/changehost.py +++ b/plugins/changehost.py @@ -1,11 +1,8 @@ """ Changehost plugin - automatically changes the hostname of matching users. """ - -# Import hacks to access utils and log. -import sys -import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) +from pylinkirc import utils, world +from pylinkirc.log import log import string @@ -13,10 +10,6 @@ import string # (pip install ircmatch) import ircmatch -import utils -import world -from log import log - # Characters allowed in a hostname. allowed_chars = string.ascii_letters + '-./:' + string.digits diff --git a/plugins/commands.py b/plugins/commands.py index afad202..80a07f9 100644 --- a/plugins/commands.py +++ b/plugins/commands.py @@ -1,12 +1,8 @@ # commands.py: base PyLink commands -import sys -import os from time import ctime -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -import utils -from log import log -import world +from pylinkirc import utils +from pylinkirc.log import log @utils.add_cmd def status(irc, source, args): diff --git a/plugins/ctcp.py b/plugins/ctcp.py index 1031131..88d1b7a 100644 --- a/plugins/ctcp.py +++ b/plugins/ctcp.py @@ -1,10 +1,6 @@ # ctcp.py: Handles basic CTCP requests. -import sys -import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - -import utils -from log import log +from pylinkirc import utils +from pylinkirc.log import log def handle_ctcpversion(irc, source, args): """ diff --git a/plugins/example.py b/plugins/example.py index e165107..d131c04 100644 --- a/plugins/example.py +++ b/plugins/example.py @@ -1,12 +1,6 @@ # example.py: An example PyLink plugin. - -# These two lines add PyLink's root directory to the PATH, so that importing things like -# 'utils' and 'log' work. -import sys, os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - -import utils -from log import log +from pylinkirc import utils +from pylinkirc.log import log import random diff --git a/plugins/exec.py b/plugins/exec.py index efa877e..289ea43 100644 --- a/plugins/exec.py +++ b/plugins/exec.py @@ -2,16 +2,11 @@ exec.py: Provides commands for executing raw code and debugging PyLink. """ -import sys -import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - -import utils -from log import log +from pylinkirc import utils, world +from pylinkirc.log import log # These imports are not strictly necessary, but make the following modules # easier to access through eval and exec. -import world import threading import re import time diff --git a/plugins/fantasy.py b/plugins/fantasy.py index 0b3d3bf..46fe169 100644 --- a/plugins/fantasy.py +++ b/plugins/fantasy.py @@ -1,11 +1,6 @@ # fantasy.py: Adds FANTASY command support, to allow calling commands in channels -import sys -import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - -import utils -import world -from log import log +from pylinkirc import utils, world +from pylinkirc.log import log def handle_fantasy(irc, source, command, args): """Fantasy command handler.""" diff --git a/plugins/games.py b/plugins/games.py index 534fba9..a673d03 100644 --- a/plugins/games.py +++ b/plugins/games.py @@ -1,18 +1,13 @@ """ games.py: Create a bot that provides game functionality (dice, 8ball, etc). """ - -import sys -import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import random import urllib.request import urllib.error from xml.etree import ElementTree -import utils -from log import log -import world +from pylinkirc import utils +from pylinkirc.log import log gameclient = utils.registerService("Games", manipulatable=True) reply = gameclient.reply # TODO find a better syntax for ServiceBot.reply() diff --git a/plugins/networks.py b/plugins/networks.py index ccf326e..b5ecf6a 100644 --- a/plugins/networks.py +++ b/plugins/networks.py @@ -1,14 +1,8 @@ """Networks plugin - allows you to manipulate connections to various configured networks.""" - -import sys -import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - import threading -import utils -import world -from log import log +from pylinkirc import utils, world +from pylinkirc.log import log @utils.add_cmd def disconnect(irc, source, args): diff --git a/plugins/opercmds.py b/plugins/opercmds.py index 2aebca6..2e28b66 100644 --- a/plugins/opercmds.py +++ b/plugins/opercmds.py @@ -2,11 +2,6 @@ opercmds.py: Provides a subset of network management commands. """ -import sys -import os -# Add the base PyLink folder to path, so we can import utils and log. -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - # ircmatch library from https://github.com/mammon-ircd/ircmatch # (pip install ircmatch) try: @@ -14,8 +9,8 @@ try: except ImportError: ircmatch = None -import utils -from log import log +from pylinkirc import utils +from pylinkirc.log import log @utils.add_cmd def checkban(irc, source, args): diff --git a/plugins/relay.py b/plugins/relay.py index c22c87e..9500249 100644 --- a/plugins/relay.py +++ b/plugins/relay.py @@ -1,17 +1,12 @@ # relay.py: PyLink Relay plugin -import sys -import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - import pickle import time import threading import string from collections import defaultdict -import utils -from log import log -import world +from pylinkirc import utils, world +from pylinkirc.log import log ### GLOBAL (statekeeping) VARIABLES relayusers = defaultdict(dict) diff --git a/plugins/servprotect.py b/plugins/servprotect.py index dd90ff6..5a1ae04 100644 --- a/plugins/servprotect.py +++ b/plugins/servprotect.py @@ -1,12 +1,8 @@ # servprotect.py: Protects against KILL and nick collision floods -import sys -import os -sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) - from expiringdict import ExpiringDict -import utils -from log import log +from pylinkirc import utils +from pylinkirc.log import log # TODO: make length and time configurable savecache = ExpiringDict(max_len=5, max_age_seconds=10) diff --git a/protocols/hybrid.py b/protocols/hybrid.py index 333ad8d..55c9cb9 100644 --- a/protocols/hybrid.py +++ b/protocols/hybrid.py @@ -3,13 +3,10 @@ import sys import os import re -curdir = os.path.dirname(__file__) -sys.path += [curdir, os.path.dirname(curdir)] -import utils -from log import log - -from classes import * -from ts6 import * +from pylinkirc import utils +from pylinkirc.log import log +from pylinkirc.classes import * +from pylinkirc.protocols.ts6 import * class HybridProtocol(TS6Protocol): def __init__(self, irc): diff --git a/protocols/inspircd.py b/protocols/inspircd.py index 07931cb..d04274f 100644 --- a/protocols/inspircd.py +++ b/protocols/inspircd.py @@ -8,14 +8,10 @@ import os import re import threading -# Import hacks to access utils and classes... -curdir = os.path.dirname(__file__) -sys.path += [curdir, os.path.dirname(curdir)] -import utils -from log import log -from classes import * - -from ts6_common import * +from pylinkirc import utils +from pylinkirc.classes import * +from pylinkirc.log import log +from pylinkirc.protocols.ts6_common import * class InspIRCdProtocol(TS6BaseProtocol): def __init__(self, irc): diff --git a/protocols/nefarious.py b/protocols/nefarious.py index c597355..15388e4 100644 --- a/protocols/nefarious.py +++ b/protocols/nefarious.py @@ -8,13 +8,9 @@ import base64 import struct from ipaddress import ip_address -# Import hacks to access utils and classes... -curdir = os.path.dirname(__file__) -sys.path += [curdir, os.path.dirname(curdir)] - -import utils -from log import log -from classes import * +from pylinkirc import utils +from pylinkirc.classes import * +from pylinkirc.log import log class P10UIDGenerator(utils.IncrementalUIDGenerator): """Implements an incremental P10 UID Generator.""" diff --git a/protocols/ts6.py b/protocols/ts6.py index 8dc3619..999f192 100644 --- a/protocols/ts6.py +++ b/protocols/ts6.py @@ -7,14 +7,10 @@ import sys import os import re -# Import hacks to access utils and classes... -curdir = os.path.dirname(__file__) -sys.path += [curdir, os.path.dirname(curdir)] -import utils -from log import log - -from classes import * -from ts6_common import * +from pylinkirc import utils +from pylinkirc.classes import * +from pylinkirc.log import log +from pylinkirc.protocols.ts6_common import * class TS6Protocol(TS6BaseProtocol): def __init__(self, irc): diff --git a/protocols/ts6_common.py b/protocols/ts6_common.py index bbfaa4c..a6544eb 100644 --- a/protocols/ts6_common.py +++ b/protocols/ts6_common.py @@ -6,14 +6,9 @@ import sys import os import string -# Import hacks to access utils and classes... -curdir = os.path.dirname(__file__) -sys.path += [curdir, os.path.dirname(curdir)] - -import utils -from log import log -from classes import * -import structures +from pylinkirc import utils, structures +from pylinkirc.classes import * +from pylinkirc.log import log class TS6SIDGenerator(): """ diff --git a/protocols/unreal.py b/protocols/unreal.py index 9a1a38d..d4c9275 100644 --- a/protocols/unreal.py +++ b/protocols/unreal.py @@ -9,14 +9,10 @@ import codecs import socket import re -# Import hacks to access utils and classes... -curdir = os.path.dirname(__file__) -sys.path += [curdir, os.path.dirname(curdir)] - -import utils -from log import log -from classes import * -from ts6_common import * +from pylinkirc import utils +from pylinkirc.classes import * +from pylinkirc.log import log +from pylinkirc.protocols.ts6_common import * class UnrealProtocol(TS6BaseProtocol): def __init__(self, irc): diff --git a/pylink b/pylink index 4ba1b0a..39e39f1 100755 --- a/pylink +++ b/pylink @@ -1,20 +1,13 @@ #!/usr/bin/env python3 import os -import sys - -# Change directory to the folder containing PyLink's source -os.chdir(os.path.dirname(os.path.abspath(__file__))) # This must be done before conf imports, so we get the real conf instead of testing one. -import world +from pylinkirc import world world.testing = False -import conf -from log import log -import classes -import utils -import coreplugin +from pylinkirc import conf, classes, utils, coreplugin +from pylinkirc.log import log if __name__ == '__main__': log.info('PyLink %s starting...', world.version) diff --git a/setup.py b/setup.py index e37a9a9..b393837 100644 --- a/setup.py +++ b/setup.py @@ -45,7 +45,7 @@ setup( install_requires=['pyyaml'], # Folders (packages of code) - packages=find_packages(exclude=['log', 'docs', 'tests']), + packages=['pylinkirc', 'pylinkirc.protocols', 'pylinkirc.plugins'], # Single modules. TODO: consider organizing this into a pylink/ folder py_modules=["classes", "conf", "coreplugin", "log", "structures", "utils", "world"], @@ -55,6 +55,8 @@ setup( '': ['example-conf.yml'], }, + package_dir = {'pylinkirc': '.'}, + # Executable scripts scripts=["pylink"], ) diff --git a/utils.py b/utils.py index 8d8942c..91283fc 100644 --- a/utils.py +++ b/utils.py @@ -11,9 +11,8 @@ import importlib import os import collections -from log import log -import world -import conf +from .log import log +from . import world, conf class NotAuthenticatedError(Exception): """