core
ruff_sync.core ¶
Core logic for ruff-sync.
__all__
module-attribute
¶
__all__ = [
"Config",
"FetchResult",
"RuffConfigFileName",
"UpstreamError",
"check",
"fetch_upstream_config",
"fetch_upstreams_concurrently",
"get_ruff_config",
"get_ruff_tool_table",
"is_ruff_toml_file",
"merge_ruff_toml",
"pull",
"resolve_raw_url",
"resolve_target_path",
"serialize_ruff_sync_config",
"to_git_url",
"toml_ruff_parse",
]
RuffConfigFileName ¶
Bases: str, Enum
Enumeration of Ruff configuration filenames.
Source code in src/ruff_sync/config_io.py
tried_order
classmethod
¶
Return the order in which configuration files should be tried.
FetchResult ¶
Bases: NamedTuple
Result of fetching an upstream configuration.
Source code in src/ruff_sync/core.py
UpstreamError ¶
Bases: Exception
Raised when one or more upstream fetches fail.
Attributes:
| Name | Type | Description |
|---|---|---|
errors |
Final[tuple[tuple[URL, BaseException], ...]]
|
A tuple of tuples containing the URL and the BaseException that occurred. |
Source code in src/ruff_sync/core.py
__init__ ¶
Initialize UpstreamError with a list of fetch failures.
Source code in src/ruff_sync/core.py
DiffContext ¶
Bases: NamedTuple
Context for printing a diff between local and merged configurations.
Source code in src/ruff_sync/core.py
Config ¶
Bases: TypedDict
Configuration schema for [tool.ruff-sync] in pyproject.toml.
Source code in src/ruff_sync/core.py
is_ruff_toml_file ¶
Return True if the path or URL indicates a ruff.toml file.
This handles cases with query strings or fragments by examining only the path component.
Source code in src/ruff_sync/config_io.py
resolve_target_path ¶
Resolve the target path for configuration files.
If 'to' is a file, it's used directly. Otherwise, it looks for existing ruff/pyproject.toml in the 'to' directory. If none found, it defaults to pyproject.toml unless the first upstream is a ruff.toml.
Source code in src/ruff_sync/config_io.py
is_git_url ¶
to_git_url ¶
Attempt to convert a browser or raw URL to a git (SSH) URL.
Supports GitHub and GitLab.
Source code in src/ruff_sync/core.py
resolve_raw_url ¶
Convert a GitHub or GitLab repository/blob URL to a raw content URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
URL
|
The URL to resolve. |
required |
branch
|
str
|
The default branch to use for repo URLs. |
'main'
|
path
|
str | None
|
The directory prefix for pyproject.toml. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
URL |
URL
|
The resolved raw content URL, or the original URL if no conversion applies. |
Source code in src/ruff_sync/core.py
download
async
¶
Download a file from a URL and return a StringIO object.
fetch_upstream_config
async
¶
Fetch the upstream pyproject.toml either via HTTP or git clone.
Source code in src/ruff_sync/core.py
get_ruff_config ¶
Get the ruff section or document from a TOML string.
If it does not exist and it is a pyproject.toml, create it.
Source code in src/ruff_sync/core.py
toml_ruff_parse ¶
Parse a TOML string for the tool.ruff section excluding certain ruff configs.
Source code in src/ruff_sync/core.py
merge_ruff_toml ¶
Merge the source and upstream tool ruff config with better whitespace preservation.
Examples:
>>> from tomlkit import parse
>>> source = parse("[tool.ruff]\nline-length = 80")
>>> upstream = parse("[tool.ruff]\nline-length = 100")["tool"]["ruff"]
>>> merged = merge_ruff_toml(source, upstream)
>>> print(merged.as_string())
[tool.ruff]
line-length = 100
Source code in src/ruff_sync/core.py
fetch_upstreams_concurrently
async
¶
Fetch multiple upstream configurations concurrently.
Uses asyncio.TaskGroup if available (Python 3.11+), otherwise falls back to asyncio.gather.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
upstreams
|
Iterable[URL]
|
The URLs to fetch. |
required |
client
|
AsyncClient
|
The HTTPX async client to use. |
required |
branch
|
str
|
The default branch for repo-root URLs. |
DEFAULT_BRANCH
|
path
|
str | None
|
The directory prefix for pyproject.toml in repo-root URLs. |
None
|
Returns:
| Type | Description |
|---|---|
list[FetchResult]
|
A list of FetchResult objects in the same order as the input upstreams. |
Raises:
| Type | Description |
|---|---|
UpstreamError
|
If one or more upstreams fail to fetch. |
Source code in src/ruff_sync/core.py
check
async
¶
Check if the local pyproject.toml / ruff.toml is in sync with the upstream.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
0 if in sync, 1 if out of sync. |
Examples:
>>> import asyncio
>>> from ruff_sync.cli import Arguments
>>> from httpx import URL
>>> import pathlib
>>> args = Arguments(
... command="check",
... upstream=URL("https://github.com/org/repo/blob/main/pyproject.toml"),
... to=pathlib.Path("pyproject.toml"),
... exclude=[],
... )
>>> # asyncio.run(check(args))
Source code in src/ruff_sync/core.py
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 | |
serialize_ruff_sync_config ¶
Serialize the ruff-sync CLI arguments into the TOML document.
Source code in src/ruff_sync/core.py
pull
async
¶
Pull the upstream ruff config and apply it to the source.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
0 on success, 1 on failure. |
Examples:
>>> import asyncio
>>> from ruff_sync.cli import Arguments
>>> from httpx import URL
>>> import pathlib
>>> args = Arguments(
... command="pull",
... upstream=URL("https://github.com/org/repo/blob/main/pyproject.toml"),
... to=pathlib.Path("pyproject.toml"),
... exclude=["lint.isort"],
... init=True,
... )
>>> # asyncio.run(pull(args))
Source code in src/ruff_sync/core.py
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 | |