#
#
#
import logging
import gi
gi.require_version('GExiv2', '0.10')
from gi.repository import GExiv2

class ImgExif:
  
  #
  def __init__(self):
    self.exif = None
    self.img_fn = None
    self.img_loaded = False

  #
  def useImg(self, img_fn):
    result = False
    try:
      self.exif = GExiv2.Metadata()
      ret = self.exif.open_path(img_fn)
      if (ret):
        result = True
        self.img_loaded = True
    except Exception as ex:
      logging.error(ex)
      raise
    self.img_fn = img_fn

  #
  def appendKey(self, key):
    logging.info('Adding key to metadata...')
    self.exif.set_comment(str(key.getKeyPEM()))

  #
  def extractKey(self):
    pemData = self.exif.get_comment()


  def saveFile(self):
    logging.info("Saving image as {}...".format(self.img_fn))
    self.exif.save_file(self.img_fn)

