def set_current(self, horizon=7, notebook_list_number=None, filter=None): """ set_current preforms double duty. 1. If there is an index from the options, use that option to set the current notebook. 2. Pull the notebooks from the DB land. """ if filter is None: filter = None lastday = None if filter: lastday = TimeStamp(filter).tomorrow() filter = TimeStamp(filter).lt(horizon) if notebook_list_number is not None: self.current_notebook = notebook_list_number self.save_state() (j, t) = self.notebooks[self.current_notebook] #if os.path.exists(j): # journal = pickle.load(open(j,'rb')) #else: #if filter: # dk = filter #else: # dk = TimeStamp().daypart() if j == 'journal' else None journal = db.loadjournal(jname=j, daykey=filter, lastday=lastday) #if not os.path.exists(t): td = db.loadtodo(j) todo = td if td else None if todo is None and os.path.exists(t): todo = pickle.load(open(t, 'rb')) else: todo = {} return (journal, todo)
def from_dict(request_dict): id = request_dict.get('id', generate_uuid()) request = TaskRequest(id=id) request.load_type = request_dict["loadType"] request.load_id = request_dict["loadId"] request.user_id = request_dict["userId"] request.earliest_pickup_time = TimeStamp.from_str( request_dict["earliestPickupTime"]) request.latest_pickup_time = TimeStamp.from_str( request_dict["latestPickupTime"]) pickup_area_dict = request_dict.get('pickup_pose', None) if pickup_area_dict: request.pickup_pose = Area.from_dict(pickup_area_dict) else: # when the provided dict is from json schema request.pickup_pose = Area() request.pickup_pose.name = request_dict.get("pickupLocation", '') request.pickup_pose.floor_number = request_dict.get( "pickupLocationLevel", 0) delivery_area_dict = request_dict.get('delivery_pose', None) if delivery_area_dict: request.delivery_pose = Area.from_dict(delivery_area_dict) else: # when the provided dict is from json schema request.delivery_pose = Area() request.delivery_pose.name = request_dict.get( "deliveryLocation", '') request.delivery_pose.floor_number = request_dict.get( "deliveryLocationLevel", 0) request.priority = request_dict["priority"] return request
def test_get_days_correct_len(self): ts = TimeStamp() startTS = ts.totimestamp(datetime.today() - timedelta(days=9)) endTS = ts.totimestamp(datetime.today()) a = api_gen(startTS, endTS, 10) self.assertTrue( len(a.daysInRange) == 10, "Should be 10 days got " + str(len(a.daysInRange)))
def test_partition_range_bins(self): ts = TimeStamp() startTS = ts.totimestamp(datetime.today() - timedelta(days=200)) endTS = ts.totimestamp(datetime.today()) a = api_gen(startTS, endTS, 10) bins = [x[1] for x in a.daysInRange] self.assertTrue(max(bins) == 10) self.assertTrue(min(bins) == 1)
def test_get_days_rolls_to_midnight(self): ts = TimeStamp() startTS = ts.totimestamp(datetime.today() - timedelta(days=2)) endTS = ts.totimestamp(datetime.today()) sdt = ts.todate(startTS) shouldStartAm = datetime(sdt.year, sdt.month, sdt.day) a = api_gen(startTS, endTS, 100) self.assertTrue( a.daysInRange[0][0][0] == shouldStartAm, "first date incorrect expected:" + str(shouldStartAm) + " got :" + str(a.daysInRange[0][0][0]))
class Subtitle: def __init__(self, name_file): self.name_file = name_file self.timestamp = TimeStamp() self.raw_subtitle = [] self.list_subtitle = [] self.timestamps = [] def separate_timestamp(self): self.raw_subtitle = HandleFiles.read_lines_from_file(self.name_file) self.raw_subtitle = Functions.remove_of_list(self.raw_subtitle, Constants.NEWLINE, False) for line in self.raw_subtitle: if self.timestamp.is_timestamp(line): tuple_time = self.timestamp.pick_timestamp(line) self.timestamps.append(tuple_time) def fix_timestamp(self): if len(self.timestamps): time_saved = self.timestamps[-1][0] timestamp_fixed = [] for line in reversed(self.timestamps): timestamp_fixed.append(tuple([line[0], time_saved])) time_saved = line[0] timestamp_fixed.reverse() timestamp_fixed.pop(-1) timestamp_fixed.append(tuple([self.timestamps[-1][0], self.timestamps[-1][1]])) return timestamp_fixed else: print("Insert lines in .srt file!\n" "Exit program!\n") sys.exit() def fix_subtitles(self, timestamp_fixed): i = 0 for line in self.raw_subtitle: if self.timestamp.is_timestamp(line): self.list_subtitle.append(f'{timestamp_fixed[i][0]}{Constants.ARROW}{timestamp_fixed[i][1]}') i += 1 else: self.list_subtitle.append(line) return self.list_subtitle def run(self, overwrite): self.separate_timestamp() time_fixed = self.fix_timestamp() subs_fixed = self.fix_subtitles(time_fixed) if overwrite == 'No': shutil.copy2(self.name_file, f'{self.name_file}_ORIGINAL') HandleFiles.write_to_file(subs_fixed, self.name_file)
def __init__(self, id=''): if not id: self.id = generate_uuid() else: self.id = id self.pickup_pose = Area() self.delivery_pose = Area() self.earliest_pickup_time = TimeStamp() self.latest_pickup_time = TimeStamp() self.user_id = '' self.load_type = '' self.load_id = '' self.priority = -1
def from_dict(sub_area_reservation_dict): sub_area_reservation = SubAreaReservation() sub_area_reservation.sub_area_id = sub_area_reservation_dict[ 'subAreaId'] sub_area_reservation.task_id = sub_area_reservation_dict['taskId'] sub_area_reservation.robot_id = sub_area_reservation_dict['robotId'] sub_area_reservation.start_time = TimeStamp.from_str( sub_area_reservation_dict['startTime']) sub_area_reservation.end_time = TimeStamp.from_str( sub_area_reservation_dict['endTime']) sub_area_reservation.status = sub_area_reservation_dict['status'] sub_area_reservation.required_capacity = sub_area_reservation_dict[ 'requiredCapacity'] return sub_area_reservation
def update_timestamp(message): header = message.get('header') if header: header.update(timeStamp=TimeStamp().to_str()) else: header = MessageFactoryBase.get_header(None) message.update(header)
def mark_done(key): (journal, todos, notebooks) = load_journal() (jornalname, _) = notebooks.notebooks[notebooks.current_notebook] rightnow = TimeStamp() elapsed_hours = rightnow.elapsed_hours(key) journal_entry = journal.pop(key) db.removeentry(key) journal_entry.title = rightnow.timestamp() old_entry = journal_entry.entry new_entry = "{}<br>@{}+{:.4f}".format(old_entry, key, elapsed_hours) journal_entry.entry = new_entry journal[journal_entry.title] = journal_entry db.updateentry(journal_entry.title, new_entry) db.addentry(jornalname, journal_entry.title, new_entry) todos.pop(key) notebooks.save_current(journal, todos)
def done(self, filter='', entry=''): cherrypy.response.headers[ 'Cache-Control'] = 'no-cache, no-store, must-revalidate' c = 0 body = "<div class=\"container\"><div class=\"col-sm-7\">" body += "<form name=\"dome\" id=\"dome\" method=\"GET\" action='add'>" body += "<textarea rows=\"5\" cols=\"49\" name=\"entry\"></textarea>" body += "<input type=\"hidden\" name=\"interval\" id=\"interval\"/>" body += "<button type=\"button\" onclick=\"domeaction();\">Enter</button>" #body += """<button type="submit"></button>""" body += "</form></div><div class=\"col-sm-5\"><h3>{VERSIONNUM}</h3><h4>{PWD}</h4></div></div>" body = body + "<table><tr><th>timestamp</th><th>Entry</th></tr>" global BASEHTML, DLINE if filter is None or filter == '': dayfilter = TimeStamp().daypart() else: dayfilter = TimeStamp(filter).daypart() title = 'Done entries' journal, todo_dict, notebook = notes.load_journal(filter=dayfilter) for i in reversed(sorted(journal.keys())): #if filter == '' or filter in i: if i in todo_dict.keys(): if todo_dict[i] == True: pass else: c = c + 1 body = body + DLINE.format(COUNT=c, DATETIME=journal[i].title, ENTRY=journal[i].entry.replace( '""', '"').replace( "''", "'")) else: c = c + 1 body = body + DLINE.format(COUNT=c, DATETIME=journal[i].title, ENTRY=journal[i].entry.replace( '""', '"').replace("''", "'")) body = body + "</table>" #print(body) VN = config.version() CD = os.getcwd() print(VN) print(CD) body = body.format(VERSIONNUM=VN, PWD=CD) return BASEHTML.format(TITLE=title, BODY=body, dayfilter=TimeStamp().daypart())
def is_executable(self): """Returns True if the given task needs to be dispatched based on the task schedule; returns False otherwise """ current_time = TimeStamp() if self.start_time < current_time: return True else: return False
class Validation: def __init__(self, dictionary=None, baseurl=None): if dictionary is None or baseurl is None: raise ValidationError('all parameters are mandatory') self._from_dict(dictionary, baseurl) def __str__(self): status = ValStatus.status.getk(self.status) if self.started is None: started = '<not started>' ended = started timetaken = started elif self.ended is not None: started = self.started ended = self.ended timetaken = ended-started else: started = self.started ended = '<not completed>' timetaken = TimeStamp()-started timetaken = str(timetaken) + ' (so far)' package = str(self.package).replace('\n', '\n ') return \ 'Validation:\n' \ ' - Id : %d\n' \ ' - Session : %s\n' \ ' - Added : %s\n' \ ' - Started : %s\n' \ ' - Ended : %s\n' \ ' - Delta : %s\n' \ ' - Status : %s\n' \ ' - %s' \ % (self.id, self.get_session_tag(), self.inserted, started, ended, timetaken, status, package) def _from_dict(self, dictionary, baseurl): self.id = dictionary['validation_id'] self.inserted = TimeStamp( dictionary['inserted'] ) if dictionary['started'] is not None: self.started = TimeStamp( dictionary['started'] ) else: self.started = None if dictionary['ended'] is not None: self.ended = TimeStamp( dictionary['ended'] ) else: self.ended = None self.status = dictionary['status'] self.package_id = dictionary['package_id'] self.package = AliPack(baseurl=baseurl, dictionary=dictionary) def get_session_tag(self): return '%s-%s-%s-%s-utc' % (self.package.version, self.package.platform, self.package.arch, self.inserted.get_formatted_str('%Y%m%d-%H%M%S'))
def copyto(daykey, journalname): today = TimeStamp() sql0 = """ SELECT entry as entry FROM journal WHERE title LIKE '{}' """ query0 = sql0.format(daykey) with sqlite3.connect('journal.db') as conz: cur = conz.cursor() execor = cur.execute(query0) for row in execor: entry = row[0] start = 1 + len(journalname) newentry = entry[start:] newkey = today.timestamp() sql = """ INSERT INTO journal VALUES('{}' , "{}" , '{}' ) """ query = sql.format(newkey, newentry, journalname) with sqlite3.connect('journal.db') as conx: cursor = conx.cursor() cursor.execute(query) return newentry
def get_header(message_type, meta_model='msg', recipients=[]): if recipients is not None and not isinstance(recipients, list): raise Exception("Recipients must be a list of strings") return { "header": { 'type': message_type, 'metamodel': 'ropod-%s-schema.json' % meta_model, 'msgId': generate_uuid(), 'timestamp': TimeStamp().to_str(), 'receiverIds': recipients } }
def add_validation(self, pack): cursor = self._db.cursor() inserted = TimeStamp() status = self.status.NOT_RUNNING cursor.execute('SELECT package_id FROM package WHERE tarball=?', (pack.tarball,)) package_id = cursor.fetchone()['package_id'] # ValueError self._log.debug('found id %d for %s' % (package_id, pack.tarball)) # if a validation for that package which is NOT_RUNNING or RUNNING already exists, don't insert cursor.execute(''' INSERT INTO validation(inserted,status,package_id) SELECT ?,?,? WHERE NOT EXISTS ( SELECT 1 FROM validation WHERE package_id=? AND ( status == ? OR status == ? ) ) ''', (inserted.get_timestamp_usec_utc(), status, package_id, package_id, self.status.NOT_RUNNING, self.status.RUNNING)) self._db.commit() if cursor.lastrowid == 0: self._log.debug('validation for %s already queued or in progress' % pack.tarball) return False else: self._log.debug('validation for %s queued with id %d' % (pack.tarball,cursor.lastrowid)) return True
def _from_dict(self, dictionary, baseurl): self.id = dictionary['validation_id'] self.inserted = TimeStamp( dictionary['inserted'] ) if dictionary['started'] is not None: self.started = TimeStamp( dictionary['started'] ) else: self.started = None if dictionary['ended'] is not None: self.ended = TimeStamp( dictionary['ended'] ) else: self.ended = None self.status = dictionary['status'] self.package_id = dictionary['package_id'] self.package = AliPack(baseurl=baseurl, dictionary=dictionary)
def all(self): _, _, notebook = notes.load_journal() body = "<form method=\"GET\" action=\"undo\"><input type=\"text\" name=\"key\"><button type=\"submit\">UNDO</button></form><hr>" body += "<table><tr><th>timestamp</th><th><Entry></th></tr>" global BASEHTML, DLINE title = 'All Journal Entries' journal, _, _ = notes.load_journal() for i in reversed(sorted(journal.keys())): body = body + FLINE.format(DATETIME=journal[i].title, ENTRY=journal[i].entry) body = body + "</table>" return BASEHTML.format( journal=notebook.notebooks[notebook.current_notebook], TITLE=title, BODY=body, dayfilter=TimeStamp().daypart())
def todo(self): body = "" body = body + "<form method=\"GET\" action='add'>" body = body + "<textarea rows=\"5\" cols=\"49\" name=\"entry\"></textarea>" body += "<input type=\"checkbox\" name=\"todo\"/>" body += "<button type=\"submit\">Enter</button>" body += "</form>" body += "<table><tr><th>timestamp</th><th><Entry></th></tr>" global BASEHTML, DLINE title = 'To do entries' journal, todo_dict, notebook = notes.load_journal() if todo_dict: for i in reversed(sorted(todo_dict.keys())): if todo_dict[i] == True and i in journal: body = body + ELINE.format(DATETIME=journal[i].title, ENTRY=journal[i].entry) body = body + "</table>" return BASEHTML.format( journal=notebook.notebooks[notebook.current_notebook], TITLE=title, BODY=body, dayfilter=TimeStamp().daypart())
def get_sensors(self): logging.debug("getting temperature and other sensor data") try: val = requests.get("https://dweet.io/get/latest/dweet/for/%s-sensors" % self._thingid, timeout=self._requests_timeout).json() delta = (TimeStamp()-TimeStamp.from_iso_str(val["with"][0]["created"])).total_seconds()*1000 if delta > self._temp_tolerance_ms: raise Exception("temperature data is too old (> %d ms)" % self._temp_tolerance_ms) self.temp = float(val["with"][0]["content"]["temp"]); self._humi = float(val["with"][0]["content"].get("humi", None)); self._sensors_errors = 0 except Exception as e: self._sensors_errors = self._sensors_errors + 1 if self._sensors_errors >= self._sensors_errors_tolerance: logging.error("too many sensors reading errors (%d out of %d): resetting data: %s" % (self._sensors_errors, self._sensors_errors_tolerance, e)) self.temp = None self._humi = None else: logging.warning("sensor error, retaining old data: %s" % e) logging.debug("sensor data: temp=%s, humidity=%s" % (self.temp and "%.1f°C" % self.temp or "n/a", self._humi and "%.1f%%" % self._humi or "n/a"))
def from_dict(task_dict): task = Task() task.id = task_dict['id'] task.loadType = task_dict['loadType'] task.loadId = task_dict['loadId'] task.team_robot_ids = task_dict['team_robot_ids'] task.earliest_start_time = TimeStamp.from_str( task_dict['earliest_start_time']) task.latest_start_time = TimeStamp.from_str( task_dict['latest_start_time']) task.estimated_duration = datetime.timedelta( minutes=task_dict['estimated_duration']) task.earliest_finish_time = TimeStamp.from_str( task_dict['earliest_finish_time']) task.latest_finish_time = TimeStamp.from_str( task_dict['latest_finish_time']) start_time = task_dict.get('start_time', None) if start_time: task.start_time = TimeStamp.from_str(start_time) else: task.start_time = start_time finish_time = task_dict.get('finish_time', None) if finish_time: task.finish_time = TimeStamp.from_str(finish_time) else: task.finish_time = finish_time task.pickup_pose = Area.from_dict(task_dict['pickup_pose']) task.delivery_pose = Area.from_dict(task_dict['delivery_pose']) task.priority = task_dict['priority'] task.status = TaskStatus.from_dict(task_dict['status']) task.hard_constraints = task_dict['hard_constraints'] for robot_id, actions in task_dict['robot_actions'].items(): task.robot_actions[robot_id] = list() for action_dict in actions: action = Action.from_dict(action_dict) task.robot_actions[robot_id].append(action) return task
def day(self, d2g, offset=0): global METAHDR offset = int(offset) body = "" if d2g is not None and len(d2g) > 0: when = TimeStamp(d2g) if offset < 0: then = when.yesterday() ts = then[0:8] return self.day(ts) #window.location.replace("day?d2g="+ts+"&offset=-1"); elif offset > 0: then = when.tomorrow()[0:8] return self.day(then) #window.location.replace("day?d2g="+then+"&offset=1"); else: then = TimeStamp(d2g) body = """ <title>{dayfilter}</title> <link rel="stylesheet" href="bootstrap/bootstrap.min.css"> <link href="static/style.css" rel="stylesheet"> <script> function dstr2date(dstr) {{ var year = timestamp.substr(0,4); var month = timestamp.substr(4,2)-1; var day = timestamp.substr(6,2); var drdt = new Date(year,month,day,0,0,0,0); }} function oops(timestamp) {{ var temp = String(timestamp); var tslc = temp.slice(0,8); document.getElementById("d2g").text=tslc; document.getElementById("offset").value=-1; //window.location.replace("day?d2g="+tslc+"&offset=-1"); document.getElementById("nomen").submit(); }} function goback(dstr) {{ var temp = String(dstr); var ts = temp.slice(0,8); document.getElementById("d2g").text=ts; document.getElementById("offset").value=1; //window.location.replace("day?d2g="+ts+"&offset=1"); document.getElementById("nomen").submit(); }} </script> </head> <body> <a href="/">Home</a> <form name="nome" id="nomen" method="get" action="day"> <input type="hidden" id="offset" name="offset"> <input type="button" value="Previous" onclick="oops({dayfilter});"> <label for="d2g">DATE: </label> <input type="text" id="d2g" name="d2g" value="{dayfilter}"> <input type="button" value="Next" onclick="goback({dayfilter});"> </form> """ bodylist = aday(d2g) for line in bodylist: body += line if type(then) == type(body): return METAHDR + body.format(dayfilter=then) else: return METAHDR + body.format(dayfilter=then.timestamp()) else: body = """ <title>Jef's Journal</title> <link rel="stylesheet" href="bootstrap/bootstrap.min.css"> <link href="static/style.css" rel="stylesheet"> </head> <body> <a href="/">Home</a> <form name="dome" id="dome" method="get" action="day"> <input type="text" id="d2g" name="d2g"> <input type="submit" value="Find"> </form> """ return METAHDR + body
def __init__(self, name_file): self.name_file = name_file self.timestamp = TimeStamp() self.raw_subtitle = [] self.list_subtitle = [] self.timestamps = []
def add(self, entry, dayfilter=None, interval=None): if dayfilter == None: dayfilter = TimeStamp().timestamp() notes.create_journal_entry(entry, dayfilter, interval) return self.done(filter=dayfilter)
def get_latest_command(self): logging.debug('checking for commands') skipped = 0 try: r = requests.get("https://dweet.io/get/dweets/for/%s" % self._thingid, timeout=self._requests_timeout) except requests.exceptions.RequestException as e: logging.error('failed to get latest commands: %s' % e) return False if r.status_code != 200: logging.error('invalid status code received: %d' % r.status_code) return False try: for idx,item in enumerate( r.json()['with'] ): try: now_ts = TimeStamp() msg_ts = TimeStamp.from_iso_str( item['created'] ) if (now_ts-msg_ts).total_seconds() <= self._cmd_expiry_s: # consider this message: it is still valid # nonce must be unique nonce = item['content']['nonce'] nonce_is_unique = True for ridx, ritem in reversed( list(enumerate(r.json()['with']))[idx+1:] ): if 'nonce' in ritem['content'] and nonce == ritem['content']['nonce']: nonce_is_unique = False break if nonce_is_unique: msg = self.decrypt_msg(item['content']) if not nonce_is_unique: logging.warning('duplicated nonce (%s): possible replay attack, ignoring' % nonce) elif msg is None: logging.warning('cannot decrypt message, check password') else: msg_real_ts = TimeStamp.from_iso_str( msg['timestamp'] ) msg_delta = msg_ts - msg_real_ts if abs(msg_delta.total_seconds())*1000 > self._tolerance_ms: logging.warning('message timestamps mismatch, ignoring') elif msg['type'].lower() == 'command': logging.debug('found valid command') logging.debug(json.dumps(msg, indent=2)) save = False if msg['override_program'] != self._override_program: save = True if msg['program'] != self._program: save = True lid = msg['id'] self._override_program = msg['override_program'] self._program = msg['program'] self._lastcmd_id = lid if save: if self.save_conf(): logging.info('new configuration saved') else: logging.error('cannot save new configuration') break else: # messages from now on are too old, no need to parse the rest logging.debug('found first outdated message, skipping the rest') break except (KeyError, TypeError) as e: logging.debug('error parsing, skipped: %s' % e) skipped = skipped+1 pass except Exception as e: logging.error('error parsing response: %s' % e) return False if skipped > 0: logging.warning('invalid messages skipped: %d' % skipped) return True
def __init__(self, entry, time=None): self.entry = self.encrypt(entry) if time: self.title=time else: self.title = TimeStamp().timestamp()
class TaskRequest(object): def __init__(self, id=''): if not id: self.id = generate_uuid() else: self.id = id self.pickup_pose = Area() self.delivery_pose = Area() self.earliest_pickup_time = TimeStamp() self.latest_pickup_time = TimeStamp() self.user_id = '' self.load_type = '' self.load_id = '' self.priority = -1 def to_dict(self): request_dict = dict() request_dict['id'] = self.id request_dict['pickupLocation'] = self.pickup_pose.name request_dict['pickupLocationLevel'] = self.pickup_pose.floor_number request_dict['deliveryLocation'] = self.delivery_pose.name request_dict['deliveryLocationLevel'] = self.delivery_pose.floor_number request_dict['earliestPickupTime'] = self.earliest_pickup_time.to_str() request_dict['latestPickupTime'] = self.latest_pickup_time.to_str() request_dict['userId'] = self.user_id request_dict['loadType'] = self.load_type request_dict['loadId'] = self.load_id request_dict['priority'] = self.priority return request_dict @staticmethod def from_dict(request_dict): id = request_dict.get('id', generate_uuid()) request = TaskRequest(id=id) request.load_type = request_dict["loadType"] request.load_id = request_dict["loadId"] request.user_id = request_dict["userId"] request.earliest_pickup_time = TimeStamp.from_str( request_dict["earliestPickupTime"]) request.latest_pickup_time = TimeStamp.from_str( request_dict["latestPickupTime"]) pickup_area_dict = request_dict.get('pickup_pose', None) if pickup_area_dict: request.pickup_pose = Area.from_dict(pickup_area_dict) else: # when the provided dict is from json schema request.pickup_pose = Area() request.pickup_pose.name = request_dict.get("pickupLocation", '') request.pickup_pose.floor_number = request_dict.get( "pickupLocationLevel", 0) delivery_area_dict = request_dict.get('delivery_pose', None) if delivery_area_dict: request.delivery_pose = Area.from_dict(delivery_area_dict) else: # when the provided dict is from json schema request.delivery_pose = Area() request.delivery_pose.name = request_dict.get( "deliveryLocation", '') request.delivery_pose.floor_number = request_dict.get( "deliveryLocationLevel", 0) request.priority = request_dict["priority"] return request @staticmethod def to_csv(task_dict): """ Prepares dict to be written to a csv :return: dict """ to_csv_dict = flatten_dict(task_dict) return to_csv_dict