示例#1
0
    def add_snapshot(options):
        path = os.path.abspath(options.file)

        # Working directory.
        with tempfile.TemporaryDirectory() as temporary_directory:

            # InContext doesn't handle TIFF files well, so we convert them to JPEG if necessary.
            filename = os.path.basename(path)
            basename, ext = os.path.splitext(filename)
            if ext.lower() == ".tiff":
                logging.info("Converting TIFF to JPEG...")

                jpeg_path = os.path.join(temporary_directory,
                                         f"{basename}.jpeg")
                subprocess.check_call(
                    ["convert", path, "-quality", "100", jpeg_path])

                # Unfortunately we also need to copy the metadata in a separate step.
                subprocess.check_call([
                    "exiftool", "-TagsFromFile", path, "-all:all>all:all",
                    jpeg_path
                ])

                path = jpeg_path

            exif_metadata = gallery.metadata_from_exif(path)
            date = exif_metadata["date"].strftime("%Y-%m-%d-%H-%M-%S")

            title = ""
            try:
                title = exif_metadata["title"]
            except KeyError:
                pass
            if options.title:
                title = options.title
            title = utils.safe_basename(title)
            basename, ext = os.path.splitext(path)
            dirname = os.path.dirname(basename)
            filename = date
            if title:
                filename = f"{date}-{title}"

            if options.title or options.description:
                sidecar_path = os.path.join(
                    incontext.configuration.site.paths.snapshots,
                    filename + ".exif")
                sidecar = {}
                if options.title:
                    sidecar["Title"] = options.title
                if options.description:
                    sidecar["Description"] = options.description
                with open(sidecar_path, "w") as fh:
                    fh.write(json.dumps(sidecar, indent=4))
                    fh.write("\n")

            destination = os.path.join(
                incontext.configuration.site.paths.snapshots, filename + ext)
            shutil.copyfile(path, destination)
示例#2
0
 def add_album(options):
     basename = utils.safe_basename(options.title)
     date = datetime.datetime.strptime(options.date, "%Y-%m-%d")
     directory = os.path.join(incontext.configuration.site.paths.photos,
                              date.strftime("%Y"), date.strftime("%m"),
                              basename)
     os.makedirs(directory)
     with open(os.path.join(directory, "index.markdown"), "w") as fh:
         fh.write(ALBUM_TEMPLATE % (options.title, options.date))
示例#3
0
 def add_draft(options):
     basename = utils.safe_basename(options.title)
     directory = os.path.join(incontext.configuration.site.paths.drafts,
                              basename)
     index = os.path.join(directory, "index.markdown")
     os.makedirs(directory)
     with open(index, "w") as fh:
         document = utils.FrontmatterDocument(
             metadata={"title": options.title})
         fh.write(frontmatter.dumps(document))
         fh.write("\n")
示例#4
0
    def add_snapshot(options):
        path = os.path.abspath(options.file)

        exif = gallery.exif(path)

        date = get_exif_date(exif).strftime("%Y-%m-%d-%H-%M-%S")

        title = ""
        try:
            title = exif["Title"]
        except KeyError:
            pass
        if options.title:
            title = options.title
        title = utils.safe_basename(title)
        basename, ext = os.path.splitext(path)
        dirname = os.path.dirname(basename)
        filename = date
        if title:
            filename = f"{date}-{title}"

        if options.title or options.description:
            sidecar_path = os.path.join(
                incontext.configuration.site.paths.snapshots,
                filename + ".exif")
            sidecar = {}
            if options.title:
                sidecar["Title"] = options.title
            if options.description:
                sidecar["Description"] = options.description
            with open(sidecar_path, "w") as fh:
                fh.write(json.dumps(sidecar, indent=4))
                fh.write("\n")

        destination = os.path.join(
            incontext.configuration.site.paths.snapshots, filename + ext)
        shutil.copyfile(path, destination)