-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_analysis.py
56 lines (44 loc) · 1.48 KB
/
file_analysis.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
#!/usr/bin/env python3
import xattr
import json
from subprocess import Popen, PIPE
import os
import datetime
import posix
from preferences import *
def bplist_to_list (bplist: bytes) -> list:
"""
Decode bplist bytestrings
Adapted from https://stackoverflow.com/questions/8856032/reading-binary-plist-files-with-python
:param bplist: bplist to decode
:return: the bplist's list representation
"""
args = ['plutil', '-convert', 'json', '-o', '-', '--', '-']
p = Popen(args, stdin=PIPE, stdout=PIPE)
out, err = p.communicate(bplist)
return json.loads (out)
def read_tags (filepath: str) -> list:
"""
Read the tags of a file/directory (macOS-specific)
:param filepath: filepath to analyse
:return: list of tags
"""
try:
tagattr = xattr.getxattr (filepath, 'com.apple.metadata:_kMDItemUserTags')
tags = bplist_to_list (tagattr)
tags = [tag.split('\n')[0] for tag in tags]
return tags
except:
return list()
# TODO: this function must implement a priority system for sources:
# 1. EXIF
# 2. File birthtime
def get_birthtime_formatted (file: posix.DirEntry) -> str:
"""
Get the birthtime of a file as a formatted string
:param filepath: filepath to analyse
:return: formatted datetime string
"""
birthtime = os.stat (file.path) .st_birthtime
birthtime_formatted = datetime.datetime.fromtimestamp (birthtime) .strftime (ALIAS_DATETIME_FORMAT)
return birthtime_formatted