formart thrift file
Project description
thrift-fmt
thrift formatter
the parser is https://github.com/alingse/thrift-parser
Usage
thrift-fmt mythrift.thrift
thrift-fmt --help
install
pip install thrift-fmt
format files
format single file and print to stdout
thrift-fmt mythrift.thrift
format and overwrite the origin file
thrift-fmt -w mythrift.thrift
format a directory, this will overwrite the origin file, please keep in track
thrift-fmt -d ./thrift_files
Use in Code
use ThriftData
parse from file / stdin / str
use ThriftFormatter
or PureThriftFormatter
format the parsed thrift data.
from thrift_fmt import ThriftFormatter, PureThriftFormatter
from thrift_parser import ThriftData
origin = '''
include "shared.thrift" // a
// work info
struct Work {
1: i32 num1 = 0,
2: required i32 num2, // num2 for
3: Operation op, // op is Operation
4: optional string comment,
5: map<string,list<string>> tags, //hello
}
'''
thrift = ThriftData.from_str(data)
out = ThriftFormatter(thrift).format()
assert out == '''
include "shared.thrift" // a
// work info
struct Work {
1: required i32 num1 = 0,
2: required i32 num2, // num2 for
3: required Operation op, // op is Operation
4: optional string comment,
5: required map<string, list<string>> tags, //hello
}
'''.strip()
# or only a single node
header = PureThriftFormatter().format_node(thrift.document.children[0])
assert header == 'include "shared.thrift"'
Feature
- suppoort keep and align the comment
- auto patch list separator and field's
required
flag - align the field's assign
- support format part of the thrift parsed result
example
struct Work {
1: required i32 number_a = 0, // hello
2: optional i32 num2 = 1, // xyz
}
TODO
- support function blank line count
- fix //a comment
- better code
- other language ?
- support function default add new line, function remove list sep
- support indent for /* */ multi line comment
- support tight map/list define ?
- any other featur ?
Dev
pdm install
pdm run pytest
pdm build
pdm run thrift-fmt --help
LICENSE
some thrift files in fixtures thrift was copy from https://github.com/apache/thrift/blob/master/tutorial/ , The Apache LICENSE
the Thrift.g4 in thrift-parser package was copy from https://github.com/antlr/grammars-v4
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file thrift-fmt-0.2.9.tar.gz
.
File metadata
- Download URL: thrift-fmt-0.2.9.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.2.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69ffa2d2e0ae68687ac9040b42ffc3c7d25cb04d6568d98fd5ee8ddbdaf7b064 |
|
MD5 | cb9e944a6447b46ba8c5bd15617875d3 |
|
BLAKE2b-256 | 33dd9c8af70c9b74909a8a99c5a32ef45cfa39ecfc55dd7d7bac65e1e604493d |
Provenance
File details
Details for the file thrift_fmt-0.2.9-py3-none-any.whl
.
File metadata
- Download URL: thrift_fmt-0.2.9-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.2.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d5f0f96a6d6d0451ced8f2cc801e8fab6788ebb3c35e17c949aa3da1296a0c9 |
|
MD5 | 0ec744d423f04bbe397808df74bdc39d |
|
BLAKE2b-256 | ab8d8cbd5c429f1e57a3cdb2b30963c877bf0f342416cd6c7c42c804e5003cc4 |