comparison piecrust/sources/posts.py @ 80:838a9dd0e23c

Better date creation for blog post scaffolding.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 30 Aug 2014 18:30:50 -0700
parents a46354306738
children b3ce11b2cf36
comparison
equal deleted inserted replaced
79:c605fd808fc5 80:838a9dd0e23c
112 parser.add_argument('-d', '--date', help="The date of the post, " 112 parser.add_argument('-d', '--date', help="The date of the post, "
113 "in `year/month/day` format (defaults to today).") 113 "in `year/month/day` format (defaults to today).")
114 parser.add_argument('slug', help="The URL slug for the new post.") 114 parser.add_argument('slug', help="The URL slug for the new post.")
115 115
116 def buildMetadata(self, args): 116 def buildMetadata(self, args):
117 today = datetime.date.today() 117 dt = datetime.date.today()
118 year, month, day = today.year, today.month, today.day
119 if args.date: 118 if args.date:
120 year, month, day = [s for s in args.date.split('/') if int(s)] 119 if args.date == 'today':
120 pass # Keep the default we had.
121 elif args.date == 'tomorrow':
122 dt += datetime.timedelta(days=1)
123 elif args.date.startswith('+'):
124 try:
125 dt += datetime.timedelta(days=int(args.date.lstrip('+')))
126 except ValueError:
127 raise Exception("Date offsets must be numbers.")
128 else:
129 try:
130 year, month, day = [int(s) for s in args.date.split('/')]
131 except ValueError:
132 raise Exception("Dates must be of the form: YEAR/MONTH/DAY.")
133 dt = datetime.date(year, month, day)
134
135 year, month, day = dt.year, dt.month, dt.day
121 return {'year': year, 'month': month, 'day': day, 'slug': args.slug} 136 return {'year': year, 'month': month, 'day': day, 'slug': args.slug}
122 137
123 def _checkFsEndpointPath(self): 138 def _checkFsEndpointPath(self):
124 if not os.path.isdir(self.fs_endpoint_path): 139 if not os.path.isdir(self.fs_endpoint_path):
125 raise InvalidFileSystemEndpointError(self.name, self.fs_endpoint_path) 140 raise InvalidFileSystemEndpointError(self.name, self.fs_endpoint_path)