File: game_data_handler.py

package info (click to toggle)
crawl 2%3A0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,188 kB
  • sloc: cpp: 363,709; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (46 lines) | stat: -rw-r--r-- 1,635 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os.path

import tornado
import tornado.web

from webtiles import config

try:
    from typing import Dict
except:
    pass

class GameDataHandler(tornado.web.StaticFileHandler):
    # async def _execute(self, transforms, *args, **kwargs):
    #     await tornado.web.StaticFileHandler._execute(self, transforms, *args, **kwargs)

    def initialize(self):
        if tornado.version_info[0] < 3:
            # ugly extreme backward compatibility hack; can hopefully be removed
            # once tornado 2.4 is out of the picture.
            super(GameDataHandler, self).initialize(".")
            self.root = "/" # just override the superclass for these old versions...
        else:
            super(GameDataHandler, self).initialize("/")

    def parse_url_path(self, url_path):
        # the path should already match "([0-9a-f]*\/.*)", from server.py
        import sys
        version, url_path = url_path.split("/", 1)
        if version not in GameDataHandler._client_paths:
            raise tornado.web.HTTPError(404)
        return super(GameDataHandler, self).parse_url_path(
                        GameDataHandler._client_paths[version] + "/" + url_path)

    def set_extra_headers(self, path):
        if config.get('game_data_no_cache'):
            self.set_header("Cache-Control",
                            "no-cache, no-store, must-revalidate")
            self.set_header("Pragma", "no-cache")
            self.set_header("Expires", "0")

    _client_paths = {} # type: Dict[str, str]

    @classmethod
    def add_version(cls, version, path):
        cls._client_paths[version] = os.path.abspath(path)