Added a more generic assets mixin
This commit is contained in:
parent
fdd2cd139d
commit
c775fc64c5
|
@ -4,7 +4,6 @@ import shutil
|
||||||
|
|
||||||
from verdandi.constants import CONTENT_DIRECTORY
|
from verdandi.constants import CONTENT_DIRECTORY
|
||||||
|
|
||||||
|
|
||||||
class AssetsMixin(object):
|
class AssetsMixin(object):
|
||||||
|
|
||||||
assets = []
|
assets = []
|
||||||
|
@ -15,53 +14,4 @@ class AssetsMixin(object):
|
||||||
return self.assets
|
return self.assets
|
||||||
|
|
||||||
def collect_assets(self, output_directory):
|
def collect_assets(self, output_directory):
|
||||||
assets = self.get_assets()
|
pass
|
||||||
|
|
||||||
for source, destination in assets:
|
|
||||||
source_path = os.path.join(self.asset_directory, source)
|
|
||||||
dest_path = os.path.join(output_directory, destination);
|
|
||||||
|
|
||||||
if os.path.isdir(source_path):
|
|
||||||
self.copy_dir(source_path, dest_path)
|
|
||||||
elif os.path.isfile(source_path):
|
|
||||||
self.copy_file(source_path, dest_path)
|
|
||||||
else:
|
|
||||||
print "Skipping %s is neither directory nor file" % source_path
|
|
||||||
|
|
||||||
|
|
||||||
def copy_file(self, source_path, dest_path):
|
|
||||||
print "Copying %s to %s" % (source_path, dest_path)
|
|
||||||
|
|
||||||
dest_dir = os.path.dirname(dest_path)
|
|
||||||
|
|
||||||
if not os.path.exists(dest_dir):
|
|
||||||
os.makedirs(dest_dir)
|
|
||||||
shutil.copy(source_path, dest_path)
|
|
||||||
|
|
||||||
|
|
||||||
def copy_dir(self, source_path, dest_path):
|
|
||||||
# /foo/bar /rofl -> contents of bar go to rofl/bar
|
|
||||||
# /foo/bar/ /rofl -> contests of bar got to rofl/
|
|
||||||
# Trailing slash on destination should have no effect
|
|
||||||
|
|
||||||
# Will be '' in case of a trailing slash: /foo/bar/ else bar
|
|
||||||
source_base = os.path.basename(source_path)
|
|
||||||
# /rofl will become /rofl/ if base is '' otherwise it will become /rofl/bar
|
|
||||||
dest_path = os.path.join(dest_path, source_base)
|
|
||||||
|
|
||||||
if not os.path.exists(dest_path):
|
|
||||||
os.makedirs(dest_path)
|
|
||||||
|
|
||||||
# Discover the whole tree and copy each file individually
|
|
||||||
for source_dir, _, files in os.walk(source_path):
|
|
||||||
rel_path = os.path.relpath(source_dir, source_path)
|
|
||||||
# Purely cosmetical for debug output
|
|
||||||
if rel_path == '.':
|
|
||||||
dest_dir = dest_path
|
|
||||||
else:
|
|
||||||
dest_dir = os.path.join(dest_path, rel_path)
|
|
||||||
|
|
||||||
for source_file in files:
|
|
||||||
file_source_path = os.path.join(source_dir, source_file)
|
|
||||||
file_dest_path = os.path.join(dest_dir, source_file)
|
|
||||||
self.copy_file(file_source_path, file_dest_path)
|
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
#!/usr/bin/env python2
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
from verdandi.mixins.assetsmixin import AssetsMixin
|
||||||
|
|
||||||
|
from verdandi.constants import CONTENT_DIRECTORY
|
||||||
|
|
||||||
|
|
||||||
|
class FileAssetsMixin(AssetsMixin):
|
||||||
|
|
||||||
|
def collect_assets(self, output_directory):
|
||||||
|
super(FileAssetsMixin, self).collect_assets(output_directory)
|
||||||
|
|
||||||
|
assets = self.get_assets()
|
||||||
|
|
||||||
|
for source, destination in assets:
|
||||||
|
source_path = os.path.join(self.asset_directory, source)
|
||||||
|
dest_path = os.path.join(output_directory, destination);
|
||||||
|
|
||||||
|
if os.path.isdir(source_path):
|
||||||
|
self.copy_dir(source_path, dest_path)
|
||||||
|
elif os.path.isfile(source_path):
|
||||||
|
self.copy_file(source_path, dest_path)
|
||||||
|
else:
|
||||||
|
print "Skipping %s is neither directory nor file" % source_path
|
||||||
|
|
||||||
|
|
||||||
|
def copy_file(self, source_path, dest_path):
|
||||||
|
print "Copying %s to %s" % (source_path, dest_path)
|
||||||
|
|
||||||
|
dest_dir = os.path.dirname(dest_path)
|
||||||
|
|
||||||
|
if not os.path.exists(dest_dir):
|
||||||
|
os.makedirs(dest_dir)
|
||||||
|
shutil.copy(source_path, dest_path)
|
||||||
|
|
||||||
|
|
||||||
|
def copy_dir(self, source_path, dest_path):
|
||||||
|
# /foo/bar /rofl -> contents of bar go to rofl/bar
|
||||||
|
# /foo/bar/ /rofl -> contests of bar got to rofl/
|
||||||
|
# Trailing slash on destination should have no effect
|
||||||
|
|
||||||
|
# Will be '' in case of a trailing slash: /foo/bar/ else bar
|
||||||
|
source_base = os.path.basename(source_path)
|
||||||
|
# /rofl will become /rofl/ if base is '' otherwise it will become /rofl/bar
|
||||||
|
dest_path = os.path.join(dest_path, source_base)
|
||||||
|
|
||||||
|
if not os.path.exists(dest_path):
|
||||||
|
os.makedirs(dest_path)
|
||||||
|
|
||||||
|
# Discover the whole tree and copy each file individually
|
||||||
|
for source_dir, _, files in os.walk(source_path):
|
||||||
|
rel_path = os.path.relpath(source_dir, source_path)
|
||||||
|
# Purely cosmetical for debug output
|
||||||
|
if rel_path == '.':
|
||||||
|
dest_dir = dest_path
|
||||||
|
else:
|
||||||
|
dest_dir = os.path.join(dest_path, rel_path)
|
||||||
|
|
||||||
|
for source_file in files:
|
||||||
|
file_source_path = os.path.join(source_dir, source_file)
|
||||||
|
file_dest_path = os.path.join(dest_dir, source_file)
|
||||||
|
self.copy_file(file_source_path, file_dest_path)
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
|
|
||||||
from verdandi.mixins.assetsmixin import AssetsMixin
|
from verdandi.mixins.fileassetsmixin import FileAssetsMixin
|
||||||
from verdandi.mixins.messagemixin import MessageMixin
|
from verdandi.mixins.messagemixin import MessageMixin
|
||||||
from verdandi.mixins.rendermixin import RenderMixin
|
from verdandi.mixins.rendermixin import RenderMixin
|
||||||
|
|
||||||
class CommonAssets(AssetsMixin, MessageMixin, RenderMixin):
|
class CommonAssets(FileAssetsMixin, MessageMixin, RenderMixin):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -7,11 +7,11 @@ import markdown
|
||||||
|
|
||||||
from verdandi.mixins.templatemixin import TemplateMixin
|
from verdandi.mixins.templatemixin import TemplateMixin
|
||||||
from verdandi.mixins.menuitemmixin import MenuItemMixin
|
from verdandi.mixins.menuitemmixin import MenuItemMixin
|
||||||
from verdandi.mixins.assetsmixin import AssetsMixin
|
from verdandi.mixins.fileassetsmixin import FileAssetsMixin
|
||||||
from verdandi.mixins.newsitemmixin import NewsItemMixin
|
from verdandi.mixins.newsitemmixin import NewsItemMixin
|
||||||
from verdandi.constants import CONTENT_DIRECTORY, MARKDOWN_EXTENSIONS
|
from verdandi.constants import CONTENT_DIRECTORY, MARKDOWN_EXTENSIONS
|
||||||
|
|
||||||
class Gallery(MenuItemMixin, NewsItemMixin, TemplateMixin, AssetsMixin):
|
class Gallery(MenuItemMixin, NewsItemMixin, TemplateMixin, FileAssetsMixin):
|
||||||
title = 'Gallery Title'
|
title = 'Gallery Title'
|
||||||
gallery_description_file = 'description.md'
|
gallery_description_file = 'description.md'
|
||||||
gallery_directory = 'gallery'
|
gallery_directory = 'gallery'
|
||||||
|
|
|
@ -7,11 +7,11 @@ from dateutil import parser
|
||||||
|
|
||||||
from verdandi.mixins.templatemixin import TemplateMixin
|
from verdandi.mixins.templatemixin import TemplateMixin
|
||||||
from verdandi.mixins.menuitemmixin import MenuItemMixin
|
from verdandi.mixins.menuitemmixin import MenuItemMixin
|
||||||
from verdandi.mixins.assetsmixin import AssetsMixin
|
from verdandi.mixins.fileassetsmixin import AssetsMixin
|
||||||
from verdandi.constants import CONTENT_DIRECTORY, MARKDOWN_EXTENSIONS
|
from verdandi.constants import CONTENT_DIRECTORY, MARKDOWN_EXTENSIONS
|
||||||
|
|
||||||
|
|
||||||
class NewsFeed(MenuItemMixin, TemplateMixin, AssetsMixin):
|
class NewsFeed(MenuItemMixin, TemplateMixin, FileAssetsMixin):
|
||||||
|
|
||||||
title = "News feed title"
|
title = "News feed title"
|
||||||
template = "newsfeed.html"
|
template = "newsfeed.html"
|
||||||
|
|
|
@ -6,11 +6,11 @@ import markdown
|
||||||
|
|
||||||
from verdandi.mixins.templatemixin import TemplateMixin
|
from verdandi.mixins.templatemixin import TemplateMixin
|
||||||
from verdandi.mixins.menuitemmixin import MenuItemMixin
|
from verdandi.mixins.menuitemmixin import MenuItemMixin
|
||||||
from verdandi.mixins.assetsmixin import AssetsMixin
|
from verdandi.mixins.fileassetsmixin import FileAssetsMixin
|
||||||
from verdandi.mixins.newsitemmixin import NewsItemMixin
|
from verdandi.mixins.newsitemmixin import NewsItemMixin
|
||||||
from verdandi.constants import CONTENT_DIRECTORY, MARKDOWN_EXTENSIONS
|
from verdandi.constants import CONTENT_DIRECTORY, MARKDOWN_EXTENSIONS
|
||||||
|
|
||||||
class Page(MenuItemMixin, NewsItemMixin, TemplateMixin, AssetsMixin):
|
class Page(MenuItemMixin, NewsItemMixin, TemplateMixin, FileAssetsMixin):
|
||||||
|
|
||||||
title = "Page Title"
|
title = "Page Title"
|
||||||
content_file = "content.md"
|
content_file = "content.md"
|
||||||
|
|
Loading…
Reference in New Issue