main.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:rpi-can-logger 作者: JonnoFTW 项目源码 文件源码
def export_files(sock):
    print("currently writing", writing_to['name'])
    for fname in sorted(glob(log_folder + "/*.json*")):
        if fname.endswith(writing_to['name']):
            print(fname, "is currently being written to")
            continue
        if fname.endswith('.done'):
            print("Skipping", fname)
            sock.send("$skipping={}!\n".format(fname))
            continue
        # we will send base64 encoded gzipped json
        with open(fname, 'rb') as infile:
            file_bytes = infile.read()
            if fname.endswith(".json"):
                if len(file_bytes) == 0:
                    print("Skipping empty file:", fname)
                    os.remove(fname)
                    continue
                json_gzip_bytes = gzip.compress(file_bytes)
            else:
                json_gzip_bytes = file_bytes
            json_gzip_base64 = base64.b64encode(json_gzip_bytes)
            try:

                if struct.unpack('I', json_gzip_bytes[-4:])[0] == 0:
                    # don't send empty files
                    logging.warning("Skipping empty file: " + pathlib.Path(fname).name)
                    os.remove(fname)
                    continue
            except:
                logging.warning("Not a GZIP file: " + fname)
                continue
            msg = '$export={}={}!\n'.format(len(json_gzip_bytes), pathlib.Path(fname).name)
            print(msg, end='')
            sock.send(msg)
            n = 900
            to_send_str = str(json_gzip_base64, 'ascii')
            lines = [to_send_str[i:i + n] for i in range(0, len(to_send_str), n)]
            for line in lines:
                sock.send("$export={}\n".format(line))
            sock.send("$done\n")
            os.rename(fname, fname+'.done')
        sock.send('$export=done\n')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号