wpsync 1.17.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wpsync/__init__.py +3 -0
- wpsync/__main__.py +3 -0
- wpsync/cli.py +1479 -0
- wpsync/config.py +178 -0
- wpsync/documents.py +294 -0
- wpsync/editors.py +68 -0
- wpsync/markup/__init__.py +0 -0
- wpsync/markup/dom.py +371 -0
- wpsync/markup/markdown.py +526 -0
- wpsync/media.py +161 -0
- wpsync/posts.py +78 -0
- wpsync/pull.py +400 -0
- wpsync/push.py +288 -0
- wpsync/state.py +219 -0
- wpsync/storage.py +28 -0
- wpsync/sync.py +501 -0
- wpsync/trash.py +132 -0
- wpsync/wordpress/__init__.py +0 -0
- wpsync/wordpress/autop.py +114 -0
- wpsync/wordpress/client.py +233 -0
- wpsync/wordpress/content.py +602 -0
- wpsync/wordpress/remote.py +264 -0
- wpsync-1.17.2.dist-info/METADATA +148 -0
- wpsync-1.17.2.dist-info/RECORD +27 -0
- wpsync-1.17.2.dist-info/WHEEL +4 -0
- wpsync-1.17.2.dist-info/entry_points.txt +2 -0
- wpsync-1.17.2.dist-info/licenses/LICENSE +61 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Expand classic-editor content the way WordPress displays it.
|
|
2
|
+
|
|
3
|
+
The classic editor stores paragraphs as blank-line-separated text and line
|
|
4
|
+
breaks as single newlines; WordPress's `wpautop` display filter adds the `<p>`
|
|
5
|
+
and `<br />` tags. This is an independent implementation of that behavior,
|
|
6
|
+
checked against WordPress's own output in characterization runs.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
BLOCKS = (
|
|
14
|
+
"table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|"
|
|
15
|
+
"pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|"
|
|
16
|
+
"section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|"
|
|
17
|
+
"summary"
|
|
18
|
+
)
|
|
19
|
+
BLOCK_TAG = rf"</?(?:{BLOCKS})[^>]*>"
|
|
20
|
+
BLOCK_NAMES = frozenset(
|
|
21
|
+
name
|
|
22
|
+
for alternative in BLOCKS.split("|")
|
|
23
|
+
for name in (
|
|
24
|
+
[f"h{level}" for level in range(1, 7)]
|
|
25
|
+
if alternative == "h[1-6]"
|
|
26
|
+
else [alternative]
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
"""Tags that `wpautop` treats as paragraph boundaries."""
|
|
30
|
+
PRE_CONTENT = re.compile(r"(<pre\b[^>]*>)(.*?)(</pre>)", re.S | re.I)
|
|
31
|
+
KEPT_NEWLINES = re.compile(r"<(script|style|svg|math)\b.*?</\1>", re.S | re.I)
|
|
32
|
+
MARKUP = re.compile(r"<!--.*?-->|<[^>]*>", re.S)
|
|
33
|
+
PRE_PLACEHOLDER = "\x00pre{}\x00"
|
|
34
|
+
NEWLINE_PLACEHOLDER = "\x01"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _sub(pattern: str, replacement: str, text: str) -> str:
|
|
38
|
+
return re.sub(pattern, replacement, text, flags=re.I)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def autop(text: str) -> str:
|
|
42
|
+
if not text.strip():
|
|
43
|
+
return ""
|
|
44
|
+
text = text.replace("\r\n", "\n").replace("\r", "\n")
|
|
45
|
+
preformatted: list[str] = []
|
|
46
|
+
|
|
47
|
+
def hide_pre(match: re.Match[str]) -> str:
|
|
48
|
+
preformatted.append(match.group(2))
|
|
49
|
+
return (
|
|
50
|
+
match.group(1)
|
|
51
|
+
+ PRE_PLACEHOLDER.format(len(preformatted) - 1)
|
|
52
|
+
+ match.group(3)
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
def keep_newlines(match: re.Match[str]) -> str:
|
|
56
|
+
return match.group(0).replace("\n", NEWLINE_PLACEHOLDER)
|
|
57
|
+
|
|
58
|
+
text = PRE_CONTENT.sub(hide_pre, text)
|
|
59
|
+
# Newlines inside a tag or comment belong to that markup, not to the text.
|
|
60
|
+
text = MARKUP.sub(keep_newlines, text)
|
|
61
|
+
text = _sub(r"<br\s*/?>\s*<br\s*/?>", "\n\n", text)
|
|
62
|
+
text = _sub(rf"(<(?:{BLOCKS})[\s/>])", r"\n\n\1", text)
|
|
63
|
+
text = _sub(rf"(</(?:{BLOCKS})>)", r"\1\n\n", text)
|
|
64
|
+
text = _sub(r"(<hr\s*/?>)", r"\1\n\n", text)
|
|
65
|
+
text = _keep_together(text)
|
|
66
|
+
# Only newlines are trimmed; other edge whitespace stays inside the paragraph.
|
|
67
|
+
chunks = (chunk.strip("\n") for chunk in re.split(r"\n\s*\n", text) if chunk)
|
|
68
|
+
text = "".join(f"<p>{chunk}</p>\n" for chunk in chunks)
|
|
69
|
+
|
|
70
|
+
text = _sub(r"<p>\s*</p>", "", text)
|
|
71
|
+
text = _sub(r"<p>([^<]+)</(div|address|form)>", r"<p>\1</p></\2>", text)
|
|
72
|
+
text = _sub(rf"<p>\s*({BLOCK_TAG})\s*</p>", r"\1", text)
|
|
73
|
+
text = _sub(r"<p>(<li.+?)</p>", r"\1", text)
|
|
74
|
+
text = _sub(r"<p><blockquote([^>]*)>", r"<blockquote\1><p>", text)
|
|
75
|
+
text = text.replace("</blockquote></p>", "</p></blockquote>")
|
|
76
|
+
text = _sub(rf"<p>\s*({BLOCK_TAG})", r"\1", text)
|
|
77
|
+
text = _sub(rf"({BLOCK_TAG})\s*</p>", r"\1", text)
|
|
78
|
+
|
|
79
|
+
text = KEPT_NEWLINES.sub(keep_newlines, text)
|
|
80
|
+
text = _sub(r"<br\s*/?>", "<br />", text)
|
|
81
|
+
text = re.sub(r"(?<!<br />)\s*\n", "<br />\n", text)
|
|
82
|
+
text = _sub(rf"({BLOCK_TAG})\s*<br />", r"\1", text)
|
|
83
|
+
text = _sub(
|
|
84
|
+
r"<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)", r"\1", text
|
|
85
|
+
)
|
|
86
|
+
text = re.sub(r"\n</p>$", "</p>", text)
|
|
87
|
+
|
|
88
|
+
text = text.replace(NEWLINE_PLACEHOLDER, "\n")
|
|
89
|
+
for index, content in enumerate(preformatted):
|
|
90
|
+
text = text.replace(PRE_PLACEHOLDER.format(index), content)
|
|
91
|
+
return text
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _keep_together(text: str) -> str:
|
|
95
|
+
"""Rejoin elements that must stay attached to their neighbors.
|
|
96
|
+
|
|
97
|
+
Option lists, object parameters, media sources, and figure captions are not
|
|
98
|
+
split into paragraphs of their own.
|
|
99
|
+
"""
|
|
100
|
+
if "<option" in text:
|
|
101
|
+
text = _sub(r"\s*<option", "<option", text)
|
|
102
|
+
text = _sub(r"</option>\s*", "</option>", text)
|
|
103
|
+
if "</object>" in text:
|
|
104
|
+
text = _sub(r"(<object[^>]*>)\s*", r"\1", text)
|
|
105
|
+
text = _sub(r"\s*</object>", "</object>", text)
|
|
106
|
+
text = _sub(r"\s*(</?(?:param|embed)[^>]*>)\s*", r"\1", text)
|
|
107
|
+
if "<source" in text or "<track" in text:
|
|
108
|
+
text = _sub(r"([<\[](?:audio|video)[^>\]]*[>\]])\s*", r"\1", text)
|
|
109
|
+
text = _sub(r"\s*([<\[]/(?:audio|video)[>\]])", r"\1", text)
|
|
110
|
+
text = _sub(r"\s*(<(?:source|track)[^>]*>)\s*", r"\1", text)
|
|
111
|
+
if "<figcaption" in text:
|
|
112
|
+
text = _sub(r"\s*(<figcaption[^>]*>)", r"\1", text)
|
|
113
|
+
text = _sub(r"</figcaption>\s*", "</figcaption>", text)
|
|
114
|
+
return text
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"""WordPress REST API access with Application Password authentication."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import html
|
|
7
|
+
import json
|
|
8
|
+
from collections.abc import Iterator
|
|
9
|
+
from datetime import UTC, datetime, timedelta, timezone, tzinfo
|
|
10
|
+
from typing import Any
|
|
11
|
+
from zoneinfo import ZoneInfo
|
|
12
|
+
|
|
13
|
+
import httpx
|
|
14
|
+
|
|
15
|
+
from wpsync import __version__
|
|
16
|
+
from wpsync.posts import ContentFormat, PostStatus, PostSummary, RemoteState
|
|
17
|
+
|
|
18
|
+
API_LINK_RELATION = "https://api.w.org/"
|
|
19
|
+
TIMEOUT = httpx.Timeout(30.0)
|
|
20
|
+
PAGE_SIZE = 100
|
|
21
|
+
SYNCED_STATUSES = "publish,future,draft,pending,private"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def local_status(status: str) -> PostStatus:
|
|
25
|
+
"""Only a published post is public; drafts, scheduled, pending, and
|
|
26
|
+
WordPress-private posts stay local until marked public."""
|
|
27
|
+
return PostStatus.PUBLIC if status == "publish" else PostStatus.PRIVATE
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
HEADERS = {"User-Agent": f"wpsync/{__version__}"}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class WordPressError(Exception):
|
|
34
|
+
"""Raised when a WordPress site cannot be reached or rejects a request."""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def request(
|
|
38
|
+
http: httpx.Client, method: str, url: str | httpx.URL, **options: Any
|
|
39
|
+
) -> httpx.Response:
|
|
40
|
+
try:
|
|
41
|
+
return http.request(method, url, **options)
|
|
42
|
+
except httpx.HTTPError as error:
|
|
43
|
+
raise WordPressError(f"could not reach {url}: {error}") from error
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def discover_api_root(site: str, transport: httpx.BaseTransport | None = None) -> str:
|
|
47
|
+
"""Find the REST API root that a WordPress site advertises."""
|
|
48
|
+
with httpx.Client(
|
|
49
|
+
headers=HEADERS, timeout=TIMEOUT, follow_redirects=True, transport=transport
|
|
50
|
+
) as http:
|
|
51
|
+
response = request(http, "GET", site)
|
|
52
|
+
link = response.links.get(API_LINK_RELATION)
|
|
53
|
+
if link is None or not link.get("url"):
|
|
54
|
+
raise WordPressError(f"{site} is not a WordPress site (no REST API link)")
|
|
55
|
+
return link["url"]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def route_url(
|
|
59
|
+
api_root: str, route: str, params: dict[str, Any] | None = None
|
|
60
|
+
) -> httpx.URL:
|
|
61
|
+
"""Build a route URL for both `/wp-json/` and `?rest_route=` API roots."""
|
|
62
|
+
root = httpx.URL(api_root)
|
|
63
|
+
if "rest_route" in root.params:
|
|
64
|
+
url = root.copy_set_param("rest_route", f"/{route}")
|
|
65
|
+
else:
|
|
66
|
+
url = root.copy_with(path=root.path.rstrip("/") + f"/{route}")
|
|
67
|
+
return url.copy_merge_params(params or {})
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _error_message(response: httpx.Response) -> str:
|
|
71
|
+
try:
|
|
72
|
+
document = response.json()
|
|
73
|
+
except ValueError:
|
|
74
|
+
document = None
|
|
75
|
+
if isinstance(document, dict) and "message" in document:
|
|
76
|
+
detail = f"{document['message']} ({document.get('code', 'unknown')})"
|
|
77
|
+
else:
|
|
78
|
+
detail = response.reason_phrase
|
|
79
|
+
message = f"WordPress rejected the request (HTTP {response.status_code}): {detail}"
|
|
80
|
+
if isinstance(document, dict) and document.get("code") == "rest_not_logged_in":
|
|
81
|
+
message += (
|
|
82
|
+
"\nWordPress ignored the credentials. Application Passwords may be "
|
|
83
|
+
"disabled on this site (they require HTTPS), or the host may strip "
|
|
84
|
+
"the Authorization header before WordPress sees it."
|
|
85
|
+
)
|
|
86
|
+
return message
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
FINGERPRINT_FIELDS = "id,title,content,slug,status,date_gmt,categories,tags"
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def fingerprint(post: dict[str, Any]) -> str:
|
|
93
|
+
"""A checksum of the stored fields WPSync synchronizes."""
|
|
94
|
+
fields = {
|
|
95
|
+
"title": post["title"]["raw"],
|
|
96
|
+
"content": post["content"]["raw"],
|
|
97
|
+
"slug": post["slug"],
|
|
98
|
+
"status": post["status"],
|
|
99
|
+
"date_gmt": post["date_gmt"],
|
|
100
|
+
"categories": sorted(post["categories"]),
|
|
101
|
+
"tags": sorted(post["tags"]),
|
|
102
|
+
}
|
|
103
|
+
encoded = json.dumps(fields, sort_keys=True, ensure_ascii=False)
|
|
104
|
+
return hashlib.sha256(encoded.encode()).hexdigest()
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def display_title(post: dict[str, Any]) -> str:
|
|
108
|
+
"""A post's title as readers see it; WordPress may store `&` as `&`."""
|
|
109
|
+
return html.unescape(post["title"]["raw"])
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def remote_state(post: dict[str, Any]) -> RemoteState:
|
|
113
|
+
blocks = "<!-- wp:" in post["content"]["raw"]
|
|
114
|
+
return RemoteState(
|
|
115
|
+
fingerprint(post),
|
|
116
|
+
ContentFormat.BLOCKS if blocks else ContentFormat.HTML,
|
|
117
|
+
post["status"] == "publish",
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def parse_gmt(value: str) -> datetime:
|
|
122
|
+
return datetime.fromisoformat(value).replace(tzinfo=UTC)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class WordPressClient:
|
|
126
|
+
def __init__(
|
|
127
|
+
self,
|
|
128
|
+
api_root: str,
|
|
129
|
+
username: str,
|
|
130
|
+
password: str,
|
|
131
|
+
transport: httpx.BaseTransport | None = None,
|
|
132
|
+
) -> None:
|
|
133
|
+
self.api_root = api_root
|
|
134
|
+
self.http = httpx.Client(
|
|
135
|
+
auth=(username, password),
|
|
136
|
+
headers=HEADERS,
|
|
137
|
+
timeout=TIMEOUT,
|
|
138
|
+
transport=transport,
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
def __enter__(self) -> WordPressClient:
|
|
142
|
+
return self
|
|
143
|
+
|
|
144
|
+
def __exit__(self, *exc_info: object) -> None:
|
|
145
|
+
self.http.close()
|
|
146
|
+
|
|
147
|
+
def get(self, route: str, params: dict[str, Any] | None = None) -> httpx.Response:
|
|
148
|
+
return self._call("GET", route, params)
|
|
149
|
+
|
|
150
|
+
def post(
|
|
151
|
+
self, route: str, params: dict[str, Any] | None = None, **options: Any
|
|
152
|
+
) -> httpx.Response:
|
|
153
|
+
return self._call("POST", route, params, **options)
|
|
154
|
+
|
|
155
|
+
def delete(
|
|
156
|
+
self, route: str, params: dict[str, Any] | None = None
|
|
157
|
+
) -> httpx.Response:
|
|
158
|
+
return self._call("DELETE", route, params)
|
|
159
|
+
|
|
160
|
+
def _call(
|
|
161
|
+
self,
|
|
162
|
+
method: str,
|
|
163
|
+
route: str,
|
|
164
|
+
params: dict[str, Any] | None,
|
|
165
|
+
**options: Any,
|
|
166
|
+
) -> httpx.Response:
|
|
167
|
+
url = route_url(self.api_root, route, params)
|
|
168
|
+
response = request(self.http, method, url, **options)
|
|
169
|
+
if response.is_error:
|
|
170
|
+
raise WordPressError(_error_message(response))
|
|
171
|
+
return response
|
|
172
|
+
|
|
173
|
+
def get_all(self, route: str, params: dict[str, Any]) -> Iterator[Any]:
|
|
174
|
+
page = 1
|
|
175
|
+
while True:
|
|
176
|
+
response = self.get(route, {**params, "per_page": PAGE_SIZE, "page": page})
|
|
177
|
+
yield from response.json()
|
|
178
|
+
if page >= int(response.headers.get("X-WP-TotalPages", "1")):
|
|
179
|
+
return
|
|
180
|
+
page += 1
|
|
181
|
+
|
|
182
|
+
def current_user(self) -> dict[str, Any]:
|
|
183
|
+
return self.get("wp/v2/users/me", {"context": "edit"}).json()
|
|
184
|
+
|
|
185
|
+
def posts(
|
|
186
|
+
self, fields: str, *, ids: list[str] | None = None, slug: str | None = None
|
|
187
|
+
) -> Iterator[dict[str, Any]]:
|
|
188
|
+
"""Posts in the synchronized statuses, oldest first, without trashed posts."""
|
|
189
|
+
params: dict[str, Any] = {
|
|
190
|
+
"context": "edit",
|
|
191
|
+
"status": SYNCED_STATUSES,
|
|
192
|
+
"orderby": "id",
|
|
193
|
+
"order": "asc",
|
|
194
|
+
"_fields": fields,
|
|
195
|
+
}
|
|
196
|
+
if slug is not None:
|
|
197
|
+
params["slug"] = slug
|
|
198
|
+
if ids is None:
|
|
199
|
+
yield from self.get_all("wp/v2/posts", params)
|
|
200
|
+
return
|
|
201
|
+
for start in range(0, len(ids), PAGE_SIZE):
|
|
202
|
+
batch = {**params, "include": ",".join(ids[start : start + PAGE_SIZE])}
|
|
203
|
+
yield from self.get_all("wp/v2/posts", batch)
|
|
204
|
+
|
|
205
|
+
def post_summaries(self) -> list[PostSummary]:
|
|
206
|
+
return [
|
|
207
|
+
PostSummary(
|
|
208
|
+
id=str(post["id"]), title=display_title(post), slug=post["slug"]
|
|
209
|
+
)
|
|
210
|
+
for post in self.posts("id,title,slug")
|
|
211
|
+
]
|
|
212
|
+
|
|
213
|
+
def post_states(self, ids: list[str]) -> dict[str, RemoteState]:
|
|
214
|
+
"""The current state of the given posts; missing or trashed posts are absent."""
|
|
215
|
+
return {
|
|
216
|
+
str(post["id"]): remote_state(post)
|
|
217
|
+
for post in self.posts(FINGERPRINT_FIELDS, ids=ids)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
def term_names(self, taxonomy: str) -> dict[int, str]:
|
|
221
|
+
terms = self.get_all(f"wp/v2/{taxonomy}", {"_fields": "id,name"})
|
|
222
|
+
return {term["id"]: html.unescape(term["name"]) for term in terms}
|
|
223
|
+
|
|
224
|
+
def create_term(self, taxonomy: str, name: str) -> int:
|
|
225
|
+
return self.post(f"wp/v2/{taxonomy}", json={"name": name}).json()["id"]
|
|
226
|
+
|
|
227
|
+
def site_timezone(self) -> tzinfo:
|
|
228
|
+
"""The time zone the site displays dates in."""
|
|
229
|
+
index = self.get("", {"_fields": "timezone_string,gmt_offset"}).json()
|
|
230
|
+
# A site set to a manual UTC offset has no time zone name.
|
|
231
|
+
if index.get("timezone_string"):
|
|
232
|
+
return ZoneInfo(index["timezone_string"])
|
|
233
|
+
return timezone(timedelta(hours=float(index.get("gmt_offset") or 0)))
|