Debian Codegraph @

pecan/commands/create.py

raw
1 """
2 Create command for Pecan
3 """
4 import importlib.metadata
5 import logging
6 from warnings import warn
7 from pecan.commands import BaseCommand
8 from pecan.scaffolds import DEFAULT_SCAFFOLD
9
10 log = logging.getLogger(__name__)
11
12
13 class ScaffoldManager(object):
14 """ Used to discover `pecan.scaffold` entry points. """
15
16 def __init__(self):
17 self.scaffolds = {}
18 self.load_scaffolds()
19
20 def load_scaffolds(self):
21 for ep in importlib.metadata.entry_points(group='pecan.scaffold'):
22 log.debug('%s loading scaffold %s', self.__class__.__name__, ep)
23 try:
24 cmd = ep.load()
25 cmd.copy_to # ensure existance; catch AttributeError otherwise
26 except Exception as e: # pragma: nocover
27 warn(
28 "Unable to load scaffold %s: %s" % (ep, e), RuntimeWarning
29 )
30 continue
31 self.add({ep.name: cmd})
32
33 def add(self, cmd):
34 self.scaffolds.update(cmd)
35
36
37 class CreateCommand(BaseCommand):
38 """
39 Creates the file layout for a new Pecan scaffolded project.
40 """
41
42 manager = ScaffoldManager()
43
44 arguments = ({
45 'name': 'project_name',
46 'help': 'the (package) name of the new project'
47 }, {
48 'name': 'template_name',
49 'metavar': 'template_name',
50 'help': 'a registered Pecan template',
51 'nargs': '?',
52 'default': DEFAULT_SCAFFOLD,
53 'choices': manager.scaffolds.keys()
54 })
55
56 def run(self, args):
57 super(CreateCommand, self).run(args)
58 self.manager.scaffolds[args.template_name]().copy_to(
59 args.project_name
60 )