<s> import ast <EOL> import sqlite3 <EOL> import os <EOL> import stat <EOL> import threading <EOL> from openpds . core . models import Profile <EOL> from openpds . accesscontrol . models import Settings , Context <EOL> from openpds import settings <EOL> import datetime <EOL> import math <EOL> class AccessControlledInternalDataStore ( object ) : <EOL> def __init__ ( self , ids ) : <EOL> self . ids = ids <EOL> self . profile = ids . profile <EOL> self . datastore_owner_id = self . profile . id <EOL> self . app_id = ids . app_id <EOL> self . lab_id = ids . lab_id <EOL> self . mapping = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> def saveAnswer ( self , key , data ) : <EOL> return self . ids . saveAnswer ( key , data ) <EOL> def getAnswer ( self , key ) : <EOL> return self . ids . getAnswer ( key ) <EOL> def getAnswerList ( self , key ) : <EOL> return self . ids . getAnswerList ( key ) <EOL> def saveData ( self , data , collectionname ) : <EOL> return self . ids . saveData ( data , collectionname ) <EOL> def testServiceEnabled ( self ) : <EOL> result = False <EOL> settingsObjects = Settings . objects . filter ( datastore_owner_id = self . datastore_owner_id , app_id = self . app_id , lab_id = self . lab_id , service_id = self . service_id ) <EOL> if len ( settingsObjects ) == <NUM_LIT:0> : <EOL> result = True <EOL> return result <EOL> def getData ( self , probe , startTime , endTime ) : <EOL> dataLocations = None <EOL> if probe == "<STR_LIT>" : <EOL> dataLocations = set ( ) <EOL> cursor = self . ids . getData ( probe , startTime , endTime ) <EOL> if cursor : <EOL> for entry in cursor : <EOL> dataLocations . add ( ( entry [ u'<STR_LIT:value>' ] [ u'<STR_LIT>' ] , entry [ u'<STR_LIT:value>' ] [ u'<STR_LIT>' ] ) ) <EOL> column = self . mapping [ probe ] <EOL> probe_flag = False <EOL> try : <EOL> settingsObject = Settings . objects . get ( datastore_owner_id = self . datastore_owner_id , app_id = self . app_id , lab_id = self . lab_id ) <EOL> try : <EOL> contextObject = Context . objects . get ( pk = settingsObject . context_label_id ) <EOL> except Context . DoesNotExist : <EOL> contextObject = None <EOL> except Settings . DoesNotExist : <EOL> settingsObject = None <EOL> contextObject = None <EOL> if settingsObject is None : <EOL> probe_flag = True <EOL> validTimes = True <EOL> validLocations = True <EOL> else : <EOL> probe_flag = settingsObject . __dict__ [ column ] <EOL> if startTime is None or endTime is None : <EOL> validTimes = False <EOL> validLocations = False <EOL> else : <EOL> validTimes = self . checkTime ( startTime , endTime , contextObject ) <EOL> validLocations = self . checkLocation ( startTime , endTime , contextObject , dataLocations ) <EOL> if probe_flag and validTimes and validLocations : <EOL> return self . ids . getData ( probe , startTime , endTime ) <EOL> else : <EOL> return None <EOL> def checkTime ( self , startTime , endTime , context ) : <EOL> timestamp_start = datetime . datetime . fromtimestamp ( startTime ) <EOL> start_hour = int ( timestamp_start . strftime ( '<STR_LIT>' ) ) <EOL> start_minute = int ( timestamp_start . strftime ( '<STR_LIT>' ) ) <EOL> start_time = datetime . datetime . now ( ) . replace ( hour = start_hour , minute = start_minute , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> start_day = timestamp_start . weekday ( ) <EOL> timestamp_end = datetime . datetime . fromtimestamp ( endTime ) <EOL> end_hour = int ( timestamp_end . strftime ( '<STR_LIT>' ) ) <EOL> end_minute = int ( timestamp_end . strftime ( '<STR_LIT>' ) ) <EOL> end_time = datetime . datetime . now ( ) . replace ( hour = end_hour , minute = end_minute , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> end_day = timestamp_end . weekday ( ) <EOL> timeFlag = False <EOL> dayFlag = True <EOL> context_start = [ int ( start . strip ( ) ) for start in context . context_duration_start . split ( '<STR_LIT::>' ) ] <EOL> context_start_hour = context_start [ <NUM_LIT:0> ] <EOL> context_start_minute = context_start [ <NUM_LIT:1> ] <EOL> context_start_time = datetime . datetime . now ( ) . replace ( hour = context_start_hour , minute = context_start_minute , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> context_end = [ int ( end . strip ( ) ) for end in context . context_duration_end . split ( '<STR_LIT::>' ) ] <EOL> context_end_hour = context_end [ <NUM_LIT:0> ] <EOL> context_end_minute = context_end [ <NUM_LIT:1> ] <EOL> context_end_time = datetime . datetime . now ( ) . replace ( hour = context_end_hour , minute = context_end_minute , second = <NUM_LIT:0> , microsecond = <NUM_LIT:0> ) <EOL> if context_start_time <= start_time and end_time <= context_end_time : <EOL> timeFlag = True <EOL> context_days = context . context_duration_days <EOL> context_days_list = [ int ( x ) for x in context_days [ <NUM_LIT:1> : - <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' ) ] <EOL> if start_day < end_day : <EOL> start_day_temp = start_day <EOL> while start_day_temp <= end_day : <EOL> if context_days_list [ start_day_temp ] != <NUM_LIT:1> : <EOL> dayFlag = False <EOL> break <EOL> start_day_temp += <NUM_LIT:1> <EOL> elif start_day > end_day : <EOL> start_day_temp = start_day <EOL> while start_day_temp <= <NUM_LIT:6> : <EOL> if context_days_list [ start_day_temp ] != <NUM_LIT:1> : <EOL> dayFlag = False <EOL> break <EOL> start_day_temp += <NUM_LIT:1> <EOL> end_day_temp = <NUM_LIT:0> <EOL> while end_day_temp <= end_day : <EOL> if context_days_list [ end_day_temp ] != <NUM_LIT:1> : <EOL> dayFlag = False <EOL> break <EOL> end_day_temp += <NUM_LIT:1> <EOL> else : <EOL> if context_days_list [ start_day ] != <NUM_LIT:1> : <EOL> dayFlag = False <EOL> return dayFlag and timeFlag <EOL> def checkLocation ( self , startTime , endTime , context , dataLocations ) : <EOL> if dataLocations is None : <EOL> dataLocations = set ( ) <EOL> cursor = self . ids . getData ( "<STR_LIT>" , startTime , endTime ) <EOL> if cursor : <EOL> for entry in cursor : <EOL> dataLocations . add ( ( entry [ u'<STR_LIT:value>' ] [ u'<STR_LIT>' ] , entry [ u'<STR_LIT:value>' ] [ u'<STR_LIT>' ] ) ) <EOL> contextLocations = set ( ) <EOL> temp_places = [ x . strip ( ) for x in context . context_places [ <NUM_LIT:1> : - <NUM_LIT:1> ] . split ( "<STR_LIT>" ) if x ] <EOL> if len ( temp_places ) == <NUM_LIT:0> : <EOL> return True <EOL> contextLocations = [ ] <EOL> for place in temp_places : <EOL> if '<STR_LIT>' in place : <EOL> placeValues = [ float ( x . strip ( ) ) for x in place [ : - <NUM_LIT:1> ] [ <NUM_LIT:1> : - <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' ) ] <EOL> else : <EOL> placeValues = [ float ( x . strip ( ) ) for x in place [ <NUM_LIT:1> : - <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' ) ] <EOL> contextLocations . append ( ( placeValues [ <NUM_LIT:0> ] , placeValues [ <NUM_LIT:1> ] ) ) <EOL> dataLocationsChecks = [ <NUM_LIT:0> ] * len ( dataLocations ) <EOL> dataLocationsIndex = <NUM_LIT:0> <EOL> R = <NUM_LIT> <EOL> for dataLocation in dataLocations : <EOL> for contextLocation in contextLocations : <EOL> dLat = math . radians ( dataLocation [ <NUM_LIT:0> ] - contextLocation [ <NUM_LIT:0> ] ) <EOL> dLon = math . radians ( dataLocation [ <NUM_LIT:1> ] - contextLocation [ <NUM_LIT:1> ] ) <EOL> lat1 = math . radians ( contextLocation [ <NUM_LIT:0> ] ) <EOL> lat2 = math . radians ( dataLocation [ <NUM_LIT:0> ] ) <EOL> a = math . sin ( dLat / <NUM_LIT:2> ) ** <NUM_LIT:2> + math . cos ( lat1 ) * math . cos ( lat2 ) * math . sin ( dLon / <NUM_LIT:2> ) ** <NUM_LIT:2> <EOL> c = <NUM_LIT:2> * math . asin ( math . sqrt ( a ) ) <EOL> distance = R * c * <NUM_LIT:1000> <EOL> if distance < <NUM_LIT> : <EOL> dataLocationsChecks [ dataLocationsIndex ] = <NUM_LIT:1> <EOL> break <EOL> dataLocationsIndex += <NUM_LIT:1> <EOL> if all ( check == <NUM_LIT:1> for check in dataLocationsChecks ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def getAccessControlledInternalDataStore ( ids ) : <EOL> try : <EOL> internalDataStore = AccessControlledInternalDataStore ( ids ) <EOL> except Exception as e : <EOL> internalDataStore = None <EOL> print str ( e ) <EOL> return internalDataStore </s>
