def get_events(self): """Get events from discovery service Paginate through responses, and execute more responses when no more event items. End requests, when no more event data received. Each event is validated as appropriate data for EventClass, then stored against their timestamp (float) in instance. """ # HTTP Requests try: print("getEvents -- Starting calendar HTTP Requests...") # Scanner Operator Availability # Loop until all pages have been processed. while self.source != None: # Get the next page. response = self.source.execute() # Accessing the response like a dict object with an 'items' key # returns a list of item objects (events). for event in response.get('items', []): # The event object is a dict object. # Store event as EventClass if EventClass.validate(event): obj = EventClass(event) self.dat[obj.get_start()] = obj # Get the next request object by passing the previous request object to # the list_next method. self.source = Configure.config['SERVICE'].events().list_next(self.source, response) except client.AccessTokenRefreshError: print ("The credentials have been revoked or expired, please re-run" "the application to re-authorize") finally: print("getEvents -- Finished calendar HTTP Requests...")
def gen_report(self): """Generate reports based on event storage """ l = list(self.dat.keys()) self.reports['COUNT'] = len(l) self.reports['FIRST'] = min(l) self.reports['TYPE'] = type(self.dat[min(l)]) ## Use first as exemplar self.reports['LAST'] = max(l) self.reports['RANGE'] = max(l) - min(l) reqfrom_t = self.dat[max(l)].get_end() + 1 self.reports['REQUESTFROM'] = EventClass.timestamp_to_datestring(reqfrom_t)
def get_events(self): """ Get events from saved local JSON data 'source' is a path directory for 'history' files """ result = True # Read from source print("History (read) -- Reading from {0}".format(self.source)) try: listing = os.listdir(self.source) for filename in listing: # TODO, deprecate JSON event_json = open(os.path.join(self.source + os.sep + filename)) time_s = float(os.path.splitext(filename)[0]) ## Filename (timestamp) to float json_data = json.load(event_json) if EventClass.validate(json_data): self.dat[time_s] = EventClass(json_data) ## print(EventClass.timestamp_to_datetime(time_s)) event_json.close() except IOError as e: print("I/O error({0}): {1}".format(e.errno, e.strerror)) result = False finally: print("History (read) -- read() finished.") self.gen_report() return result ## # Search SLEIC json ## def _get_event_ref(pi=None,project=None): ## out = [] ## for pi_line in self.config['REQUEST']['EVENT_PARSE']['SLEIC_REF']: ## if pi is not None: ## print('looking for',pi) ## if pi_line["pi"]==pi: ## print('found',pi_line["pi"]) ## for project_line in pi_line["project"]: ## if project is not None: ## print('looking for',project) ## if project_line["id"]==project: ## print('found',project_line["id"]) ## out = project_line["subject"] ## else: ## out.append(project_line["id"]) ## else: ## out.append(pi_line["pi"]) ## ## return out ### Open SLEIC dictionary resource ## try: ## sleic_json = open(SLEIC) ## except IOError as e: ## print("I/O error({0}): {1}".format(e.errno, e.strerror)) ## ## # SLEIC PI, project, and subject parsing ## try: ## sleic_data = json.load(sleic_json) ## #### head = ['pi', 'project-id', 'full-event','date', 'start', 'end', 'duration', 'hrs-use-category','subject-id'] #### #### fwrite = csv.writer(csvfile, delimiter=',') #### write_csv(fwrite,head) ## ## lastweek = (datetime.today() - timedelta(weeks=1)).timestamp() ## tomorrow = (datetime.today() + timedelta(days=1)).timestamp() ## for event in mrislots_events.keys(): ## if (event > lastweek) and (event < tomorrow): #### print(mrislots_events[event].event) ## summary = mrislots_events[event].event.get('summary') ## print('Full Event:', summary) ## pi = re.search('\w{3}\d{1,4}',summary) ## if pi: ## print('PI:',pi.group(0)) ## sub1 = re.sub(pi.group(0),'',summary,1) ## Remove PI ## sub2 = re.sub('^[_\s]*(?<=\w)','',sub1) ## Remove leading underscores ## sumsplit = re.split('[_\s]',sub2,1) ## Split one underscore or space #### proj = re.search('([^_\s]{3,4})(?=\W)?',sub1) ## if len(sumsplit)==1: ## if len(sumsplit[0])==4: ## proj = sumsplit[0] ## print('Suggested project ID:',proj) ## else: ## print('Unclear segment:',sumsplit[0]) ## ## elif len(sumsplit) == 2: ## print('Segment 1:',sumsplit[0]) ## print('Segment 2:',sumsplit[1]) #### print('Project ID:',proj.group(0)) #### sub2 = re.sub(proj.group(0),'',sub1,1) #### sub3 = re.sub('[_\s]','',sub2) #### if sub3: #### print('Subject:',sub3) ## print(mrislots_events[event].event.get('start').get('dateTime')) ## print(mrislots_events[event].event.get('end').get('dateTime')) ## print(mrislots_events[event].duration('m')) ## except IOError as e: ## print("I/O error({0}): {1}".format(e.errno, e.strerror)) ## finally: ## sleic_json.close() ## ##def main(): ## pass ## ##if __name__ == '__main__': ## main()
def start_log(self): self.log['TIME'] = EventClass.today()