mirror of
https://github.com/shlomif/PySolFC.git
synced 2025-04-05 00:02:29 -04:00
Refactor initCardsets
Make variable names descriptive. Don't check for duplicate or empty strings in result of getSearchDirs() because that is not necessary. Document those properties in a comment in resource.py. Wrap try...except more closely around expressions where we want to catch.
This commit is contained in:
parent
24ccdf634b
commit
1c79d5437e
2 changed files with 37 additions and 43 deletions
|
@ -1075,60 +1075,52 @@ Please select a %(correct_type)s type cardset.
|
||||||
# set offsets from options.cfg
|
# set offsets from options.cfg
|
||||||
if cs.ident in self.opt.offsets:
|
if cs.ident in self.opt.offsets:
|
||||||
cs.CARD_XOFFSET, cs.CARD_YOFFSET = self.opt.offsets[cs.ident]
|
cs.CARD_XOFFSET, cs.CARD_YOFFSET = self.opt.offsets[cs.ident]
|
||||||
|
|
||||||
if cs.CARDD > self.top.winfo_screendepth():
|
|
||||||
return None
|
|
||||||
return cs
|
return cs
|
||||||
|
|
||||||
def initCardsets(self):
|
def initCardsets(self):
|
||||||
|
"""Load all valid cardset config.txt files and ignore invalid ones.
|
||||||
|
"""
|
||||||
|
screendepth = self.top.winfo_screendepth()
|
||||||
manager = self.cardset_manager
|
manager = self.cardset_manager
|
||||||
# find all available cardsets
|
# find all available cardsets
|
||||||
dirs = manager.getSearchDirs(self, ("cardsets", ""), "PYSOL_CARDSETS")
|
dirs = manager.getSearchDirs(self, ("cardsets", ""), "PYSOL_CARDSETS")
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
dirs += manager.getSearchDirs(self, "cardsets-*")
|
dirs += manager.getSearchDirs(self, "cardsets-*")
|
||||||
# print dirs
|
found = []
|
||||||
found, t = [], {}
|
found_names = [] # (to check for duplicates)
|
||||||
fnames = {} # (to check for duplicates)
|
|
||||||
for dirname in dirs:
|
for dirname in dirs:
|
||||||
dirname = dirname.strip()
|
|
||||||
try:
|
try:
|
||||||
names = []
|
subdirs = [os.path.join(dirname, subdir)
|
||||||
if dirname and os.path.isdir(dirname) and dirname not in t:
|
for subdir in os.listdir(dirname)
|
||||||
t[dirname] = 1
|
if subdir.startswith('cardset-')]
|
||||||
names = os.listdir(dirname)
|
|
||||||
names.sort()
|
|
||||||
for name in names:
|
|
||||||
if not name.startswith('cardset-'):
|
|
||||||
continue
|
|
||||||
d = os.path.join(dirname, name)
|
|
||||||
if not os.path.isdir(d):
|
|
||||||
continue
|
|
||||||
f = os.path.join(d, "config.txt")
|
|
||||||
if os.path.isfile(f):
|
|
||||||
try:
|
|
||||||
cs = self._readCardsetConfig(d, f)
|
|
||||||
if cs:
|
|
||||||
# from pprint import pprint
|
|
||||||
# print cs.name
|
|
||||||
# pprint(cs.__dict__)
|
|
||||||
back = cs.backnames[cs.backindex]
|
|
||||||
f1 = os.path.join(d, back)
|
|
||||||
f2 = os.path.join(d, "shade" + cs.ext)
|
|
||||||
if (cs.ext in IMAGE_EXTENSIONS and
|
|
||||||
cs.name not in fnames and
|
|
||||||
os.path.isfile(f1) and
|
|
||||||
os.path.isfile(f2)):
|
|
||||||
found.append(cs)
|
|
||||||
# print '+', cs.name
|
|
||||||
fnames[cs.name] = 1
|
|
||||||
else:
|
|
||||||
print_err('failed to parse cardset file: %s'
|
|
||||||
% f)
|
|
||||||
except Exception:
|
|
||||||
# traceback.print_exc()
|
|
||||||
pass
|
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
pass
|
traceback.print_exc()
|
||||||
|
continue
|
||||||
|
subdirs.sort()
|
||||||
|
for d in subdirs:
|
||||||
|
config_txt_path = os.path.join(d, "config.txt")
|
||||||
|
if not os.path.isfile(config_txt_path):
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
cs = self._readCardsetConfig(d, config_txt_path)
|
||||||
|
except Exception:
|
||||||
|
traceback.print_exc()
|
||||||
|
cs = None
|
||||||
|
if not cs:
|
||||||
|
print_err('failed to parse cardset file: %s'
|
||||||
|
% config_txt_path)
|
||||||
|
continue
|
||||||
|
back = cs.backnames[cs.backindex]
|
||||||
|
back_im_path = os.path.join(d, back)
|
||||||
|
shade_im_path = os.path.join(d, "shade" + cs.ext)
|
||||||
|
if (cs.name not in found_names and
|
||||||
|
cs.ext in IMAGE_EXTENSIONS and
|
||||||
|
cs.CARDD <= screendepth and
|
||||||
|
os.path.isfile(back_im_path) and
|
||||||
|
os.path.isfile(shade_im_path)):
|
||||||
|
found.append(cs)
|
||||||
|
found_names.append(cs.name)
|
||||||
|
|
||||||
# register cardsets
|
# register cardsets
|
||||||
for obj in found:
|
for obj in found:
|
||||||
if not manager.getByName(obj.name):
|
if not manager.getByName(obj.name):
|
||||||
|
|
|
@ -123,6 +123,8 @@ class ResourceManager:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def getSearchDirs(self, app, search, env=None):
|
def getSearchDirs(self, app, search, env=None):
|
||||||
|
"""Get a list of normalized directory paths. The returned list has no
|
||||||
|
duplicates."""
|
||||||
if isinstance(search, str):
|
if isinstance(search, str):
|
||||||
search = (search,)
|
search = (search,)
|
||||||
result = []
|
result = []
|
||||||
|
|
Loading…
Add table
Reference in a new issue