def _get_merged_params(self, params, plugin_info_vo): """ if there is plugin_info at params, We need to merge plugin_info with plugin_info_vo """ if 'plugin_info' in params: plugin_info = PluginInfo(plugin_info_vo) dict_plugin_info = MessageToDict(plugin_info, preserving_proto_field_name=True) #dict_plugin_info = plugin_info_vo.to_dict() new_plugin_info = params.get('plugin_info', {}) # Check version db_version = dict_plugin_info['version'] req_version = new_plugin_info['version'] version_check = False if db_version != req_version: # update metadata version_check = True # new_plugin_info.update(dict_plugin_info) dict_plugin_info.update(new_plugin_info) new_params = params.copy() new_params['plugin_info'] = dict_plugin_info return new_params, version_check else: return params, False
def to_dict(self): claim = MessageToDict(self.claim.message, preserving_proto_field_name=True) claim.update(claim.pop(self.claim_type)) if 'languages' in claim: claim['languages'] = self.langtags return claim
async def play(self, ws, observation): success = True request_data = api.Request( data=api.RequestData(ability_id=True, unit_type_id=True, upgrade_id=True) ) await asyncio.wait_for(ws.send(request_data.SerializeToString()), 5) try: result = await asyncio.wait_for(ws.recv(), 5) data_response = api.Response.FromString(result) game_data = data_response.data request_game_info = api.Request(game_info=api.RequestGameInfo()) await asyncio.wait_for(ws.send(request_game_info.SerializeToString()), 5) result = await asyncio.wait_for(ws.recv(), 5) game_info_response = api.Response.FromString(result) # If game is still on if game_data.units: obj = decode_observation(observation.observation.observation, game_data, game_info_response) obs = MessageToDict(observation) obs = str(obs) obs = obs.replace("\'", "\"") obs = obs.replace("False", "false") obs = obs.replace("True", "true") obs = json.loads(obs,encoding="UTF-8") game_meta = api.Request( game_info=api.RequestGameInfo() ) await ws.send(game_meta.SerializeToString()) result = await ws.recv() game_meta = api.Response.FromString(result) game_meta = MessageToDict(game_meta) game_meta = str(game_meta) game_meta = game_meta.replace("\'", "\"") game_meta = game_meta.replace("False", "false") game_meta = game_meta.replace("True", "true") game_meta = json.loads(game_meta,encoding="UTF-8") game_meta = game_meta.get("gameInfo", None) game_meta.pop("modNames") game_meta.pop("options") game_meta.pop("mapName") if("localMapPath" in game_meta.keys()): game_meta.pop("localMapPath") game_meta.pop("playerInfo") game_meta.update(game_meta["startRaw"]) game_meta.pop("startRaw") game_meta.pop("mapSize") await self.process_step(ws, obj, raw=(obs, game_meta, game_data)) # function = self.decision_function # alvailable_actions = self.query_alvailable_actions() # to_do_action = function(observation, alvailable_actions) # while(to_do_action and alvailable_actions): # self.send_order(self, to_do_action) # to_do_action = self.query_alvailable_actions() except asyncio.TimeoutError: return False return True
async def deserialize_user(REST_API_URL, address): state_data = await address_state(REST_API_URL, address) ##decoding data stored on the blockchain if not state_data: return False acc = user_pb2.UserAccount() acc.ParseFromString(state_data) account = MessageToDict(acc, preserving_proto_field_name=True) account.update({"address": address}) return account
async def deserialize_share_asset(REST_API_URL, address): logging.info(f"Now deserializing share_asset present on {address}") state_data = await address_state(REST_API_URL, address) ##decoding data stored on the blockchain if not state_data: return False acc = share_asset_pb2.ShareAsset() acc.ParseFromString(state_data) asset = MessageToDict(acc, preserving_proto_field_name=True) asset.update({"address": address}) return asset
async def deserialize_org_account(REST_API_URL, address): logging.info( f"Now deserializing organization account present on {address}") state_data = await address_state(REST_API_URL, address) ##decoding data stored on the blockchain if not state_data: return False acc = organization_account_pb2.OrganizationAccount() acc.ParseFromString(state_data) account = MessageToDict(acc, preserving_proto_field_name=True) account.update({"address": address}) return account
def _check_plugin_state(self, plugins, params): """ Check plugin state first if state == RE_PROVISIONING, delete plugin first """ for plugin in plugins: dict_plugin = MessageToDict(plugin, preserving_proto_field_name=True) dict_plugin.update(params) state = dict_plugin.get('state', None) _LOGGER.debug(f'[_check_plugin_state] plugin_info: {dict_plugin}') if state == 'RE_PROVISIONING' or state == 'ERROR': _LOGGER.debug(f'[_check_plugin_state] params: {params}') self.install_plugin(dict_plugin) delete_params = { 'plugin_id': dict_plugin['plugin_id'], 'version': dict_plugin['version'], 'domain_id': dict_plugin['domain_id'] } self.delete_plugin(delete_params)
def _install_plugins(self, plugins, params): """ Install plugin based on plugins Args: params (dict): { 'hostname': str, 'name': str, 'domain_id': str } """ for plugin in plugins: dict_plugin = MessageToDict(plugin, preserving_proto_field_name=True) dict_plugin.update(params) _LOGGER.debug(f'[_install_plugins] plugin_info: {dict_plugin}') if not self._exist_plugin(dict_plugin): _LOGGER.debug(f'[_install_plugins] params: {params}') self.install_plugin(dict_plugin) _LOGGER.debug(f'[_install_plugins] installed: {params}')
async def deserialize_asset(REST_API_URL, address): logging.info(f"Now deserializing asset present on {address}") state_data = await address_state(REST_API_URL, address) ##decoding data stored on the blockchain if not state_data: logging.error(f"No asset data present corresponding to {address}") return False acc = asset_pb2.Asset() acc.ParseFromString(state_data) asset = MessageToDict(acc, preserving_proto_field_name=True) ##This is True, implies this asset has been created by the owner ot its ##child but havent been transffered to anyone else if not asset.get("ownership_received"): data = { "ownership_received": None, "received_on": None, "parent_address": None, "issuer_child_zero_pub": None } asset.update(data) if not asset.get("child_zero_pub"): asset.update({"child_zero_pub": None}) asset.update({"address": address}) return asset
async def deserialize_float_account(REST_API_URL, address): state_data = await address_state(REST_API_URL, address) ##decoding data stored on the flt_acc = float_account_pb2.FloatAccount() flt_acc.ParseFromString(state_data) float_account = MessageToDict(flt_acc, preserving_proto_field_name=True) float_account.update({"address": address}) ##this is to handle accounts which havent claimed their account if float_account.get("claimed_by"): account_address = addresser.create_organization_account_address( float_account["claimed_by"], 0) else: account_address = None data = {"claimed": None, "claimed_by": None, "claimed_on": None} float_account.update(data) float_account.update({"account_address": account_address}) if not float_account.get("child_zero_pub"): float_account.update({"child_zero_pub": None}) return float_account
def write_json_measurements(episode_path, data_point_id, measurements, control, control_noise, state, directions=None): with open( os.path.join(episode_path, 'measurements_' + data_point_id.zfill(5) + '.json'), 'w') as fo: jsonObj = MessageToDict(measurements) jsonObj.update(state) jsonObj.update({'steer': control.steer}) jsonObj.update({'throttle': control.throttle}) jsonObj.update({'brake': control.brake}) jsonObj.update({'hand_brake': control.hand_brake}) jsonObj.update({'reverse': control.reverse}) jsonObj.update({'steer_noise': control_noise.steer}) jsonObj.update({'throttle_noise': control_noise.throttle}) jsonObj.update({'brake_noise': control_noise.brake}) if directions is not None: jsonObj.update({'directions': directions}) fo.write(json.dumps(jsonObj, sort_keys=True, indent=4))
async def load_replay(self, replay_file, id=0): print(replay_file) async with websockets.connect("ws://{0}:{1}/sc2api".format( self.host.address, self.host.port)) as ws: replay_meta = api.Request(replay_info=api.RequestReplayInfo( replay_path=replay_file)) await ws.send(replay_meta.SerializeToString()) result = await ws.recv() metadata = api.Response.FromString(result) self.replay_info = { "map": metadata.replay_info.map_name, "races": [ metadata.replay_info.player_info[0].player_info. race_requested, metadata.replay_info.player_info[1].player_info. race_requested, ], "results": [ metadata.replay_info.player_info[0].player_result.result, metadata.replay_info.player_info[1].player_result.result, ], } print(self.replay_info) msg = api.Request(start_replay=api.RequestStartReplay( replay_path=replay_file, observed_player_id=id, options=api.InterfaceOptions(raw=True, score=False), )) await ws.send(msg.SerializeToString()) time.sleep(1) result = await ws.recv() response = api.Response.FromString(result) print(response) game_meta = api.Request(game_info=api.RequestGameInfo()) await ws.send(game_meta.SerializeToString()) result = await ws.recv() game_meta = api.Response.FromString(result) game_meta = MessageToDict(game_meta) game_meta = str(game_meta) game_meta = game_meta.replace("\'", "\"") game_meta = game_meta.replace("False", "false") game_meta = game_meta.replace("True", "true") game_meta = json.loads(game_meta, encoding="UTF-8") if "gameInfo" in game_meta.keys(): game_meta = game_meta.get("gameInfo", None) game_meta.pop("modNames") game_meta.pop("options") game_meta.pop("mapName") if ("localMapPath" in game_meta.keys()): game_meta.pop("localMapPath") game_meta.pop("playerInfo") game_meta.update(game_meta["startRaw"]) game_meta.pop("startRaw") game_meta.pop("mapSize") self.game_info = game_meta self.status = "started" return True else: return False