def new_post(self): settings = self._get_settings() template_file = 'post.rst' template_path = get_template_path(template_file) with open(template_path, "r") as f: content = f.read() slug = slugify(self.args['<name>']) now = datetime.now() content = content.replace('TITLE', self.args['<name>']) content = content.replace( 'DATE', now.strftime('%Y-%m-%d %H:%M:%S') ) post_path = os.path.join( settings['SOURCE_PATH'], 'posts', '{}-{}.rst'.format(now.strftime('%Y-%m-%d'), slug) ) if (os.path.isfile(post_path) and not self.args['--force']): raise PostExists( "The post '{}' already exists (use --force).".format(post_path) ) with open(post_path, "w") as f: f.write(content) logger.info("New post created : {}".format(post_path))
def new_page(self): settings = self._get_settings() template_file = 'page.rst' template_path = get_template_path(template_file) with open(template_path, "r") as f: content = f.read() slug = slugify(self.args['<name>']) content = content.replace('TITLE', self.args['<name>']) page_path = os.path.join( settings['SOURCE_PATH'], 'pages', '{}.rst'.format(slug) ) if (os.path.isfile(page_path) and not self.args['--force']): raise PageExists( "The page '{}' already exists (use --force).".format(page_path) ) with open(page_path, "w") as f: f.write(content) logger.info("New page created : {}".format(page_path))
def test_project_path(self): project = get_template_path("project") self.assertTrue(project.endswith('skeletons/project')) self.assertGreater(len([ f for f in os.listdir(project) ]), 1)
def new_project(self): template_path = get_template_path('project') project_path = os.path.realpath(self.args['<name>']) copy_project( template_path, project_path, self.args['--blank'] ) logger.info("New project created : {}".format(project_path))
def test_copy_empty_dir(self): project = get_template_path("project") with tmp_folder() as tmpdir: copy_project(project, os.path.join(tmpdir, "foo"), True) root = [f for f in os.listdir( os.path.join(tmpdir, "foo")) ] post = [f for f in os.listdir( os.path.join(tmpdir, "foo", "posts")) ] page = [f for f in os.listdir( os.path.join(tmpdir, "foo", "pages")) ] self.assertEqual(len(root), 5) self.assertEqual(len(post), 0) self.assertEqual(len(page), 0)
def tmp_folder(project=False): old_cwd = os.getcwd() tmp_dir = mkdtemp() os.chdir(tmp_dir) if project: template_path = get_template_path('project') distutils.dir_util.copy_tree(template_path, tmp_dir) old_post = os.path.join( tmp_dir, 'posts', "welcome-to-radric.rst" ) now = datetime.now() with open(old_post, 'r+') as f: content = f.read() content = content.replace( 'DATE', now.strftime('%Y-%m-%d %H:%M:%S') ) f.seek(0) f.truncate() f.write(content) new_post = os.path.join( tmp_dir, 'posts', "{}-welcome-to-radric.rst".format( now.strftime('%Y-%m-%d') ) ) os.rename(old_post, new_post) try: yield tmp_dir finally: rmtree(tmp_dir) os.chdir(old_cwd)
def test_return_template_not_found_exception(self): with self.assertRaises(TemplateNotFound): get_template_path('foo')
def test_page_path(self): page = get_template_path("page.rst") self.assertTrue(page.endswith('skeletons/page.rst'))
def test_post_path(self): post = get_template_path("post.rst") self.assertTrue(post.endswith('skeletons/post.rst'))