EditorConfig Core Python

0.17.1 · active · verified Thu Apr 09

The editorconfig-core-py library provides a Python implementation for locating and interpreting `.editorconfig` files. It allows applications to retrieve editor configuration properties (like indentation style, tab width, and line endings) for a given file path, ensuring consistent coding styles across different editors and IDEs. The current version is 0.17.1, with releases typically tied to core EditorConfig specification updates or Python compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `editorconfig.get_properties()` to retrieve configuration for different file types. It creates a temporary `.editorconfig` file and two test files, then prints the resolved properties. Remember to clean up temporary files in production code or use temporary file utilities.

import os
import editorconfig

# Create a dummy .editorconfig file and a test file for demonstration
temp_dir = "temp_editorconfig_test"
os.makedirs(temp_dir, exist_ok=True)

editorconfig_path = os.path.join(temp_dir, ".editorconfig")
with open(editorconfig_path, "w") as f:
    f.write("[*.py]\nindent_style = space\nindent_size = 4\nend_of_line = lf\n\n[*.md]\ntrim_trailing_whitespace = true\n")

test_file_path_py = os.path.join(temp_dir, "my_module.py")
with open(test_file_path_py, "w") as f:
    f.write("# Dummy Python file content")

test_file_path_md = os.path.join(temp_dir, "README.md")
with open(test_file_path_md, "w") as f:
    f.write("# Dummy Markdown content")

try:
    properties_py = editorconfig.get_properties(test_file_path_py)
    print(f"EditorConfig properties for {test_file_path_py}:")
    for key, value in properties_py.items():
        print(f"  {key}: {value}")

    print("\n---\n")

    properties_md = editorconfig.get_properties(test_file_path_md)
    print(f"EditorConfig properties for {test_file_path_md}:")
    for key, value in properties_md.items():
        print(f"  {key}: {value}")

finally:
    # Clean up dummy files and directory
    os.remove(editorconfig_path)
    os.remove(test_file_path_py)
    os.remove(test_file_path_md)
    os.rmdir(temp_dir)

view raw JSON →