Пример #1
0
def parse(osr: bytes):
    offset = 0

    mode = osr[0]
    offset += 1

    gamever, offset = util.parse_uint(osr, offset)
    beatmaphash, offset = util.parse_string(osr, offset)
    playername, offset = util.parse_string(osr, offset)
    replayhash, offset = util.parse_string(osr, offset)

    permodeoff = offset
    offset += 12

    totalscore, offset = util.parse_uint(osr, offset)
    maxcombo, offset = util.parse_short(osr, offset)
    perfect, offset = util.parse_bool(osr, offset)
    modsint, offset = util.parse_uint(osr, offset)
    lifebar, offset = util.parse_string(osr, offset)
    timestamp, offset = util.parse_long(osr, offset)

    # datalength, offset = util.parse_uint(osr, offset)
    # offset += datalength
    #
    # scoreid, offset = util.parse_long(osr, offset)
    # modinfo, offset = util.parse_double(osr, offset)

    info = {
        "mode": mode,
        "gamever": gamever,
        "beatmaphash": beatmaphash,
        "playername": playername,
        "replayhash": replayhash,
        "totalscore": totalscore,
        "maxcombo": maxcombo,
        "perfect": perfect,
        "mods": parse_mods(modsint),
        "lifebar": parse_lifebar(lifebar),
        "timestamp": util.winticks_to_unix(timestamp),
        # "scoreid": str(scoreid),
        # "modinfo": modinfo
    }

    permode = {}
    if mode == 0:
        # osu! Standard
        permode = parse_standard(osr[permodeoff:permodeoff + 12])

    info["score"] = permode
    return info
Пример #2
0
    def get(self):
        """Handle user request

        According to the edit item main page form submition, construct
        add, edit, delete item HTML page
        """
        category_name = self.request.get('category_name')
        if not category_name:
            self.redirect('/{path}?'.format(path=main_page_path) + 
                          urllib.urlencode({'select_category': 'Nothing'}))
            return

        method = self.request.get('method')
        user = users.get_current_user()
        url = users.create_logout_url(self.request.uri)
        items = get_items(author=user, category_name=category_name)

        if 'Add' in method:
            invalid_name = self.request.get('invalid_name')
            template_values = {
                'category_name': category_name,
                'items': items,
                'url': url,
                'user': user,
                'invalid_name': invalid_name,
            }
            template = jinja_environment.get_template('{path}.html'.format(path=add_page_path))
        elif 'Edit' in method:
            invalid_select = self.request.get('select_item')
            template_values = {
                'category_name': category_name,
                'items': items,
                'url': url,
                'user': user,
               'invalid_select': invalid_select,
            }
            template = jinja_environment.get_template('{path}.html'.format(path=select_item_page_path))
        elif 'Delete' in method:
            delete_name = self.request.get('delete_name')
            delete_names = util.parse_string(delete_name) if delete_name else None
            template_values = {
                'category_name': category_name,
                'items': items,
                'url': url,
                'user': user,
                'delete_names': delete_names,
            }
            template = jinja_environment.get_template('{path}.html'.format(path=delete_page_path))
        self.response.out.write(template.render(template_values))
Пример #3
0
    def get(self):
        """Handle user request

        The delete category HTML page would list all categories that user
        already have as well as categories that user just deleted
        """
        delete_name = self.request.get("delete_name")
        delete_names = util.parse_string(delete_name) if delete_name else None
        user = users.get_current_user()
        categories = get_categories(author=user)
        url = users.create_logout_url(self.request.uri)

        template_value = {"categories": categories, "url": url, "user": user, "delete_names": delete_names}
        template = jinja_environment.get_template("{path}.html".format(path=delete_page_path))
        self.response.out.write(template.render(template_value))
Пример #4
0
def decompress(source, target):
    # some info
    logging.debug("Starting decompression of %s to %s", repr(source),
                  repr(target))

    with open(source, "rb") as fsource:
        # some info
        logging.debug("Parsing header")
        magic, method, majorversion, minorversion, pagesize, uncompressed_size = util.parse_header(
            fsource)
        logging.debug("    Magic number: %s", repr(magic))
        logging.debug("    Method: %s", repr(method))
        logging.debug("    Major version number: %d", majorversion)
        logging.debug("    Minor version number: %d", minorversion)
        logging.debug("    Page size: %d", pagesize)
        logging.debug("    Uncompressed size: %d", uncompressed_size)
        nointra, delta, inner = parse_method_name(method)
        fsource.flush()
        tmphandle, tmpfile = None, None
        if inner == "gzip":
            fsource = gzip.GzipFile(fileobj=fsource,
                                    mode="rb",
                                    compresslevel=9)
        elif inner == "bzip2":
            fsource = bz2file.BZ2File(filename=fsource,
                                      mode="rb",
                                      compresslevel=9)
        elif inner == "7zip":
            util.create_dir(".tmp")
            tmphandle, tmpfile = tempfile.mkstemp(dir=".tmp")
            with open(tmpfile, "wb") as ftmp:
                p = subprocess.Popen(["7za", "x", "-txz", "-si", "-so"],
                                     stdin=fsource,
                                     stdout=ftmp,
                                     stderr=subprocess.PIPE)
                p.communicate()
            fsource = open(tmpfile, "rb")
        try:
            reference = util.parse_string(fsource)
            logging.debug("Reference dump: %s", reference)

            # parse deduplicated pages
            fills = {}
            reference_list = list(util.parse_pagenr_list(fsource))
            for i in xrange(len(reference_list)):
                for left, right in util.parse_interval_list(fsource):
                    for pagenr in xrange(left, right + 1):
                        fills[pagenr] = reference_list[i]

            # parse diffs
            if delta:
                diffs = {}
                pagenrs = list(util.parse_pagenr_list(fsource))
                for i in xrange(len(pagenrs)):
                    diffs[pagenrs[i]] = util.parse_diff(fsource, pagesize)

            # parse new pages
            newpages = {}
            newdistinct = set()
            if nointra:
                for left, right in list(util.parse_interval_list(fsource)):
                    for j in xrange(left, right + 1):
                        page = fsource.read(pagesize)
                        newdistinct.add(page)
                        newpages[j] = page
            else:
                newcnt = util.parse_int(fsource, 4)
                intervals = []
                for _ in xrange(newcnt):
                    intervals.append(list(util.parse_interval_list(fsource)))
                for i in xrange(newcnt):
                    page = fsource.read(pagesize)
                    for left, right in intervals[i]:
                        for j in xrange(left, right + 1):
                            newdistinct.add(page)
                            newpages[j] = page
        finally:
            if tmphandle is not None:
                os.close(tmphandle)
                os.remove(tmpfile)
            if inner is not None:
                fsource.close()

        # reconstruct file
        pagenr = 0
        final = uncompressed_size / pagesize
        same_distinct, same_total = set(), 0
        different_distinct, different_total = set(), 0
        seen = set()
        diff_seen = set()
        with open(reference, "rb") as freference:
            with open(target, "wb") as ftarget:
                while pagenr < final:
                    if pagenr in fills:
                        freference.seek(pagesize * fills[pagenr])
                        page = freference.read(pagesize)
                        seen.add(page)
                        different_distinct.add(page)
                        different_total += 1
                        ftarget.write(page)
                    elif delta and pagenr in diffs:
                        freference.seek(pagenr * pagesize)
                        page = freference.read(pagesize)
                        newpage = util.apply_diff(page, diffs[pagenr])
                        diff_seen.add(newpage)
                        ftarget.write(newpage)
                    elif pagenr in newpages:
                        seen.add(newpages[pagenr])
                        ftarget.write(newpages[pagenr])
                    else:
                        freference.seek(pagesize * pagenr)
                        page = freference.read(pagesize)
                        seen.add(page)
                        same_distinct.add(page)
                        same_total += 1
                        ftarget.write(page)
                    pagenr += 1

    # some info
    logging.debug("New pages: %d/%d (%d/%d)", len(newpages), final,
                  len(newdistinct), len(seen))
    logging.debug("Deduplicated pages at the same offset: %d/%d (%d/%d)",
                  same_total, final, len(same_distinct), len(seen))
    logging.debug("Deduplicated pages at different offsets: %d/%d (%d/%d)",
                  len(fills), final, len(different_distinct), len(seen))
    logging.debug("Deduplicated pages in total: %d/%d (%d/%d)",
                  same_total + len(fills), final,
                  len(same_distinct | different_distinct), len(seen))
    if delta:
        logging.debug("Diffed pages: %d/%d (%d/%d)", len(diffs), final,
                      len(diff_seen), len(seen))
    logging.debug("Done")

    return 0