Beispiel #1
0
 def __init__(
     self,
     mold: Mold,
     s: Union[Dict[str, Any], str, bytes, None] = None,
     pack_lib: PackerLibrary = CAKEABLES,
 ):
     store: Dict[str, Any] = {}
     if s is not None:
         if isinstance(s, dict):
             store.update(s)
         elif isinstance(s, str):
             store.update(json_decode(s))
         elif isinstance(s, bytes):
             store.update(json_decode(utf8_decode(s)))
         else:
             raise AssertionError(f"Unexpected object: {s!r}")
     mold.check_overlaps(store)
     for ae in mold.attrs.values():
         if ae.name in store:
             v = ae.convert(store[ae.name], Conversion.TO_OBJECT)
             packer = pack_lib[ae.typing.val_cref.cls]
             if packer is not None:
                 v = Cakeable.from_data(packer, v)
             store[ae.name] = v
     self.mold = mold
     self.store = store
Beispiel #2
0
 def post_json(self, data, endpoint='-/api/post'):
     meta_url = self.url + endpoint
     in_data = json_encoder.encode(data)
     r = requests.post(meta_url, headers=self.headers, data=in_data)
     out_data = r.text
     log.debug('{{ "url": "{meta_url}",\n'
               '"in": {in_data},\n'
               '"out": {out_data} }}'.format(**locals()))
     return json_decode(out_data)
async def test_ignore_policy(dc_cb: OnNewDirContent, file_extra_factory,
                             ignore_symlinks: bool):

    a_b_1_5 = seed_file(scan_dir / "a" / "b", 1, 5)
    x_f_b_1_5 = seed_file(scan_dir / "x" / "f" / "b", 1, 5)
    c_b_2_7 = seed_file(scan_dir / "c" / "b", 2, 7)
    c_b_1_5 = seed_file(scan_dir / "c" / "b", 1, 5)

    try:
        (scan_dir / "b").symlink_to(scan_dir / "a")
    except:
        pass

    rules = DEFAULT_IGNORE_POLICY.apply(scan_dir)
    rules.ignore_symlinks = ignore_symlinks
    entry = await process_dir(scan_dir, rules, dc_cb, file_extra_factory)
    assert entry.name() == "scanning"
    extras = entry.xtra.extras
    first_level = ["a", "c", "x"]
    if not ignore_symlinks:
        first_level.insert(1, "b")
        # same symlinked directories
        assert (extras[0].xtra.extras[0].xtra.extras[0].xtra ==
                extras[1].xtra.extras[0].xtra.extras[0].xtra)
        assert json_encode(to_json(extras[0].xtra.extras[0])) == json_encode(
            to_json(extras[1].xtra.extras[0]))

    assert [e.name() for e in extras] == first_level

    json = json_encode(to_json(entry))

    fe = FileExtra.from_json(json_decode(json))
    assert [x.name() for x in fe.xtra] == [x.name() for x in entry.xtra.extras]
    for i in range(len(entry.xtra)):
        assert fe.xtra[i].name() == entry.xtra[i].name()

    if file_extra_factory == Cake.from_file and ignore_symlinks:
        json = re.sub(r'"mod": "[^"]+",', "", json)
        print(json)
        assert (
            json ==
            '{ "name": "scanning", "size": 22, "type": "TREE", "xtra": '
            '[{ "name": "a", "size": 5, "type": "TREE", "xtra": '
            '[{ "name": "b", "size": 5, "type": "TREE", "xtra": '
            '[{ "name": "1_5.dat", "size": 5, "type": "FILE", '
            '"xtra": "xzbg412Ar6X0AOPfFUg3nc3wSoSQ96jak07pdh7jrOH"}]}]}, '
            '{ "name": "c", "size": 12, "type": "TREE", "xtra": '
            '[{ "name": "b", "size": 12, "type": "TREE", "xtra": '
            '[{ "name": "1_5.dat", "size": 5, "type": "FILE", '
            '"xtra": "xzbg412Ar6X0AOPfFUg3nc3wSoSQ96jak07pdh7jrOH"}, '
            '{ "name": "2_7.dat", "size": 7, "type": "FILE", '
            '"xtra": "6L1wce6TqSrO61DDPCNGnMabCQxko3hyEvEd9ZCXFg0"}]}]}, '
            '{ "name": "x", "size": 5, "type": "TREE", "xtra": '
            '[{ "name": "f", "size": 5, "type": "TREE", "xtra": '
            '[{ "name": "b", "size": 5, "type": "TREE", "xtra": '
            '[{ "name": "1_5.dat", "size": 5, "type": "FILE", '
            '"xtra": "xzbg412Ar6X0AOPfFUg3nc3wSoSQ96jak07pdh7jrOH"}]}]}]}]}')
Beispiel #4
0
def test_json_encode_decode():
    class q:
        pass

    try:
        kernel.json_encode(q())
        assert False
    except:
        assert "is not JSON serializable" in kernel.exception_message()

    assert (kernel.json_encode(
        datetime.datetime(2019, 4, 26, 19, 46, 50,
                          217946)) == '"2019-04-26T19:46:50.217946"')
    assert kernel.json_encode(datetime.date(2019, 4, 26)) == '"2019-04-26"'
    assert kernel.json_encode(JsonableExample("z", 5)) == '{"i": 5, "s": "z"}'
    assert kernel.json_decode('{"i": 5, "s": "z"}') == {"i": 5, "s": "z"}
    try:
        kernel.json_decode('{"i": 5, "s": "z"')
        assert False
    except ValueError:
        assert 'text=\'{"i": 5, "s": "z"\'' in kernel.exception_message()
Beispiel #5
0
 def get_content(_, cake_or_path, skinny=True):
     if isinstance(cake_or_path, Cake):
         if cake_or_path.has_data():
             return Content.from_data_and_role(
                 data=cake_or_path.data(),
                 role=cake_or_path.header.role)
     if skinny:
         info = {'mime': BINARY_MIME}
     else:
         resp = self.get_response('-/get/info', cake_or_path)
         info = json_decode(resp.text)
     return Content(
         stream_fn=(lambda: self.get_stream(cake_or_path)), **info)
Beispiel #6
0
 def __init__(self,
              s: Union[str, bytes, None] = None,
              mold: Any = None) -> None:
     if mold is not None:
         self.mold = Mold.ensure_it(mold)
     else:
         self.mold = Mold.ensure_it(
             type(self).__item_cref__.cls)  # type: ignore
     self.data: List[List[Any]] = []
     if s is not None:
         s = ensure_string(s)
         lines = filter(not_zero_len, (s.strip() for s in s.split("\n")))
         header_line = next(lines)
         if header_line[0] != "#":
             raise AttributeError(f'header should start with "#":'
                                  f" {header_line}")
         header = json_decode(header_line[1:])
         cols = tuple(header["columns"])
         # TODO support: [ int, bool, str, float, date, datetime, object ]
         mold_cols = tuple(self.mold.keys)
         if mold_cols != cols:
             raise AttributeError(f" mismatch: {cols} {mold_cols}")
         for l in lines:
             self.add_row(json_decode(l))
Beispiel #7
0
 def __init__(self, name, typing=None, default=None):
     self.default = default
     self._doc = None
     self.index = None
     default_s = None
     if typing is None:
         split = name.split("=", 1)
         if len(split) == 2:
             name, default_s = split
         name, typing = name.split(":", 1)
     self.name = name
     self.typing = typing_factory(typing)
     if default_s is not None:
         self.default = self.typing.convert(json_decode(default_s),
                                            Conversion.TO_OBJECT)
Beispiel #8
0
 def write_content(_, fp):
     r = requests.post(self.url + '-/api/up',
                       headers=self.headers,
                       data=fp)
     log.debug('text: {r.text}'.format(**locals()))
     return ContentAddress.ensure_it(json_decode(r.text))
Beispiel #9
0
    def post(self):
        req = json_decode(utf8_decode(self.request.body))
        res = self.access.process_api_call(req['call'], req['msg'])

        self.write(json_encode(res))
        self.finish()