forked from BurnySc2/python-sc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps.py
53 lines (40 loc) · 1.4 KB
/
maps.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
from .paths import Paths
from loguru import logger
def get(name=None):
maps = []
for mapdir in (p for p in Paths.MAPS.iterdir()):
if mapdir.is_dir():
for mapfile in (p for p in mapdir.iterdir() if p.is_file()):
if mapfile.suffix == ".SC2Map":
maps.append(Map(mapfile))
elif mapdir.is_file():
if mapdir.suffix == ".SC2Map":
maps.append(Map(mapdir))
if name is None:
return maps
for m in maps:
if m.matches(name):
return m
raise KeyError(f"Map '{name}' was not found. Please put the map file in \"/StarCraft II/Maps/\".")
class Map:
def __init__(self, path):
self.path = path
if self.path.is_absolute():
try:
self.relative_path = self.path.relative_to(Paths.MAPS)
except ValueError: # path not relative to basedir
logger.warning(f"Using absolute path: {self.path}")
self.relative_path = self.path
else:
self.relative_path = self.path
@property
def name(self):
return self.path.stem
@property
def data(self):
with open(self.path, "rb") as f:
return f.read()
def matches(self, name):
return self.name.lower().replace(" ", "") == name.lower().replace(" ", "")
def __repr__(self):
return f"Map({self.path})"