forked from 529324416/MapEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
100 lines (74 loc) · 2.31 KB
/
utils.py
File metadata and controls
100 lines (74 loc) · 2.31 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
import os
import time
import copy
import json
import queue
def read(filepath:str) -> str:
'''以文本的形式读取一个文件的所有信息'''
with open(filepath, "r", encoding='utf-8') as f:
return f.read()
def save(cnt:str, filepath:str) -> None:
'''存储文本到指定的文件夹'''
with open(filepath, "w", encoding='utf-8') as f:
f.write(cnt)
def save_json(obj:dict, filepath:str) -> None:
'''存储一个dict到指定的JSON文件中'''
content = json.dumps(obj).encode("utf-8").decode("unicode-escape")
save(content, filepath)
def now():
'''以默认的格式[hh:mm:ss]获取当前的时间信息'''
t = time.localtime()
return f"{t[3]}:{t[4]}:{t[5]}"
def clearFolder(folder:str, clear=True):
'''删除文件夹下所有的文件'''
for file in os.listdir(folder):
path = f"{folder}/{file}"
if os.path.isfile(path):
os.remove(path)
else:
clearFolder(path, True)
if clear:
os.rmdir(folder)
def parseNameFromPath(path:str,sep='/'):
'''从path解析出文件名'''
try:
return path.split(sep)[-1].split('.')[0]
except:
return path
class Counter:
'''计数器'''
def __init__(self, entry=0):
self._id = entry
self._queue = queue.Queue()
def load_json(self, data):
'''从json数据中恢复counter的状态'''
self._id = data["currentId"]
self._queue = queue.Queue()
for value in data["recycleList"]:
self._queue.put(value)
@property
def json(self):
'''将计数器存储为JSON数据'''
_queue = copy.copy(self._queue)
recycleList = list()
for i in range(_queue.qsize()):
recycleList.append(_queue.get())
# while _queue.not_empty:
# recycleList.append(_queue.get())
return {
"currentId":self._id,
"recycleList":recycleList
}
@property
def next_id(self):
'''获取下一个id'''
if self._queue.empty():
buf = self._id
self._id += 1
return buf
return self._queue.get()
def recycle(self, _id):
'''回收一个id'''
self._queue.put(_id)
if __name__ == '__main__':
print(parseNameFromPath("./test.png"))