Skip to main content

TextMate grammar parser for python

Install

pip install python-textmate

Usage

The parser can parse one line, to parse multiple lines iterate over each line and call parse()

>>> from textmate import TextMateGrammar, TextMateGrammarRepository
>>> import json
>>> 
>>> def load_json(file_path):
...     with open(file_path, "r") as f:
...         return json.load(f)
... 
>>> # Initialize the grammar repository
>>> repository = TextMateGrammarRepository([
...     load_json("MagicPython.tmLanguage.json"),
...     load_json("MagicRegExp.tmLanguage.json")
... ])
>>> 
>>> # Get the Python grammar
>>> python_grammar = repository.get_grammar_by_language("python")
>>> 
>>> if python_grammar:
...     # Initiatee the grammar parser
...     grammar = TextMateGrammar(python_grammar, repository)
...     3 Parse one line of code
...     code = "print(True) # the parser only parses one line."
...     result = grammar.parse(code)
...     print(result)
... 
[('punctuation.definition.comment.python', (12, 13)),
 ('comment.line.number-sign.python', (12, 46)),
 ('constant.language.python', (6, 10)),
 ('punctuation.parenthesis.begin.python', (5, 6)),
 ('punctuation.parenthesis.end.python', (10, 11)),
 ('constant.language.python', (6, 10)),
 ('punctuation.definition.arguments.end.python', (10, 11)),
 ('meta.function-call.python', (0, 11)),
 ('keyword.illegal.name.python', (6, 10)),
 ('support.function.builtin.python', (0, 5)),
 ('meta.function-call.generic.python', (0, 5)),
 ('meta.function-call.generic.python', (6, 10)),
 ('meta.function-call.arguments.python', (6, 10)),
 ('constant.language.python', (6, 10)),
 ('support.function.builtin.python', (0, 5)),
 ('punctuation.separator.period.python', (45, 46)),
 ('meta.member.access.python', (45, 46))]

Metadata

Release files for python-textmate 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-textmate 0.2.0
File Size Uploaded Provenance Actions
python_textmate-0.2.0.tar.gz 6.2 kB Without provenance Details for python_textmate-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-textmate 0.2.0
File Interpreter ABI Platform Actions
python_textmate-0.2.0-py3-none-any.whl Python 3 none any Details for python_textmate-0.2.0-py3-none-any.whl

Total release size: 13.8 kB

Release files / python_textmate-0.2.0.tar.gz

Download URL python_textmate-0.2.0.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
620417891826cc79a3c16ff15ca04b033b63bc4470f66fd92e96041a2009a1a6
BLAKE2b-256 checksum
How to use checksums
3689ac5e7e75f99116c1a1b687d73eb60c6a2415ec1871c122efda3a8238d118
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.11 Linux/6.9.3-76060903-generic

Release files / python_textmate-0.2.0-py3-none-any.whl

Download URL python_textmate-0.2.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b98678769583339589e04d27b9b482ba9830a951a910e0909a015cb8a81e8a2a
BLAKE2b-256 checksum
How to use checksums
aac5130c9a7bc50a94466608d839e7eccb551a3bcfbf2233edfc6b308814d27b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.11 Linux/6.9.3-76060903-generic

Security for python-textmate 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page