Use pathlib and appdirs modules
Better cross-OS support of paths and config/cache dirs.master
parent
b69d0b55e9
commit
f887f5fd84
|
@ -4,18 +4,23 @@ import datetime
|
|||
import html
|
||||
import json
|
||||
import logging
|
||||
import os, os.path
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
import lokrez.export as export
|
||||
import lokrez.resources as resources
|
||||
import lokrez.smashgg as smashgg
|
||||
import lokrez.version as version
|
||||
import appdirs
|
||||
|
||||
from . import export
|
||||
from . import resources
|
||||
from . import smashgg
|
||||
from . import version
|
||||
|
||||
# =============================================================================
|
||||
__version__ = version.__version__
|
||||
__license__ = version.__license__
|
||||
|
||||
ROOTDIR = os.path.dirname(os.path.abspath(__file__))
|
||||
ROOTDIR = pathlib.Path(__file__).absolute().parent
|
||||
|
||||
APPDIRS = appdirs.AppDirs(version.NAME, version.ENTITY)
|
||||
|
||||
# =============================================================================
|
||||
def main():
|
||||
|
@ -36,15 +41,10 @@ def main():
|
|||
help = "the proxy to use",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--token", "-t",
|
||||
default = None,
|
||||
help = "the authentication token to use",
|
||||
)
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
init_parser = subparsers.add_parser(
|
||||
"init",
|
||||
formatter_class = argparse.ArgumentDefaultsHelpFormatter,
|
||||
)
|
||||
|
||||
init_parser.add_argument(
|
||||
|
@ -55,13 +55,15 @@ def main():
|
|||
|
||||
init_parser.add_argument(
|
||||
"--imgdir", "-ID",
|
||||
default = os.path.join(ROOTDIR, "res"),
|
||||
type = pathlib.Path,
|
||||
default = pathlib.Path(APPDIRS.user_data_dir) / "res",
|
||||
help = "The directory we should download the resources to",
|
||||
)
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
top8_parser = subparsers.add_parser(
|
||||
"top8",
|
||||
formatter_class = argparse.ArgumentDefaultsHelpFormatter,
|
||||
)
|
||||
|
||||
top8_parser.add_argument(
|
||||
|
@ -69,21 +71,31 @@ def main():
|
|||
default = None,
|
||||
help = "The tournament slug or id",
|
||||
)
|
||||
|
||||
top8_parser.add_argument(
|
||||
"--token", "-t",
|
||||
default = None,
|
||||
help = "the authentication token to use",
|
||||
)
|
||||
|
||||
top8_parser.add_argument(
|
||||
"--imgdir", "-ID",
|
||||
default = os.path.join(ROOTDIR, "res"),
|
||||
type = pathlib.Path,
|
||||
default = pathlib.Path(APPDIRS.user_data_dir) / "res",
|
||||
help = "The directories containing images, be careful whether " \
|
||||
"you specify an absolute path or a relative one.",
|
||||
)
|
||||
top8_parser.add_argument(
|
||||
"--playerskinsdb", "-PD",
|
||||
default = os.path.join(ROOTDIR, "data", "playerskinsdb.json"),
|
||||
type = pathlib.Path,
|
||||
default = ROOTDIR / "data" / "playerskinsdb.json",
|
||||
help = "A JSON file matching player tags, characters and " \
|
||||
"preferred skins",
|
||||
)
|
||||
top8_parser.add_argument(
|
||||
"--templatesdir", "-TD",
|
||||
default = os.path.join(ROOTDIR, "templates"),
|
||||
type = pathlib.Path,
|
||||
default = ROOTDIR / "templates",
|
||||
help = "The local result templates directory",
|
||||
)
|
||||
top8_parser.add_argument(
|
||||
|
@ -93,15 +105,17 @@ def main():
|
|||
)
|
||||
top8_parser.add_argument(
|
||||
"--lkrz-file", "-f",
|
||||
type = pathlib.Path,
|
||||
default = None,
|
||||
help = "The lkrz file in which the results are stored ; if it " \
|
||||
"does not exist, one will be created from the smashgg data",
|
||||
)
|
||||
top8_parser.add_argument(
|
||||
"--outfile", "-o",
|
||||
type = pathlib.Path,
|
||||
default = None,
|
||||
help = "The SVG local result file to output to ; defaults to " \
|
||||
"./tournament-slug.svg",
|
||||
help = "The SVG or PNG local result file to output to ; if it's " \
|
||||
"not specified, it will use the tournament slug as name",
|
||||
)
|
||||
top8_parser.add_argument(
|
||||
"--name-seo-delimiter",
|
||||
|
@ -161,8 +175,10 @@ def main():
|
|||
|
||||
# -------------------------------------------------------------------------
|
||||
if args.command == "init":
|
||||
args.imgdir.mkdir(parents=True, exist_ok=True)
|
||||
resources.download_res_ssbu(
|
||||
dstdir = args.imgdir,
|
||||
proxy = args.proxy,
|
||||
log = log,
|
||||
)
|
||||
return 0
|
||||
|
@ -179,9 +195,9 @@ def main():
|
|||
tournament = None
|
||||
top_players = {}
|
||||
|
||||
try:
|
||||
if args.lkrz_file is not None:
|
||||
lkrz = configparser.ConfigParser()
|
||||
lkrz.read(args.lkrz_file)
|
||||
lkrz.read(str(args.lkrz_file))
|
||||
|
||||
log.info("Loading data from '{}'".format(args.lkrz_file))
|
||||
|
||||
|
@ -221,9 +237,7 @@ def main():
|
|||
|
||||
top_players[player.gamerTag] = player
|
||||
|
||||
except Exception as e:
|
||||
|
||||
log.warning(e)
|
||||
else:
|
||||
|
||||
# Get infos from smash.gg and write the config file
|
||||
tournament, top_players = getTournamentTop(
|
||||
|
@ -247,14 +261,18 @@ def main():
|
|||
)
|
||||
|
||||
if args.lkrz_file is None:
|
||||
args.lkrz_file = "{}.lkrz".format(tournament.slug)
|
||||
args.lkrz_file = pathlib.Path(
|
||||
"{}.lkrz".format(tournament.slug)
|
||||
)
|
||||
|
||||
with open(args.lkrz_file, "w", encoding="utf8") as f:
|
||||
with open(str(args.lkrz_file), "w", encoding="utf8") as f:
|
||||
f.write(lkrz_data)
|
||||
|
||||
# Default outfile is 'tournament-slug.svg'
|
||||
if args.outfile is None:
|
||||
args.outfile = "{}.svg".format(tournament.slug)
|
||||
args.outfile = pathlib.Path(
|
||||
"{}.svg".format(tournament.slug),
|
||||
)
|
||||
|
||||
# Build the context which will be passed to the template
|
||||
context = {
|
||||
|
@ -263,7 +281,7 @@ def main():
|
|||
top_players.values(),
|
||||
key = lambda p: p.placement,
|
||||
),
|
||||
"dir_res_ssbu": args.imgdir,
|
||||
"dir_res_ssbu": args.imgdir.as_posix(),
|
||||
}
|
||||
|
||||
rv = export.generate_outfile(
|
||||
|
@ -271,7 +289,8 @@ def main():
|
|||
args.template,
|
||||
context,
|
||||
args.outfile,
|
||||
log,
|
||||
log = log,
|
||||
cachedir = pathlib.Path(APPDIRS.user_cache_dir),
|
||||
)
|
||||
|
||||
if rv is None:
|
||||
|
@ -336,7 +355,7 @@ def getTournamentTop(
|
|||
"id" : int(id_or_slug), # If this fails, it's a slug
|
||||
"top": top,
|
||||
},
|
||||
query_dir = os.path.join( ROOTDIR, "queries" ),
|
||||
query_dir = ROOTDIR / "queries",
|
||||
token = token,
|
||||
proxy = proxy,
|
||||
log = log,
|
||||
|
@ -349,7 +368,7 @@ def getTournamentTop(
|
|||
"slug" : id_or_slug,
|
||||
"top": top,
|
||||
},
|
||||
query_dir = os.path.join( ROOTDIR, "queries" ),
|
||||
query_dir = ROOTDIR / "queries",
|
||||
token = token,
|
||||
proxy = proxy,
|
||||
log = log,
|
||||
|
@ -434,7 +453,7 @@ def getTournamentTop(
|
|||
"tournamentId" : int(tournament.id),
|
||||
"entrantIds": [ id for id in top_players.keys() ],
|
||||
},
|
||||
query_dir = os.path.join( ROOTDIR, "queries" ),
|
||||
query_dir = ROOTDIR / "queries",
|
||||
token = token,
|
||||
proxy = proxy,
|
||||
log = log,
|
||||
|
@ -488,3 +507,4 @@ def getTournamentTop(
|
|||
if __name__ == '__main__':
|
||||
rv = main()
|
||||
sys.exit(rv)
|
||||
|
||||
|
|
Loading…
Reference in New Issue