def __init__(self, backend=None, noshutdown=False, db=None, opts=None): self.db = DBCache(db) self.log = MythLog(self.logmodule, db=self.db) self.hostname = None if opts is None: self.opts = BEConnection.BEConnOpts(noshutdown) else: self.opts = opts if backend is None: # no backend given, use master self.host = self.db.settings.NULL.MasterServerIP else: if self._reip.match(backend): # given backend is IP address self.host = backend else: # given backend is hostname, pull address from database self.hostname = backend self.host = self.db.settings[backend].BackendServerIP if not self.host: raise MythDBError(MythError.DB_SETTING, 'BackendServerIP', backend) if self.hostname is None: # reverse lookup hostname from address with self.db.cursor(self.log) as cursor: if cursor.execute( """SELECT hostname FROM settings WHERE value='BackendServerIP' AND data=?""", [self.host]) == 0: # no match found raise MythDBError(MythError.DB_SETTING, 'BackendServerIP', self.host) self.hostname = cursor.fetchone()[0] # lookup port from database self.port = int(self.db.settings[self.hostname].BackendServerPort) if not self.port: raise MythDBError(MythError.DB_SETTING, 'BackendServerPort', self.port) self._uuid = uuid4() self._ident = '%s:%d' % (self.host, self.port) if self._ident in self._shared: # existing connection found # register and reconnect if necessary self.be = self._shared[self._ident] self.be.registeruser(self._uuid, self.opts) self.be.reconnect() else: # no existing connection, create new self.be = BEConnection(self.host, self.port, \ self.db.gethostname(), self.opts) self.be.registeruser(self._uuid, self.opts) self._shared[self._ident] = self.be
def __init__(self, backend=None, noshutdown=False, db=None, opts=None): self.db = DBCache(db) self.log = MythLog(self.logmodule, db=self.db) self.hostname = None if opts is None: self.opts = BEConnection.BEConnOpts(noshutdown) else: self.opts = opts if backend is None: # no backend given, use master self.host = self.db.settings.NULL.MasterServerIP else: if self._reip.match(backend): # given backend is IP address self.host = backend else: # given backend is hostname, pull address from database self.hostname = backend self.host = self.db.settings[backend].BackendServerIP if not self.host: raise MythDBError(MythError.DB_SETTING, 'BackendServerIP', backend) if self.hostname is None: # reverse lookup hostname from address with self.db.cursor(self.log) as cursor: if cursor.execute("""SELECT hostname FROM settings WHERE value='BackendServerIP' AND data=?""", [self.host]) == 0: # no match found raise MythDBError(MythError.DB_SETTING, 'BackendServerIP', self.host) self.hostname = cursor.fetchone()[0] # lookup port from database self.port = int(self.db.settings[self.hostname].BackendServerPort) if not self.port: raise MythDBError(MythError.DB_SETTING, 'BackendServerPort', self.port) self._uuid = uuid4() self._ident = '%s:%d' % (self.host, self.port) if self._ident in self._shared: # existing connection found # register and reconnect if necessary self.be = self._shared[self._ident] self.be.registeruser(self._uuid, self.opts) self.be.reconnect() else: # no existing connection, create new self.be = BEConnection(self.host, self.port, \ self.db.gethostname(), self.opts) self.be.registeruser(self._uuid, self.opts) self._shared[self._ident] = self.be
def __init__(self, backend=None, noshutdown=False, systemevents=False,\ generalevents=True, db=None, opts=None): if opts is None: opts = BEConnection.BEConnOpts(noshutdown,\ systemevents, generalevents) BECache.__init__(self, backend, db=db, opts=opts) # must be done to retain hard reference self._events = self._listhandlers() for func in self._events: self.registerevent(func)
def __init__(self, host, port, localname, filename, sgroup, mode): self.filename = filename self.sgroup = sgroup self.mode = mode BEConnection.__init__(self, host, port, localname)
class BECache( SplitInt ): """ BECache(backend=None, noshutdown=False, db=None) -> MythBackend connection object Basic class for mythbackend socket connections. Offers connection caching to prevent multiple connections. 'backend' allows a hostname or IP address to connect to. If not provided, connect will be made to the master backend. Port is always taken from the database. 'db' allows an existing database object to be supplied. 'noshutdown' specified whether the backend will be allowed to go into automatic shutdown while the connection is alive. Available methods: joinInt() - convert two signed ints to one 64-bit signed int splitInt() - convert one 64-bit signed int to two signed ints backendCommand() - Sends a formatted command to the backend and returns the response. """ logmodule = 'Python Backend Connection' _shared = weakref.WeakValueDictionary() _reip = re.compile('(?:\d{1,3}\.){3}\d{1,3}') def __repr__(self): return "<%s 'myth://%s:%d/' at %s>" % \ (str(self.__class__).split("'")[1].split(".")[-1], self.hostname, self.port, hex(id(self))) def __init__(self, backend=None, noshutdown=False, db=None, opts=None): self.db = DBCache(db) self.log = MythLog(self.logmodule, db=self.db) self.hostname = None if opts is None: self.opts = BEConnection.BEConnOpts(noshutdown) else: self.opts = opts if backend is None: # no backend given, use master self.host = self.db.settings.NULL.MasterServerIP else: if self._reip.match(backend): # given backend is IP address self.host = backend else: # given backend is hostname, pull address from database self.hostname = backend self.host = self.db.settings[backend].BackendServerIP if not self.host: raise MythDBError(MythError.DB_SETTING, 'BackendServerIP', backend) if self.hostname is None: # reverse lookup hostname from address with self.db.cursor(self.log) as cursor: if cursor.execute("""SELECT hostname FROM settings WHERE value='BackendServerIP' AND data=?""", [self.host]) == 0: # no match found raise MythDBError(MythError.DB_SETTING, 'BackendServerIP', self.host) self.hostname = cursor.fetchone()[0] # lookup port from database self.port = int(self.db.settings[self.hostname].BackendServerPort) if not self.port: raise MythDBError(MythError.DB_SETTING, 'BackendServerPort', self.port) self._uuid = uuid4() self._ident = '%s:%d' % (self.host, self.port) if self._ident in self._shared: # existing connection found # register and reconnect if necessary self.be = self._shared[self._ident] self.be.registeruser(self._uuid, self.opts) self.be.reconnect() else: # no existing connection, create new self.be = BEConnection(self.host, self.port, \ self.db.gethostname(), self.opts) self.be.registeruser(self._uuid, self.opts) self._shared[self._ident] = self.be def backendCommand(self, data): """ obj.backendCommand(data) -> response string Sends a formatted command via a socket to the mythbackend. """ return self.be.backendCommand(data)
def _newcmdconn(self): return BEConnection(self.host, self.port, self.db.gethostname(), self.blockshutdown)
class BECache(SplitInt): """ BECache(backend=None, noshutdown=False, db=None) -> MythBackend connection object Basic class for mythbackend socket connections. Offers connection caching to prevent multiple connections. 'backend' allows a hostname or IP address to connect to. If not provided, connect will be made to the master backend. Port is always taken from the database. 'db' allows an existing database object to be supplied. 'noshutdown' specified whether the backend will be allowed to go into automatic shutdown while the connection is alive. Available methods: joinInt() - convert two signed ints to one 64-bit signed int splitInt() - convert one 64-bit signed int to two signed ints backendCommand() - Sends a formatted command to the backend and returns the response. """ logmodule = 'Python Backend Connection' _shared = weakref.WeakValueDictionary() _reip = re.compile('(?:\d{1,3}\.){3}\d{1,3}') def __repr__(self): return "<%s 'myth://%s:%d/' at %s>" % \ (str(self.__class__).split("'")[1].split(".")[-1], self.hostname, self.port, hex(id(self))) def __init__(self, backend=None, noshutdown=False, db=None, opts=None): self.db = DBCache(db) self.log = MythLog(self.logmodule, db=self.db) self.hostname = None if opts is None: self.opts = BEConnection.BEConnOpts(noshutdown) else: self.opts = opts if backend is None: # no backend given, use master self.host = self.db.settings.NULL.MasterServerIP else: if self._reip.match(backend): # given backend is IP address self.host = backend else: # given backend is hostname, pull address from database self.hostname = backend self.host = self.db.settings[backend].BackendServerIP if not self.host: raise MythDBError(MythError.DB_SETTING, 'BackendServerIP', backend) if self.hostname is None: # reverse lookup hostname from address with self.db.cursor(self.log) as cursor: if cursor.execute( """SELECT hostname FROM settings WHERE value='BackendServerIP' AND data=?""", [self.host]) == 0: # no match found raise MythDBError(MythError.DB_SETTING, 'BackendServerIP', self.host) self.hostname = cursor.fetchone()[0] # lookup port from database self.port = int(self.db.settings[self.hostname].BackendServerPort) if not self.port: raise MythDBError(MythError.DB_SETTING, 'BackendServerPort', self.port) self._uuid = uuid4() self._ident = '%s:%d' % (self.host, self.port) if self._ident in self._shared: # existing connection found # register and reconnect if necessary self.be = self._shared[self._ident] self.be.registeruser(self._uuid, self.opts) self.be.reconnect() else: # no existing connection, create new self.be = BEConnection(self.host, self.port, \ self.db.gethostname(), self.opts) self.be.registeruser(self._uuid, self.opts) self._shared[self._ident] = self.be def backendCommand(self, data): """ obj.backendCommand(data) -> response string Sends a formatted command via a socket to the mythbackend. """ return self.be.backendCommand(data)