changeset 892:c445a3d5d950

internal: Make `createContent` use a dictionary-like object.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 02 Jul 2017 22:20:32 -0700
parents f13d618cfec6
children 14cca285f73b
files piecrust/commands/builtin/scaffolding.py piecrust/sources/default.py piecrust/sources/posts.py
diffstat 3 files changed, 19 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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",
--- 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)
--- 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):