lektor_ng.quickstart

src/lektor_ng/quickstart.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import getpass
import os
import re
import shutil
import sys
from contextlib import contextmanager
from datetime import datetime
from functools import partial
from importlib import import_module
from subprocess import PIPE, run
from tempfile import TemporaryDirectory

import click
from jinja2 import Environment, PackageLoader

from lektor_ng.utils import locate_executable, slugify

pwd = import_module("pwd") if os.name != "nt" else None


_var_re = re.compile(r"@([^@]+)@")


class Generator:
    def __init__(self, base):
        self.question = 0
        self.jinja_env = Environment(
            loader=PackageLoader("lektor_ng", os.path.join("quickstart-templates", base)),
            line_statement_prefix="%%",
            line_comment_prefix="##",
            variable_start_string="${",
            variable_end_string="}",
            block_start_string="<%",
            block_end_string="%>",
            comment_start_string="/**",
            comment_end_string="**/",
        )
        self.options = {}
        # term width in [1, 78]
        self.term_width = min(max(shutil.get_terminal_size()[0], 1), 78)
        self.e = click.secho
        self.w = partial(click.wrap_text, width=self.term_width)

    @staticmethod
    def abort(message):
        click.echo(f"Error: {message}", err=True)
        raise click.Abort()

    def prompt(self, text, default=None, info=None):
        self.question += 1
        self.e("")
        self.e(f"Step {self.question}:", fg="yellow")
        if info is not None:
            self.e(click.wrap_text(info, self.term_width, "| ", "| "))
        text = "> " + click.style(text, fg="green")

        if default is True or default is False:
            return click.confirm(text, default=default)
        return click.prompt(text, default=default, show_default=True)

    def title(self, title):
        self.e(title, fg="cyan")
        self.e("=" * len(title), fg="cyan")
        self.e("")

    def warn(self, text):
        self.e(self.w(text), fg="magenta")

    def text(self, text):
        self.e(self.w(text))

    def confirm(self, prompt):
        self.e("")
        click.confirm(prompt, default=True, abort=True, prompt_suffix=" ")

    @contextmanager
    def make_target_directory(self, path):
        here = os.path.abspath(os.getcwd())
        path = os.path.abspath(path)
        if here != path:
            try:
                os.makedirs(path)
            except OSError as e:
                self.abort(f"Could not create target folder: {e}")

        if os.path.isdir(path):
            try:
                if len(os.listdir(path)) != 0:
                    raise OSError("Directory not empty")
            except OSError as e:
                self.abort(f"Bad target folder: {e}")

        with TemporaryDirectory() as scratch:
            yield scratch

            # Use shutil.move here in case we move across a file system
            # boundary.
            for filename in os.listdir(scratch):
                shutil.move(os.path.join(scratch, filename), os.path.join(path, filename))

    @staticmethod
    def expand_filename(base, ctx, template_filename):
        def _repl(match):
            return ctx[match.group(1)]

        return os.path.join(base, _var_re.sub(_repl, template_filename))[:-3]

    def run(self, ctx, path):
        with self.make_target_directory(path) as scratch:
            for template in self.jinja_env.list_templates():
                if not template.endswith(".in"):
                    continue
                fn = self.expand_filename(scratch, ctx, template)
                tmpl = self.jinja_env.get_template(template)
                rv = tmpl.render(ctx).strip("\r\n")
                if rv:
                    directory = os.path.dirname(fn)
                    try:
                        os.makedirs(directory)
                    except OSError:
                        pass
                    with open(fn, "wb") as f:
                        f.write((rv + "\n").encode("utf-8"))


def get_default_author() -> str:
    """Attempt to guess an the name of the current user."""
    if sys.platform == "win32":
        return getpass.getuser()
    import pwd

    try:
        pw_gecos = pwd.getpwuid(os.getuid()).pw_gecos
    except KeyError:
        pass
    else:
        full_name = pw_gecos.split(",", 1)[0].strip()
        if full_name:
            return full_name

    return getpass.getuser()


def get_default_author_email() -> str | None:
    """Attempt to guess an email address for the current user.

    May return an empty string if not reasonable guess can be made.
    """
    git = locate_executable("git")
    if git:
        proc = run((git, "config", "user.email"), stdout=PIPE, errors="strict", check=False)
        if proc.returncode == 0:
            return proc.stdout.strip()

    email = os.environ.get("EMAIL", "").strip()
    if email:
        return email
    # We could fall back to f"{getpass.getuser()}@{socket.getfqdn()}",
    # but it is probably better just to go with no default in that
    # case.
    return None


def project_quickstart(defaults=None):
    if not defaults:
        defaults = {}

    g = Generator("project")

    g.title("Lektor Quickstart")
    g.text(
        "This wizard will generate a new basic project with some sensible "
        "defaults for getting started quickly.  We just need to go through "
        "a few questions so that the project is set up correctly for you."
    )

    name = defaults.get("name")
    if name is None:
        name = g.prompt(
            "Project Name",
            None,
            "A project needs a name.  The name is primarily used for the admin "
            "UI and some other places to refer to your project to not get "
            "confused if multiple projects exist.  You can change this at "
            "any later point.",
        )

    author_name = g.prompt(
        "Author Name",
        get_default_author(),
        "Your name.  This is used in a few places in the default template "
        "to refer to in the default copyright messages.",
    )

    path = defaults.get("path")
    if path is None:
        default_project_path = os.path.join(os.getcwd(), name)
        path = g.prompt(
            "Project Path",
            default_project_path,
            "This is the path where the project will be located.  You can "
            "move a project around later if you do not like the path.  If "
            "you provide a relative path it will be relative to the working "
            "directory.",
        )
        path = os.path.expanduser(path)

    with_blog = g.prompt(
        "Add Basic Blog",
        True,
        "Do you want to generate a basic blog module?  If you enable this "
        "the models for a very basic blog will be generated.",
    )

    g.confirm("That's all. Create project?")

    g.run(
        {
            "project_name": name,
            "project_slug": slugify(name),
            "project_path": path,
            "with_blog": with_blog,
            "this_year": datetime.utcnow().year,
            "today": datetime.utcnow().strftime("%Y-%m-%d"),
            "author_name": author_name,
        },
        path,
    )


def plugin_quickstart(defaults=None, project=None):
    if defaults is None:
        defaults = {}

    g = Generator("plugin")

    plugin_name = defaults.get("plugin_name")
    if plugin_name is None:
        plugin_name = g.prompt(
            "Plugin Name",
            default=None,
            info="This is the human readable name for this plugin",
        )

    plugin_id = plugin_name.lower()
    plugin_id = plugin_id.removeprefix("lektor")
    plugin_id = plugin_id.removesuffix("plugin")
    plugin_id = slugify(plugin_id)

    path = defaults.get("path")
    if path is None:
        if project is not None:
            default_path = os.path.join(project.tree, "packages", plugin_id)
        else:
            if len(os.listdir(".")) == 0:
                default_path = os.getcwd()
            else:
                default_path = os.path.join(os.getcwd(), plugin_id)
        path = g.prompt(
            "Plugin Path",
            default_path,
            "The place where you want to initialize the plugin",
        )

    author_name = g.prompt(
        "Author Name",
        get_default_author(),
        "Your name as it will be embedded in the plugin metadata.",
    )

    author_email = g.prompt(
        "Author E-Mail",
        get_default_author_email(),
        "Your e-mail address for the plugin info.",
    )

    g.confirm("Create Plugin?")

    g.run(
        {
            "plugin_name": plugin_name,
            "plugin_id": plugin_id,
            "plugin_class": plugin_id.title().replace("-", "") + "Plugin",
            "plugin_module": "lektor_" + plugin_id.replace("-", "_"),
            "author_name": author_name,
            "author_email": author_email,
        },
        path,
    )


def theme_quickstart(defaults=None, project=None):
    if defaults is None:
        defaults = {}

    g = Generator("theme")

    theme_name = defaults.get("theme_name")
    if theme_name is None:
        theme_name = g.prompt(
            "Theme Name",
            default=None,
            info="This is the human readable name for this theme",
        )

    theme_id = theme_name.lower()
    if theme_id != "lektor" and theme_id.startswith("lektor"):
        theme_id = theme_id[6:].strip()
    if theme_id != "theme" and theme_id.startswith("theme"):
        theme_id = theme_id[5:]
    if theme_id != "theme" and theme_id.endswith("theme"):
        theme_id = theme_id[:-5]
    theme_id = slugify(theme_id)

    path = defaults.get("path")
    if path is None:
        if project is not None:
            default_path = os.path.join(project.tree, "themes", f"lektor-theme-{theme_id}")
        else:
            if len(os.listdir(".")) == 0:
                default_path = os.getcwd()
            else:
                default_path = os.path.join(os.getcwd(), theme_id)
        path = g.prompt(
            "Theme Path",
            default_path,
            "The place where you want to initialize the theme",
        )

    author_name = g.prompt(
        "Author Name",
        get_default_author(),
        "Your name as it will be embedded in the theme metadata.",
    )

    author_email = g.prompt(
        "Author E-Mail",
        get_default_author_email(),
        "Your e-mail address for the theme info.",
    )

    g.confirm("Create Theme?")

    g.run(
        {
            "theme_name": theme_name,
            "theme_id": theme_id,
            "author_name": author_name,
            "author_email": author_email,
        },
        path,
    )

    # symlink
    theme_dir = os.getcwd()
    example_themes = os.path.join(path, "example-site/themes")
    os.makedirs(example_themes)
    os.chdir(example_themes)
    try:
        os.symlink(
            f"../../../lektor-theme-{theme_id}",
            f"lektor-theme-{theme_id}",
        )
    except OSError as exc:
        # Windows, by default, only allows members of the "Administrators" group
        # to create symlinks. For users who are not allowed to create symlinks,
        # error Code 1314 - "A required privilege is not held by the client"
        # is raised.
        if getattr(exc, "winerror", None) != 1314:
            raise
        g.warn("Could not automatically make a symlink to have your example-siteeasily pick up your theme.")
    os.chdir(theme_dir)

    # Sample image
    os.makedirs(os.path.join(path, "images"))
    source_image_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "quickstart-templates/theme/images/homepage.png",
    )
    destination_image_path = os.path.join(path, "images/homepage.png")
    with open(source_image_path, "rb") as f:
        image = f.read()
    with open(destination_image_path, "wb") as f:
        f.write(image)