Skip to content

Commit 3456f76

Browse files
committed
[conf] Move Deployment to _conf/deploy.py
1 parent f802b6b commit 3456f76

File tree

2 files changed

+55
-56
lines changed

2 files changed

+55
-56
lines changed

_conf/deploy.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
from enum import Enum, auto
3+
4+
class Deployment(Enum):
5+
Github = auto()
6+
Gitee = auto()
7+
Raspi = auto() # Raspberry Pi
8+
Local = auto()
9+
10+
@classmethod
11+
def current(cls) -> 'Deployment':
12+
if os.environ.get('GITHUB_REPOSITORY') == 'SilverRainZ/ronin':
13+
return Deployment.Raspi
14+
if os.environ.get('GITHUB_WORKFLOW') == 'Publish Github Pages':
15+
return Deployment.Github
16+
if os.environ.get('GITHUB_WORKFLOW') == 'Publish Gitee Pages':
17+
return Deployment.Gitee
18+
return Deployment.Local
19+
20+
def is_private(self) -> bool:
21+
return not self.is_public()
22+
23+
def is_public(self) -> bool:
24+
return self in [Deployment.Github, Deployment.Gitee]
25+
26+
def is_mirror(self) -> bool:
27+
return self is not Deployment.Github
28+
29+
def url(self) -> str:
30+
if self == Deployment.Github:
31+
return 'https://silverrainz.me/'
32+
elif self == Deployment.Gitee:
33+
return 'https://silverrainz.gitee.io/'
34+
elif self == Deployment.Raspi:
35+
return 'https://rpi3/bullet'
36+
else:
37+
# file:///build_dir/html/index.html
38+
return 'TODO'
39+
40+
41+
D = Deployment.current()
42+
print('Deployment:', D)

conf.py

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66

77
# -- Path setup --------------------------------------------------------------
88

9-
# If extensions (or modules to document with autodoc) are in another directory,
10-
# add these directories to sys.path here. If the directory is relative to the
11-
# documentation root, use os.path.abspath to make it absolute, like shown here.
12-
#
139
from __future__ import annotations
14-
import os
1510
from datetime import datetime
16-
from enum import Enum, auto
1711
import yaml
1812

1913
# -- Split configurations ----------------------------------------------------
2014

2115
from _conf.schemas import _schemas
16+
from _conf.deploy import D
17+
18+
# For `.. only::` directive.
19+
if D.is_private():
20+
tags.add('private') # type: ignore
2221

2322
with open('./_conf/redirect.yml') as data:
2423
_redirects = yaml.safe_load(data)
@@ -39,48 +38,6 @@
3938

4039
# -- Enviroment information -----------------------------------------------------
4140

42-
class Deployment(Enum):
43-
Github = auto()
44-
Gitee = auto()
45-
Raspi = auto() # Raspberry Pi
46-
Local = auto()
47-
48-
@classmethod
49-
def current(cls) -> Deployment:
50-
if os.environ.get('GITHUB_REPOSITORY') == 'SilverRainZ/ronin':
51-
return Deployment.Raspi
52-
if os.environ.get('GITHUB_WORKFLOW') == 'Publish Github Pages':
53-
return Deployment.Github
54-
if os.environ.get('GITHUB_WORKFLOW') == 'Publish Gitee Pages':
55-
return Deployment.Gitee
56-
return Deployment.Local
57-
58-
def is_private(self) -> bool:
59-
return not self.is_public()
60-
61-
def is_public(self) -> bool:
62-
return self in [Deployment.Github, Deployment.Gitee]
63-
64-
def is_mirror(self) -> bool:
65-
return self is not Deployment.Github
66-
67-
def url(self) -> str:
68-
if self == Deployment.Github:
69-
return 'https://silverrainz.me/'
70-
elif self == Deployment.Gitee:
71-
return 'https://silverrainz.gitee.io/'
72-
elif self == Deployment.Raspi:
73-
return 'https://rpi3/bullet'
74-
else:
75-
# file:///build_dir/html/index.html
76-
return 'TODO'
77-
78-
D = Deployment.current()
79-
print('Deployment:', D)
80-
# For `.. only::` directive.
81-
if D.is_private():
82-
tags.add('private') # type: ignore
83-
8441
# -- General configuration ---------------------------------------------------
8542

8643
# Add any Sphinx extension module names here, as strings. They can be
@@ -179,7 +136,7 @@ def url(self) -> str:
179136
# html_theme_options['announcement'] = '</p>blahblah… </p>
180137

181138
if D.is_mirror():
182-
src = Deployment.Github
139+
src = D.Github
183140
msg = f'<p> 这是部署于 {D} 的镜像,访问源站点:<a class="source-page" href="{src.url()}">{src}</a></p>'
184141
if html_theme_options.get('announcement'):
185142
html_theme_options['announcement'] += msg
@@ -277,12 +234,12 @@ def url(self) -> str:
277234

278235
if D.is_public():
279236
extensions.append('sphinxcontrib.gtagjs')
280-
if D is Deployment.Github:
237+
if D is D.Github:
281238
gtagjs_ids = ['G-FYHS50G6DL']
282-
elif D is Deployment.Gitee:
239+
elif D is D.Gitee:
283240
gtagjs_ids = ['G-5MZDR9VPYN']
284241

285-
if D is Deployment.Local:
242+
if D is D.Local:
286243
extensions.append('sphinxnotes.snippet.ext')
287244
snippet_config = {}
288245
snippet_patterns = {
@@ -339,7 +296,7 @@ def url(self) -> str:
339296
lilypond_audio_volume = 300
340297
lilypond_audio_format = 'mp3'
341298

342-
if D is not Deployment.Local:
299+
if D is not D.Local:
343300
extensions.append('sphinxnotes.recentupdate')
344301
recentupdate_date_format = datefmt
345302
recentupdate_exclude_path = ['_templates']
@@ -353,7 +310,7 @@ def url(self) -> str:
353310
ogp_site_name = project
354311
ogp_image = D.url() + logo
355312

356-
if D is not Deployment.Local:
313+
if D is not D.Local:
357314
# Doesn't work locally
358315
extensions.append('notfound.extension')
359316
notfound_urls_prefix = ''
@@ -390,13 +347,13 @@ def url(self) -> str:
390347
'rst': 'reStructuredText',
391348
}
392349

393-
if D is not Deployment.Local:
350+
if D is not D.Local:
394351
# Speed up local build (prevent read git timestamp).
395352
extensions.append('sphinx_last_updated_by_git')
396353

397354
# For .. pdf-include:: directive.
398355
extensions.append('sphinx_simplepdf')
399356

400-
if D is Deployment.Local:
357+
if D is D.Local:
401358
# Speed up local incremental HTML build (may cause document inconsistencies).
402359
extensions.append('sphinxnotes.fasthtml')

0 commit comments

Comments
 (0)