def do_profile(self, name: str): if self.client is None: self.info('login first') return profile = None if name is None: identifier = g_facebook.current_user.identifier elif name.startswith('{') and name.endswith('}'): identifier = g_facebook.current_user.identifier profile = json.loads(name) else: identifier = g_facebook.identifier(name) if identifier is None: self.info('I don\'t understand.') return if profile: private_key = g_facebook.private_key_for_signature( identifier=g_facebook.current_user.identifier) assert private_key is not None, 'failed to get private key for client: %s' % self.client # create new profile and set all properties tai = Profile.new(identifier=identifier) for key in profile: tai.set_property(key, profile.get(key)) tai.sign(private_key=private_key) cmd = ProfileCommand.response(identifier=identifier, profile=tai) else: cmd = ProfileCommand.query(identifier=identifier) self.client.send_command(cmd=cmd)
def load_station(identifier: str) -> Station: """ Load station info from 'etc' directory :param identifier - station ID :return station with info from 'dims/etc/{address}/*' """ identifier = g_facebook.identifier(identifier) # check meta meta = g_facebook.meta(identifier=identifier) if meta is None: # load from 'etc' directory meta = Meta( load_station_info(identifier=identifier, filename='meta.js')) if meta is None: raise LookupError('failed to get meta for station: %s' % identifier) elif not g_facebook.save_meta(meta=meta, identifier=identifier): raise ValueError('meta error: %s' % meta) # check private key private_key = g_facebook.private_key_for_signature(identifier=identifier) if private_key is None: # load from 'etc' directory private_key = PrivateKey( load_station_info(identifier=identifier, filename='secret.js')) if private_key is None: pass elif not g_facebook.save_private_key(key=private_key, identifier=identifier): raise AssertionError('failed to save private key for ID: %s, %s' % (identifier, private_key)) # check profile profile = load_station_info(identifier=identifier, filename='profile.js') if profile is None: raise LookupError('failed to get profile for station: %s' % identifier) Log.info('station profile: %s' % profile) name = profile.get('name') host = profile.get('host') port = profile.get('port') # create station if private_key is None: # remote station station = Station(identifier=identifier, host=host, port=port) else: # create profile profile = Profile.new(identifier=identifier) profile.set_property('name', name) profile.set_property('host', host) profile.set_property('port', port) profile.sign(private_key=private_key) if not g_facebook.save_profile(profile=profile): raise AssertionError('failed to save profile: %s' % profile) # local station station = Server(identifier=identifier, host=host, port=port) g_facebook.cache_user(user=station) Log.info('station loaded: %s' % station) return station
def __load_profile(self, identifier: ID) -> Profile: path = self.__path(identifier=identifier) self.info('Loading profile from: %s' % path) dictionary = self.read_json(path=path) if dictionary is not None: # compatible with v1.0 data = dictionary.get('data') if data is None: data = dictionary.get('profile') if data is not None: dictionary['data'] = data dictionary.pop('profile') return Profile(dictionary)
def profile(self, identifier: ID) -> Optional[Profile]: # 1. get from cache info = self.__caches.get(identifier) if info is not None: if 'data' not in info: self.info('empty profile: %s' % info) return info # 2. load from storage info = self.__load_profile(identifier=identifier) if info is None: info = Profile.new(identifier=identifier) # 3. update memory cache self.__caches[identifier] = info return info
def load_user(identifier: str) -> User: identifier = g_facebook.identifier(identifier) # check meta meta = g_facebook.meta(identifier=identifier) if meta is None: # load from 'etc' directory meta = Meta(load_robot_info(identifier=identifier, filename='meta.js')) if meta is None: raise LookupError('failed to get meta for robot: %s' % identifier) elif not g_facebook.save_meta(meta=meta, identifier=identifier): raise ValueError('meta error: %s' % meta) # check private key private_key = g_facebook.private_key_for_signature(identifier=identifier) if private_key is None: # load from 'etc' directory private_key = PrivateKey( load_robot_info(identifier=identifier, filename='secret.js')) if private_key is None: pass elif not g_facebook.save_private_key(key=private_key, identifier=identifier): raise AssertionError('failed to save private key for ID: %s, %s' % (identifier, private_key)) if private_key is None: raise AssertionError('private key not found for ID: %s' % identifier) # check profile profile = load_robot_info(identifier=identifier, filename='profile.js') if profile is None: raise LookupError('failed to get profile for robot: %s' % identifier) Log.info('robot profile: %s' % profile) name = profile.get('name') avatar = profile.get('avatar') # create profile profile = Profile.new(identifier=identifier) profile.set_property('name', name) profile.set_property('avatar', avatar) profile.sign(private_key=private_key) if not g_facebook.save_profile(profile): raise AssertionError('failed to save profile: %s' % profile) # create local user return g_facebook.user(identifier=identifier)