Exemple #1
0
    def test_fix(self):
        v = BuildNumber()
        v.version = [23, 73, 234]
        v.fix()

        self.assertEqual(23, v.version[0])
        self.assertEqual(73, v.version[1])
        self.assertEqual(235, v.version[2])
Exemple #2
0
    def test_minor(self):
        v = BuildNumber()
        v.version = [23, 73, 234]
        v.minor()

        self.assertEqual(23, v.version[0])
        self.assertEqual(74, v.version[1])
        self.assertEqual(0, v.version[2])
Exemple #3
0
    def test_saveToFile(self):
        v = BuildNumber()
        v.version = [23, 73, 234]
        v.saveToFile(self.file.name)

        # load js version file
        with open(self.file.name, 'r') as h:
            c = h.read()
        self.assertEqual(c, 'const version = "23.73.234";')
Exemple #4
0
    def test_loadFromFile(self):
        # create js version file
        with open(self.file.name, 'w') as h:
            h.write('const version = "15.624.115";')

        v = BuildNumber()
        v.loadFromFile(self.file.name)
        self.assertEqual(15, v.version[0])
        self.assertEqual(624, v.version[1])
        self.assertEqual(115, v.version[2])
Exemple #5
0
    def __init__(self, argv=list(), pref_dir=None):
        appname = 'pyvtt'
        for arg in argv:
            if arg.startswith('--appname='):
                appname = arg.split('--appname=')[1]

            elif arg.startswith('--prefdir='):
                pref_dir = arg.split('--prefdir=')[1]

        self.paths = utils.PathApi(appname=appname, root=pref_dir)

        # setup per-game stuff
        self.checksums = dict()
        self.locks = dict()

        # webserver stuff
        self.host = '0.0.0.0'
        self.hosting = {
            'domain': 'localhost',
            'port': 8080,
            'socket': '',
            'ssl': False,
            'hosting_url': ''
        }
        self.debug = False
        self.quiet = False
        self.shards = list()

        self.main_db = None

        # blacklist for GM names and game URLs
        self.gm_blacklist = [
            '', 'static', 'token', 'music', 'vtt', 'websocket', 'thumbnail'
        ]
        self.url_regex = '^[A-Za-z0-9_\-.]+$'

        # maximum file sizes for uploads (in MB)
        self.file_limit = {
            "token": 2,
            "background": 10,
            "game": 30,
            "music": 10,
            "num_music": 5
        }
        self.playercolors = [
            '#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#FF00FF'
        ]

        self.local_gm = False
        self.localhost = False
        self.title = appname
        self.links = list()
        self.expire = 3600 * 24 * 30  # default: 30d
        self.login = dict()  # login settings
        self.login['type'] = ''
        self.login_api = None  # login api instance
        self.notify = dict()  # crash notify settings
        self.notify['type'] = ''
        self.notify_api = None  # notify api instance

        self.cache = None  # later engine cache

        # handle commandline arguments
        self.debug = '--debug' in argv
        self.quiet = '--quiet' in argv
        self.local_gm = '--local-gm' in argv
        self.localhost = '--localhost' in argv

        if self.localhost:
            assert (not self.local_gm)

        self.logging = utils.LoggingApi(
            quiet=self.quiet,
            info_file=self.paths.getLogPath('info'),
            error_file=self.paths.getLogPath('error'),
            access_file=self.paths.getLogPath('access'),
            warning_file=self.paths.getLogPath('warning'),
            stats_file=self.paths.getLogPath('stats'))

        self.logging.info(
            'Started Modes: debug={0}, quiet={1}, local_gm={2} localhost={3}'.
            format(self.debug, self.quiet, self.local_gm, self.localhost))

        # load fancy url generator api ... lol
        self.url_generator = utils.FancyUrlApi(self.paths)

        # handle settings
        settings_path = self.paths.getSettingsPath()
        if not os.path.exists(settings_path):
            # create default settings
            settings = {
                'title': self.title,
                'links': self.links,
                'file_limit': self.file_limit,
                'playercolors': self.playercolors,
                'shards': self.shards,
                'expire': self.expire,
                'hosting': self.hosting,
                'login': self.login,
                'notify': self.notify
            }
            with open(settings_path, 'w') as h:
                json.dump(settings, h, indent=4)
                self.logging.info('Created default settings file')
        else:
            # load settings
            with open(settings_path, 'r') as h:
                settings = json.load(h)
                self.title = settings['title']
                self.links = settings['links']
                self.file_limit = settings['file_limit']
                self.playercolors = settings['playercolors']
                self.shards = settings['shards']
                self.expire = settings['expire']
                self.hosting = settings['hosting']
                self.login = settings['login']
                self.notify = settings['notify']
            self.logging.info('Settings loaded')

        # export server constants to javascript-file
        self.constants = utils.ConstantExport()
        self.constants(self)

        # show argv help
        if '--help' in argv:
            print('Commandline args:')
            print('    --debug       Starts in debug mode.')
            print('    --quiet       Starts in quiet mode.')
            print('    --local-gm    Starts in local-GM-mode.')
            print('    --localhost   Starts in localhost mode.')
            print('')
            print('Debug Mode:     Enables debug level logging.')
            print('Quiet Mode:     Disables verbose outputs.')
            print(
                'Local-GM Mode:  Replaces `localhost` in all created links by the public ip.'
            )
            print(
                'Localhost Mode: Restricts server for being used via localhost only. CANNOT BE USED WITH --local-gm'
            )
            print('')
            print('See {0} for custom settings.'.format(settings_path))
            sys.exit(0)

        self.logging.info('Loading domain...')
        if self.localhost:
            # overwrite domain and host to localhost
            self.host = '127.0.0.1'
            self.hosting['domain'] = 'localhost'
            self.logging.info('Overwriting connections to localhost')
        elif self.local_gm or self.hosting['domain'] == '':
            # overwrite domain with public ip
            self.hosting['domain'] = self.getPublicIp()
            self.logging.info('Overwriting Domain by Public IP: {0}'.format(
                self.hosting['domain']))

        self.logging.info('Loading login API...')
        if self.local_gm:
            self.login['type'] = ''
            self.logging.info('Defaulting to dev-login for local-gm')
        else:
            # load patreon API
            if self.login['type'] == 'patreon':
                protocol = 'https' if self.hasSsl() else 'http'
                port = self.hosting['port']
                if port in [80, 443]:
                    port_suffix = ''  # port not required in URL
                else:
                    port_suffix = ':{0}'.format(port)
                host_callback = '{0}/vtt/patreon/callback'.format(
                    self.getUrl())
                # create patreon query API
                self.login_api = utils.PatreonApi(host_callback=host_callback,
                                                  **self.login)

        if self.notify['type'] == 'email':
            # create email notify API
            self.notify_api = utils.EmailApi(self,
                                             appname=appname,
                                             **self.notify)

        self.logging.info('Loading main database...')
        # create main database
        self.main_db = createMainDatabase(self)

        # setup db_session to all routes
        self.app = bottle.default_app()
        self.app.install(db_session)

        # setup error catching
        if self.debug:
            # let bottle catch exceptions
            self.app.catchall = True

        else:
            # use custom middleware
            self.error_reporter = utils.ErrorReporter(self)
            self.app.install(self.error_reporter.plugin)

        # dice roll specific timers
        self.recent_rolls = 30  # rolls within past 30s are recent
        self.latest_rolls = 60 * 10  # rolls within the past 10min are up-to-date

        # load version number
        bn = BuildNumber()
        bn.loadFromFile(pathlib.Path('static') / 'version.js')
        self.version = str(bn)

        # game cache
        self.cache = EngineCache(self)
Exemple #6
0
 def test_str(self):
     v = BuildNumber()
     v.version = ['a', 'b', 'c']
     s = '{0}'.format(v)
     self.assertEqual(s, 'a.b.c')
Exemple #7
0
 def test_init(self):
     v = BuildNumber()
     self.assertEqual(0, v.version[0])
     self.assertEqual(0, v.version[1])
     self.assertEqual(1, v.version[2])