当前位置: 首页>>代码示例>>Python>>正文


Python Library.fetch_subfolders方法代码示例

本文整理汇总了Python中library.Library.fetch_subfolders方法的典型用法代码示例。如果您正苦于以下问题:Python Library.fetch_subfolders方法的具体用法?Python Library.fetch_subfolders怎么用?Python Library.fetch_subfolders使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在library.Library的用法示例。


在下文中一共展示了Library.fetch_subfolders方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: SaveThumbnails

# 需要导入模块: from library import Library [as 别名]
# 或者: from library.Library import fetch_subfolders [as 别名]
class SaveThumbnails(object):
    def __init__(self, path, photos_path, tmp_db):
        self.path = os.path.abspath(path)
        self.photos_path = photos_path

        db_path = os.path.join(photos_path, "database")
        self.library = Library(db_path, tmp_db)

        self.photos = dict()

    def build(self):
        logger.debug("Fetch photos")
        self.photos = dict((p.id, p) for p in self.library.fetch_photos())

        logger.debug("Save folders and albums")
        self.save_folder(self.library.top_folder, None)

    def save_folder(self, folder, parent_path):
        logger.debug("Save %s with parent %s", folder, parent_path)
        path = os.path.join(parent_path, folder.name) if folder != self.library.top_folder else "."
        logger.debug("Create directory %s", path)
        if not os.path.exists(os.path.join(self.path, path)):
            os.makedirs(os.path.join(self.path, path))
        for sub_folder in self.library.fetch_subfolders(folder):
            self.save_folder(sub_folder, path)
        for album in self.library.fetch_albums(folder):
            self.save_album(album, path)

    def save_album(self, album, parent_path):
        logger.debug("Save %s with parent %s", album, parent_path)
        path = os.path.join(parent_path, album.name)
        logger.debug("Create directory %s", path)
        if not os.path.exists(os.path.join(self.path, path)):
            os.makedirs(os.path.join(self.path, path))
        photos = set(self.photos[i] for i in self.library.fetch_album_photo_id_list(album))
        for photo in sorted(photos, key=lambda  p: p.date):
            self.save_photo(photo, path)

    def save_photo(self, photo, parent_path):
        caption = photo.name or "Photo_%d" % photo.id
        file_name = photo.thumbnails["hd"] or photo.thumbnails["mini"] or photo.original
        src = os.path.join(self.photos_path, file_name)
        ext = os.path.splitext(src)[1]
        dst = os.path.join(self.path, parent_path, caption + ext)
        logger.debug("Copy %s to %s", src, dst)
        shutil.copy(src, dst)
开发者ID:KageKirin,项目名称:mac_photos,代码行数:48,代码来源:create_thumbnails.py

示例2: AlbumData

# 需要导入模块: from library import Library [as 别名]
# 或者: from library.Library import fetch_subfolders [as 别名]
class AlbumData(object):
    def __init__(self, path, disable_rolls=None, disable_rating=None, tmp_db=None, gen_caption=None):
        self.path = os.path.abspath(path)

        db_path = os.path.join(path, "database")
        self.library = Library(db_path, tmp_db)

        self.data = copy.deepcopy(BASE)
        self.data[ARCHIVE_PATH] = self.path
        self.data[ALBUMS] = []

        self.photos = dict()

        self.disable_rolls = disable_rolls
        self.disable_rating = disable_rating
        self.all_roll_id = 5

        self.generate_caption = gen_caption

    def build(self):
        logger.debug("Fetch photos")
        self.photos = dict((p.id, p) for p in self.library.fetch_photos())

        logger.debug("Save folders and albums")
        self.data[ALBUMS] += [self._all_photos_album, self._flagged_album]
        self.save_folder(self.library.top_folder, None)

        logger.debug("Save rolls")
        if not self.disable_rolls:
            self.data["List of Rolls"] = [self._all_roll]

        logger.debug("Save images")
        self.data[IMAGES] = dict((str(p.id), self._photo(p)) for p in self.photos.values())

    def save_folder(self, folder, parent):
        logger.debug("Save %s with parent %s", folder, parent)
        photo_ids = set()
        if folder != self.library.top_folder:
            data = self._album_base(folder, [], parent=parent)
            self.data[ALBUMS].append(data)
        parent = folder if folder != self.library.top_folder else None
        for sub_folder in self.library.fetch_subfolders(folder):
            photo_ids |= self.save_folder(sub_folder, parent)
        for album in self.library.fetch_albums(folder):
            photo_ids |= self.save_album(album, parent)
        if folder != self.library.top_folder:
            self._append_photos(data, photo_ids)
        logger.debug("Got %s photos for %s", len(photo_ids), folder)
        return photo_ids

    def save_album(self, album, parent):
        logger.debug("Save %s with parent %s", album, parent)
        photo_ids = set(self.library.fetch_album_photo_id_list(album))
        self.data[ALBUMS].append(self._album_base(album, photo_ids, parent=parent))
        return photo_ids

    def walk_through_tree(self, folder):
        logger.info("Process %s", folder)
        for album in self.library.fetch_albums(folder):
            logger.info("Write %s", album)

        for folder in self.library.fetch_subfolders(folder):
            self.walk_through_tree(folder)

    @property
    def _all_photos_album(self):
        album = self.library.all_photos_album
        photo_ids = self.photos.keys()
        data = self._album_base(album, photo_ids, album_type="99", name="Photos")
        data["Master"] = True
        return data

    @property
    def _flagged_album(self):
        album = self.library.favorites
        photo_ids = list(str(p.id) for p in self.photos.values() if p.is_favorite)
        return self._album_base(album, photo_ids, album_type="Flagged", name="Flagged", sort_order="1")

    @property
    def _last_imported_album(self):
        album = self.library.last_import_album
        photo_ids = set(self.library.fetch_album_photo_id_list(album))
        return self._album_base(album, photo_ids)

    def _album_base(self, album, photo_ids, name=None, album_type=None, sort_order=None, parent=None):
        if album_type is None:
            if isinstance(album, Album):
                album_type = "Regular"
            elif isinstance(album, Folder):
                album_type = "Folder"
        data = {
            "AlbumId": _iphoto_id(album),
            "AlbumName": name or album.name,
            "GUID": album.uuid,
            "Album Type": album_type,
        }
        if album.poster_id:
            data["KeyPhotoKey"] = str(album.poster_id)
        if sort_order:
            data["Sort Order"] = sort_order
#.........这里部分代码省略.........
开发者ID:KageKirin,项目名称:mac_photos,代码行数:103,代码来源:create_albumdata.py


注:本文中的library.Library.fetch_subfolders方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。