def update_connection_status(self): servers_connected = 0 servers_known = 0 available_space = 0 for gateway in self.gateways: try: prev_servers = gateway.status['servers_connected'] except KeyError: pass try: gateway.update_status() servers_connected += gateway.status['servers_connected'] servers_known += gateway.status['servers_known'] available_space += h2b(gateway.status['total_available_space']) except (OSError, IndexError): # XXX pass try: if prev_servers != gateway.status['servers_connected']: # TODO: Notify on (dis)connects # FIXME: This should only be called if a Tahoe flag is set logging.debug("New storage node (dis)connected.") #reactor.callInThread(gateway.adjust) except UnboundLocalError: pass self.servers_connected = servers_connected self.total_available_space = b2h(available_space) self.servers_known = servers_known # XXX Add logic to check for paused state, etc. self.status_text = "Status: Connected ({} of {} servers)".format( self.servers_connected, self.servers_known)
def update_status(self): # https://tahoe-lafs.org/trac/tahoe-lafs/ticket/2476 html = requests.get(self.node_url).text p = re.compile("Connected to <span>(.+?)</span>") self.status['servers_connected'] = int(re.findall(p, html)[0]) p = re.compile("of <span>(.+?)</span> known storage servers") self.status['servers_known'] = int(re.findall(p, html)[0]) servers = {} p = re.compile('<div class="nodeid">(.+?)</div>') nodeid = re.findall(p, html) for item in nodeid: servers[item] = {} def insert_all(s, tag='td'): p = re.compile('<{} class="{}">(.+?)</{}>'.format(tag, s, tag)) for index, item in enumerate(re.findall(p, html)): key = s.replace('service-', '').replace('-', '_').replace(' ', '_') servers[nodeid[index]][key] = item insert_all('nickname', 'div') insert_all('address') insert_all('service-service-name') insert_all('service-since timestamp') insert_all('service-announced timestamp') insert_all('service-version') insert_all('service-available-space') p = re.compile('<div class="furl">(.+?)</div>') r = re.findall(p, html) self.status['introducer'] = {'furl': r[0]} self.status['helper'] = {'furl': r[1]} self.status['servers'] = servers p = re.compile('<div class="status-indicator">(.+?)</div>') l = re.findall(p, html) for index, item in enumerate(l): p = re.compile('alt="(.+?)"') status = re.findall(p, item)[0] if index == 0: self.status['introducer']['status'] = status elif index == 1: self.status['helper']['status'] = status else: self.status['servers'][nodeid[index - 2]]['status'] = status total_available_space = 0 for _, v in self.status['servers'].items(): try: total_available_space += h2b(v['available_space']) except ValueError: pass self.status['total_available_space'] = b2h(total_available_space)
def update_status(self): # https://tahoe-lafs.org/trac/tahoe-lafs/ticket/2476 html = requests.get(self.node_url).text p = re.compile("Connected to <span>(.+?)</span>") self.status['servers_connected'] = int(re.findall(p, html)[0]) p = re.compile("of <span>(.+?)</span> known storage servers") self.status['servers_known'] = int(re.findall(p, html)[0]) servers = {} p = re.compile('<div class="nodeid">(.+?)</div>') nodeid = re.findall(p, html) for item in nodeid: servers[item] = {} def insert_all(s, tag='td'): p = re.compile('<{} class="{}">(.+?)</{}>'.format(tag, s, tag)) for index, item in enumerate(re.findall(p, html)): key = s.replace('service-', '').replace('-', '_').replace(' ', '_') servers[nodeid[index]][key] = item insert_all('nickname', 'div') insert_all('address') insert_all('service-service-name') insert_all('service-since timestamp') insert_all('service-announced timestamp') insert_all('service-version') insert_all('service-available-space') p = re.compile('<div class="furl">(.+?)</div>') r = re.findall(p, html) self.status['introducer'] = { 'furl': r[0] } self.status['helper'] = { 'furl': r[1] } self.status['servers'] = servers p = re.compile('<div class="status-indicator">(.+?)</div>') l = re.findall(p, html) for index, item in enumerate(l): p = re.compile('alt="(.+?)"') status = re.findall(p, item)[0] if index == 0: self.status['introducer']['status'] = status elif index == 1: self.status['helper']['status'] = status else: self.status['servers'][nodeid[index - 2]]['status'] = status total_available_space = 0 for _, v in self.status['servers'].items(): try: total_available_space += h2b(v['available_space']) except ValueError: pass self.status['total_available_space'] = b2h(total_available_space)
def test_1_2h(): assert b2h(1) == '1.0 B'
def test_1024_2h(): assert b2h(1024) == '1.0 KB'
def test_274877906944_2h(): assert b2h(274877906944) == '256.0 GB'