Exemple #1
0
def update_stats_topusers(ctx: context.Context, today: str) -> None:
    """Counts the top housenumber editors as of today."""
    statedir = ctx.get_abspath("workdir/stats")
    csv_path = os.path.join(statedir, "%s.csv" % today)
    if not ctx.get_file_system().path_exists(csv_path):
        return
    topusers_path = os.path.join(statedir, "%s.topusers" % today)
    usercount_path = os.path.join(statedir, "%s.usercount" % today)
    users: Dict[str, int] = {}
    with ctx.get_file_system().open(csv_path, "rb") as stream:
        for line_bytes in stream.readlines():
            line = util.from_bytes(line_bytes)
            # Only care about the last column.
            user = line[line.rfind("\t"):].strip()
            if user in users:
                users[user] += 1
            else:
                users[user] = 1
    with ctx.get_file_system().open(topusers_path, "wb") as stream:
        for user in sorted(users,
                           key=cast(Callable[[str], int], users.get),
                           reverse=True)[:20]:
            line = str(users[user]) + " " + user
            stream.write(util.to_bytes(line + "\n"))

    with ctx.get_file_system().open(usercount_path, "wb") as stream:
        stream.write(util.to_bytes(str(len(users)) + "\n"))
Exemple #2
0
    def _join_room(self, room):
        if room.isdigit():
            self.transport.write(to_bytes("Room name is invalid\n"))
            return
        if room in self.rooms:
            self.transport.write(to_bytes("You are already in a room\n"))
            return

        # check if room exists
        if room not in self.factory.rooms:
            self.factory.rooms[room] = []

        # join room
        self.rooms.append(room)
        self.factory.rooms[room].append(self.login)
        self.factory.clients[self.login]["rooms"].append(room)

        self._write_service_message(data=ChatDataResponse(msg="entering room: {}".format(room), channel='0'))

        for person in self.factory.rooms[room]:
            if self.login != person:
                # search for this client
                client = self.factory.clients[person]
                protocol = client['protocol']
                resp = ChatDataResponse(msg=self._ser_message("user {} joined chat".format(self.login), is_system=True),
                                        channel=room, author=self.login)
                self._write_usual_message(protocol, data=resp)
Exemple #3
0
    def _login(self, login):
        # check if login already exists
        if login in self.factory.clients:
            self.transport.write(to_bytes("Name taken.\n"))
            self.transport.write(to_bytes("Login Name?\n"))
            return

        self.login = login
        # TODO Load user rooms from storage
        user_rooms = []
        self.factory.clients[self.login] = {'protocol': self, 'rooms': user_rooms}
        self._write_service_message(data=ChatDataResponse(msg="Hello {}!".format(self.login), channel='0'))
Exemple #4
0
def get_missing_housenumbers_txt(ctx: context.Context,
                                 relation: areas.Relation) -> str:
    """Gets the cached plain text of the missing housenumbers for a relation."""
    output = ""
    if is_missing_housenumbers_txt_cached(ctx, relation):
        with relation.get_files().get_housenumbers_txtcache_stream(
                "rb") as stream:
            output = util.from_bytes(stream.read())
        return output

    ongoing_streets, _ignore = relation.get_missing_housenumbers()
    table = []
    for result in ongoing_streets:
        range_list = util.get_housenumber_ranges(result[1])
        range_strings = [i.get_number() for i in range_list]
        # Street name, only_in_reference items.
        if not relation.get_config().get_street_is_even_odd(
                result[0].get_osm_name()):
            result_sorted = sorted(range_strings, key=util.split_house_number)
            row = result[0].get_osm_name() + "\t[" + ", ".join(
                result_sorted) + "]"
        else:
            elements = util.format_even_odd(range_list, doc=None)
            row = result[0].get_osm_name() + "\t[" + "], [".join(
                elements) + "]"
        table.append(row)
    table.sort(key=util.get_lexical_sort_key())
    output += "\n".join(table)

    with relation.get_files().get_housenumbers_txtcache_stream("wb") as stream:
        stream.write(util.to_bytes(output))
    return output
Exemple #5
0
def base_decode(v, length, base):
    """ decode v into a string of len bytes."""
    # assert_bytes(v)
    v = to_bytes(v, 'ascii')
    assert base in (58, 43)
    chars = __b58chars
    if base == 43:
        chars = __b43chars
    long_value = 0
    for (i, c) in enumerate(v[::-1]):
        long_value += chars.find(bytes([c])) * (base**i)
    result = bytearray()
    while long_value >= 256:
        div, mod = divmod(long_value, 256)
        result.append(mod)
        long_value = div
    result.append(long_value)
    nPad = 0
    for c in v:
        if c == chars[0]:
            nPad += 1
        else:
            break
    result.extend(b'\x00' * nPad)
    if length is not None and len(result) != length:
        return None
    result.reverse()
    return bytes(result)
Exemple #6
0
 def lineReceived(self, line):
     line = line.decode()
     if len(line) == 0:
         return
     try:
         msg_data = ChatMessage(**json.loads(line))
     except ValidationError as e:
         self._write_service_message(err=ChatErrorResponse(error=ChatErrorState.protocol_error.value,
                                                           msg="Protocol Error"))
         return
     except ValueError as e:
         self._write_service_message(err=ChatErrorResponse(error=ChatErrorState.serialize_error.value,
                                                           msg="Serialize error"))
         return
     if len(msg_data.msg) == 0:
         return
     if msg_data.msg.startswith('/') and msg_data.channel == '0':
         command, *args = msg_data.msg[1:].split(' ')
         args = [i for i in map(lambda x: x.strip(), args)]
         if command in self.factory.supported_command:
             try:
                 getattr(self, self.factory.supported_command[command])(*args)
             except TypeError as e:
                 self._write_service_message(err=ChatErrorResponse(error=ChatErrorState.command_syntax_failed.value,
                                                        msg='Syntax error'))
         else:
             self._write_service_message(err=ChatErrorResponse(error=ChatErrorState.command_not_found.value,
                                                           msg="Command not supported"))
     elif not self.login:
         self.transport.write(to_bytes("Please use /LOGIN YOUR_NICKNAME to login\n"))
     else:
         self._msg(msg_data)
Exemple #7
0
def base_decode(v, length, base):
    """ decode v into a string of len bytes."""
    # assert_bytes(v)
    v = to_bytes(v, 'ascii')
    if base not in (58, 43):
        raise ValueError('not supported base: {}'.format(base))
    chars = __b58chars
    if base == 43:
        chars = __b43chars
    long_value = 0
    for (i, c) in enumerate(v[::-1]):
        digit = chars.find(bytes([c]))
        if digit == -1:
            raise ValueError('Forbidden character {} for base {}'.format(
                c, base))
        long_value += digit * (base**i)
    result = bytearray()
    while long_value >= 256:
        div, mod = divmod(long_value, 256)
        result.append(mod)
        long_value = div
    result.append(long_value)
    nPad = 0
    for c in v:
        if c == chars[0]:
            nPad += 1
        else:
            break
    result.extend(b'\x00' * nPad)
    if length is not None and len(result) != length:
        return None
    result.reverse()
    return bytes(result)
Exemple #8
0
    def test_happy(self) -> None:
        """Tests tha happy path."""
        ctx = test_context.make_test_context()
        file_system = test_context.TestFileSystem()

        today_csv = util.to_bytes(
            """addr:postcode	addr:city	addr:street	addr:housenumber	@user
7677	Orfű	Dollár utca	1	mgpx
""")
        today_csv_value = io.BytesIO(today_csv)
        today_csv_value.__setattr__("close", lambda: None)
        today_topusers_value = io.BytesIO()
        today_topusers_value.__setattr__("close", lambda: None)
        today_usercount_value = io.BytesIO()
        today_usercount_value.__setattr__("close", lambda: None)
        files = {
            ctx.get_abspath("workdir/stats/2020-05-10.csv"):
            today_csv_value,
            ctx.get_abspath("workdir/stats/2020-05-10.topusers"):
            today_topusers_value,
            ctx.get_abspath("workdir/stats/2020-05-10.usercount"):
            today_usercount_value,
        }
        file_system.set_files(files)
        ctx.set_file_system(file_system)

        cron.update_stats_topusers(ctx, "2020-05-10")

        self.assertTrue(today_topusers_value.tell())
        self.assertTrue(today_usercount_value.tell())
Exemple #9
0
def get_missing_housenumbers_html(ctx: context.Context,
                                  relation: areas.Relation) -> yattag.doc.Doc:
    """Gets the cached HTML of the missing housenumbers for a relation."""
    doc = yattag.doc.Doc()
    if is_missing_housenumbers_html_cached(ctx, relation):
        with relation.get_files().get_housenumbers_htmlcache_stream(
                "rb") as stream:
            doc.asis(util.from_bytes(stream.read()))
        return doc

    ret = relation.write_missing_housenumbers()
    todo_street_count, todo_count, done_count, percent, table = ret

    with doc.tag("p"):
        prefix = ctx.get_ini().get_uri_prefix()
        relation_name = relation.get_name()
        doc.text(
            tr("OpenStreetMap is possibly missing the below {0} house numbers for {1} streets."
               ).format(str(todo_count), str(todo_street_count)))
        doc.text(
            tr(" (existing: {0}, ready: {1}).").format(
                str(done_count), util.format_percent(str(percent))))
        doc.stag("br")
        with doc.tag(
                "a",
                href="https://github.com/vmiklos/osm-gimmisn/tree/master/doc"):
            doc.text(tr("Filter incorrect information"))
        doc.text(".")
        doc.stag("br")
        with doc.tag(
                "a",
                href=prefix +
                "/missing-housenumbers/{}/view-turbo".format(relation_name)):
            doc.text(tr("Overpass turbo query for the below streets"))
        doc.stag("br")
        with doc.tag("a",
                     href=prefix +
                     "/missing-housenumbers/{}/view-result.txt".format(
                         relation_name)):
            doc.text(tr("Plain text format"))
        doc.stag("br")
        with doc.tag("a",
                     href=prefix +
                     "/missing-housenumbers/{}/view-result.chkl".format(
                         relation_name)):
            doc.text(tr("Checklist format"))

    doc.asis(util.html_table_from_list(table).getvalue())
    doc.asis(
        util.invalid_refstreets_to_html(
            relation.get_invalid_refstreets()).getvalue())
    doc.asis(
        util.invalid_filter_keys_to_html(
            relation.get_invalid_filter_keys()).getvalue())

    with relation.get_files().get_housenumbers_htmlcache_stream(
            "wb") as stream:
        stream.write(util.to_bytes(doc.getvalue()))

    return doc
Exemple #10
0
    def write_missing_housenumbers(
            self) -> Tuple[int, int, int, str, List[List[yattag.doc.Doc]]]:
        """
        Calculate a write stat for the house number coverage of a relation.
        Returns a tuple of: todo street count, todo count, done count, percent and table.
        """
        ongoing_streets, done_streets = self.get_missing_housenumbers()

        table, todo_count = self.numbered_streets_to_table(ongoing_streets)

        done_count = 0
        for result in done_streets:
            number_ranges = util.get_housenumber_ranges(result[1])
            done_count += len(number_ranges)
        if done_count > 0 or todo_count > 0:
            percent = "%.2f" % (done_count / (done_count + todo_count) * 100)
        else:
            percent = "100.00"

        # Write the bottom line to a file, so the index page show it fast.
        with self.get_files().get_housenumbers_percent_stream(
                self.__ctx, "wb") as stream:
            stream.write(util.to_bytes(percent))

        return len(ongoing_streets), todo_count, done_count, percent, table
Exemple #11
0
def challenge41(ciphertext, pub, decrypt_once):
    C = util.from_bytes(ciphertext)
    E, N = pub
    S = random.randint(2, N - 1)
    C2 = (pow(S, E, N) * C) % N
    P2 = decrypt_once(C2)
    P2 = (P2 * rsa.modinv(S, N)) % N
    return util.to_bytes(P2)
Exemple #12
0
 def write(self, s):
     if not (self.flags & O_BINARY):
         s = to_bytes(s)
     if not (self.flags & (os.O_RDWR | os.O_WRONLY)):
         raise IOError
     r = self.fs.ops.write(self.fd, self.ofs, s)
     self.ofs += r
     return r
Exemple #13
0
def write_city_count_path(ctx: context.Context, city_count_path: str,
                          cities: Dict[str, Set[str]]) -> None:
    """Writes a daily .citycount file."""
    with ctx.get_file_system().open(city_count_path, "wb") as stream:
        # Locale-aware sort, by key.
        lexical_sort_key = util.get_lexical_sort_key()
        for key, value in sorted(cities.items(),
                                 key=lambda item: lexical_sort_key(item[0])):
            stream.write(util.to_bytes(key + "\t" + str(len(value)) + "\n"))
Exemple #14
0
def _hash_password(password: Union[bytes, str], *, version: int) -> bytes:
    pw = to_bytes(password, 'utf8')
    if version not in SUPPORTED_PW_HASH_VERSIONS:
        raise UnsupportedPasswordHashVersion(version)
    if version == 1:
        return sha256d(pw)
    else:
        assert version not in KNOWN_PW_HASH_VERSIONS
        raise UnexpectedPasswordHashVersion(version)
Exemple #15
0
    def write_additional_streets(self) -> List[util.Street]:
        """Calculate and write stat for the unexpected street coverage of a relation."""
        additional_streets = self.get_additional_streets()

        # Write the count to a file, so the index page show it fast.
        with self.get_files().get_streets_additional_count_stream(
                self.__ctx, "wb") as stream:
            stream.write(util.to_bytes(str(len(additional_streets))))

        return additional_streets
Exemple #16
0
    def write_ref_streets(self, reference: str) -> None:
        """Gets known streets (not their coordinates) from a reference site, based on relation names
        from OSM."""
        memory_cache = util.build_street_reference_cache(reference)

        lst = self.get_config().build_ref_streets(memory_cache)

        lst = sorted(set(lst))
        with self.get_files().get_ref_streets_stream(self.__ctx, "wb") as sock:
            for line in lst:
                sock.write(util.to_bytes(line + "\n"))
Exemple #17
0
def pw_encode(data: str, password: Union[bytes, str, None], *,
              version: int) -> str:
    if not password:
        return data
    if version not in KNOWN_PW_HASH_VERSIONS:
        raise UnexpectedPasswordHashVersion(version)
    # derive key from password
    secret = _hash_password(password, version=version)
    # encrypt given data
    ciphertext = EncodeAES_bytes(secret, to_bytes(data, "utf8"))
    ciphertext_b64 = base64.b64encode(ciphertext)
    return ciphertext_b64.decode('utf8')
Exemple #18
0
def challenge46(pub, ciphertext, parity_oracle):
    ciphernum = util.from_bytes(ciphertext)
    e, n = pub
    multiplier = pow(2, e, n)
    result = 0
    power = pow(2, n.bit_length())
    for _ in range(n.bit_length()):
        ciphernum = ciphernum * multiplier % n
        p = parity_oracle(ciphernum)
        result = 2 * result + p
    result = n * (result + 1) // power
    return util.to_bytes(result)
Exemple #19
0
 def sign_message(self, message, is_compressed):
     message = to_bytes(message, 'utf8')
     signature = self.sign(Hash(msg_magic(message)))
     for i in range(4):
         sig = bytes([27 + i + (4 if is_compressed else 0)]) + signature
         try:
             self.verify_message(sig, message)
             return sig
         except Exception as e:
             continue
     else:
         raise Exception("error: cannot sign message")
Exemple #20
0
 def os_stat(self, path):
     if path == '/':
         path = b'.'
     else:
         assert '/' not in path
         path = to_bytes(path)
     inode = llfuse.ROOT_INODE
     try:
         attrs = self.ops.lookup(inode, path, self.rctx_user)
     except llfuse.FUSEError:
         raise FileNotFoundError
     self.ops.forget1(attrs.st_ino)
     return attrs
Exemple #21
0
def generate_json(ctx: context.Context, state_dir: str,
                  stream: BinaryIO) -> None:
    """Generates the stats json and writes it to `stream`."""
    j: Dict[str, Any] = {}
    handle_progress(ctx, state_dir, j)
    handle_topusers(ctx, state_dir, j)
    handle_topcities(ctx, state_dir, j)
    handle_user_total(ctx, state_dir, j)
    handle_daily_new(ctx, state_dir, j)
    handle_daily_total(ctx, state_dir, j)
    handle_monthly_new(ctx, state_dir, j)
    handle_monthly_total(ctx, state_dir, j)
    stream.write(util.to_bytes(json.dumps(j)))
Exemple #22
0
def permute_r_key(r, keysize):
    r_bytes = util.to_bytes(r)
    if keysize == 512:
        return util.to_number(hashlib.sha256(r_bytes).digest())
    elif keysize == 1024:
        return util.to_number(hashlib.sha512(r_bytes).digest())
    elif keysize == 2048:
        first_hash = hashlib.sha512(r_bytes).digest()
        second_hash = hashlib.sha512(first_hash).digest()
        return util.to_number(first_hash + second_hash)
    else:
        print('[-] Cannot permute r key')
        return None
Exemple #23
0
def recover_privkey_helper(pubkey, keysize, keyparams):
    print('[*] [RSA-%d] Finding the prime number p...' % keysize)
    n_bytes = util.to_bytes(pubkey.n)

    kp_keysize = keysize // 2
    kp_pubkey = RSA.importKey(keyparams.get_pubkey(kp_keysize))
    kp_r2_bytes = base64.b64decode(keyparams.get_r2(kp_keysize))
    kp_r2 = util.to_number(kp_r2_bytes)
    kp_privkey = RSA.importKey(keyparams.get_privkey(kp_keysize))

    encrypted_p_xor_r1_xor_r2 = n_bytes[0:len(kp_r2_bytes)]
    original_kp_r1_bytes = base64.b64decode(keyparams.get_r1(kp_keysize))

    for i in range(0, 0xffffff):
        kp_r2_bytes = util.to_bytes(kp_r2)
        encrypted_p_xor_r1 = bytes_xor(encrypted_p_xor_r1_xor_r2, kp_r2_bytes)
        p_xor_r1_bytes = util.rsa_decrypt(kp_privkey,
                                          bytes(encrypted_p_xor_r1))
        kp_r1 = util.to_number(original_kp_r1_bytes)
        for j in range(0, 0xa):
            for k in range(0, 0xa):
                kp_r1_bytes = util.to_bytes(kp_r1)
                p_bytes = bytes_xor(p_xor_r1_bytes, kp_r1_bytes)
                p = util.to_number(p_bytes)
                util.print_message('[RSA-%d] [%d:%d:%d] %d' %
                                   (keysize, i, j, k, p))
                if sympy.isprime(p) and sympy.isprime((p - 1) // 2):
                    privkey = util.rsa_construct_private_key(p, pubkey)
                    if privkey:
                        print('\n[+] [RSA-%d] p = %d' % (keysize, p))
                        print('[+] [RSA-%d] Private key is recovered' %
                              keysize)
                        return privkey
                kp_r1 += 1
            kp_r1 = permute_r_key(kp_r1, keysize)
        kp_r2 += 1
    print('\n[-] [RSA-%d] Cannot recover the private key' % keysize)
    return None
Exemple #24
0
    def _leave_room(self, room):
        if room not in self.rooms:
            self.transport.write(to_bytes("You not in a room\n"))
            return

        # leave room
        self.factory.rooms[room].remove(self.login)
        self.factory.clients[self.login]["room"] = None

        for person in self.factory.rooms[room]:
            if person != self.login:
                client = self.factory.clients[person]
                protocol = client['protocol']
                resp = ChatDataResponse(msg=self._ser_message("user {} has left chat".format(self.login),
                                                              is_system=True), channel=room, author=self.login)
                self._write_usual_message(protocol, data=resp)
Exemple #25
0
    def write_additional_housenumbers(
            self) -> Tuple[int, int, List[List[yattag.doc.Doc]]]:
        """
        Calculate and write stat for the unexpected house number coverage of a relation.
        Returns a tuple of: todo street count, todo count and table.
        """
        ongoing_streets = self.get_additional_housenumbers()

        table, todo_count = self.numbered_streets_to_table(ongoing_streets)

        # Write the street count to a file, so the index page show it fast.
        with self.get_files().get_housenumbers_additional_count_stream(
                "wb") as stream:
            stream.write(util.to_bytes(str(todo_count)))

        return len(ongoing_streets), todo_count, table
Exemple #26
0
def update_stats(ctx: context.Context, overpass: bool) -> None:
    """Performs the update of country-level stats."""

    # Fetch house numbers for the whole country.
    info("update_stats: start, updating whole-country csv")
    query = util.from_bytes(
        util.get_content(
            ctx.get_abspath("data/street-housenumbers-hungary.txt")))
    statedir = ctx.get_abspath("workdir/stats")
    os.makedirs(statedir, exist_ok=True)
    today = time.strftime("%Y-%m-%d")
    csv_path = os.path.join(statedir, "%s.csv" % today)

    if overpass:
        retry = 0
        while should_retry(retry):
            if retry > 0:
                info("update_stats: try #%s", retry)
            retry += 1
            overpass_sleep(ctx)
            response, err = overpass_query.overpass_query(ctx, query)
            if err:
                info("update_stats: http error: %s", err)
                continue
            with open(csv_path, "wb") as stream:
                stream.write(util.to_bytes(response))
            break

    update_stats_count(ctx, today)
    update_stats_topusers(ctx, today)
    update_stats_refcount(ctx, statedir)

    # Remove old CSV files as they are created daily and each is around 11M.
    current_time = time.time()
    for csv in glob.glob(os.path.join(statedir, "*.csv")):
        creation_time = os.path.getmtime(csv)
        if (current_time - creation_time) // (24 * 3600) >= 7:
            os.unlink(csv)
            info("update_stats: removed old %s", csv)

    info("update_stats: generating json")
    json_path = os.path.join(statedir, "stats.json")
    with ctx.get_file_system().open(json_path, "wb") as stream:
        stats.generate_json(ctx, statedir, stream)

    info("update_stats: end")
Exemple #27
0
    def write_missing_streets(self) -> Tuple[int, int, str, List[str]]:
        """Calculate and write stat for the street coverage of a relation."""
        todo_streets, done_streets = self.get_missing_streets()
        streets = []
        for street in todo_streets:
            streets.append(street)
        todo_count = len(todo_streets)
        done_count = len(done_streets)
        if done_count > 0 or todo_count > 0:
            percent = "%.2f" % (done_count / (done_count + todo_count) * 100)
        else:
            percent = "100.00"

        # Write the bottom line to a file, so the index page show it fast.
        with self.get_files().get_streets_percent_stream(self.__ctx,
                                                         "wb") as stream:
            stream.write(util.to_bytes(percent))

        return todo_count, done_count, percent, streets
Exemple #28
0
    def sign_message(self, message: bytes, is_compressed: bool) -> bytes:
        def bruteforce_recid(sig_string):
            for recid in range(4):
                sig65 = construct_sig65(sig_string, recid, is_compressed)
                try:
                    self.verify_message_for_address(sig65, message)
                    return sig65, recid
                except Exception as e:
                    continue
            else:
                raise Exception("error: cannot sign message. no recid fits..")

        message = to_bytes(message, 'utf8')
        msg_hash = Hash(msg_magic(message))
        sig_string = self.sign(msg_hash,
                               sigencode=sig_string_from_r_and_s,
                               sigdecode=get_r_and_s_from_sig_string)
        sig65, recid = bruteforce_recid(sig_string)
        return sig65
Exemple #29
0
    def authenticate(self, headers):
        if self.rpc_password == '':
            # RPC authentication is disabled
            return

        auth_string = headers.get('Authorization', None)
        if auth_string is None:
            raise RPCAuthCredentialsMissing()

        (basic, _, encoded) = auth_string.partition(' ')
        if basic != 'Basic':
            raise RPCAuthUnsupportedType()

        encoded = util.to_bytes(encoded, 'utf8')
        credentials = util.to_string(b64decode(encoded), 'utf8')
        (username, _, password) = credentials.partition(':')
        if not (util.constant_time_compare(username, self.rpc_user)
                and util.constant_time_compare(password, self.rpc_password)):
            time.sleep(0.050)
            raise RPCAuthCredentialsInvalid()
Exemple #30
0
 def open(self, filename, mode):
     filename = to_bytes(filename)
     assert b'/' not in filename
     flags = 0
     char2flag = {'r': (os.O_RDONLY, 0),
                  'w': (os.O_WRONLY | os.O_TRUNC | os.O_CREAT, 0),
                  '+': (os.O_RDWR, os.O_RDONLY | os.O_WRONLY),
                  'a': (os.O_WRONLY | os.O_CREAT | os.O_APPEND, 0),
                  'b': (O_BINARY, 0)}
     for char in mode:
         setbits, clearbits = char2flag[char]
         flags |= setbits
         flags &= ~clearbits
     fd = None
     try:
         _debug('attempting to lookup %s', filename)
         attrs = self.ops.lookup(llfuse.ROOT_INODE, filename,
                                 self.rctx_user)
         if flags & (os.O_CREAT | os.O_EXCL) == (os.O_CREAT | os.O_EXCL):
             if attrs:
                 self.ops.forget1(attrs.st_ino)
                 raise IOError
         else:
             fd = self.ops.open(attrs.st_ino, flags, self.rctx_user)
             self.ops.forget1(attrs.st_ino)
     except llfuse.FUSEError as e:
         _debug('exception %s', repr(e))
     if fd is None:
         if not (flags & os.O_CREAT):
             raise IOError(errno.ENOENT)
         if not (flags & (os.O_WRONLY | os.O_RDWR)):
             raise IOError(errno.ENOENT)
         mode = 0o600
         fd, attrs = self.ops.create(llfuse.ROOT_INODE, filename,
                                     mode, flags, self.rctx_user)
         assert attrs.st_ino
     assert fd
     return MockFile(self, fd, flags)
Exemple #31
0
def make_unsigned_request(req):
    addr = req['address']
    time = req.get('time', 0)
    exp = req.get('exp', 0)
    if time and type(time) != int:
        time = 0
    if exp and type(exp) != int:
        exp = 0
    amount = req['amount']
    if amount is None:
        amount = 0
    memo = req['memo']
    script = bfh(util.pay_script(TYPE_ADDRESS, addr))
    outputs = [(script, amount)]
    pd = pb2.PaymentDetails()
    for script, amount in outputs:
        pd.outputs.add(amount=amount, script=script)
    pd.time = time
    pd.expires = time + exp if exp else 0
    pd.memo = memo
    pr = pb2.PaymentRequest()
    pr.serialized_payment_details = pd.SerializeToString()
    pr.signature = util.to_bytes('')
    return pr
Exemple #32
0
def get_additional_housenumbers_html(
        ctx: context.Context, relation: areas.Relation) -> yattag.doc.Doc:
    """Gets the cached HTML of the additional housenumbers for a relation."""
    doc = yattag.doc.Doc()
    if is_additional_housenumbers_html_cached(ctx, relation):
        with relation.get_files().get_additional_housenumbers_htmlcache_stream(
                "rb") as stream:
            doc.asis(util.from_bytes(stream.read()))
        return doc

    ret = relation.write_additional_housenumbers()
    todo_street_count, todo_count, table = ret

    with doc.tag("p"):
        doc.text(
            tr("OpenStreetMap additionally has the below {0} house numbers for {1} streets."
               ).format(str(todo_count), str(todo_street_count)))
        doc.stag("br")
        with doc.tag(
                "a",
                href="https://github.com/vmiklos/osm-gimmisn/tree/master/doc"):
            doc.text(tr("Filter incorrect information"))

    doc.asis(util.html_table_from_list(table).getvalue())
    doc.asis(
        util.invalid_refstreets_to_html(
            relation.get_invalid_refstreets()).getvalue())
    doc.asis(
        util.invalid_filter_keys_to_html(
            relation.get_invalid_filter_keys()).getvalue())

    with relation.get_files().get_additional_housenumbers_htmlcache_stream(
            "wb") as stream:
        stream.write(util.to_bytes(doc.getvalue()))

    return doc
Exemple #33
0
    def write_ref_housenumbers(self, references: List[str]) -> None:
        """
        Writes known house numbers (not their coordinates) from a reference, based on street names
        from OSM. Uses build_reference_cache() to build an indexed reference, the result will be
        used by get_ref_housenumbers().
        """
        memory_caches = util.build_reference_caches(
            references,
            self.get_config().get_refcounty())

        streets = [i.get_osm_name() for i in self.get_osm_streets()]

        lst: List[str] = []
        for street in streets:
            for index, memory_cache in enumerate(memory_caches):
                suffix = RelationBase.__get_ref_suffix(index)
                lst += self.build_ref_housenumbers(memory_cache, street,
                                                   suffix)

        lst = sorted(set(lst))
        with self.get_files().get_ref_housenumbers_stream(self.__ctx,
                                                          "wb") as sock:
            for line in lst:
                sock.write(util.to_bytes(line + "\n"))
Exemple #34
0
 def os_unlink(self, path):
     path = to_bytes(path)
     assert b'/' not in path
     inode = llfuse.ROOT_INODE
     self.ops.unlink(inode, path, self.rctx_user)
Exemple #35
0
 def sendData(self, data):
     self.transport.write(to_bytes(data) + console_delimiter)
Exemple #36
0
 def _write_usual_message(self, protocol, data):
     resp = ChatResponse(data=data)
     protocol.sendLine(to_bytes(json.dumps(resp.to_dict())))
Exemple #37
0
def test_to_bytes():
    assert util.to_bytes(b'foo') == b'foo'
    assert util.to_bytes('foo') == b'foo'
Exemple #38
0
def challenge42(pub, message):
    md = hashlib.sha1(message).digest()
    md = b'\x00\x01\xff\x00' + md + (b'\x00' * (124 - len(md)))
    e = pub[0]
    return util.to_bytes(util.nth_root(util.from_bytes(md), e) + 1)
Exemple #39
0
 def _write_service_message(self, data=None, err=None):
     resp = {}
     resp['error'] = err
     resp['data'] = data
     self.transport.write(to_bytes(json.dumps(ChatResponse(**resp).to_dict()) + '\n'))
Exemple #40
0
    def _quit(self):
        for room in self.rooms:
            self._leave_room(room)

        self.transport.write(to_bytes('BYE!\n'))
        self.transport.loseConnection()