wpsync 1.2.1__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.
Potentially problematic release.
This version of wpsync might be problematic. Click here for more details.
- wpsync/__init__.py +3 -0
- wpsync/__main__.py +3 -0
- wpsync/cli.py +740 -0
- wpsync/config.py +173 -0
- wpsync/documents.py +258 -0
- wpsync/markup/__init__.py +0 -0
- wpsync/markup/dom.py +313 -0
- wpsync/markup/markdown.py +526 -0
- wpsync/media.py +92 -0
- wpsync/posts.py +76 -0
- wpsync/pull.py +324 -0
- wpsync/state.py +177 -0
- wpsync/storage.py +28 -0
- wpsync/sync.py +260 -0
- wpsync/wordpress/__init__.py +0 -0
- wpsync/wordpress/autop.py +114 -0
- wpsync/wordpress/client.py +208 -0
- wpsync/wordpress/content.py +565 -0
- wpsync/wordpress/remote.py +100 -0
- wpsync-1.2.1.dist-info/METADATA +312 -0
- wpsync-1.2.1.dist-info/RECORD +24 -0
- wpsync-1.2.1.dist-info/WHEEL +4 -0
- wpsync-1.2.1.dist-info/entry_points.txt +2 -0
- wpsync-1.2.1.dist-info/licenses/LICENSE +61 -0
wpsync/sync.py
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"""Comparing local post files with their last synchronized state."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import difflib
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from enum import StrEnum
|
|
9
|
+
from pathlib import Path, PurePosixPath
|
|
10
|
+
from typing import Generic, Protocol, TypeVar
|
|
11
|
+
|
|
12
|
+
from wpsync.documents import (
|
|
13
|
+
FIELDS,
|
|
14
|
+
POSTS_DIR,
|
|
15
|
+
LocalDocument,
|
|
16
|
+
body_checksum,
|
|
17
|
+
fields_checksum,
|
|
18
|
+
)
|
|
19
|
+
from wpsync.posts import ContentFormat, Post, PostSummary, RemoteState
|
|
20
|
+
from wpsync.state import PostRecord
|
|
21
|
+
|
|
22
|
+
C = TypeVar("C", bound="Candidate")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class SelectionError(ValueError):
|
|
26
|
+
"""Raised when a post selector matches no post or several."""
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Remote(Protocol):
|
|
30
|
+
def post_states(self, ids: list[str]) -> dict[str, RemoteState]: ...
|
|
31
|
+
|
|
32
|
+
def post_summaries(self) -> list[PostSummary]: ...
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class Selection:
|
|
37
|
+
documents: list[LocalDocument]
|
|
38
|
+
records: dict[str, PostRecord]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def select(
|
|
42
|
+
selector: str | None,
|
|
43
|
+
documents: list[LocalDocument],
|
|
44
|
+
records: dict[str, PostRecord],
|
|
45
|
+
*,
|
|
46
|
+
root: Path,
|
|
47
|
+
base: Path,
|
|
48
|
+
) -> Selection:
|
|
49
|
+
"""Narrow to one post chosen by post ID, file path, or slug."""
|
|
50
|
+
if selector is None:
|
|
51
|
+
return Selection(documents, records)
|
|
52
|
+
if selector.isdigit():
|
|
53
|
+
chosen = [item for item in documents if item.document.post_id == selector]
|
|
54
|
+
if chosen or selector in records:
|
|
55
|
+
record = {selector: records[selector]} if selector in records else {}
|
|
56
|
+
return Selection(chosen, record)
|
|
57
|
+
raise SelectionError(f"no local post has id {selector}")
|
|
58
|
+
if "/" in selector or selector.endswith(".md"):
|
|
59
|
+
path = (base / selector).resolve()
|
|
60
|
+
try:
|
|
61
|
+
relative = PurePosixPath(path.relative_to(root).as_posix())
|
|
62
|
+
except ValueError as error:
|
|
63
|
+
raise SelectionError(f"{selector} is outside the profile folder") from error
|
|
64
|
+
chosen = [item for item in documents if PurePosixPath(item.path) == relative]
|
|
65
|
+
if not chosen:
|
|
66
|
+
raise SelectionError(f"{selector} is not a post file under {POSTS_DIR}/")
|
|
67
|
+
else:
|
|
68
|
+
chosen = [item for item in documents if item.document.post.slug == selector]
|
|
69
|
+
if not chosen:
|
|
70
|
+
raise SelectionError(f"no local post has slug '{selector}'")
|
|
71
|
+
if len(chosen) > 1:
|
|
72
|
+
paths = ", ".join(str(item.path) for item in chosen)
|
|
73
|
+
raise SelectionError(f"slug '{selector}' is used by {paths}")
|
|
74
|
+
post_id = chosen[0].document.post_id
|
|
75
|
+
record = {post_id: records[post_id]} if post_id in records else {}
|
|
76
|
+
return Selection(chosen, record)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class Candidate(Protocol):
|
|
80
|
+
@property
|
|
81
|
+
def id(self) -> str: ...
|
|
82
|
+
|
|
83
|
+
@property
|
|
84
|
+
def title(self) -> str: ...
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def slug(self) -> str: ...
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass(frozen=True)
|
|
91
|
+
class Match(Generic[C]):
|
|
92
|
+
"""How a post relates to the posts on the other side."""
|
|
93
|
+
|
|
94
|
+
post: C | None
|
|
95
|
+
reason: str = ""
|
|
96
|
+
candidates: tuple[C, ...] = ()
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def match_post(post: Post, candidates: Sequence[C]) -> Match[C]:
|
|
100
|
+
"""Match by slug and title, never choosing among several candidates."""
|
|
101
|
+
by_slug = [c for c in candidates if post.slug and c.slug == post.slug]
|
|
102
|
+
title = post.title.strip()
|
|
103
|
+
by_title = [c for c in candidates if title and c.title.strip() == title]
|
|
104
|
+
found = {c.id: c for c in by_slug + by_title}
|
|
105
|
+
if not found:
|
|
106
|
+
return Match(None)
|
|
107
|
+
if len(found) > 1:
|
|
108
|
+
return Match(None, candidates=tuple(found.values()))
|
|
109
|
+
reasons = [
|
|
110
|
+
name for name, group in (("title", by_title), ("slug", by_slug)) if group
|
|
111
|
+
]
|
|
112
|
+
return Match(next(iter(found.values())), "same " + " and ".join(reasons))
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class Action(StrEnum):
|
|
116
|
+
NEW = "+"
|
|
117
|
+
CHANGED = "~"
|
|
118
|
+
DELETED = "-"
|
|
119
|
+
CONFLICT = "?"
|
|
120
|
+
UNCHANGED = "="
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@dataclass(frozen=True)
|
|
124
|
+
class DiffEntry:
|
|
125
|
+
action: Action
|
|
126
|
+
side: str
|
|
127
|
+
"""'l' when the difference is local, 'r' when remote, blank for both."""
|
|
128
|
+
post_id: str | None
|
|
129
|
+
path: PurePosixPath
|
|
130
|
+
detail: str = ""
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@dataclass(frozen=True)
|
|
134
|
+
class Diff:
|
|
135
|
+
entries: list[DiffEntry]
|
|
136
|
+
notes: list[str]
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _local_changes(document: LocalDocument, record: PostRecord) -> list[str]:
|
|
140
|
+
post = document.document.post
|
|
141
|
+
changes = []
|
|
142
|
+
if body_checksum(post) != record.body_checksum:
|
|
143
|
+
changes.append("content")
|
|
144
|
+
if fields_checksum(post) != record.fields_checksum:
|
|
145
|
+
changes.append("details")
|
|
146
|
+
return changes
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _typo_notes(document: LocalDocument) -> list[str]:
|
|
150
|
+
notes = []
|
|
151
|
+
for key in document.document.extra_keys:
|
|
152
|
+
close = difflib.get_close_matches(key, FIELDS, n=1, cutoff=0.75)
|
|
153
|
+
if close:
|
|
154
|
+
notes.append(
|
|
155
|
+
f"{document.path}: '{key}' is not synced; did you mean '{close[0]}'?"
|
|
156
|
+
)
|
|
157
|
+
return notes
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def compare(selection: Selection, remote: Remote) -> Diff:
|
|
161
|
+
"""Classify the selected posts, contacting the remote only when needed."""
|
|
162
|
+
entries: list[DiffEntry] = []
|
|
163
|
+
changed: dict[str, tuple[LocalDocument, list[str]]] = {}
|
|
164
|
+
unlinked: list[LocalDocument] = []
|
|
165
|
+
new: list[LocalDocument] = []
|
|
166
|
+
notes: list[str] = []
|
|
167
|
+
present = set()
|
|
168
|
+
for document in selection.documents:
|
|
169
|
+
notes.extend(_typo_notes(document))
|
|
170
|
+
post_id = document.document.post_id
|
|
171
|
+
path = PurePosixPath(document.path)
|
|
172
|
+
if post_id is None:
|
|
173
|
+
new.append(document)
|
|
174
|
+
continue
|
|
175
|
+
present.add(post_id)
|
|
176
|
+
record = selection.records.get(post_id)
|
|
177
|
+
if record is None:
|
|
178
|
+
unlinked.append(document)
|
|
179
|
+
continue
|
|
180
|
+
changes = _local_changes(document, record)
|
|
181
|
+
if changes:
|
|
182
|
+
changed[post_id] = (document, changes)
|
|
183
|
+
else:
|
|
184
|
+
entries.append(DiffEntry(Action.UNCHANGED, "", post_id, path))
|
|
185
|
+
for post_id, record in selection.records.items():
|
|
186
|
+
if post_id not in present:
|
|
187
|
+
entries.append(
|
|
188
|
+
DiffEntry(
|
|
189
|
+
Action.DELETED,
|
|
190
|
+
"l",
|
|
191
|
+
post_id,
|
|
192
|
+
record.path,
|
|
193
|
+
"deleted locally; WordPress keeps the post",
|
|
194
|
+
)
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
ids = sorted({*changed, *(d.document.post_id for d in unlinked)}, key=int)
|
|
198
|
+
states = remote.post_states(ids) if ids else {} # type: ignore[arg-type]
|
|
199
|
+
for post_id, (document, changes) in changed.items():
|
|
200
|
+
record = selection.records[post_id]
|
|
201
|
+
state = states.get(post_id)
|
|
202
|
+
path = PurePosixPath(document.path)
|
|
203
|
+
detail = ", ".join(changes)
|
|
204
|
+
if state is None:
|
|
205
|
+
entries.append(
|
|
206
|
+
DiffEntry(
|
|
207
|
+
Action.CONFLICT,
|
|
208
|
+
"",
|
|
209
|
+
post_id,
|
|
210
|
+
path,
|
|
211
|
+
f"{detail}; deleted on WordPress",
|
|
212
|
+
)
|
|
213
|
+
)
|
|
214
|
+
elif state.fingerprint != record.remote_fingerprint:
|
|
215
|
+
entries.append(
|
|
216
|
+
DiffEntry(
|
|
217
|
+
Action.CONFLICT,
|
|
218
|
+
"",
|
|
219
|
+
post_id,
|
|
220
|
+
path,
|
|
221
|
+
f"{detail}; also changed on WordPress since the last sync",
|
|
222
|
+
)
|
|
223
|
+
)
|
|
224
|
+
else:
|
|
225
|
+
if "content" in changes and state.format is ContentFormat.BLOCKS:
|
|
226
|
+
detail += "; block post becomes plain HTML on push"
|
|
227
|
+
entries.append(DiffEntry(Action.CHANGED, "l", post_id, path, detail))
|
|
228
|
+
for document in unlinked:
|
|
229
|
+
post_id = document.document.post_id
|
|
230
|
+
exists = post_id in states
|
|
231
|
+
detail = (
|
|
232
|
+
"linked to a post that was never synced here"
|
|
233
|
+
if exists
|
|
234
|
+
else "linked to a post that is not on WordPress"
|
|
235
|
+
)
|
|
236
|
+
entries.append(
|
|
237
|
+
DiffEntry(
|
|
238
|
+
Action.CONFLICT, "", post_id, PurePosixPath(document.path), detail
|
|
239
|
+
)
|
|
240
|
+
)
|
|
241
|
+
if new:
|
|
242
|
+
summaries = remote.post_summaries()
|
|
243
|
+
for document in new:
|
|
244
|
+
entries.append(_new_entry(document, summaries))
|
|
245
|
+
entries.sort(key=lambda entry: str(entry.path))
|
|
246
|
+
return Diff(entries, notes)
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _new_entry(document: LocalDocument, summaries: Sequence[PostSummary]) -> DiffEntry:
|
|
250
|
+
path = PurePosixPath(document.path)
|
|
251
|
+
match = match_post(document.document.post, summaries)
|
|
252
|
+
if match.candidates:
|
|
253
|
+
listed = ", ".join(f"#{post.id} {post.title!r}" for post in match.candidates)
|
|
254
|
+
return DiffEntry(
|
|
255
|
+
Action.CONFLICT, "l", None, path, f"matches several posts: {listed}"
|
|
256
|
+
)
|
|
257
|
+
if match.post is not None:
|
|
258
|
+
detail = f"matches #{match.post.id} {match.post.title!r} ({match.reason})"
|
|
259
|
+
return DiffEntry(Action.NEW, "l", None, path, detail)
|
|
260
|
+
return DiffEntry(Action.NEW, "l", None, path, "new post")
|
|
File without changes
|
|
@@ -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,208 @@
|
|
|
1
|
+
"""WordPress REST API access with Application Password authentication."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
from collections.abc import Iterator
|
|
8
|
+
from datetime import UTC, datetime, timedelta, timezone, tzinfo
|
|
9
|
+
from typing import Any
|
|
10
|
+
from zoneinfo import ZoneInfo
|
|
11
|
+
|
|
12
|
+
import httpx
|
|
13
|
+
|
|
14
|
+
from wpsync import __version__
|
|
15
|
+
from wpsync.posts import ContentFormat, PostStatus, PostSummary, RemoteState
|
|
16
|
+
|
|
17
|
+
API_LINK_RELATION = "https://api.w.org/"
|
|
18
|
+
TIMEOUT = httpx.Timeout(30.0)
|
|
19
|
+
PAGE_SIZE = 100
|
|
20
|
+
SYNCED_STATUSES = "publish,future,draft,pending,private"
|
|
21
|
+
STATUSES = {
|
|
22
|
+
"publish": PostStatus.PUBLISHED,
|
|
23
|
+
"future": PostStatus.SCHEDULED,
|
|
24
|
+
"draft": PostStatus.DRAFT,
|
|
25
|
+
"pending": PostStatus.PENDING,
|
|
26
|
+
"private": PostStatus.PRIVATE,
|
|
27
|
+
}
|
|
28
|
+
HEADERS = {"User-Agent": f"wpsync/{__version__}"}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class WordPressError(Exception):
|
|
32
|
+
"""Raised when a WordPress site cannot be reached or rejects a request."""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def request(
|
|
36
|
+
http: httpx.Client, method: str, url: str | httpx.URL, **options: Any
|
|
37
|
+
) -> httpx.Response:
|
|
38
|
+
try:
|
|
39
|
+
return http.request(method, url, **options)
|
|
40
|
+
except httpx.HTTPError as error:
|
|
41
|
+
raise WordPressError(f"could not reach {url}: {error}") from error
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def discover_api_root(site: str, transport: httpx.BaseTransport | None = None) -> str:
|
|
45
|
+
"""Find the REST API root that a WordPress site advertises."""
|
|
46
|
+
with httpx.Client(
|
|
47
|
+
headers=HEADERS, timeout=TIMEOUT, follow_redirects=True, transport=transport
|
|
48
|
+
) as http:
|
|
49
|
+
response = request(http, "GET", site)
|
|
50
|
+
link = response.links.get(API_LINK_RELATION)
|
|
51
|
+
if link is None or not link.get("url"):
|
|
52
|
+
raise WordPressError(
|
|
53
|
+
f"{site} does not advertise a WordPress REST API "
|
|
54
|
+
f"(HTTP {response.status_code}, no '{API_LINK_RELATION}' link)"
|
|
55
|
+
)
|
|
56
|
+
return link["url"]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def route_url(
|
|
60
|
+
api_root: str, route: str, params: dict[str, Any] | None = None
|
|
61
|
+
) -> httpx.URL:
|
|
62
|
+
"""Build a route URL for both `/wp-json/` and `?rest_route=` API roots."""
|
|
63
|
+
root = httpx.URL(api_root)
|
|
64
|
+
if "rest_route" in root.params:
|
|
65
|
+
url = root.copy_set_param("rest_route", f"/{route}")
|
|
66
|
+
else:
|
|
67
|
+
url = root.copy_with(path=root.path.rstrip("/") + f"/{route}")
|
|
68
|
+
return url.copy_merge_params(params or {})
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _error_message(response: httpx.Response) -> str:
|
|
72
|
+
try:
|
|
73
|
+
document = response.json()
|
|
74
|
+
except ValueError:
|
|
75
|
+
document = None
|
|
76
|
+
if isinstance(document, dict) and "message" in document:
|
|
77
|
+
detail = f"{document['message']} ({document.get('code', 'unknown')})"
|
|
78
|
+
else:
|
|
79
|
+
detail = response.reason_phrase
|
|
80
|
+
message = f"WordPress rejected the request (HTTP {response.status_code}): {detail}"
|
|
81
|
+
if isinstance(document, dict) and document.get("code") == "rest_not_logged_in":
|
|
82
|
+
message += (
|
|
83
|
+
"\nWordPress ignored the credentials. Application Passwords may be "
|
|
84
|
+
"disabled on this site (they require HTTPS), or the host may strip "
|
|
85
|
+
"the Authorization header before WordPress sees it."
|
|
86
|
+
)
|
|
87
|
+
return message
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
FINGERPRINT_FIELDS = "id,title,content,slug,status,date_gmt,categories,tags"
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def fingerprint(post: dict[str, Any]) -> str:
|
|
94
|
+
"""A checksum of the stored fields WPSync synchronizes."""
|
|
95
|
+
fields = {
|
|
96
|
+
"title": post["title"]["raw"],
|
|
97
|
+
"content": post["content"]["raw"],
|
|
98
|
+
"slug": post["slug"],
|
|
99
|
+
"status": post["status"],
|
|
100
|
+
"date_gmt": post["date_gmt"],
|
|
101
|
+
"categories": sorted(post["categories"]),
|
|
102
|
+
"tags": sorted(post["tags"]),
|
|
103
|
+
}
|
|
104
|
+
encoded = json.dumps(fields, sort_keys=True, ensure_ascii=False)
|
|
105
|
+
return hashlib.sha256(encoded.encode()).hexdigest()
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def remote_state(post: dict[str, Any]) -> RemoteState:
|
|
109
|
+
blocks = "<!-- wp:" in post["content"]["raw"]
|
|
110
|
+
return RemoteState(
|
|
111
|
+
fingerprint(post), ContentFormat.BLOCKS if blocks else ContentFormat.HTML
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def parse_gmt(value: str) -> datetime:
|
|
116
|
+
return datetime.fromisoformat(value).replace(tzinfo=UTC)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class WordPressClient:
|
|
120
|
+
def __init__(
|
|
121
|
+
self,
|
|
122
|
+
api_root: str,
|
|
123
|
+
username: str,
|
|
124
|
+
password: str,
|
|
125
|
+
transport: httpx.BaseTransport | None = None,
|
|
126
|
+
) -> None:
|
|
127
|
+
self.api_root = api_root
|
|
128
|
+
self.http = httpx.Client(
|
|
129
|
+
auth=(username, password),
|
|
130
|
+
headers=HEADERS,
|
|
131
|
+
timeout=TIMEOUT,
|
|
132
|
+
transport=transport,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
def __enter__(self) -> WordPressClient:
|
|
136
|
+
return self
|
|
137
|
+
|
|
138
|
+
def __exit__(self, *exc_info: object) -> None:
|
|
139
|
+
self.http.close()
|
|
140
|
+
|
|
141
|
+
def get(self, route: str, params: dict[str, Any] | None = None) -> httpx.Response:
|
|
142
|
+
response = request(self.http, "GET", route_url(self.api_root, route, params))
|
|
143
|
+
if response.is_error:
|
|
144
|
+
raise WordPressError(_error_message(response))
|
|
145
|
+
return response
|
|
146
|
+
|
|
147
|
+
def get_all(self, route: str, params: dict[str, Any]) -> Iterator[Any]:
|
|
148
|
+
page = 1
|
|
149
|
+
while True:
|
|
150
|
+
response = self.get(route, {**params, "per_page": PAGE_SIZE, "page": page})
|
|
151
|
+
yield from response.json()
|
|
152
|
+
if page >= int(response.headers.get("X-WP-TotalPages", "1")):
|
|
153
|
+
return
|
|
154
|
+
page += 1
|
|
155
|
+
|
|
156
|
+
def current_user(self) -> dict[str, Any]:
|
|
157
|
+
return self.get("wp/v2/users/me", {"context": "edit"}).json()
|
|
158
|
+
|
|
159
|
+
def posts(
|
|
160
|
+
self, fields: str, *, ids: list[str] | None = None, slug: str | None = None
|
|
161
|
+
) -> Iterator[dict[str, Any]]:
|
|
162
|
+
"""Posts in the synchronized statuses, oldest first, without trashed posts."""
|
|
163
|
+
params: dict[str, Any] = {
|
|
164
|
+
"context": "edit",
|
|
165
|
+
"status": SYNCED_STATUSES,
|
|
166
|
+
"orderby": "id",
|
|
167
|
+
"order": "asc",
|
|
168
|
+
"_fields": fields,
|
|
169
|
+
}
|
|
170
|
+
if slug is not None:
|
|
171
|
+
params["slug"] = slug
|
|
172
|
+
if ids is None:
|
|
173
|
+
yield from self.get_all("wp/v2/posts", params)
|
|
174
|
+
return
|
|
175
|
+
for start in range(0, len(ids), PAGE_SIZE):
|
|
176
|
+
batch = {**params, "include": ",".join(ids[start : start + PAGE_SIZE])}
|
|
177
|
+
yield from self.get_all("wp/v2/posts", batch)
|
|
178
|
+
|
|
179
|
+
def post_summaries(self) -> list[PostSummary]:
|
|
180
|
+
return [
|
|
181
|
+
PostSummary(
|
|
182
|
+
id=str(post["id"]),
|
|
183
|
+
title=post["title"]["raw"],
|
|
184
|
+
slug=post["slug"],
|
|
185
|
+
status=STATUSES[post["status"]],
|
|
186
|
+
date=parse_gmt(post["date_gmt"]),
|
|
187
|
+
)
|
|
188
|
+
for post in self.posts("id,title,slug,status,date_gmt")
|
|
189
|
+
]
|
|
190
|
+
|
|
191
|
+
def post_states(self, ids: list[str]) -> dict[str, RemoteState]:
|
|
192
|
+
"""The current state of the given posts; missing or trashed posts are absent."""
|
|
193
|
+
return {
|
|
194
|
+
str(post["id"]): remote_state(post)
|
|
195
|
+
for post in self.posts(FINGERPRINT_FIELDS, ids=ids)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
def term_names(self, taxonomy: str) -> dict[int, str]:
|
|
199
|
+
terms = self.get_all(f"wp/v2/{taxonomy}", {"_fields": "id,name"})
|
|
200
|
+
return {term["id"]: term["name"] for term in terms}
|
|
201
|
+
|
|
202
|
+
def site_timezone(self) -> tzinfo:
|
|
203
|
+
"""The time zone the site displays dates in."""
|
|
204
|
+
index = self.get("", {"_fields": "timezone_string,gmt_offset"}).json()
|
|
205
|
+
# A site set to a manual UTC offset has no time zone name.
|
|
206
|
+
if index.get("timezone_string"):
|
|
207
|
+
return ZoneInfo(index["timezone_string"])
|
|
208
|
+
return timezone(timedelta(hours=float(index.get("gmt_offset") or 0)))
|