Skip to main content

formart thrift file

Project description

thrift-fmt

thrift formatter

the parser is https://github.com/alingse/thrift-parser

Usage

install

pip install thrift-fmt

format files

single file

thrift-fmt mythrift.thrift
thrift-fmt mythrift.thrift -w

or directory (this will overwrite the origin file, please keep in track)

thrift-fmt -d ./thrift_files

or more options see help

thrift-fmt --help

Use in Code

use ThriftData parse from file / stdin / str

use ThriftFormatter or PureThriftFormatter format the 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

  1. suppoort keep the comment
  2. patch the required field
  3. align the fields comment
  4. format only single field

TODO

  1. support function blank line count
  2. fix //a comment
  3. support Enum field
  4. better code
  5. other language ?

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thrift-fmt-0.2.3.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

thrift_fmt-0.2.3-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file thrift-fmt-0.2.3.tar.gz.

File metadata

  • Download URL: thrift-fmt-0.2.3.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for thrift-fmt-0.2.3.tar.gz
Algorithm Hash digest
SHA256 106208d20ed1c24ee2127e0e7901f8ccaf8eb3ff910fb3c1878b81f10cc04bf2
MD5 5b7c98d3df17ce6f43f96653a1cdee50
BLAKE2b-256 051cdc03f2d9c14e5c367ac9a6eba84affe77f9fec0812f7d0e19abe7aab5387

See more details on using hashes here.

Provenance

File details

Details for the file thrift_fmt-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: thrift_fmt-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for thrift_fmt-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cbfca0237ac7486bd5d9b8ff792ae1161e2d70e5e9a63ff82f145c076f052304
MD5 a9bc21a4bc6f1b88e829ee45306f4c6c
BLAKE2b-256 35402c4b7c1cabf022425f440e09a19d1ae82adbfb501c69784f38c280abd61b

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page