def __init__(self, user, game): self.lock = Lock() self.game = game self.user = user try: path = config['gameprofile_db'] except KeyError: LOG.error('gameprofile_db path config variable not set') return # Create gameprofile folder and user folder on the game path path = join_path(path, game.slug) if not create_dir(path): error_msg = 'User GameProfile path \"%s\" could not be created.' % path LOG.error(error_msg) raise GameProfileError(error_msg) self.path = get_absolute_path(path) self.defaults = {} default_yaml_path = unicode(get_absolute_path(join_path(game.path, 'defaultgameprofiles.yaml'))) if path_exists(default_yaml_path): with open(default_yaml_path, 'r') as f: try: file_defaults = yaml.load(f) self.defaults = dict((v['user'], v['value']) for v in file_defaults['profiles']) except (yaml.YAMLError, KeyError, TypeError) as e: LOG.error('Failed loading default game profiles: %s', str(e))
def __init__(self, user, game): self.lock = Lock() self.game = game self.user = user try: path = config['gameprofile_db'] except KeyError: LOG.error('gameprofile_db path config variable not set') return # Create gameprofile folder and user folder on the game path path = join_path(path, game.slug) if not create_dir(path): error_msg = 'User GameProfile path \"%s\" could not be created.' % path LOG.error(error_msg) raise GameProfileError(error_msg) self.path = get_absolute_path(path) self.defaults = {} default_yaml_path = unicode( get_absolute_path(join_path(game.path, 'defaultgameprofiles.yaml'))) if path_exists(default_yaml_path): with open(default_yaml_path, 'rt') as f: try: file_defaults = yaml.load(f) self.defaults = dict((v['user'], v['value']) for v in file_defaults['profiles']) except (yaml.YAMLError, KeyError, TypeError) as e: LOG.error('Failed loading default game profiles: %s' % str(e))
def _set_userbadges_path(self): if not self.userbadges_path: try: path = config['userbadges_db'] except KeyError: LOG.error('badges_db path config variable not set') return if not create_dir(path): LOG.error('Game badges path \"%s\" could not be created.' % path) self.userbadges_path = norm_path(join_path(get_absolute_path(path), self.game.slug) + '.yaml')
def post(self, slug, filename): """ Saves given contents to file to game folder. """ game = get_game_by_slug(slug) if not game: self.set_status(404) return self.finish({'ok': False, 'msg': 'Game does not exist: %s' % slug}) if not filename: self.set_status(400) return self.finish({'ok': False, 'msg': 'Missing filename'}) if '..' in filename: self.set_status(403) return self.finish({'ok': False, 'msg': 'Cannot write outside game folder'}) content = self.get_argument('content') self.request.body = None self.request.arguments = None file_path = path_join(get_absolute_path(game.get_path()), normpath(filename)) file_dir = dirname(file_path) if not create_dir(file_dir): LOG.error('Failed to create directory at "%s"', file_dir) self.set_status(500) return self.finish({'ok': False, 'msg': 'Failed to create directory'}) if content: try: content = content.encode('utf-8') except UnicodeEncodeError as e: LOG.error('Failed to encode file contents: %s', str(e)) self.set_status(500) return self.finish({'ok': False, 'msg': 'Failed to encode file contents'}) LOG.info('Writing file at "%s" (%d bytes)', file_path, len(content)) else: LOG.info('Writing empty file at "%s"', file_path) try: file_obj = open(file_path, 'wb') try: file_obj.write(content) finally: file_obj.close() except IOError as e: LOG.error('Failed to write file at "%s": %s', file_path, str(e)) self.set_status(500) return self.finish({'ok': False, 'msg': 'Failed to write file'}) return self.finish({'ok': True})
def _set_userbadges_path(self): if not self.userbadges_path: try: path = config['userbadges_db'] except KeyError: LOG.error('badges_db path config variable not set') return if not create_dir(path): LOG.error('Game badges path \"%s\" could not be created.', path) self.userbadges_path = norm_path(join_path(get_absolute_path(path), self.game.slug) + '.yaml')
def _set_path(self): if not self.path: try: path = config['leaderboards_db'] except KeyError: LOG.error('leaderboards_db path config variable not set') return path = join_path(path, self.game.slug) if not create_dir(path): LOG.error('Game leaderboards path \"%s\" could not be created.', path) self.path = join_path(path, self.key + '.yaml')
def create_path(self): try: path = config['datashare_db'] except KeyError: LOG.error('datashare_db path config variable not set') raise # Create datashare folder path = join_path(path, self.game.slug) if not create_dir(path): LOG.error('DataShare path \"%s\" could not be created.' % path) raise IOError('DataShare path \"%s\" could not be created.' % path) return get_absolute_path(path)
def _get_task_path(slug, recipient, notification_type, filename=None): try: path = config['notifications_db'] except KeyError: raise GameNotificationsUnsupportedException('notifications_db path config variable not set') path = join_path(path, slug, recipient, notification_type) if not create_dir(path): raise GameNotificationPathError('User GameNotification path \"%s\" could not be created.' % path) if filename: return get_absolute_path(join_path(path, filename)) else: return path
def _get_task_path(slug, recipient, notification_type, filename=None): try: path = config['notifications_db'] except KeyError: raise GameNotificationsUnsupportedException( 'notifications_db path config variable not set') path = join_path(path, slug, recipient, notification_type) if not create_dir(path): raise GameNotificationPathError( 'User GameNotification path \"%s\" could not be created.' % path) if filename: return get_absolute_path(join_path(path, filename)) else: return path
def __init__(self, session=None, game=None, user=None): if session is None: self.game = game self.user = user else: self.game = session.game self.user = session.user try: path = config['userdata_db'] except KeyError: LOG.error('userdata_db path config variable not set') return # Create userdata folder and user folder on the game path path = join_path(path, self.game.slug, self.user.username) if not create_dir(path): raise UserDataPathError('User UserData path \"%s\" could not be created.' % path) self.path = get_absolute_path(path)
def __init__(self, user, game, game_store_items): self.user = user self.game = game self.game_store_items = game_store_items self.user_items = {} try: path = config['storeitems_db'] except KeyError: LOG.error('storeitems_db path config variable not set') return # Create store items folder and user folder on the game path path = join_path(path, self.game.slug) if not create_dir(path): raise StoreError('User store items path \"%s\" could not be created.' % path) self.path = get_absolute_path(path) self.lock = Lock() self._read()
def save(self, attrs): """ Save this game object in persistent storage. """ # check that there's a path in the given attributes if 'path' not in attrs.keys(): raise GamePathNotFoundError('No Path given') # check that it can be used if not create_dir(attrs['path']): raise GamePathError('Path "%s" could not be created.' % attrs['path']) # update the game self.update(attrs) # update modified time t = localtime(time()) self.modified = strftime("%H:%M | %d/%m/%Y", t) # trim unnecessary values and write game to manifest file write_manifest(self.to_dict(), self.manifest_name) # if the game has been saved, it's not temporary anymore self.is_temporary = False
def __init__(self, user, game, game_store_items): self.user = user self.game = game self.game_store_items = game_store_items self.user_items = {} try: path = config['storeitems_db'] except KeyError: LOG.error('storeitems_db path config variable not set') return # Create store items folder and user folder on the game path path = join_path(path, self.game.slug) if not create_dir(path): raise StoreError( 'User store items path \"%s\" could not be created.' % path) self.path = get_absolute_path(path) self.lock = Lock() self._read()
def post(self, slug, filename): """ Saves given contents to file to game folder. """ game = get_game_by_slug(slug) if not game: self.set_status(404) return self.finish({ 'ok': False, 'msg': 'Game does not exist: %s' % slug }) if not filename: self.set_status(400) return self.finish({'ok': False, 'msg': 'Missing filename'}) if '..' in filename: self.set_status(403) return self.finish({ 'ok': False, 'msg': 'Cannot write outside game folder' }) content_type = self.request.headers.get('Content-Type', '') if content_type and 'application/x-www-form-urlencoded' in content_type: content = self.get_argument('content') binary = False else: content = self.request.body binary = True self.request.body = None self.request.arguments = None file_path = path_join(get_absolute_path(game.get_path()), normpath(filename)) file_dir = dirname(file_path) if not create_dir(file_dir): LOG.error('Failed to create directory at "%s"', file_dir) self.set_status(500) return self.finish({ 'ok': False, 'msg': 'Failed to create directory' }) if content: if not binary: try: content = content.encode('utf-8') except UnicodeEncodeError as e: LOG.error('Failed to encode file contents: %s', str(e)) self.set_status(500) return self.finish({ 'ok': False, 'msg': 'Failed to encode file contents' }) LOG.info('Writing file at "%s" (%d bytes)', file_path, len(content)) else: LOG.info('Writing empty file at "%s"', file_path) try: file_obj = open(file_path, 'wb') try: file_obj.write(content) finally: file_obj.close() except IOError as e: LOG.error('Failed to write file at "%s": %s', file_path, str(e)) self.set_status(500) return self.finish({'ok': False, 'msg': 'Failed to write file'}) return self.finish({'ok': True})