-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.py
262 lines (226 loc) · 7.57 KB
/
Utils.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ------------------------------------------------
# Author: Konfido
# Created Date: July 26th 2020
# ------------------------------------------------
import ast
import datetime
import json
import locale
import os
import re
import shutil
import sys
import time
import urllib.request
class Utils():
# ---------------------
# Basic Utils
# ---------------------
@staticmethod
def open(path, finder=False):
""" open file / folder """
if finder:
os.system("open -R \"{}\"".format(path))
else:
os.system("open \"{}\"".format(path))
@classmethod
def delete(cls, path):
""" Delete the selected file"""
# Files can only be detected as `Removed` in `fswatch` when
# you conduct the `rm` command. Hence we copy the file to Trash
# before the actual removal to avoid incorrect deletion.
cls.copy(path, "~/.Trash")
os.remove(path)
@classmethod
def copy(cls, source, target):
"""copy source file/folder to target """
shutil.copy(cls.get_abspath(source), cls.get_abspath(target))
@staticmethod
def format_date(float_date, fmt="%Y-%m-%d %H:%M:%S"):
""" float time to string """
return time.strftime(fmt, time.localtime(float_date))
@staticmethod
def get_now(fmt="%Y-%m-%d %H:%M:%S"):
""" Get formated current date&time string """
now = datetime.datetime.now()
return now.strftime(fmt)
@classmethod
def get_locale(cls):
loc = locale.getlocale()
return loc
@classmethod
def mkdir(cls, path):
"""Check if dir exists and recursively mkdir"""
if not cls.path_exists(path):
os.makedirs(path)
return 1
else:
return 0
@staticmethod
def get_cwd():
return os.getcwd()
@classmethod
def get_abspath(cls, path, query_dict=False):
"""Return str() if get error """
if path.startswith('~/'):
abs_path = os.path.expanduser(path)
elif path.startswith('/Users'):
abs_path = path
elif query_dict == True:
# convert relative_path to abs_path by querying stored path's info
file_name = os.path.basename(path)
paths_dict = cls.json_load(cls.path_join(
cls.get_env("alfred_workflow_data"), 'paths.json'))
try:
abs_path = paths_dict[file_name]
except KeyError:
abs_path = ""
else:
abs_path = path
return abs_path
@classmethod
def get_relpath(cls, path, start):
relpath = os.path.relpath(path, start)
return relpath
@classmethod
def path_exists(cls, path):
return os.path.exists(path)
@staticmethod
def path_join(root, file):
return os.path.join(root, file)
@staticmethod
def json_load(file):
with open(file, 'r') as f:
var = json.load(f)
return var
@staticmethod
def json_dump(var, file):
with open(file, 'w') as f:
json.dump(var, f, indent=4, ensure_ascii=False)
@staticmethod
def str_replace(string, replace_map):
for r in replace_map.keys():
string = string.replace(r, replace_map[r])
return string
@staticmethod
def literal_eval(var):
return ast.literal_eval(var)
# return eval(var)
# ------------------------------------------------
# Advanced Utils: fetch information
# ------------------------------------------------
@staticmethod
def get_env(var):
""" Reads environment variable """
return os.getenv(var) if os.getenv(var) is not None else str()
@staticmethod
def get_query(lower=False):
try:
query = sys.argv[1].lower() if lower else sys.argv[1]
except:
query = ""
return query
@staticmethod
def get_search_type():
try:
search_type = sys.argv[2]
except:
search_type = 'normal'
return search_type
@staticmethod
def get_file_meta(path, key):
"""Get file's size, ctime, mtime"""
metas = os.stat(path)
try:
return metas.__getattribute__(key)
except AttributeError:
raise
return None
@staticmethod
def get_file_name(path, with_ext=False):
""" get file's name from path """
file = os.path.basename(path)
if not with_ext:
file, ext = os.path.splitext(file)
return file
@staticmethod
def get_file_content(path):
# only process Markdown file
if str(path).endswith(".md"):
with open(path, 'r') as f:
content = f.read()
else:
content = str()
return content
# @staticmethod
@classmethod
def get_typora_filename(cls):
"""
Return
file's name, if any note opened in Typora
"", if otherwise
"""
filename = os.popen("""osascript <<EOF
tell application "System Events"
get name of front window of process "Typora"
end tell\nEOF""")
return filename.read().strip()
@classmethod
def get_all_files_path(cls, paths):
""" support multi note paths """
file_paths_list = []
if isinstance(paths, str):
paths = [paths]
for path in paths:
path = cls.get_abspath(path)
# support subfolders
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith(".md"):
file_paths_list.append(os.path.join(root, name))
return file_paths_list
@staticmethod
def get_corelocation():
"""Return a dict of corelocation's info"""
corelocation = os.popen('swift ./corelocation.swift -json').read()
null = ''
loc_dict = eval(corelocation)
loc_dict['address'] = loc_dict['address'].replace('\n', ',')
return loc_dict
@classmethod
def get_weather(cls, lat, lon, api, lang=""):
lang = cls.get_locale()[0] if not lang else lang
url = f"http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api}&lang={lang}"
response = urllib.request.urlopen(url)
html = response.read().decode("utf-8")
weather = json.loads(html)
return weather["weather"][0]['description']
# ------------------------------------------------
# Advanced Utils: perform operation
# ------------------------------------------------
@staticmethod
def output(string):
sys.stdout.write(string)
@staticmethod
def log(message):
sys.stderr.write('LOG: {0}\n'.format(message))
@classmethod
def notify(cls, message, title="PKManger", log=False):
""" Send Notification to mac Notification Center """
# os.system("""osascript -e 'display notification "{}" with title "{}"'
# """.format(message, title))
os.system("""./terminal-notifier.app/Contents/MacOS/terminal-notifier \
-title "{}" -message "{}" \
-appIcon icon.png \
-sender com.runningwithcrayons.Alfred \
-group com.runningwithcrayons.Alfred""".format(title, message))
if log:
cls.log(message)
@staticmethod
def to_clipboard(content):
os.system(
"""osascript -e \
'tell application "System Events" to set the clipboard to "{}"'
""".format(content))