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"}]}]}]}]}')
def __str__(self): def_s = "" if self.default is not None: v = json_encode( self.typing.convert(self.default, Conversion.TO_JSON)) def_s = f"={v}" return f"{self.name}:{self.typing}{def_s}"
def run_server(self): app_dir = os.path.join(os.path.dirname(__file__), 'app') class AppContentHandler(_ContentHandler): def content(self, path): file = os.path.join(app_dir, path) return Content.from_file(file) class IndexHandler(_ContentHandler): def content(self, _): file = os.path.join(app_dir, 'index.html') return Content.from_file(file) class FavIconHandler(_ContentHandler): def content(self, _): file = os.path.join(app_dir, 'favicon.ico') return Content.from_file(file) pid = str(os.getpid()) server_id = json_encode( (str(self.config.id), str(SaltedSha.from_secret(str(self.config.secret))))) store_ref = {'store': self.store} handlers = [( r'/-/(pid)$', _string_handler(pid), ), ( r'/-/(server_id)$', _string_handler(server_id), ), (r'/-/api/up$', StreamHandler, store_ref), (r'/-/api/post$', PostHandler, store_ref), (r'/-/get/(.*)$', GetCakeHandler, store_ref), ( r'/-/app/(.*)$', AppContentHandler, ), ( r'/(favicon.ico)$', FavIconHandler, ), ( r'(.*)$', IndexHandler, ) # - ~ _ ] application = tornado.web.Application(handlers) signal.signal(signal.SIGINT, stop_server) http_server = tornado.httpserver.HTTPServer( application, max_body_size=self.max_file_size) http_server.listen(self.config.port) logging.info('CakeServer({0.store.store_dir}) ' 'listening=0.0.0.0:{0.config.port}'.format(self)) tornado.ioloop.IOLoop.instance().start() logging.info('Finished')
def content(self, path): cake = cake_or_path(path[5:], relative_to_root=True) prefix = path[:5] content = self.access.get_content(cake) if 'data/' == prefix: return content elif 'info/' == prefix: return Content(data=utf8_encode(json_encode(content.to_json())), mime='application/json') else: raise AssertionError('Unknown prefix: %s' % prefix)
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()
async def main(): path = Path(sys.argv[1]).absolute() print( json_encode( to_json(await process_dir( path, DEFAULT_IGNORE_POLICY.apply(path), content_cb=print_dc, file_extra_factory=Cake.from_file, ))), file=sys.stderr, )
def gen(): yield "#" + json_encode({"columns": self.mold.keys}) for row in self.data: yield json_encode(self.mold.mold_row(row, Conversion.TO_JSON)) yield ""
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()
def post(self): k = self.w.done() log.info('write_content: %s' % k) self.write(json_encode(k)) self.finish()