1
- # -*- coding: utf-8 -*-
2
-
3
1
import os
4
2
import shlex
5
3
import shutil
@@ -14,61 +12,66 @@ from pelican.server import ComplexHTTPRequestHandler, RootedHTTPServer
14
12
from pelican.settings import DEFAULT_CONFIG, get_settings_from_file
15
13
16
14
OPEN_BROWSER_ON_SERVE = True
17
- SETTINGS_FILE_BASE = ' pelicanconf.py'
15
+ SETTINGS_FILE_BASE = " pelicanconf.py"
18
16
SETTINGS = {}
19
17
SETTINGS.update(DEFAULT_CONFIG)
20
18
LOCAL_SETTINGS = get_settings_from_file(SETTINGS_FILE_BASE)
21
19
SETTINGS.update(LOCAL_SETTINGS)
22
20
23
21
CONFIG = {
24
- ' settings_base' : SETTINGS_FILE_BASE,
25
- ' settings_publish': ' publishconf.py' ,
22
+ " settings_base" : SETTINGS_FILE_BASE,
23
+ " settings_publish": " publishconf.py" ,
26
24
# Output path. Can be absolute or relative to tasks.py. Default: 'output'
27
- ' deploy_path' : SETTINGS[' OUTPUT_PATH' ],
25
+ " deploy_path" : SETTINGS[" OUTPUT_PATH" ],
28
26
{% if ssh %}
29
27
# Remote server configuration
30
- ' ssh_user': ' {{ssh_user}}' ,
31
- ' ssh_host': ' {{ssh_host}}' ,
32
- ' ssh_port': ' {{ssh_port}}' ,
33
- ' ssh_path': ' {{ssh_target_dir}}' ,
28
+ " ssh_user": " {{ssh_user}}" ,
29
+ " ssh_host": " {{ssh_host}}" ,
30
+ " ssh_port": " {{ssh_port}}" ,
31
+ " ssh_path": " {{ssh_target_dir}}" ,
34
32
{% endif %}
35
33
{% if cloudfiles %}
36
34
# Rackspace Cloud Files configuration settings
37
- ' cloudfiles_username': ' {{cloudfiles_username}}' ,
38
- ' cloudfiles_api_key': ' {{cloudfiles_api_key}}' ,
39
- ' cloudfiles_container': ' {{cloudfiles_container}}' ,
35
+ " cloudfiles_username": " {{cloudfiles_username}}" ,
36
+ " cloudfiles_api_key": " {{cloudfiles_api_key}}" ,
37
+ " cloudfiles_container": " {{cloudfiles_container}}" ,
40
38
{% endif %}
41
39
{% if github %}
42
40
# Github Pages configuration
43
- ' github_pages_branch': ' {{github_pages_branch}}' ,
44
- ' commit_message': "'Publish site on {}'".format( datetime.date.today().isoformat()) ,
41
+ " github_pages_branch": " {{github_pages_branch}}" ,
42
+ " commit_message": f "'Publish site on {datetime.date.today().isoformat()}'" ,
45
43
{% endif %}
46
44
# Host and port for `serve`
47
- ' host': ' localhost' ,
48
- ' port' : 8000,
45
+ " host": " localhost" ,
46
+ " port" : 8000,
49
47
}
50
48
49
+
51
50
@task
52
51
def clean(c):
53
52
"""Remove generated files"""
54
- if os.path.isdir(CONFIG['deploy_path']):
55
- shutil.rmtree(CONFIG['deploy_path'])
56
- os.makedirs(CONFIG['deploy_path'])
53
+ if os.path.isdir(CONFIG["deploy_path"]):
54
+ shutil.rmtree(CONFIG["deploy_path"])
55
+ os.makedirs(CONFIG["deploy_path"])
56
+
57
57
58
58
@task
59
59
def build(c):
60
60
"""Build local version of site"""
61
- pelican_run('-s {settings_base}'.format(**CONFIG))
61
+ pelican_run("-s {settings_base}".format(**CONFIG))
62
+
62
63
63
64
@task
64
65
def rebuild(c):
65
66
"""`build` with the delete switch"""
66
- pelican_run('-d -s {settings_base}'.format(**CONFIG))
67
+ pelican_run("-d -s {settings_base}".format(**CONFIG))
68
+
67
69
68
70
@task
69
71
def regenerate(c):
70
72
"""Automatically regenerate site upon file modification"""
71
- pelican_run('-r -s {settings_base}'.format(**CONFIG))
73
+ pelican_run("-r -s {settings_base}".format(**CONFIG))
74
+
72
75
73
76
@task
74
77
def serve(c):
@@ -78,54 +81,59 @@ def serve(c):
78
81
allow_reuse_address = True
79
82
80
83
server = AddressReuseTCPServer(
81
- CONFIG['deploy_path'],
82
- (CONFIG['host'], CONFIG['port']),
83
- ComplexHTTPRequestHandler)
84
+ CONFIG["deploy_path"],
85
+ (CONFIG["host"], CONFIG["port"]),
86
+ ComplexHTTPRequestHandler,
87
+ )
84
88
85
89
if OPEN_BROWSER_ON_SERVE:
86
90
# Open site in default browser
87
91
import webbrowser
92
+
88
93
webbrowser.open("http://{host}:{port}".format(**CONFIG))
89
94
90
- sys.stderr.write(' Serving at {host}:{port} ...\n' .format(**CONFIG))
95
+ sys.stderr.write(" Serving at {host}:{port} ...\n" .format(**CONFIG))
91
96
server.serve_forever()
92
97
98
+
93
99
@task
94
100
def reserve(c):
95
101
"""`build`, then `serve`"""
96
102
build(c)
97
103
serve(c)
98
104
105
+
99
106
@task
100
107
def preview(c):
101
108
"""Build production version of site"""
102
- pelican_run('-s {settings_publish}'.format(**CONFIG))
109
+ pelican_run("-s {settings_publish}".format(**CONFIG))
110
+
103
111
104
112
@task
105
113
def livereload(c):
106
114
"""Automatically reload browser tab upon file modification."""
107
115
from livereload import Server
108
116
109
117
def cached_build():
110
- cmd = ' -s {settings_base} -e CACHE_CONTENT=true LOAD_CONTENT_CACHE=true'
118
+ cmd = " -s {settings_base} -e CACHE_CONTENT=true LOAD_CONTENT_CACHE=true"
111
119
pelican_run(cmd.format(**CONFIG))
112
120
113
121
cached_build()
114
122
server = Server()
115
- theme_path = SETTINGS[' THEME' ]
123
+ theme_path = SETTINGS[" THEME" ]
116
124
watched_globs = [
117
- CONFIG[' settings_base' ],
118
- '{ }/templates/**/*.html'.format(theme_path) ,
125
+ CONFIG[" settings_base" ],
126
+ f"{theme_path }/templates/**/*.html" ,
119
127
]
120
128
121
- content_file_extensions = [' .md', ' .rst' ]
129
+ content_file_extensions = [" .md", " .rst" ]
122
130
for extension in content_file_extensions:
123
- content_glob = '{0 }/**/*{1}' .format(SETTINGS[' PATH' ], extension)
131
+ content_glob = "{ }/**/*{}" .format(SETTINGS[" PATH" ], extension)
124
132
watched_globs.append(content_glob)
125
133
126
- static_file_extensions = [' .css', ' .js' ]
134
+ static_file_extensions = [" .css", " .js" ]
127
135
for extension in static_file_extensions:
128
- static_file_glob = '{0 }/static/**/*{1}'.format(theme_path, extension)
136
+ static_file_glob = f"{theme_path }/static/**/*{extension}"
129
137
watched_globs.append(static_file_glob)
130
138
131
139
for glob in watched_globs:
@@ -134,43 +142,57 @@ def livereload(c):
134
142
if OPEN_BROWSER_ON_SERVE:
135
143
# Open site in default browser
136
144
import webbrowser
145
+
137
146
webbrowser.open("http://{host}:{port}".format(**CONFIG))
138
147
139
- server.serve(host=CONFIG['host'], port=CONFIG['port'], root=CONFIG['deploy_path'])
148
+ server.serve(host=CONFIG["host"], port=CONFIG["port"], root=CONFIG["deploy_path"])
149
+
140
150
141
151
{% if cloudfiles %}
142
152
@task
143
153
def cf_upload(c):
144
154
"""Publish to Rackspace Cloud Files"""
145
155
rebuild(c)
146
- with cd(CONFIG['deploy_path']):
147
- c.run('swift -v -A https://auth.api.rackspacecloud.com/v1.0 '
148
- '-U {cloudfiles_username} '
149
- '-K {cloudfiles_api_key} '
150
- 'upload -c {cloudfiles_container} .'.format(**CONFIG))
156
+ with cd(CONFIG["deploy_path"]):
157
+ c.run(
158
+ "swift -v -A https://auth.api.rackspacecloud.com/v1.0 "
159
+ "-U {cloudfiles_username} "
160
+ "-K {cloudfiles_api_key} "
161
+ "upload -c {cloudfiles_container} .".format(**CONFIG)
162
+ )
163
+
164
+
151
165
{% endif %}
152
166
167
+
153
168
@task
154
169
def publish(c):
155
170
"""Publish to production via rsync"""
156
- pelican_run(' -s {settings_publish}' .format(**CONFIG))
171
+ pelican_run(" -s {settings_publish}" .format(**CONFIG))
157
172
c.run(
158
173
'rsync --delete --exclude ".DS_Store" -pthrvz -c '
159
174
'-e "ssh -p {ssh_port}" '
160
- '{} {ssh_user}@{ssh_host}:{ssh_path}'.format(
161
- CONFIG['deploy_path'].rstrip('/') + '/',
162
- **CONFIG))
175
+ "{} {ssh_user}@{ssh_host}:{ssh_path}".format(
176
+ CONFIG["deploy_path"].rstrip("/") + "/", **CONFIG
177
+ )
178
+ )
179
+
163
180
164
181
{% if github %}
165
182
@task
166
183
def gh_pages(c):
167
184
"""Publish to GitHub Pages"""
168
185
preview(c)
169
- c.run('ghp-import -b {github_pages_branch} '
170
- '-m {commit_message} '
171
- '{deploy_path} -p'.format(**CONFIG))
186
+ c.run(
187
+ "ghp-import -b {github_pages_branch} "
188
+ "-m {commit_message} "
189
+ "{deploy_path} -p".format(**CONFIG)
190
+ )
191
+
192
+
172
193
{% endif %}
173
194
195
+
174
196
def pelican_run(cmd):
175
- cmd += ' ' + program.core.remainder # allows to pass-through args to pelican
197
+ cmd += " " + program.core.remainder # allows to pass-through args to pelican
176
198
pelican_main(shlex.split(cmd))
0 commit comments