Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_url_from_path():
assert utils.url_from_path(os.sep.join(['foo', 'bar'])) == 'foo/bar'
def test_url_from_windows_path(monkeypatch):
monkeypatch.setattr('os.sep', "\\")
path = os.sep.join(['foo', 'bar'])
assert path == r'foo\bar'
assert utils.url_from_path(path) == 'foo/bar'
def random_thumbnail(self):
try:
return url_from_path(join(self.name,
random.choice(self.medias).thumbnail))
except IndexError:
return self.thumbnail
def big_url(self):
"""URL of the original media."""
if self.big is not None:
return url_from_path(self.big)
def url(self):
"""URL of the media."""
return url_from_path(self.filename)
def generate_context(self, album):
"""Generate the context dict for the given path."""
from . import __url__ as sigal_link
self.logger.info("Output album : %r", album)
return {
'album': album,
'index_title': self.index_title,
'settings': self.settings,
'sigal_link': sigal_link,
'theme': {'name': os.path.basename(self.theme),
'url': url_from_path(os.path.relpath(self.theme_path,
album.dst_path))},
}
def breadcrumb(self):
"""List of ``(url, title)`` tuples defining the current breadcrumb
path.
"""
if self.path == '.':
return []
path = self.path
breadcrumb = [((self.url_ext or '.'), self.title)]
while True:
path = os.path.normpath(os.path.join(path, '..'))
if path == '.':
break
url = (url_from_path(os.path.relpath(path, self.path)) + '/' +
self.url_ext)
breadcrumb.append((url, self.gallery.albums[path].title))
breadcrumb.reverse()
return breadcrumb
self._thumbnail = None
if path == '.':
self.src_path = settings['source']
self.dst_path = settings['destination']
else:
self.src_path = join(settings['source'], path)
self.dst_path = join(settings['destination'], path)
self.logger = logging.getLogger(__name__)
self._get_metadata()
# optionally add index.html to the URLs
self.url_ext = self.output_file if settings['index_in_url'] else ''
self.index_url = url_from_path(os.path.relpath(
settings['destination'], self.dst_path)) + '/' + self.url_ext
#: List of all medias in the album (:class:`~sigal.gallery.Image` and
#: :class:`~sigal.gallery.Video`).
self.medias = medias = []
self.medias_count = defaultdict(int)
for f in filenames:
ext = splitext(f)[1]
if ext.lower() in settings['img_extensions']:
media = Image(f, self.path, settings)
elif ext.lower() in settings['video_extensions']:
media = Video(f, self.path, settings)
else:
continue
try:
# if thumbnail is missing (if settings['make_thumbs'] is False)
s = self.settings
if self.type == 'image':
image.generate_thumbnail(
path, self.thumb_path, s['thumb_size'],
fit=s['thumb_fit'])
elif self.type == 'video':
video.generate_thumbnail(
path, self.thumb_path, s['thumb_size'],
s['thumb_video_delay'], fit=s['thumb_fit'],
converter=s['video_converter'])
except Exception as e:
self.logger.error('Failed to generate thumbnail: %s', e)
return
return url_from_path(self.thumb_name)
def thumbnail(self):
"""Path to the thumbnail of the album."""
if self._thumbnail:
# stop if it is already set
return self._thumbnail
# Test the thumbnail from the Markdown file.
thumbnail = self.meta.get('thumbnail', [''])[0]
if thumbnail and isfile(join(self.src_path, thumbnail)):
self._thumbnail = url_from_path(join(
self.name, get_thumb(self.settings, thumbnail)))
self.logger.debug("Thumbnail for %r : %s", self, self._thumbnail)
return self._thumbnail
else:
# find and return the first landscape image
for f in self.medias:
ext = splitext(f.filename)[1]
if ext.lower() in self.settings['img_extensions']:
# Use f.size if available as it is quicker (in cache), but
# fallback to the size of src_path if dst_path is missing
size = f.size
if size is None:
size = get_size(f.src_path)
if size['width'] > size['height']:
self._thumbnail = (url_quote(self.name) + '/' +