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