format thrift file
Project description
thrift-fmt
Thrift Formatter
can be used as command line tool thrift-fmt
and python sdk thrift_fmt
use thrift-parser https://github.com/thrift-labs/thrift-parser as parser
Usage
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 -r ./thrift_files
Feature
- keep and align all comments
- patch list separator
- patch missed field's
required
flag - align by the field's assign (like go)
- align by each field's part
- Format only part of the parsed thrift
example
struct Work {
1: required i32 number_a = 0, // hello
2: optional i32 num2 = 1, // xyz
}
align by each field's part
struct Work {
1: required i32 number_a = 0 , // hello
2: required i32 num2 = 1 , // xyz
3: required list<i32> num3 = [ 1, 2, 3 ] , // num3
11: required string str_b = "hello-world",
}
Use as sdk
use thrift_parser.ThriftData
to parse from file or 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"'
TODO
- better code
- support function default add new line
- support indent for /* */ multi line comment
- support tight map/list define ?
- any other feature ?
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.3.0.tar.gz
.
File metadata
- Download URL: thrift-fmt-0.3.0.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.4.8 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c351469724046e71090d7990e8c94ab693096525b4ec81a9380156f63f65e56 |
|
MD5 | efeb89e222c10a89723c8aea91cf6a6b |
|
BLAKE2b-256 | 354dc1a07e4927405695a47b11f8ea21304bb353a910ba916e6632d51b1c6362 |
File details
Details for the file thrift_fmt-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: thrift_fmt-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.4.8 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 834d46caa2cf1714396eb2a88520c1f7c90247aff1971c2c8117ddef4f227f36 |
|
MD5 | f4c445f2b95c22ad78d4de6aa2af3609 |
|
BLAKE2b-256 | ceacc6b124130216e8af2917e551e683c18153bd262dab671bea6b2df2f102e6 |