add data_directory() func to BaseModule, to get path to data file

This commit is contained in:
jesopo 2020-01-27 23:22:45 +00:00
parent b4deae3e2d
commit 7397e36837

View file

@ -1,5 +1,5 @@
import enum, gc, glob, importlib, importlib.util, io, inspect, os, sys import dataclasses, enum, gc, glob, importlib, importlib.util, io, inspect, os
import typing, uuid import sys, typing, uuid
from src import Config, EventManager, Exports, IRCBot, Logging, Timers, utils from src import Config, EventManager, Exports, IRCBot, Logging, Timers, utils
class ModuleException(Exception): class ModuleException(Exception):
@ -42,19 +42,15 @@ class TryReloadResult(object):
self.success = success self.success = success
self.message = message self.message = message
@dataclasses.dataclass
class BaseModule(object): class BaseModule(object):
def __init__(self, definition: "ModuleDefinition"
bot: "IRCBot.Bot", bot: "IRCBot.Bot"
events: EventManager.Events, events: EventManager.Events
exports: Exports.Exports, exports: Exports.Exports
timers: Timers.Timers, timers: Timers.Timers
log: Logging.Log): log: Logging.Log
self.bot = bot
self.events = events
self.exports = exports
self.timers = timers
self.log = log
self.on_load()
def on_load(self): def on_load(self):
pass pass
def unload(self): def unload(self):
@ -65,6 +61,15 @@ class BaseModule(object):
def on_resume(self): def on_resume(self):
pass pass
def data_directory(self, filename: str):
path, filename = os.path.split(filename)
path = os.path.join(self.bot.data_directory, "mod-data",
self.definition.name, path)
if not os.path.isdir(path):
os.makedirs(path)
return os.path.join(path, filename)
class ModuleDefinition(object): class ModuleDefinition(object):
def __init__(self, def __init__(self,
name: str, name: str,
@ -242,8 +247,9 @@ class ModuleManager(object):
context_events = self.events.new_context(context) context_events = self.events.new_context(context)
context_exports = self.exports.new_context(context) context_exports = self.exports.new_context(context)
context_timers = self.timers.new_context(context) context_timers = self.timers.new_context(context)
module_object = module_object_pointer(bot, context_events, module_object = module_object_pointer(definition, bot, context_events,
context_exports, context_timers, self.log) context_exports, context_timers, self.log)
module_object.on_load()
module_title = (getattr(module_object, "_name", None) or module_title = (getattr(module_object, "_name", None) or
definition.name.title()) definition.name.title())