Example #1
0
def compile_mo_file(domain, lc_messages_path):
    """Creates or updates a mo file in the locales folder."""
    if not HAS_PYTHON_GETTEXT:
        logger.critical(
            "Unable to compile messages: Python `gettext` library missing.")
        return

    base = join(lc_messages_path, domain)
    pofile = str(base + '.po')
    mofile = str(base + '.mo')

    po_mtime = 0
    try:
        po_mtime = os.stat(pofile)[ST_MTIME]
    except (IOError, OSError):
        return

    mo_mtime = 0
    if os.path.exists(mofile):
        # Update mo file?
        try:
            mo_mtime = os.stat(mofile)[ST_MTIME]
        except (IOError, OSError):
            return

    if po_mtime > mo_mtime:
        try:
            mo = Msgfmt(pofile, domain).getAsFile()
            fd = open(mofile, 'wb')
            fd.write(mo.read())
            fd.close()
        except (IOError, OSError, PoSyntaxError):
            logger.warn('Error while compiling %s' % pofile)
def compile_mo_file(domain, lc_messages_path):
    """Creates or updates a mo file in the locales folder."""
    if not HAS_PYTHON_GETTEXT:
        logger.warn("Unable to compile messages: Python `gettext` library missing.")
        return

    base = join(lc_messages_path, domain)
    pofile = str(base + '.po')
    mofile = str(base + '.mo')

    po_mtime = 0
    try:
        po_mtime = os.stat(pofile)[ST_MTIME]
    except (IOError, OSError):
        return

    mo_mtime = 0
    if os.path.exists(mofile):
        # Update mo file?
        try:
            mo_mtime = os.stat(mofile)[ST_MTIME]
        except (IOError, OSError):
            return

    if po_mtime > mo_mtime:
        try:
            mo = Msgfmt(pofile, domain).getAsFile()
            fd = open(mofile, 'wb')
            fd.write(mo.read())
            fd.close()
        except (IOError, OSError, PoSyntaxError):
            logger.warn('Error while compiling %s' % pofile)
Example #3
0
 def test_test4(self):
     po_file = open(os.path.join(FOLDER, "test4.po"), "rb")
     po = Msgfmt(po_file)
     po.read(header_only=True)
     po_file.close()
     self.assertTrue(po.messages[u""].startswith("Project-Id-Version: foo"))
     self.assertEqual(po.encoding, u"iso-8859-1")
Example #4
0
 def test_test6_unicode_header(self):
     po_file = open(os.path.join(FOLDER, "test6.po"), "rb")
     po = Msgfmt(po_file)
     po.read(header_only=True)
     po_file.close()
     self.assertTrue(po.messages[u""].startswith(u"Project-Id-Version: Tøst 1.0"))
     self.assertEqual(po.encoding, u"utf-8")
Example #5
0
 def test_test4(self):
     po_file = open(os.path.join(FOLDER, 'test4.po'), 'rb')
     po = Msgfmt(po_file)
     po.read(header_only=True)
     po_file.close()
     self.assertTrue(
         po.messages[u('')].startswith('Project-Id-Version: foo'))
     self.assertEqual(po.encoding, u('iso-8859-1'))
Example #6
0
 def test_test6_unicode_header(self):
     po_file = open(os.path.join(FOLDER, 'test6.po'), 'rb')
     po = Msgfmt(po_file)
     po.read(header_only=True)
     po_file.close()
     self.assertTrue(po.messages[u('')].startswith(
         u('Project-Id-Version: Tøst 1.0', 'utf-8')))
     self.assertEqual(po.encoding, u('utf-8'))
Example #7
0
 def test_test5_unicode_name(self):
     po_file = open(os.path.join(FOLDER, 'test5.po'), 'rb')
     po = Msgfmt(po_file, name=u('dømain', 'utf-8'))
     try:
         with self.assertRaises(PoSyntaxError):
             po.read()
     finally:
         po_file.close()
     self.assertEqual(po.encoding, u('utf-8'))
Example #8
0
 def test_test5_unicode_name(self):
     po_file = open(os.path.join(FOLDER, "test5.po"), "rb")
     po = Msgfmt(po_file, name=u"dømain")
     try:
         with self.assertRaises(PoSyntaxError):
             po.read()
     finally:
         po_file.close()
     self.assertEqual(po.encoding, u"utf-8")
Example #9
0
 def test_test5(self):
     po_file = open(os.path.join(FOLDER, 'test5.po'), 'rb')
     po = Msgfmt(po_file)
     try:
         with self.assertRaises(PoSyntaxError):
             po.read()
     finally:
         po_file.close()
     self.assertEqual(po.encoding, u'utf-8')
Example #10
0
 def test_plural(self):
     po_file = open(os.path.join(FOLDER, "test_plural.po"), "rb")
     po = Msgfmt(po_file)
     try:
         po.read()
     finally:
         po_file.close()
     self.assertEqual(
         set(po.messages.keys()), set([u"", u"m1", u"m2 ø\x00{d} ømsgid", u"øcontext\x04m3 ø\x00{d} ømsgid context"])
     )
Example #11
0
 def test_escape(self):
     po_file = open(os.path.join(FOLDER, "test_escape.po"), "rb")
     po = Msgfmt(po_file)
     try:
         with self.assertRaises(PoSyntaxError) as e:
             po.read()
         self.assertTrue("line 19" in e.exception.msg)
         self.assertEqual(po.encoding, u"utf-8")
     finally:
         po_file.close()
Example #12
0
 def test_escape(self):
     po_file = open(os.path.join(FOLDER, 'test_escape.po'), 'rb')
     po = Msgfmt(po_file)
     try:
         with self.assertRaises(PoSyntaxError) as e:
             po.read()
         self.assertTrue('line 19' in e.exception.msg)
         self.assertEqual(po.encoding, u('utf-8'))
     finally:
         po_file.close()
Example #13
0
 def test_plural(self):
     po_file = open(os.path.join(FOLDER, 'test_plural.po'), 'rb')
     po = Msgfmt(po_file)
     try:
         po.read()
     finally:
         po_file.close()
     self.assertEqual(
         set(po.messages.keys()),
         set([u'', u'm1', u'm2 ø\x00{d} ømsgid',
              u'øcontext\x04m3 ø\x00{d} ømsgid context']))
Example #14
0
def compile_mo_file(domain, lc_messages_path):
    """Creates or updates a mo file in the locales folder."""

    base = join(lc_messages_path, domain)
    pofile = str(base + '.po')
    mofile = str(base + '.mo')

    po_mtime = _safe_mtime(pofile)
    mo_mtime = _safe_mtime(mofile) or 0

    if po_mtime is None:
        logger.debug("Unable to access %s (%s)",
                     pofile, po_mtime)
        return

    if po_mtime > mo_mtime:
        try:
            # Msgfmt.getAsFile returns io.BytesIO on Python 3, and cStringIO.StringIO
            # on Python 2; sadly StringIO isn't a proper context manager, so we have to
            # wrap it with `closing`. Also, Msgfmt doesn't properly close a file
            # it opens for reading if you pass the path, but it does if you pass
            # the file.
            with open(pofile, 'rb') as pofd:
                with closing(Msgfmt(pofd, domain).getAsFile()) as mo:
                    with open(mofile, 'wb') as fd:
                        fd.write(mo.read())
        except PoSyntaxError as err:
            logger.warning('Syntax error while compiling %s (%s).', pofile, err.msg)
        except (IOError, OSError) as err:
            logger.warning('Error while compiling %s (%s).', pofile, err)
def _updateMoFile(name, msgpath, lang, domain, mofile):
    """
    Creates or updates a mo file in the locales folder. Returns True if a
    new file was created.
    """
    pofile = join(msgpath, name)
    create = False
    update = False

    try:
        po_mtime = os.stat(pofile)[ST_MTIME]
    except (IOError, OSError):
        po_mtime = 0

    if os.path.exists(mofile):
        # Update mo file?
        try:
            mo_mtime = os.stat(mofile)[ST_MTIME]
        except (IOError, OSError):
            mo_mtime = 0

        if po_mtime > mo_mtime:
            # Update mo file
            update = True
        else:
            # Mo file is current
            return
    else:
        # Create mo file
        create = True

    if create or update:
        try:
            mo = Msgfmt(pofile, domain).getAsFile()
            fd = open(mofile, 'wb')
            fd.write(mo.read())
            fd.close()

        except (IOError, OSError, PoSyntaxError):
            log('Error while compiling %s' % pofile, logging.WARNING)
            return

        if create:
            return True

    return None
Example #16
0
 def compile_mo_file(podir, pofile):
     domain = pofile[:-3]
     mofile = os.path.join(podir, domain + '.mo')
     pofile = os.path.join(podir, pofile)
     # check timestamps:
     try:
         do_compile = os.stat(mofile).st_mtime < os.stat(pofile).st_mtime
     except OSError:
         do_compile = True
     if do_compile:
         self.logger.debug('Compiling po-file: %s' % pofile)
         try:
             mo = Msgfmt(pofile, name=domain).getAsFile()
             fd = open(mofile, 'wb')
             fd.write(mo.read())
             fd.close()
         except (IOError, OSError, PoSyntaxError):
             self.logger.warn('Error while compiling %s' % pofile)
def compile(conf, domain, lang):
    path = os.path.join(os.environ.get('INSTANCE_HOME'), 'var/amberjack_i18n')
    path = os.path.normpath(path)
    if not os.path.isdir(path):
        os.makedirs(path)

    filename = '%s-%s' % (lang, os.path.basename(conf.name))

    po = open(os.path.join(path, filename), 'w')
    po.write(conf.read())
    po.close()
    po = open(po.name, 'r')
    _mo = Msgfmt(po, domain).getAsFile()
    mo = open(os.path.join(path, filename.replace('.po','.mo')), 'wb')
    mo.write(_mo.read())
    mo.close()
    _mo.close()
    return str(mo.name)
Example #18
0
def compile_mo_files(input_dir=I18N_PATH, output_dir=MO_FILES_BASE):
    """
    Compile all cc-style po files to mo files.

    Keyword arguments:
    - input_dir: Directory of input files to compile
    - output_dir: Directory where we'll put compiled MO files
    """
    for catalog in os.listdir(input_dir):
        catalog_path = os.path.join(input_dir, catalog)

        po_path = os.path.join(catalog_path, 'cc_org.po')

        if not os.path.isdir(catalog_path) or not os.path.exists(po_path):
            continue

        po_mtime = os.stat(po_path)[ST_MTIME]

        if not os.path.exists(output_dir):
            os.mkdir(output_dir)
        if not os.path.exists(os.path.join(output_dir, catalog)):
            os.mkdir(os.path.join(output_dir, catalog))
        if not os.path.exists(os.path.join(
                output_dir, catalog, 'LC_MESSAGES')):
            os.mkdir(os.path.join(
                    output_dir, catalog, 'LC_MESSAGES'))

        mo_path = os.path.join(
            output_dir, catalog, 'LC_MESSAGES', 'cc_org.mo')

        # don't compile mo files when we don't need to.
        if os.path.exists(mo_path):
            mo_mtime = os.stat(mo_path)[ST_MTIME]
            if po_mtime == mo_mtime:
                continue

        mo_data = Msgfmt(po_path, 'cc_org').getAsFile()
        fd = open(mo_path, 'wb')
        fd.write(mo_data.read())
        fd.close()
def _load_i18n_dir(basepath):
    """
    Loads an i18n directory (Zope3 PTS format)
    Format:
        Products/MyProduct/i18n/*.po
    The language and domain are stored in the po file
    """
    # load po files
    basepath = os.path.normpath(basepath)
    log('Looking for po files in ' + basepath, logging.DEBUG)
    names = fnmatch.filter(os.listdir(basepath), '*.po')
    if not names:
        log('Nothing found in ' + basepath, logging.DEBUG)
        return

    registered = []

    for name in names:
        lang = None
        domain = None
        pofile = join(basepath, name)
        po = Msgfmt(pofile, None)
        po.read(header_only=True)
        header = po.messages.get('', None)
        if header is not None:
            mime_header = {}
            pairs = [l.split(':', 1) for l in header.split('\n') if l]
            for key, value in pairs:
                mime_header[key.strip().lower()] = value.strip()
            lang = mime_header.get('language-code', None)
            domain = mime_header.get('domain', None)
            if lang is not None and domain is not None:
                if _checkLanguage(lang):
                    reg = (name, basepath, lang, domain, True)
                    _register_catalog_file(*reg)
                    registered.append(name)

    log('Initialized:', detail = str(len(registered)) +
        (' message catalogs in %s\n' % basepath))
 def compile_mo_file(podir, pofile):
     domain = pofile[:-3]
     mofile = os.path.join(podir, domain + '.mo')
     pofile = os.path.join(podir, pofile)
     # check timestamps:
     try:
         do_compile = os.stat(mofile).st_mtime < os.stat(pofile).st_mtime
     except OSError:
         do_compile = True
     if do_compile:
         self.logger.debug('Compiling po-file: %s' % pofile)
         try:
             mo = Msgfmt(pofile, name=domain).getAsFile()
             fd = open(mofile, 'wb')
             fd.write(mo.read())
             fd.close()
         except (IOError, OSError, PoSyntaxError):
             msg = "Error while compiling language file %s" % mofile
             if self._quiet:
                 self.logger.debug(msg)
             else:
                 self.logger.error(msg)
Example #21
0
def compile_mo_files(input_dir=I18N_PATH, output_dir=MO_FILES_BASE):
    """
    Compile all cc-style po files to mo files.

    Keyword arguments:
    - input_dir: Directory of input files to compile
    - output_dir: Directory where we'll put compiled MO files
    """
    for catalog in os.listdir(input_dir):
        catalog_path = os.path.join(input_dir, catalog)

        po_path = os.path.join(catalog_path, 'cc_org.po')

        if not os.path.isdir(catalog_path) or not os.path.exists(po_path):
            continue

        po_mtime = os.stat(po_path)[ST_MTIME]

        if not os.path.exists(output_dir):
            os.mkdir(output_dir)
        if not os.path.exists(os.path.join(output_dir, catalog)):
            os.mkdir(os.path.join(output_dir, catalog))
        if not os.path.exists(os.path.join(output_dir, catalog,
                                           'LC_MESSAGES')):
            os.mkdir(os.path.join(output_dir, catalog, 'LC_MESSAGES'))

        mo_path = os.path.join(output_dir, catalog, 'LC_MESSAGES', 'cc_org.mo')

        # don't compile mo files when we don't need to.
        if os.path.exists(mo_path):
            mo_mtime = os.stat(mo_path)[ST_MTIME]
            if po_mtime == mo_mtime:
                continue

        mo_data = Msgfmt(po_path, 'cc_org').getAsFile()
        fd = open(mo_path, 'wb')
        fd.write(mo_data.read())
        fd.close()
Example #22
0
    def run(self):
        pot_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                               'pymget', 'i18n')

        for path, dirs, files in os.walk(pot_dir):
            for f in files:

                if not f.endswith('.pot'):
                    continue

                lang = os.path.splitext(f)[0]
                src = os.path.join(path, f)
                dest_path = os.path.join(self.build_lib, 'pymget', 'i18n',
                                         lang, 'LC_MESSAGES')
                dest = os.path.join(dest_path, 'messages.mo')

                if not os.path.exists(dest_path):
                    os.makedirs(dest_path)

                with open(dest, 'wb') as mo_file:
                    msg_fmt = Msgfmt(src)
                    mo_data = msg_fmt.get()
                    mo_file.write(mo_data)
 def compile_mo_file(podir, pofile):
     domain = pofile[:-3]
     mofile = os.path.join(podir, domain + ".mo")
     pofile = os.path.join(podir, pofile)
     # check timestamps:
     try:
         do_compile = os.stat(mofile).st_mtime < os.stat(
             pofile).st_mtime
     except OSError:
         do_compile = True
     if do_compile:
         self.logger.debug("Compiling po-file: %s" % pofile)
         try:
             mo = Msgfmt(pofile, name=domain).getAsFile()
             fd = open(mofile, "wb")
             fd.write(mo.read())
             fd.close()
         except (IOError, OSError, PoSyntaxError):
             msg = "Error while compiling language file %s" % mofile
             if self._quiet:
                 self.logger.debug(msg)
             else:
                 self.logger.error(msg)
Example #24
0
    def compare_po_mo(self, poname, moname):
        po_file = None
        mo_file = None
        try:
            po_file = file(os.path.join(self.folder, poname), 'rb')
            po = Msgfmt(po_file).get()
            mo_file = file(os.path.join(self.folder, moname), 'rb')
            mo = ''.join(mo_file.readlines())
        finally:
            if po_file is not None:
                po_file.close()
            if mo_file is not None:
                mo_file.close()

        self.failUnless(mo == po)
Example #25
0
    def compare_po_mo(self, poname, moname):
        po_file = None
        mo_file = None
        try:
            po_file = open(os.path.join(FOLDER, poname), 'rb')
            po = Msgfmt(po_file).get()
            mo_file = open(os.path.join(FOLDER, moname), 'rb')
            mo = b('').join(mo_file.readlines())
        finally:
            if po_file is not None:
                po_file.close()
            if mo_file is not None:
                mo_file.close()

        self.assertEqual(mo, po)
Example #26
0
def compile_po(path, dry_run=False):
    """path is a LC_MESSAGES directory.  Compile *.po into *.mo.

    Accepts an optional dry_run argument.  When True, only reports the
    found po files, without compiling them.

    Adapted from collective.releaser.
    """
    for domain_file in os.listdir(path):
        if domain_file.endswith('.po'):
            file_path = join(path, domain_file)
            if dry_run:
                logger.info("Found .po file: %s" % file_path)
                continue
            logger.info("Building .mo for %s" % file_path)
            mo_file = join(path, '%s.mo' % domain_file[:-3])
            mo_content = Msgfmt(file_path, name=file_path).get()
            mo = open(mo_file, 'wb')
            mo.write(mo_content)
            mo.close()
Example #27
0
 def test_test4(self):
     po_file = file(os.path.join(FOLDER, 'test4.po'), 'rb')
     po = Msgfmt(po_file)
     po.read(header_only=True)
     self.assertTrue(po.messages[''].startswith('Project-Id-Version: foo'))
Example #28
0
 def test_test5(self):
     po_file = file(os.path.join(FOLDER, 'test5.po'), 'rb')
     po = Msgfmt(po_file)
     with self.assertRaises(PoSyntaxError):
         po.read()
Example #29
0
 def compilePo(self, catalog):
     """Compile a po file to mo. Returns a file handler."""
     mo = Msgfmt(catalog._readFile(), catalog.getId())
     return mo.getAsFile()
Example #30
0
 def _getMoFile(self):
     """get compiled version of the po file as file object
     """
     mo = Msgfmt(self._readFile(), self.getId())
     return mo.getAsFile()
Example #31
0
 def test_test5_unicode_name(self):
     po_file = file(os.path.join(FOLDER, 'test5.po'), 'rb')
     po = Msgfmt(po_file, name=u'dømain')
     with self.assertRaises(PoSyntaxError):
         po.read()
Example #32
0
 def test_test4(self):
     po_file = file(os.path.join(self.folder, 'test4.po'), 'rb')
     po = Msgfmt(po_file)
     po.read(header_only=True)
     self.failUnless(po.messages[''].startswith('Project-Id-Version: foo'))
Example #33
0
 def test_test4(self):
     po_file = file(os.path.join(self.folder, 'test4.po'), 'rb')
     po = Msgfmt(po_file)
     po.read(header_only=True)
     self.failUnless(po.messages[''].startswith('Project-Id-Version: foo'))
Example #34
0
 def test_escape(self):
     po_file = file(os.path.join(FOLDER, 'test_escape.po'), 'rb')
     po = Msgfmt(po_file)
     with self.assertRaises(PoSyntaxError) as e:
         po.read()
     self.assertTrue('line 19' in e.exception.msg)
Example #35
0
 def test_test5_unicode_name(self):
     po_file = file(os.path.join(FOLDER, 'test5.po'), 'rb')
     po = Msgfmt(po_file, name=u'dømain')
     with self.assertRaises(PoSyntaxError):
         po.read()
Example #36
0
 def test_test5(self):
     po_file = file(os.path.join(FOLDER, 'test5.po'), 'rb')
     po = Msgfmt(po_file)
     with self.assertRaises(PoSyntaxError):
         po.read()
Example #37
0
 def test_test4(self):
     po_file = file(os.path.join(FOLDER, 'test4.po'), 'rb')
     po = Msgfmt(po_file)
     po.read(header_only=True)
     self.assertTrue(po.messages[''].startswith('Project-Id-Version: foo'))
Example #38
0
 def _getMoFile(self):
     """get compiled version of the po file as file object
     """
     mo = Msgfmt(self._readFile(), self.getId())
     return mo.getAsFile()
Example #39
0
 def compilePo(self, catalog):
     """Compile a po file to mo. Returns a file handler."""
     mo = Msgfmt(catalog._readFile(), catalog.getId())
     return mo.getAsFile()
Example #40
0
 def test_escape(self):
     po_file = file(os.path.join(FOLDER, 'test_escape.po'), 'rb')
     po = Msgfmt(po_file)
     with self.assertRaises(PoSyntaxError) as e:
         po.read()
     self.assertTrue('line 19' in e.exception.msg)
Example #41
0
async def init_state(app):
    # We have to put "kill" into a dict to prevent getting:
    # DeprecationWarning: Changing state of started or joined application is deprecated
    app["data"] = {"kill": False}

    if "db" not in app:
        app["db"] = None

    app["users"] = {
        "Random-Mover": User(app, bot=True, username="******"),
        "Fairy-Stockfish": User(app, bot=True, username="******"),
        "Discord-Relay": User(app, anon=True, username="******"),
    }
    app["users"]["Random-Mover"].online = True
    app["lobbysockets"] = {}
    app["seeks"] = {}
    app["games"] = {}
    app["invites"] = {}
    app["chat"] = collections.deque([], 100)
    app["game_channels"] = set()
    app["invite_channels"] = set()
    app["highscore"] = {variant: ValueSortedDict(neg) for variant in VARIANTS}
    app["crosstable"] = {}
    app["stats"] = {}

    # counters for games
    app["g_cnt"] = 0

    # last game played
    app["tv"] = None

    # fishnet active workers
    app["workers"] = set()
    # fishnet works
    app["works"] = {}
    # fishnet worker tasks
    app["fishnet"] = asyncio.PriorityQueue()
    # fishnet workers monitor
    app["fishnet_monitor"] = {}
    app["fishnet_versions"] = {}
    for key in FISHNET_KEYS:
        app["fishnet_monitor"][FISHNET_KEYS[key]] = collections.deque([], 50)

    rm = app["users"]["Random-Mover"]
    for variant in VARIANTS:
        variant960 = variant.endswith("960")
        variant_name = variant[:-3] if variant960 else variant
        byoyomi = variant.endswith("shogi") or variant in ("dobutsu",
                                                           "gorogoro",
                                                           "janggi", "shogun")
        seek = Seek(rm,
                    variant_name,
                    base=5,
                    inc=30 if byoyomi else 3,
                    level=0,
                    chess960=variant960,
                    byoyomi_period=1 if byoyomi else 0)
        app["seeks"][seek.id] = seek
        rm.seeks[seek.id] = seek

    ai = app["users"]["Fairy-Stockfish"]

    asyncio.create_task(BOT_task(ai, app))
    asyncio.create_task(BOT_task(rm, app))

    # Configure templating.
    app["jinja"] = {}
    base = os.path.dirname(__file__)
    for lang in LANGUAGES:
        # Generate compiled mo file
        folder = os.path.join(base, "../lang/", lang, "LC_MESSAGES")
        poname = os.path.join(folder, "server.po")
        moname = os.path.join(folder, "server.mo")
        try:
            with open(poname, 'rb') as po_file:
                po_lines = [
                    line for line in po_file if line[:8] != b"#, fuzzy"
                ]
                mo = Msgfmt(po_lines).get()
                with open(moname, 'wb') as mo_file:
                    mo_file.write(mo)
        except PoSyntaxError:
            log.error("PoSyntaxError in %s", poname)

        # Create translation class
        try:
            translation = gettext.translation("server",
                                              localedir="lang",
                                              languages=[lang])
        except FileNotFoundError:
            log.warning("Missing translations file for lang %s", lang)
            translation = gettext.NullTranslations()

        env = jinja2.Environment(enable_async=True,
                                 extensions=['jinja2.ext.i18n'],
                                 loader=jinja2.FileSystemLoader("templates"),
                                 autoescape=jinja2.select_autoescape(["html"]))
        env.install_gettext_translations(translation, newstyle=True)
        env.globals["static"] = static_url

        app["jinja"][lang] = env

    if app["db"] is None:
        return

    # Read users and highscore from db
    try:
        cursor = app["db"].user.find()
        async for doc in cursor:
            if doc["_id"] not in app["users"]:
                perfs = doc.get("perfs")
                if perfs is None:
                    perfs = {variant: DEFAULT_PERF for variant in VARIANTS}

                app["users"][doc["_id"]] = User(
                    app,
                    username=doc["_id"],
                    title=doc.get("title"),
                    first_name=doc.get("first_name"),
                    last_name=doc.get("last_name"),
                    country=doc.get("country"),
                    bot=doc.get("title") == "BOT",
                    perfs=perfs,
                    enabled=doc.get("enabled", True))

        db_collections = await app["db"].list_collection_names()

        if "highscore" not in db_collections:
            await generate_highscore(app["db"])
        cursor = app["db"].highscore.find()
        async for doc in cursor:
            app["highscore"][doc["_id"]] = ValueSortedDict(neg, doc["scores"])

        if "crosstable" not in db_collections:
            await generate_crosstable(app["db"])
        cursor = app["db"].crosstable.find()
        async for doc in cursor:
            app["crosstable"][doc["_id"]] = doc

        await app["db"].game.create_index("us")
        await app["db"].game.create_index("v")
        await app["db"].game.create_index("y")
        await app["db"].game.create_index("by")

    except Exception:
        print("Maybe mongodb is not running...")
        raise
Example #42
0
async def init_state(app):
    # We have to put "kill" into a dict to prevent getting:
    # DeprecationWarning: Changing state of started or joined application is deprecated
    app["data"] = {"kill": False}
    app["date"] = {"startedAt": datetime.now(timezone.utc)}

    if "db" not in app:
        app["db"] = None

    app["users"] = {
        "Random-Mover": User(app, bot=True, username="******"),
        "Fairy-Stockfish": User(app, bot=True, username="******"),
        "Discord-Relay": User(app, anon=True, username="******"),
    }
    app["users"]["Random-Mover"].online = True
    app["lobbysockets"] = {
    }  # one dict only! {user.username: user.tournament_sockets, ...}
    app["lobbychat"] = collections.deque([], MAX_CHAT_LINES)

    app["tourneysockets"] = {
    }  # one dict per tournament! {tournamentId: {user.username: user.tournament_sockets, ...}, ...}
    app["tourneynames"] = {
    }  # cache for profile game list page {tournamentId: tournament.name, ...}
    app["tournaments"] = {}
    app["tourneychat"] = {
    }  # one deque per tournament! {tournamentId: collections.deque([], MAX_CHAT_LINES), ...}

    app["seeks"] = {}
    app["games"] = {}
    app["invites"] = {}
    app["game_channels"] = set()
    app["invite_channels"] = set()
    app["highscore"] = {variant: ValueSortedDict(neg) for variant in VARIANTS}
    app["crosstable"] = {}
    app["shield"] = {}
    app["shield_owners"] = {}  # {variant: username, ...}

    app["stats"] = {}
    app["stats_humans"] = {}

    # counters for games
    app["g_cnt"] = [0]

    # last game played
    app["tv"] = None

    app["twitch"] = Twitch(app)
    if not DEV:
        await app["twitch"].init_subscriptions()

    # fishnet active workers
    app["workers"] = set()
    # fishnet works
    app["works"] = {}
    # fishnet worker tasks
    app["fishnet"] = asyncio.PriorityQueue()
    # fishnet workers monitor
    app["fishnet_monitor"] = {}
    app["fishnet_versions"] = {}
    for key in FISHNET_KEYS:
        app["fishnet_monitor"][FISHNET_KEYS[key]] = collections.deque([], 50)

    rm = app["users"]["Random-Mover"]
    ai = app["users"]["Fairy-Stockfish"]

    asyncio.create_task(BOT_task(ai, app))
    asyncio.create_task(BOT_task(rm, app))

    # Configure templating.
    app["jinja"] = {}
    base = os.path.dirname(__file__)
    for lang in LANGUAGES:
        # Generate compiled mo file
        folder = os.path.join(base, "../lang/", lang, "LC_MESSAGES")
        poname = os.path.join(folder, "server.po")
        moname = os.path.join(folder, "server.mo")
        try:
            with open(poname, 'rb') as po_file:
                po_lines = [
                    line for line in po_file if line[:8] != b"#, fuzzy"
                ]
                mo = Msgfmt(po_lines).get()
                with open(moname, 'wb') as mo_file:
                    mo_file.write(mo)
        except PoSyntaxError:
            log.error("PoSyntaxError in %s", poname)

        # Create translation class
        try:
            translation = gettext.translation("server",
                                              localedir="lang",
                                              languages=[lang])
        except FileNotFoundError:
            log.warning("Missing translations file for lang %s", lang)
            translation = gettext.NullTranslations()

        env = jinja2.Environment(enable_async=True,
                                 extensions=['jinja2.ext.i18n'],
                                 loader=jinja2.FileSystemLoader("templates"),
                                 autoescape=jinja2.select_autoescape(["html"]))
        env.install_gettext_translations(translation, newstyle=True)
        env.globals["static"] = static_url

        app["jinja"][lang] = env

    if app["db"] is None:
        return

    # Read tournaments, users and highscore from db
    try:
        cursor = app["db"].user.find()
        async for doc in cursor:
            if doc["_id"] not in app["users"]:
                perfs = doc.get("perfs")
                if perfs is None:
                    perfs = {variant: DEFAULT_PERF for variant in VARIANTS}

                app["users"][doc["_id"]] = User(app,
                                                username=doc["_id"],
                                                title=doc.get("title"),
                                                bot=doc.get("title") == "BOT",
                                                perfs=perfs,
                                                enabled=doc.get(
                                                    "enabled", True))

        cursor = app["db"].tournament.find()
        cursor.sort('startsAt', -1)
        counter = 0
        async for doc in cursor:
            if doc["status"] in (T_CREATED, T_STARTED):
                await load_tournament(app, doc["_id"])
                counter += 1
                if counter > 3:
                    break

        await generate_shield(app)

        db_collections = await app["db"].list_collection_names()

        # if "highscore" not in db_collections:
        # Always create new highscore lists on server start
        await generate_highscore(app["db"])
        cursor = app["db"].highscore.find()
        async for doc in cursor:
            app["highscore"][doc["_id"]] = ValueSortedDict(neg, doc["scores"])

        if "crosstable" not in db_collections:
            await generate_crosstable(app["db"])
        cursor = app["db"].crosstable.find()
        async for doc in cursor:
            app["crosstable"][doc["_id"]] = doc

        await app["db"].game.create_index("us")
        await app["db"].game.create_index("v")
        await app["db"].game.create_index("y")
        await app["db"].game.create_index("by")

    except Exception:
        print("Maybe mongodb is not running...")
        raise

    # create test tournament
    if 1:
        pass