# HG changeset patch # User Ludovic Chabant # Date 1499059232 25200 # Node ID c445a3d5d950898293e709e65f11f915e1635fcf # Parent f13d618cfec69ab4992efafe5e9518107d01cc8d internal: Make `createContent` use a dictionary-like object. diff -r f13d618cfec6 -r c445a3d5d950 piecrust/commands/builtin/scaffolding.py --- a/piecrust/commands/builtin/scaffolding.py Sun Jul 02 22:19:58 2017 -0700 +++ b/piecrust/commands/builtin/scaffolding.py Sun Jul 02 22:20:32 2017 -0700 @@ -82,7 +82,7 @@ raise Exception("Error loading template: %s" % tpl_name) source = ctx.args.source - content_item = source.createContent(ctx.args) + content_item = source.createContent(vars(ctx.args)) config_tokens = { '%title%': "Untitled Content", diff -r f13d618cfec6 -r c445a3d5d950 piecrust/sources/default.py --- a/piecrust/sources/default.py Sun Jul 02 22:19:58 2017 -0700 +++ b/piecrust/sources/default.py Sun Jul 02 22:20:32 2017 -0700 @@ -92,10 +92,7 @@ parser.add_argument('uri', help='The URI for the new page.') def createContent(self, args): - if not hasattr(args, 'uri'): - uri = None - else: - uri = args.uri + uri = args.get('uri') if not uri: uri = '_index' path = os.path.join(self.fs_endpoint_path, uri) diff -r f13d618cfec6 -r c445a3d5d950 piecrust/sources/posts.py --- a/piecrust/sources/posts.py Sun Jul 02 22:19:58 2017 -0700 +++ b/piecrust/sources/posts.py Sun Jul 02 22:20:32 2017 -0700 @@ -154,30 +154,37 @@ def createContent(self, args): dt = datetime.date.today() - if args.date: - if args.date == 'today': + date = args.get('date') + if isinstance(date, str): + if date == 'today': pass # Keep the default we had. - elif args.date == 'tomorrow': + elif date == 'tomorrow': dt += datetime.timedelta(days=1) - elif args.date.startswith('+'): + elif date.startswith('+'): try: - dt += datetime.timedelta(days=int(args.date.lstrip('+'))) + dt += datetime.timedelta(days=int(date.lstrip('+'))) except ValueError: raise Exception("Date offsets must be numbers.") else: try: - year, month, day = [int(s) for s in args.date.split('/')] + year, month, day = [int(s) for s in date.split('/')] except ValueError: raise Exception("Dates must be of the form: " "YEAR/MONTH/DAY.") dt = datetime.date(year, month, day) + elif isinstance(date, datetime.datetime): + year = date.year + month = date.month + day = date.day + else: + raise Exception("Unknown date: %s" % date) - slug, ext = os.path.splitext(args.slug) + slug, ext = os.path.splitext(args.get('slug')) if not ext: ext = self.default_auto_format year, month, day = dt.year, dt.month, dt.day tokens = { - 'slug': args.slug, + 'slug': args.get('slug'), 'ext': ext, 'year': '%04d' % year, 'month': '%02d' % month, @@ -185,9 +192,8 @@ } rel_path = self.path_format % tokens path = os.path.join(self.fs_endpoint_path, rel_path) - metadata = { - 'config': {'title': uri_to_title(slug)} - } + metadata = self._parseMetadataFromPath(path) + metadata['config'] = {'title': uri_to_title(slug)} return ContentItem(path, metadata) def getInteractiveFields(self):