|
1 | 1 | # python-df-parser
|
2 | 2 |
|
3 |
| -Parser for (dataforge)[http://npm.mipt.ru/dataforge/] envelope format |
| 3 | +Python parser for [dataforge envelope](http://npm.mipt.ru/dataforge/) format. |
| 4 | +Envelope format designed to transport and store tuples with binary data and |
| 5 | +metadata in a single packet [more info](http://npm.mipt.ru/dataforge/docs.html#envelope_format). |
4 | 6 |
|
5 |
| -## Installation |
6 |
| -Latest version on PyPi can be installed by command: |
| 7 | +Implementation currently supports: |
| 8 | + - Parse and serialize metadata in JSON format. |
| 9 | + - Handle 0x14000 and DF02 protocol versions. |
| 10 | + - Transparent binary data compression (only zlib supported) |
7 | 11 |
|
8 |
| - pip3 install dfparser |
9 |
| - |
10 |
| -To install latest version use: |
11 |
| - |
12 |
| - pip3 install https://github.com/kapot65/python-df-parser/archive/master.zip |
13 |
| - |
14 |
| -## Build |
15 |
| -To update protobuf formats use: |
| 12 | +## Installation |
| 13 | +Latest version on PyPi can be installed by command `pip3 install dfparser` |
16 | 14 |
|
17 |
| - cd configs && protoc rsb_event.proto --python_out ../ && cd .. |
| 15 | +## Usage |
| 16 | +#### Create simple message |
| 17 | + - 0x14000 protocol version |
| 18 | + >>> import dfparser |
| 19 | + >>> dfparser.create_message({"param": "abc"}, data=b'bnary') |
| 20 | + b'#!\x00\x01@\x00pY_2\x00\x01\x00\x00\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x05!#\r\n{\n "param": "abc"\n}\r\n\r\nbnary' |
| 21 | + - DF02 version |
| 22 | + >>> import dfparser |
| 23 | + >>> dfparser.create_message({"param": "abc"}, b'binary', version=b'DF02') |
| 24 | + b'#~DF02JS\x00\x00\x00\x1a\x00\x00\x00\x06~#\r\n{\n "param": "abc"\n}\r\n\r\nbinary' |
| 25 | + |
| 26 | +#### Parse message |
| 27 | + - From file |
| 28 | + >>> import dfparser |
| 29 | + >>> header, meta, data = envelope_parser.parse_from_file("df02.df") |
| 30 | + >>> # Or read only metadata |
| 31 | + >>> header, meta, _ = envelope_parser.parse_from_file("df02.df", nodata=True) |
| 32 | + - From bytes |
| 33 | + >>> import dfparser |
| 34 | + >>> data = b'#~DF02JS\x00\x00\x00\x1a\x00\x00\x00\x06~#\r\n{\n "param": "abc"\n}\r\n\r\nbinary' |
| 35 | + >>> envelope_parser.parse_message(data) |
| 36 | + ({'data_len': 6, 'meta_len': 26, 'meta_type': b'JS', 'type': b'DF02'}, |
| 37 | + {'param': 'abc'}, |
| 38 | + b'binary') |
| 39 | + |
| 40 | + - From stream |
| 41 | + >>> import dfparser |
| 42 | + >>> # stream.df contains multiple messages sequentaly written |
| 43 | + >>> data = open("/home/chernov/stream.df", 'rb').read() |
| 44 | + >>> dfparser.get_messages_from_stream(data) |
| 45 | + ([{'data': b'bnary', |
| 46 | + 'header': {'data_len': 5, |
| 47 | + 'data_type': 0, |
| 48 | + 'meta_len': 26, |
| 49 | + 'meta_type': 65536, |
| 50 | + 'time': 1885524830, |
| 51 | + 'type': b'\x00\x01@\x00'}, |
| 52 | + 'meta': {'param': 'abc'}}, |
| 53 | + {'data': b'binary', |
| 54 | + 'header': {'data_len': 6, |
| 55 | + 'meta_len': 26, |
| 56 | + 'meta_type': b'JS', |
| 57 | + 'type': b'DF02'}, |
| 58 | + 'meta': {'param': 'abc'}}], |
| 59 | + b'') |
18 | 60 |
|
19 |
| -(Protobuf 3.2.0+)[https://github.com/google/protobuf/releases] should be installed |
| 61 | +### Transparent compression |
| 62 | + To apply transparent compression to message meta should contains field `"compression": "zlib"` |
| 63 | + >>> import dfparser |
| 64 | + >>> data = b''.join(b'0' for _ in range(100)) |
| 65 | + >>> compr = dfparser.create_message({"compression": "zlib"}, data) |
| 66 | + >>> compr |
| 67 | + b'#!\x00\x01@\x00pt\xf7\xa7\x00\x01\x00\x00\x00\x00\x00!\x00\x00\x00\x00\x00\x00\x00\x0c!#\r\n{\n "compression": "zlib"\n}\r\n\r\nx\x9c30\xa0=\x00\x00\xb3q\x12\xc1' |
| 68 | + >>> _, _, decompr = envelope_parser.parse_message(compr) |
| 69 | + >>> data == decompr |
| 70 | + True |
| 71 | + |
| 72 | +## Build |
| 73 | + # Update protobuf formats use: |
| 74 | + #(Protobuf 3.2.0+)[https://github.com/google/protobuf/releases] should |
| 75 | + # be installed and be in $PATH |
| 76 | + cd configs && protoc rsb_event.proto --python_out ../ && cd .. |
| 77 | + python3 setup.py build |
0 commit comments