-
-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathfileNaviCommand.py
More file actions
150 lines (137 loc) · 4.47 KB
/
fileNaviCommand.py
File metadata and controls
150 lines (137 loc) · 4.47 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
File Navigation Commands
"""
import os as _vp_os
import stat as _vp_stat
import ctypes as _vp_ctypes
def _vp_get_userprofile_path():
"""
Get UserProfile Path (Home in Linux)
"""
if _vp_os.name == 'nt':
# windows
return _vp_os.getenv('USERPROFILE')
else:
# linux
return _vp_os.getenv('HOME')
def _vp_get_downloads_path():
"""
Get Download Path
Returns: the default downloads path for linux or windows
"""
if _vp_os.name == 'nt':
# windows
import winreg
sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
location = winreg.QueryValueEx(key, downloads_guid)[0]
return location
else:
# linux
return _vp_os.path.join(_vp_os.path.expanduser('~'), 'downloads')
def _vp_get_desktop_path():
"""
Get Desktop Path
"""
if _vp_os.name == 'nt':
# windows
return _vp_os.path.join(_vp_get_userprofile_path(), 'Desktop')
else:
# linux
return _vp_os.path.join(_vp_os.path.expanduser('~'), 'Desktop')
def _vp_get_documents_path():
"""
Get Documents Path
"""
if _vp_os.name == 'nt':
# windows
return _vp_os.path.join(_vp_get_userprofile_path(), 'Documents')
else:
# linux
return _vp_os.path.join(_vp_os.path.expanduser('~'), 'Documents')
def _vp_sizeof_fmt(num, suffix='B'):
"""
Return resized image
"""
for unit in ['','K','M','G','T','P','E','Z']:
if abs(num) < 1024.0:
return '%3.1f%s%s' % (num, unit, suffix)
num /= 1024.0
return '%.1f%s%s' % (num, 'Yi', suffix)
def _vp_search_path(path, show_hidden=False):
"""
Search child folder and file list under the given path
path: str
path to search file/dir list
show_hidden: bool (optional; default: False)
set True for show hidden file/dir
returns: list
list with scanned file/dir list
0 element with current and parent path information
1~n elements with file/dir list under given path
"""
import datetime as _dt
_current = _vp_os.path.abspath(path)
_parent = _vp_os.path.dirname(_current)
with _vp_os.scandir(_current) as i:
_info = []
_info.append({'current':_current,'parent':_parent})
for _entry in i:
if show_hidden or _vp_check_hidden(_entry.path) == False:
_name = _entry.name
_path = _entry.path # file path
_stat = _entry.stat()
_size = _vp_sizeof_fmt(_stat.st_size) # file size
_a_time = _stat.st_atime # current access time
_a_dt = _dt.datetime.fromtimestamp(_a_time).strftime('%Y-%m-%d %H:%M')
_m_time = _stat.st_mtime # current modified time
_m_dt = _dt.datetime.fromtimestamp(_m_time).strftime('%Y-%m-%d %H:%M')
_e_type = 'other'
if _entry.is_file():
_e_type = 'file'
elif _entry.is_dir():
_e_type = 'dir'
_info.append({'name':_name, 'type':_e_type, 'path':_path, 'size':_size, 'atime':str(_a_dt), 'mtime':str(_m_dt)})
return _info
def _vp_get_image_by_path(path):
"""
Get image file by path
"""
from PIL import Image
img = Image.open(path)
return img
def _vp_get_relative_path(start, path):
"""
Get relative path using start path and current path
start: str
start path+
path: str
current path
returns: str
current relative path
"""
return _vp_os.path.relpath(path, start)
def _vp_check_hidden(path):
"""
Check if it's hidden
path: str
file path
returns: bool
True for hidden file/dir, False for others
"""
try:
return bool(_vp_os.stat(path).st_file_attributes & _vp_stat.FILE_ATTRIBUTE_HIDDEN)
except:
return _vp_is_hidden(path)
def _vp_is_hidden(filepath):
name = _vp_os.path.basename(_vp_os.path.abspath(filepath))
return name.startswith('.') or _vp_has_hidden_attribute(filepath)
def _vp_has_hidden_attribute(filepath):
try:
attrs = _vp_ctypes.windll.kernel32.GetFileAttributesW(str(filepath))
assert attrs != -1
result = bool(attrs & 2)
except (AttributeError, AssertionError):
result = False
return result