exiv2_parser.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import re
  2. import subprocess
  3. from typing import List, Tuple
  4. from . import util
  5. class Keyword:
  6. def __init__(self, *, keyword:str, kind: str, count: int):
  7. self.keyword = keyword
  8. self.kind = kind
  9. self.count = count
  10. class Exiv2Parser:
  11. @classmethod
  12. def get_exiv2_version(cls) -> Tuple[str, str]:
  13. commands = ['exiv2', '--version']
  14. process = subprocess.Popen(commands, stdout=subprocess.PIPE)
  15. output = util.to_str(process.communicate()[0])
  16. match = re.search(r'exiv2 ([\d.]+) (\w+)', output)
  17. if match is not None:
  18. return match.groups()
  19. return None
  20. @classmethod
  21. def get_values(cls, file_path: str) -> dict:
  22. keywords = cls.__get_keys(file_path)
  23. result = dict()
  24. for key in keywords:
  25. commands = ['exiv2', '-K', key.keyword, '-P', 't', 'print', file_path]
  26. process = subprocess.Popen(commands, stdout=subprocess.PIPE)
  27. output = util.to_str(process.communicate()[0]).rstrip('\n')
  28. # Check if the key is a list or scalar
  29. if key.count > 1:
  30. result[key.keyword] = output.split('\n') # Assume the output is like keywords, one per line
  31. else:
  32. result[key.keyword] = output # Assume the whole input is the value
  33. return result
  34. @classmethod
  35. def __get_keys(cls, file_path: str) -> List[Keyword]:
  36. found_keywords = dict()
  37. commands = ['exiv2', '-P', 'ky', 'print', file_path]
  38. process = subprocess.Popen(commands, stdout=subprocess.PIPE)
  39. output = util.to_str(process.communicate()[0])
  40. for match in re.finditer(r'([\w.]+)\W+(\w+)\W*\n?', output):
  41. code, kind = match.groups()
  42. keyword = found_keywords.get(code, Keyword(keyword=code, kind=kind, count=0))
  43. keyword.count += 1
  44. found_keywords[code] = keyword
  45. return list(found_keywords.values())
  46. if __name__ == '__main__':
  47. #data = Exiv2Parser.get_values('content/blog/terms2.jpeg')
  48. #print(data)
  49. version_info = Exiv2Parser.get_exiv2_version()
  50. print(version_info)