def load_grid(self, game_data): self.game_data = game_data loaded_grid = self.game_data.get_data('board') if not loaded_grid: print('Board.load_grid: unfound data, so create new one.') self.load_new_grid() self.save_grid() print('Board.load_grid: created.') else: # build Pieces from data for line_key in loaded_grid: line = loaded_grid[line_key] for cell_key in line: cell = line[cell_key] if cell == '-': utils.access(self.grid, '%s/%s' % (line_key, cell_key), '-') else: if cell['s'] == 'w': side = self.sides['white'] else: side = self.sides['black'] piece = ChessPiece.PiecePawn(self, cell['n'], side) utils.access(self.grid, '%s/%s' % (line_key, cell_key), piece)
def set_data(self, path, new_data): """ writes data, at given path if specified """ """ - data : dict """ """ - path : url-style, leaf content is set. if no path specified, data is written at root """ if path: # write data in path data = json.loads(self.data) utils.access(data, path, new_data) self.data = json.dumps(data, separators=(',', ':'), cls=MyEncoder) else: # write data at root self.data = json.dumps(new_data, separators=(',', ':'), cls=MyEncoder) self.save() return True
def get_data(self, path=None): """ loads data, from given key if specified """ """ - path : url-style, leaf content is returned. if no path specified, all data is returned """ data = json.loads(self.data) if path: return utils.access(data, path) else: return data
def set(self, path, value): _data = getattr(self._modelObject, self._modelField) utils.access(_data, path, value) getattr(self._modelObject, 'save')()
def get(self, path=None): _data = getattr(self._modelObject, self._modelField) return utils.access(_data, path)
def set_piece_at(self, x, y, piece): return utils.access(self.grid, '%s/%s' % (x, y), piece)
def get_piece_at(self, x, y): return utils.access(self.grid, '%s/%s' % (x, y))
def get(self, path, default=None): _res = utils.access(self.data, path) if _res is None: return default return _res
def access(value, path): jdata = json.loads(value) data = utils.access(jdata, path) return data
def render_realjson2html(value, path): data = utils.access(value, path) return json2html.convert(json=data)
def render_json2html(value, path): jdata = json.loads(value) data = utils.access(jdata, path) return json2html.convert(json=data)