diff --git a/software/pyGI/configurator.py b/software/pyGI/configurator.py index f5820ef..943b649 100644 --- a/software/pyGI/configurator.py +++ b/software/pyGI/configurator.py @@ -1,4 +1,4 @@ -import ConfigParser +import configparser import logging import sys,os,uuid @@ -17,21 +17,21 @@ class Configurator(): def __init__(self): - self.static_conf = ConfigParser.SafeConfigParser() + self.static_conf = configparser.SafeConfigParser() #uuid try: self.static_conf.readfp(open(PATH_UUID)) log.info("node uuid: %s"%self.static_conf.get('node','uuid')) - except (IOError, ConfigParser.NoOptionError, ConfigParser.NoSectionError) as e: + except (IOError, configparser.NoOptionError, configparser.NoSectionError) as e: log.warn("No uuid set!") new_uuid = str(uuid.uuid1()) log.warn("Setting new uuid: %s"%new_uuid) - self.static_conf = ConfigParser.SafeConfigParser() + self.static_conf = configparser.SafeConfigParser() self.static_conf.add_section('node') self.static_conf.set('node','uuid',new_uuid) - with open(PATH_UUID,'wb') as f: + with open(PATH_UUID,'w') as f: self.static_conf.write(f) self.static_conf.readfp(open(PATH_DEFAULT)) @@ -43,7 +43,7 @@ def __init__(self): log.info('reading configuration %s'%f) - self.dynamic_conf = ConfigParser.SafeConfigParser() + self.dynamic_conf = configparser.SafeConfigParser() self.read_dynamic() def read_dynamic(self): @@ -52,7 +52,7 @@ def read_dynamic(self): log.info('reading configuration %s'%f) def write_dynamic(self): - with open(PATH_DYNAMIC,'wb') as f: + with open(PATH_DYNAMIC,'w') as f: self.dynamic_conf.write(f) def clear_dynamic(self): @@ -61,15 +61,15 @@ def clear_dynamic(self): except OSError: pass - self.dynamic_conf = ConfigParser.SafeConfigParser() + self.dynamic_conf = configparser.SafeConfigParser() self.read_dynamic() def get(self,section,option): try: return self.dynamic_conf.get(section,option) - except ConfigParser.NoOptionError: + except configparser.NoOptionError: pass - except ConfigParser.NoSectionError: + except configparser.NoSectionError: pass return self.static_conf.get(section,option) @@ -92,22 +92,22 @@ def getboolean(self,section,option): def set(self,section,option,value): if not self.static_conf.has_section(section): - raise ConfigParser.NoSectionError(section) + raise configparser.NoSectionError(section) if not self.static_conf.has_option(section,option): - raise ConfigParser.NoOptionError(option,section) + raise configparser.NoOptionError(option,section) if not self.dynamic_conf.has_section(section): self.dynamic_conf.add_section(section) - self.dynamic_conf.set(section,option,value) + self.dynamic_conf.set(section,option,str(value)) cfg = Configurator() if __name__ == "__main__": logging.basicConfig(level=1) cfg = Configurator() - print cfg.get('server','port') + print(cfg.get('server','port')) cfg.set('server','port','80') - print cfg.get('server','port') + print(cfg.get('server','port')) cfg.write_dynamic() cfg.read_dynamic() - print cfg.get('server','port') + print(cfg.get('server','port')) #print cfg.static_conf.get('server','2p') diff --git a/software/pyGI/entropygenerator.py b/software/pyGI/entropygenerator.py index 0642e0d..e5d182f 100644 --- a/software/pyGI/entropygenerator.py +++ b/software/pyGI/entropygenerator.py @@ -49,7 +49,7 @@ def tick (self): elif d0 < d1: self.bitstring += "0" if self.toggle else "1" else: #d0 = d1 - print "Collision" + print("Collision") self.toggle = not self.toggle @@ -58,7 +58,7 @@ def tick (self): def handle_bitstring(self): - with open(self.outfile, "ab") as f: + with open(self.outfile, "a") as f: while len(self.bitstring)>=8: byte_bin = self.bitstring[:8] self.bitstring = self.bitstring[8:] diff --git a/software/pyGI/geigerclient.py b/software/pyGI/geigerclient.py index e2d5669..dee94a3 100644 --- a/software/pyGI/geigerclient.py +++ b/software/pyGI/geigerclient.py @@ -7,8 +7,8 @@ import sys from geventwebsocket import WebSocketError -from configurator import cfg -import geigercounter +from .configurator import cfg +from . import geigercounter import logging @@ -31,7 +31,7 @@ def send(self,msg): except WebSocketError: self.active = False log.error("could not write to socket %s (client %s)"%(self.ws,self.session_id)) - except Exception, e: + except Exception as e: self.active = False log.error(e) @@ -75,7 +75,7 @@ def receive_commands(self,handler): ts = msg.get("timestamp") text = msg.get("text") handler.geigerlog.set_annotation(ts,text) - handler.send_log(start=age_from,end=age_to,amount=1000,static=True) + handler.send_log(self,start=age_from,end=age_to,amount=1000,static=True) #Config elif cmd == "get": diff --git a/software/pyGI/geigercounter.py b/software/pyGI/geigercounter.py index ed524f5..d544eb9 100644 --- a/software/pyGI/geigercounter.py +++ b/software/pyGI/geigercounter.py @@ -6,8 +6,8 @@ import logging from collections import deque -from configurator import cfg -from entropygenerator import EntropyGenerator +from .configurator import cfg +from .entropygenerator import EntropyGenerator log = logging.getLogger(__name__) diff --git a/software/pyGI/geigerlog.py b/software/pyGI/geigerlog.py index d1bb197..318c13b 100644 --- a/software/pyGI/geigerlog.py +++ b/software/pyGI/geigerlog.py @@ -7,7 +7,7 @@ import json import logging from collections import deque -from configurator import cfg +from .configurator import cfg log = logging.getLogger(__name__) @@ -19,6 +19,12 @@ def dt2unix(dt): return int(dt.strftime("%s")) +def int_to_bytes(int): + return (int).to_bytes(int.bit_length(), byteorder='big') + +def bytes_to_int(bytes): + return int.from_bytes(bytes, byteorder='big') + def get_last_totalcount(): log.info("Getting last totalcount") @@ -30,7 +36,7 @@ def get_last_totalcount(): # Check for empty leveldb instance try: - db.RangeIter(include_value=False).next() + next(db.RangeIter(include_value=False)) except StopIteration: log.info("Empty LevelDB") return (0,0) @@ -38,7 +44,7 @@ def get_last_totalcount(): while not last_entries_keys: log.debug("Searching further (%d)..."%d) - last_entries_keys = list(db.RangeIter(key_from=str(now-d),include_value=False)) + last_entries_keys = list(db.RangeIter(key_from=int_to_bytes(now-d),include_value=False)) d = d*2 i = i+1 @@ -88,7 +94,7 @@ def run(self): log.info("Starting geigerlog") avg_age = dt2unix(datetime.now() - timedelta(minutes=15)) avg_list = deque() - entries_list = list(self.db.RangeIter(key_from=str(avg_age))) + entries_list = list(self.db.RangeIter(key_from=int_to_bytes(avg_age))) for e in entries_list: avg_list.append(json.loads(e[1])) while True: time.sleep(LOG_WRITE_RATE) @@ -101,33 +107,35 @@ def run(self): avg_list.append(state) avg = round(sum([e["data"]["edr"] for e in avg_list])/len(avg_list),3) state["data"]["edr_avg"] = avg - key = str(state["timestamp"]) - value = json.dumps(state) + key = int_to_bytes(state["timestamp"]) + value = json.dumps(state).encode('utf-8') self.db.Put(key, value) self.last_log = state log.debug("Logging: %s : %s"%(key,value)) - log.debug(self.db.GetStats()) + #log.debug(self.db.GetStats()) def get_log_entries_all(self,start,end): result = [] - entries_list = list(self.db.RangeIter(key_from=str(start),fill_cache=True)) + entries_list = list(self.db.RangeIter(key_from=int_to_bytes(start),fill_cache=True)) last_time = start for e in entries_list: entry = json.loads(e[1]) - if int(entry["timestamp"])-last_time > MAX_ENTRY_DIST: + timestamp = int(entry["timestamp"]) + if timestamp-last_time > MAX_ENTRY_DIST: insert_time = last_time + LOG_WRITE_RATE record_insert = dummy_entry(insert_time,entry['data']['totalcount'],entry['data']['totalcount_dtc']) - while insert_time < int(entry["timestamp"]): + while insert_time < timestamp: result.append(record_insert.copy()) insert_time += 10 record_insert["timestamp"]=insert_time - last_time = int(entry["timestamp"]) + last_time = timestamp result.append(entry) if result: last = result[-1] - if end - int(last["timestamp"]) > MAX_ENTRY_DIST: - insert_time = int(last["timestamp"]) + LOG_WRITE_RATE + timestamp = int(last["timestamp"]) + if end - timestamp > MAX_ENTRY_DIST: + insert_time = timestamp + LOG_WRITE_RATE record_insert = dummy_entry(insert_time,entry['data']['totalcount'],entry['data']['totalcount_dtc']) while insert_time < end: result.append(record_insert.copy()) @@ -145,20 +153,20 @@ def get_log_entries_sparse(self,start,end,amount): if t > end: break if step >= 1: t_prev = start + delta_step * (step - 1) - annotation_keys = list(self.db_annotation.RangeIter(key_from=str(t_prev),key_to=str(t),include_value=False)) + annotation_keys = list(self.db_annotation.RangeIter(key_from=int_to_bytes(int(t_prev)),key_to=int_to_bytes(int(t)),include_value=False)) if annotation_keys: for key in annotation_keys: result.append(json.loads(self.db.Get(key))) - db_iter = self.db.RangeIter(key_from=str(t),fill_cache=True) + db_iter = self.db.RangeIter(key_from=int_to_bytes(int(t)),fill_cache=True) try: - (timestamp,entry_json) = db_iter.next() + (timestamp,entry_json) = next(db_iter) except StopIteration: break; entry = json.loads(entry_json) - if int(timestamp)-t>MAX_ENTRY_DIST: + if bytes_to_int(timestamp)-t>MAX_ENTRY_DIST: entry=dummy_entry(t,entry['data']['totalcount'],entry['data']['totalcount_dtc']) @@ -177,7 +185,7 @@ def get_log_entries(self,start=None,end=None,age=None,amount=500): if age: start = end - age elif start is None: - start = int(self.db.RangeIter(key_from="0",include_value=False).next()) + start = bytes_to_int(next(self.db.RangeIter(key_from=int_to_bytes(int(0)),include_value=False))) log.info("Fetching %s log entries from %d to %s"%(str(amount),start,end)) @@ -188,20 +196,20 @@ def get_log_entries(self,start=None,end=None,age=None,amount=500): def set_annotation(self,ts,text): try: - key = str(int(ts)) + key = int_to_bytes(int(ts)) entry_json = self.db.Get(key) except KeyError: try: - (key,entry_json) = self.db.RangeIter(key_from=str(int(ts))).next() + (key,entry_json) = next(self.db.RangeIter(key_from=int_to_bytes(int(ts)))) except StopIteration: log.ERROR("Annotation timestamp out of log range: %s"%key) return entry = json.loads(entry_json) entry['annotation'] = text - entry_json = json.dumps(entry) + entry_json = json.dumps(entry).encode('utf-8') self.db.Put(key,entry_json) if text: - self.db_annotation.Put(key,text) + self.db_annotation.Put(key,text.encode('utf-8')) else: self.db_annotation.Delete(key) @@ -226,4 +234,4 @@ def dummy_entry(timestamp,total,total_dtc): if __name__ == "__main__": - print get_last_totalcount() + print(get_last_totalcount()) diff --git a/software/pyGI/geigerserver.py b/software/pyGI/geigerserver.py index 236d6d0..af52735 100644 --- a/software/pyGI/geigerserver.py +++ b/software/pyGI/geigerserver.py @@ -8,10 +8,10 @@ import os import sys -from configurator import cfg +from .configurator import cfg -import geigercounter -import geigerclient +from . import geigercounter +from . import geigerclient log=logging.getLogger(__name__) diff --git a/software/pyGIserver.py b/software/pyGIserver.py index 88926d9..0a04a53 100755 --- a/software/pyGIserver.py +++ b/software/pyGIserver.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import logging from pyGI.configurator import cfg