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