def generate(self, documents): posts_number = self._conf['posts_number'] save_as = self._conf['save_as'] # we are interested only in 'post' documents because 'pages' # usually are not a part of feed since they are not intended # to deliver content with regular updates posts = (doc for doc in documents if isinstance(doc, Post)) # since we could have a lot of posts, it's a common practice to # use in feed only last N posts posts = sorted(posts, key=lambda d: d.published, reverse=True) posts = posts[:posts_number] credentials = { 'siteurl_self': self._url, 'siteurl_alt': normalize_url(self._appconf['site.url']), 'site': self._appconf['site'], 'date': datetime.datetime.utcnow().replace(microsecond=0), } save_as = os.path.join(self._appconf['paths.output'], save_as) mkdir(os.path.dirname(save_as)) encoding = self._appconf['encoding.output'] with open(save_as, 'w', encoding=encoding) as f: f.write(self._template.render( documents=posts, credentials=credentials, encoding=encoding))
def generate(self, documents): # we are interested only in 'post' documents because 'pages' # usually are not marked with tags posts = (doc for doc in documents if isinstance(doc, Post)) # map: tag -> [posts] tags = defaultdict(list) for post in posts: if hasattr(post, 'tags'): if not isinstance(post.tags, (list, tuple)): logger.warning( 'Tags must be wrapped with list or tuple in %s', post.short_source) continue tag_objects = [] for tag in post.tags: tags[tag].append(post) tag_objects.append(Tag(tag, self._conf['output'])) post.tags = tag_objects template = self._app.jinja_env.get_template(self._conf['template']) for tag in tags: save_as = self._save_as.format(tag=tag) mkdir(os.path.dirname(save_as)) with open(save_as, 'w', encoding=self._encoding) as f: f.write(template.render(posts=tags[tag]))
def generate(self, documents): posts_number = self._conf['posts_number'] save_as = self._conf['save_as'] # we are interested only in 'post' documents because 'pages' # usually are not a part of feed since they are not intended # to deliver content with regular updates posts = (doc for doc in documents if isinstance(doc, Post)) # since we could have a lot of posts, it's a common practice to # use in feed only last N posts posts = sorted(posts, key=lambda d: d.published, reverse=True) posts = posts[:posts_number] credentials = { 'siteurl_self': self._url, 'siteurl_alt': normalize_url(self._appconf['site.url']), 'site': self._appconf['site'], 'date': datetime.datetime.utcnow().replace(microsecond=0), } save_as = os.path.join(self._appconf['paths.output'], save_as) mkdir(os.path.dirname(save_as)) encoding = self._appconf['encoding.output'] with open(save_as, 'w', encoding=encoding) as f: f.write( self._template.render(documents=posts, credentials=credentials, encoding=encoding))
def test_makedirs_is_not_called(self, makedirs, _): """ Tests that os.makedirs hasn't been called if path exist. """ mkdir('path/to/dir') makedirs.assert_not_called()
def test_makedirs_is_called(self, makedirs, _): """ Tests that os.makedirs has been called if path not exists. """ mkdir('path/to/dir') makedirs.assert_called_with('path/to/dir')
def test_makedirs_is_not_called(self, makedirs, _): """ Tests that os.makedirs hasn't been called if path exist. """ mkdir("path/to/dir") makedirs.assert_not_called()
def test_makedirs_is_called(self, makedirs, _): """ Tests that os.makedirs has been called if path not exists. """ mkdir("path/to/dir") makedirs.assert_called_with("path/to/dir")