def initalize_loggers(): module_path = os.path.join(".", utils.get_module()) # logging.config.fileConfig(os.path.join(module_path, 'logging.conf')) # Disable base logger logging base_log = logging.getLogger("") base_log.removeHandler(base_log.handlers[0]) global root_log, net_log, chat_log root_log = logging.getLogger("pygab") root_log.setLevel(logging.INFO) handler = logging.StreamHandler(sys.stdout) handler.setFormatter( logging.Formatter("%(levelname)s:%(name)s:%(asctime)s:%(message)s", iMan.config.system.timeformat) ) root_log.addHandler(handler) # --- Connection Logger ----------------------------------------------------- net_log = logging.getLogger("pygab.net") handler = logging.handlers.TimedRotatingFileHandler( os.path.join(module_path, iMan.config.system.logpath, "connection.log"), "midnight", 1, 0, "utf-8" ) handler.setFormatter(logging.Formatter("%(levelname)s %(asctime)s %(message)s", iMan.config.system.timeformat)) net_log.addHandler(handler) # --- Chat Logger ----------------------------------------------------------- chat_log = logging.getLogger("pygab.chat") handler = logging.handlers.TimedRotatingFileHandler( os.path.join(module_path, iMan.config.system.logpath, "history.log"), "midnight", 1, 0, "utf-8" ) handler.setFormatter(logging.Formatter("%(asctime)s %(message)s", iMan.config.system.timeformat)) chat_log.addHandler(handler)
def __init__(self, server_node, iface_cls, config): self._section_name = utils.get_module(__name__) self._logger = logging.getLogger(__name__) self._host = server_node.split(":")[0] self._port = int(server_node.split(":")[1]) self._iface_cls = iface_cls self._get_conn_timeout = config.getint(self._section_name, "pool_timeout", default=settings.DEFAULT_POOL_TIMEOUT) self._socket_timeout = config.getint(self._section_name, "request_timeout", default=settings.DEFAULT_REQUEST_TIMEOUT) * 1000 self._size = config.getint(self._section_name, "pool_size", default=settings.DEFAULT_POOL_SIZE) self._c_module_serialize = config.getboolean(self._section_name, "c_module_serialize", default=settings.USE_C_MODULE_SERIALIZE) self._closed = False if ASYNC_TAG: from gevent.lock import BoundedSemaphore from gevent import queue as Queue self._semaphore = BoundedSemaphore(self._size) self._connection_queue = Queue.LifoQueue(self._size) self._QueueEmpty = Queue.Empty else: from threading import BoundedSemaphore import Queue self._semaphore = BoundedSemaphore(self._size) self._connection_queue = Queue.LifoQueue(self._size) self._QueueEmpty = Queue.Empty
def __init__(self): logging.info('Running %s' % utils.get_module()) #Start all the behind the scenes functions. BotFramework.__init__(self, server.username, server.password, server.domain) PluginFramework.__init__(self) # Load Module specific ini files. self.module = utils.get_module() # Start timers. self.addTimer(20, iMan.saveall, None, type='seconds') plugins_to_load = iMan.config.system.plugins if isinstance(plugins_to_load, basestring): plugins_to_load = plugins_to_load.split(' ') self.load_plugins(plugins_to_load)
def find_plugins(n): for name in plugins.apps: m = get_module("%s.bots.%s" % (name, n)) if m is not None: break else: assert False, "[%s] client class should be added!" % n return m.__file__.split(os.sep)[-3]
def thread(self, user, args): iMan.load([utils.get_module(), 'roster']) args = args.lower() if args in iMan.roster.keys(): del iMan.roster[args] self.parent.sendto(user, 'Removed %s from the roster' % args) else: self.parent.sendto(user, 'Unknown User: %s' % args) iMan.unload('roster')
def init__charge(self): for name in self.apps: m = get_module("%s.interface" % name) if m: for k in dir(m): f = getattr(m, k) if isinstance(f, types.FunctionType) and inspect.getargspec(f)[0] == ["ordersID", "entityDBID", "data"] and k not in self.interface_charge_map: self.interface_charge_map[k] = f
def init__guest_event(self): for name in self.apps: m = get_module("%s.interface" % name) if m: for k in dir(m): f = getattr(m, k) if isinstance(f, types.FunctionType) and inspect.getargspec(f)[0] == ["data", "callback"] and k not in self.interface_guest_event_map: self.interface_guest_event_map[k] = f
def __init__(self, hosts, config): self._section_name = utils.get_module(__name__) self._max_delay = config.getint( self._section_name, "max_retry_delay", default=settings.DEFAULT_ZK_RETRY_MAX_DELAY) self._timeout = config.getint( self._section_name, "time_out", default=settings.DEFAULT_ZK_CONNECTION_TIMEOUT) connection_retry = KazooRetry(max_tries=-1, max_delay=self._max_delay) super(HARpcZKClientManager, self).__init__(hosts=hosts, timeout=self._timeout, connection_retry=connection_retry)
def open_async(self): assert self.app not in ("base", "cell") settings = get_module("settings") AsyncHttp = get_module_attr("common.asyncHttp.AsyncHttp") asyncio_loop = get_module_attr("common.asyncio.asyncio_loop") def onAsyncHttpTick(timerID): AsyncHttp.run_frame() def onAsyncioTick(timerID): asyncio_loop.run_frame() gameTimeInterval = settings.Global.gameTimeInterval if self.app == "bots": gameTimeInterval *= 2 if settings.Global.enableAsyncHttp: KBEngine.addTimer(gameTimeInterval, gameTimeInterval, onAsyncHttpTick) if settings.Global.enableAsyncio: KBEngine.addTimer(gameTimeInterval, gameTimeInterval, onAsyncioTick)
def __init__(self, strategy, dynamic_host_set, config, collector=None): self._section_name = utils.get_module(__name__) self._logger = logging.getLogger(__name__) self._strategy = strategy self._collector = collector self._heartbeat_interval = config.getint( self._section_name, "heartbeat_interval", default=settings.DEFAULT_HEARTBEAT_INTERVAL) self._monitor = settings.SERVICE_MONITOR self._dynamic_host_set = dynamic_host_set self._dynamic_host_set.set_onchange(self._create_hostset_onchange()) self._result_options = { "SUCCESS": self._success, "TIMEOUT": self._timeout, "DEAD": self._dead, "FAILED": self._failed } self.spawn(self._heartbeat)
def __init__(self, zkclient, config, is_server=False): self._section_name = utils.get_module(__name__) self._logger = logging.getLogger(__name__) self._zkclient = zkclient self._is_server = is_server self.queue = multiprocessing.Queue() self._info = {} self._current_info = StatisticsInfo() self._total_info = StatisticsInfo() self._lock = threading.RLock() self._path = None self._start_time = None self._last_update_time = None self.__class__.collect = self.__class__._update_info self._collect_interval = config.getint(self._section_name, "interval", default=settings.DEFAULT_SEND_INFO_INTERVAL) self._collect_nodes = config.getint(self._section_name, "node_num", default=settings.DEFAULT_MAX_DETAILINFO_NODE_NUM) if self._is_server: self.__class__.collect = self.__class__._server_collect
def __init__(self, processor, handler, config): self._logger = logging.getLogger(__name__) self._section_name = utils.get_module(__name__) self._server = None self._service_name = config.get(self._section_name, "service", required=True) self._port = config.getint(self._section_name, "port", required=True) self._zk_connect_str = config.get(self._section_name, "zk_connect_str", required=True) self._auth_user = config.get(self._section_name, "auth_user", required=True) self._auth_password = config.get(self._section_name, "auth_password", required=True) self._monitor = config.getboolean(self._section_name, "monitor", default=settings.SERVICE_MONITOR) self._c_module_serialize = config.getboolean( self._section_name, "c_module_serialize", default=settings.USE_C_MODULE_SERIALIZE) self._server_name = config.get(self._section_name, "name") self._owner = config.get(self._section_name, "owner") hosts = "%s/%s" % (self._zk_connect_str, settings.DEFAULT_ZK_NAMESPACE_ROOT) self._server_path = "%s/%s" % (self._service_name, settings.DEFAULT_ZK_NAMESPACE_SERVERS) self._statistic_path = "%s/%s/%s" % ( self._service_name, settings.DEFAULT_ZK_NAMESPACE_STATISTICS, settings.DEFAULT_ZK_NAMESPACE_SERVERS) # create zk acl self._acls = [] self._acls.append( make_digest_acl(self._auth_user, self._auth_password, all=True)) self._acls.append(make_acl("world", "anyone", read=True)) # create zk_client self._zkclient = HARpcZKClientManager.make(hosts, config, "server") self._zkclient.add_auth( "digest", "%s:%s" % (self._auth_user, self._auth_password)) # create zkpath if not self._zkclient.exists(self._service_name): self._zkclient.create(self._service_name, makepath=True) if not self._zkclient.exists(self._server_path): self._zkclient.create(self._server_path, acl=self._acls) if not self._zkclient.exists(self._statistic_path): self._zkclient.create(self._statistic_path, makepath=True) self.transport = TSocket.TServerSocket(port=self._port) self.tfactory = TTransport.TBufferedTransportFactory() if self._monitor: self._collector = StatisticsCollector(self._zkclient, config, is_server=True) self._processor = self._statistics_wrapper(processor)(handler) else: self._processor = processor(handler) if self._c_module_serialize: self.pfactory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory() else: self.pfactory = TBinaryProtocol.TBinaryProtocolFactory()
def initialize(self): iMan.load([utils.get_module(), "roster"])
def init__rsa(self): crypto = get_module("oscrypto.asymmetric") public_key, private_key = crypto.generate_pair("rsa", 1024) self.public_key = public_key self.write(crypto.dump_private_key(private_key, None).decode("utf-8"), self.RES_KEY_DIR, "kbengine_private.key") self.write(crypto.dump_public_key(public_key).decode("utf-8"), self.RES_KEY_DIR, "kbengine_public.key")
def __init__(self): # Plugin hashing dictionary self._pluginhash = {} self.pluginpaths = [utils.get_module(), ""]
msg = ' '.join(map(unicode,args)) # Replace unencodable charactors with ? msg = '%s %s' % (time.strftime("%Y-%m-%d %H:%M:%S"), msg.encode(sys.getdefaultencoding(), "replace")) print msg print >> me.logf, msg me.logf.flush() from common import const, mounts, utils from common.ini import iMan from framework.bot import BotFramework from framework.plugin import attach_hooks, attach_post_hook, PluginFramework from gbot import * iMan.load(utils.get_module(), 'config') # Contains all the server information server = iMan.config.server conn_log = logging.getLogger('pygab.net') class ConferenceBot(BotFramework, PluginFramework): def __init__(self): logging.info('Running %s' % utils.get_module()) #Start all the behind the scenes functions. BotFramework.__init__(self, server.username, server.password, server.domain) PluginFramework.__init__(self) # Load Module specific ini files.
def thread(self, user, args): if args == self.grant_pass: iMan.load([utils.get_module(), 'roster']) iMan.roster[utils.getname(user).lower()].rank = const.RANK_ADMIN iMan.unload('roster') self.parent.sendto(user, "You've been granted Admin status.")
from __future__ import with_statement import logging import logging.handlers import os import re import sys import traceback from common import const, mounts, utils from common.ini import iMan _plugin_log = logging.getLogger('pygab.plugins') _handler = logging.handlers.RotatingFileHandler( os.path.join('.', utils.get_module(), 'plugin_errors.log'), maxBytes=256000, backupCount=3, encoding='utf-8', delay=True ) _handler.setLevel(logging.ERROR) _plugin_log.addHandler(_handler) def attach_hooks(hook_name=''): """Attach both pre- and -post hooks. """ def decorator(func): def wrapper(self, *args): self.hook('%s_pre' % (hook_name or func.__name__), *args) func(self, *args) self.hook('%s_post' % (hook_name or func.__name__), *args) return wrapper
def __init__(self, folder_name="plugins", name_format="plugin_%s.py"): #Plugin hashing dictionary self._pluginhash = {} self.pluginpaths = [utils.get_module(), ''] self.folder_name = folder_name self.name_format = name_format
# - Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # - Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from common import utils from core.const import * try: exec(utils.get_import( mod=utils.get_module(), from_=['const'], import_=['*'])) except ImportError, e: # If the bot module doesn't overwrite anything, no problem. pass #raise
"""Replacement for print, which doesn't deal with unicode well""" global me msg = ' '.join(map(unicode,args)) # Replace unencodable charactors with ? msg = '%s %s' % (time.strftime("%Y-%m-%d %H:%M:%S"), msg.encode(sys.getdefaultencoding(), "replace")) print msg print >> me.logf, msg me.logf.flush() from common import const, mounts, utils from common.ini import iMan from framework import BotFramework, PluginFramework from gbot import * iMan.load('config', utils.get_module()) # Contains all the server information server = iMan.config.server class ConferenceBot(BotFramework, PluginFramework): def __init__(self): #Start all the behind the scenes functions. BotFramework.__init__(self, server.username, server.password, server.domain) PluginFramework.__init__(self) # Load Module specific ini files. self.module = utils.get_module() # Start timers. self.addTimer(20, iMan.saveall, None, type='seconds')
def initialize(self): iMan.load([utils.get_module(), 'roster']) iMan.load([utils.get_module(), 'mail'])
# - Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # - Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from common import utils from core.mounts import * try: exec(utils.get_import( mod=utils.get_module(), from_=['mounts'], import_=['*'])) except ImportError, e: # If the bot module doesn't overwrite anything, no problem. pass #raise
# # - Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # - Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from common import utils from core.mounts import * try: exec(utils.get_import(mod=utils.get_module(), from_=["mounts"], import_=["*"])) except ImportError, e: # If the bot module doesn't overwrite anything, no problem. pass # raise