Esempio n. 1
0
def convert_to_bookchapter(absolute_path, level):

    # album_title, album_description, album_cover, album_sorted_rev,
    # album_hide_cover, modified_albums_on_top, ignoreinbook)
    album_metadata = pyntrest_io.read_optional_album_metadata(
        absolute_path, pyntrest_config.META_INI_FILE_PATTERN)

    book_chapter = BookChapter()
    book_chapter.level = level
    book_chapter.title = album_metadata[0]
    book_chapter.subchapters = []
    book_chapter.ignore = album_metadata[6]

    subfiles = pyntrest_io.get_immediate_subfiles(absolute_path)
    content = []
    for subfile in subfiles:
        if re.match(pyntrest_config.INTRO_MD_FILE_PATTERN, subfile):
            html_content, _ = pyntrest_io.get_html_content(
                path.join(absolute_path, subfile))
            content.insert(0, html_content)
        if re.match(pyntrest_config.TEXT_MD_FILE_PATTERN, subfile):
            html_content, _ = pyntrest_io.get_html_content(
                path.join(absolute_path, subfile))
            content.append(html_content)

    book_chapter.html_content = u'\n'.join(content)

    # remove emoticons
    try:
        # Wide UCS-4 build
        myre = re.compile(
            u'['
            u'\U0001F300-\U0001F64F'
            u'\U0001F680-\U0001F6FF'
            u'\u2600-\u26FF\u2700-\u27BF]+', re.UNICODE)
    except re.error:
        # Narrow UCS-2 build
        myre = re.compile(
            u'('
            u'\ud83c[\udf00-\udfff]|'
            u'\ud83d[\udc00-\ude4f\ude80-\udeff]|'
            u'[\u2600-\u26FF\u2700-\u27BF])+', re.UNICODE)
    book_chapter.html_content = myre.sub(u'',
                                         book_chapter.html_content)  # no emoji

    for direct in pyntrest_io.get_immediate_subdirectories(
            absolute_path, True):
        book_chapter.subchapters.append(
            convert_to_bookchapter(path.join(absolute_path, direct),
                                   level + 1))

    return book_chapter
Esempio n. 2
0
def convert_to_bookchapter( absolute_path, level ):

    # album_title, album_description, album_cover, album_sorted_rev,
    # album_hide_cover, modified_albums_on_top, ignoreinbook)
    album_metadata = pyntrest_io.read_optional_album_metadata (
         absolute_path, pyntrest_config.META_INI_FILE_PATTERN)

    book_chapter = BookChapter()
    book_chapter.level = level
    book_chapter.title = album_metadata[0]
    book_chapter.subchapters = []
    book_chapter.ignore = album_metadata[6]

    subfiles = pyntrest_io.get_immediate_subfiles(absolute_path)
    content = []
    for subfile in subfiles:
        if re.match(pyntrest_config.INTRO_MD_FILE_PATTERN, subfile):
            html_content, _ = pyntrest_io.get_html_content(
                path.join(absolute_path, subfile))
            content.insert(0, html_content)
        if re.match(pyntrest_config.TEXT_MD_FILE_PATTERN, subfile):
            html_content, _ = pyntrest_io.get_html_content(
                path.join(absolute_path, subfile))
            content.append(html_content)

    book_chapter.html_content = u'\n'.join(content)

    # remove emoticons
    try:
        # Wide UCS-4 build
        myre = re.compile(u'['
            u'\U0001F300-\U0001F64F'
            u'\U0001F680-\U0001F6FF'
            u'\u2600-\u26FF\u2700-\u27BF]+',
            re.UNICODE)
    except re.error:
        # Narrow UCS-2 build
        myre = re.compile(u'('
            u'\ud83c[\udf00-\udfff]|'
            u'\ud83d[\udc00-\ude4f\ude80-\udeff]|'
            u'[\u2600-\u26FF\u2700-\u27BF])+',
            re.UNICODE)
    book_chapter.html_content = myre.sub(
        u'', book_chapter.html_content) # no emoji

    for direct in pyntrest_io.get_immediate_subdirectories(absolute_path, True):
        book_chapter.subchapters.append(
            convert_to_bookchapter(path.join(absolute_path, direct), level+1))

    return book_chapter
Esempio n. 3
0
 def test_read_optional_album_metadata (self):
     ini_filename = '__info__.ini'
     self.assertRaises(TypeError, pyntrest_io.read_optional_album_metadata)
     self.assertRaises(TypeError, pyntrest_io.read_optional_album_metadata, 'asdasd_:asd')
     self.assertRaises(TypeError, pyntrest_io.read_optional_album_metadata, 'asdasd_:asd', '')
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata('', ini_filename)
     self.assertEqual(path.basename(path.abspath('')), a)
     self.assertEqual('', b)
     self.assertEqual(None, c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content = []
     dirname = self.create_temp_file_with_content(content, ini_filename )
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertEqual(path.basename(dirname), a)
     self.assertEqual('', b)
     self.assertEqual(None, c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('[AlbumInfo]')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertEqual(path.basename(dirname), a)
     self.assertEqual('', b)
     self.assertEqual(None, c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('Title=Pyntrest')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertEqual('Pyntrest', a)
     self.assertEqual('', b)
     self.assertEqual(None, c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('Description=Automated web photo albums for convenience lovers')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertEqual('Pyntrest', a)
     self.assertEqual('Automated web photo albums for convenience lovers', b)
     self.assertEqual(None, c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('CoverImage=im-001.jpg')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertEqual('Pyntrest', a)
     self.assertEqual('Automated web photo albums for convenience lovers', b)
     self.assertEqual('im-001.jpg', c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('ReverseImages=asdasd')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     del content[-1]
     content.append('ReverseImages=True')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertTrue(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('HideCover=asdasd')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertFalse(e)
     self.assertFalse(f)
     del content[-1]
     content.append('HideCover=True')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertTrue(e)
     self.assertFalse(f)
     content.append('ModifiedAlbumsOnTop=asdasd')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertFalse(f)
     del content[-1]
     content.append('ModifiedAlbumsOnTop=True')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(dirname, ini_filename )
     self.assertTrue(f)
Esempio n. 4
0
    def process_subalbum (self, subalbums, subalbum_name, local_albumpath_rel,
        local_albumpath_abs):
        """Uses the given sub-album name and creates a Album model for it,
        including the search for optional descriptions and album covers and
        the creation of all necessary thumbnails. Finally it appends the Album
        to the provided sub-album list."""

        local_subalbumpath_abs = path.join(local_albumpath_abs, subalbum_name)
        modified, lastmodified = is_modified(
            local_subalbumpath_abs, False,
            pyntrest_config.MAX_AGE_OF_NEW_IMAGES_H,
            pyntrest_config.HIGHLIGHT_NEW_IMAGES,
            pyntrest_config.IMAGE_FILE_PATTERN)
        meta_title, meta_description, meta_cover, _, _, _, _ = (
            read_optional_album_metadata (local_subalbumpath_abs,
                            pyntrest_config.META_INI_FILE_PATTERN))

        local_subalbumcover_abs = None

        # find and set cover image

        # If sub album cover manually set..
        if meta_cover:
            # Check for existence..
            cover_candidate = path.join(local_subalbumpath_abs, meta_cover)
            # on windows we also allow uncased matches (or .. lower())
            if path.exists(cover_candidate) or path.exists(cover_candidate.lower()):
                local_subalbumcover_abs = cover_candidate

        # if no album cover was set manually or if it did not exist...
        if not local_subalbumcover_abs:
            # ... get first in folder
            for cover_candidate in listdir(local_subalbumpath_abs):
                if pyntrest_config.IMAGE_FILE_PATTERN.match(
                    cover_candidate.lower()):
                    local_subalbumcover_abs = path.join(
                    local_subalbumpath_abs,
                    cover_candidate)
                    break

        subalbum_webpath = path.join(local_albumpath_rel, subalbum_name)
        subalbum_webpath = '/' + sub('\\\\', '/', subalbum_webpath)

        # If still no album cover was found then this is an empty album hence
        # we need to let the template use a default image
        if not local_subalbumcover_abs:

            # setup template context
            subalbum = Album(title=meta_title, description=meta_description,
                      path=subalbum_webpath,
                      width=pyntrest_config.IMAGE_THUMB_WIDTH,
                      height=pyntrest_config.IMAGE_THUMB_HEIGHT, cover=None,
                      modified=modified, last_modified=lastmodified)

        else:
            # otherwise prepare it for static serving
            image_basename = path.basename(local_subalbumcover_abs)
            local_thumbnail_folder_abs = path.join (
                self.static_athumbs_path, local_albumpath_rel, subalbum_name)
            thumbnail_webpath = path.join (local_albumpath_rel, subalbum_name,
                                           image_basename)
            target_file = path.join(local_thumbnail_folder_abs, image_basename)
            mkdirs(local_thumbnail_folder_abs)
            self.pil_handler.create_album_thumbnail_if_not_present(
                local_subalbumcover_abs, target_file)

            # setup template context
            subalbum = Album(   title=meta_title,
                                description=meta_description,
                                path=subalbum_webpath,
                                width=pyntrest_config.IMAGE_THUMB_WIDTH,
                                height=pyntrest_config.IMAGE_THUMB_HEIGHT,
                                cover=thumbnail_webpath,
                                modified=modified,
                                last_modified=lastmodified)

        # append subalbum to context
        subalbums.append(subalbum)
Esempio n. 5
0
    def generate_view_context(self, request_path):
        """The core "magic" method that reads the requested album filesystem
        path related to the virtual album path from the request and obtains all
        necessary information to create a neat web photo album"""

        local_albumpath_abs = convert_url_path_to_local_filesystem_path(
                  self.main_images_path, request_path)
        local_albumpath_rel = convert_url_path_to_local_filesystem_path(
                                                     '', request_path)
        mkdirs(path.join (self.static_fullsize_path, local_albumpath_rel))
        mkdirs(path.join (self.static_ithumbs_path, local_albumpath_rel))

        (album_title, album_description, album_cover, reversed_sorting,
         hide_cover, mods_on_top, _) = read_optional_album_metadata (
            local_albumpath_abs, pyntrest_config.META_INI_FILE_PATTERN)

        # setup sub albums
        subalbums = []
        for subalbum_name in get_immediate_subdirectories(local_albumpath_abs):
            self.process_subalbum (subalbums, subalbum_name,
            local_albumpath_rel, local_albumpath_abs)

        # sort subalbums by path
        if (mods_on_top):
            subalbums = sorted(subalbums, key=lambda subalbum:
                subalbum.last_modified, reverse=True)
        else:
            subalbums = sorted(subalbums, key=lambda subalbum:
                subalbum.path, reverse=False)

        # setup images
        images = []
        for image_name in listdir(local_albumpath_abs):
            subimage = self.process_subimage(image_name,
                                    local_albumpath_rel, local_albumpath_abs)
            if subimage is None:
                continue
            if (album_cover is None or hide_cover is False):
                images.append(subimage)
            else:
                if album_cover == image_name:
                    pass # ignore
                else:
                    images.append(subimage)

        # update image descriptions
        image_descriptions = read_optional_image_metadata(
                    local_albumpath_abs, pyntrest_config.META_INI_FILE_PATTERN)
        for image in images:
            basename = path.basename(image.location).lower()
            try:
                image_description = image_descriptions[basename]
                image.description = image_description
            except KeyError:
                pass # Ignore

        # sort images by path
        images = sorted(images, key=lambda albumimage: albumimage.location,
                        reverse=reversed_sorting)

        # setup breadcrumb
        breadcrumbs = []
        breadcrumb_paths = get_absolute_breadcrumb_filesystem_paths (
            request_path)
        local_albumpath_abs = self.main_images_path
        path_string = ''
        for breadcrumb_path in breadcrumb_paths:
            local_albumpath_abs = path.join(local_albumpath_abs,
                breadcrumb_path)
            path_string = path_string + '/' + breadcrumb_path
            path_string = sub ('[/]+' , '/', path_string)
            album_title, album_description, _, _, _, _, _= (
                read_optional_album_metadata (local_albumpath_abs,
                    pyntrest_config.META_INI_FILE_PATTERN))
            url_path = path_string
            if pyntrest_config.EXTERNAL_BASE_URL is not None:
                url_path  = pyntrest_config.EXTERNAL_BASE_URL + url_path

            web_path = WebPath(title=album_title, path=url_path)
            breadcrumbs.append(web_path)
        page_title = breadcrumbs[0].title
        breadcrumbs[0].title = pyntrest_config.WORDING_HOME

        # check for intro text
        intro_content = None
        intro_file = None
        subfiles = get_immediate_subfiles(local_albumpath_abs)
        for subfile in subfiles:
            matching = match(pyntrest_config.INTRO_MD_FILE_PATTERN,
                             subfile.lower())
            if matching != None:
                intro_file = path.join(local_albumpath_abs, subfile)

        if intro_file and path.isfile(intro_file):
            intro_content, _ = get_html_content(intro_file)

        # show header?
        show_breadcrumb = True
        first_page = len(breadcrumbs) == 1
        try:
            if first_page and pyntrest_config.SUPPRESS_BREADCRUMB_ON_HOME:
                show_breadcrumb = False
        except AttributeError:
            pass # just show it.

        context = { 'page_title': page_title,
                    'col_width': pyntrest_config.IMAGE_THUMB_WIDTH,
                    'col_height' : pyntrest_config.IMAGE_THUMB_HEIGHT,
                    'images': images,
                    'show_breadcrum': False,
                    'first_page': first_page,
                    'subalbums': subalbums,
                    'album_title' : album_title,
                    'album_description' : album_description,
                    'lang_images' : pyntrest_config.WORDING_IMAGES,
                    'lang_albums' : pyntrest_config.WORDING_ALBUM,
                    'breadcrumbs' : breadcrumbs,
                    'show_breadcrumb': show_breadcrumb,
                    'show_headings' :
                        pyntrest_config.SHOW_ALBUM_IMAGES_WORDINGS,
                   'intro_content' : intro_content}

        return context
Esempio n. 6
0
 def test_read_optional_album_metadata(self):
     ini_filename = '__info__.ini'
     self.assertRaises(TypeError, pyntrest_io.read_optional_album_metadata)
     self.assertRaises(TypeError, pyntrest_io.read_optional_album_metadata,
                       'asdasd_:asd')
     self.assertRaises(TypeError, pyntrest_io.read_optional_album_metadata,
                       'asdasd_:asd', '')
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         '', ini_filename)
     self.assertEqual(path.basename(path.abspath('')), a)
     self.assertEqual('', b)
     self.assertEqual(None, c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content = []
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertEqual(path.basename(dirname), a)
     self.assertEqual('', b)
     self.assertEqual(None, c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('[AlbumInfo]')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertEqual(path.basename(dirname), a)
     self.assertEqual('', b)
     self.assertEqual(None, c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('Title=Pyntrest')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertEqual('Pyntrest', a)
     self.assertEqual('', b)
     self.assertEqual(None, c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append(
         'Description=Automated web photo albums for convenience lovers')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertEqual('Pyntrest', a)
     self.assertEqual('Automated web photo albums for convenience lovers',
                      b)
     self.assertEqual(None, c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('CoverImage=im-001.jpg')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertEqual('Pyntrest', a)
     self.assertEqual('Automated web photo albums for convenience lovers',
                      b)
     self.assertEqual('im-001.jpg', c)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('ReverseImages=asdasd')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertFalse(d)
     self.assertFalse(e)
     self.assertFalse(f)
     del content[-1]
     content.append('ReverseImages=True')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertTrue(d)
     self.assertFalse(e)
     self.assertFalse(f)
     content.append('HideCover=asdasd')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertFalse(e)
     self.assertFalse(f)
     del content[-1]
     content.append('HideCover=True')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertTrue(e)
     self.assertFalse(f)
     content.append('ModifiedAlbumsOnTop=asdasd')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertFalse(f)
     del content[-1]
     content.append('ModifiedAlbumsOnTop=True')
     dirname = self.create_temp_file_with_content(content, ini_filename)
     a, b, c, d, e, f, _ = pyntrest_io.read_optional_album_metadata(
         dirname, ini_filename)
     self.assertTrue(f)