Skip to main content

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

  1. suppoort keep and align the comment
  2. auto patch list separator and field's required flag
  3. align the field's assign
  4. 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

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

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


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.7.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

thrift_fmt-0.2.7-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: thrift-fmt-0.2.7.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.1.3 CPython/3.10.6

File hashes

Hashes for thrift-fmt-0.2.7.tar.gz
Algorithm Hash digest
SHA256 27bcf450c397f514584df78e91d4f65665af6b1a2d6c1fd6f8e3a8aa77a308c4
MD5 c5f26e19f20b1228da42f74994016136
BLAKE2b-256 16635f75ca71a474399a45929e4c5fbc1f9f139116e91a3d5bfd48181ba9e087

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: thrift_fmt-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.1.3 CPython/3.10.6

File hashes

Hashes for thrift_fmt-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 c61c93f686c7d8e91c06e5753fa8fbff2d385512ee82d0121665b2d4ca00f6d8
MD5 37f2c8cdb7ea4cf464bd83e78a19b12a
BLAKE2b-256 0f0572e5e6bd68f81335023d7a070d7fa5732c2f16bd5ecf1cfad67d6cfc559f

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