EditorConfig Core Python
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
- breaking Python 2 support was dropped in version 0.15.0. This library now requires Python 3.9 or newer.
- gotcha All property values returned by `get_properties` are strings, even for properties that represent numbers (e.g., `indent_size`) or booleans (e.g., `trim_trailing_whitespace`).
- gotcha The library correctly implements the EditorConfig specification for resolving properties, which means properties are determined by the closest `.editorconfig` file to the target path. Misunderstanding how file resolution works (e.g., the effect of `root = true` stopping the search) can lead to unexpected configurations.
Install
-
pip install editorconfig
Imports
- get_properties
import editorconfig properties = editorconfig.get_properties(filepath)
Quickstart
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)