def loadlastfm(filename): if not os.path.exists(filename): print("File could not be found.") return if os.path.exists(datadir("scrobbles/lastfmimport.tsv")): overwrite = ask("Already imported Last.FM data. Overwrite?",default=False) if not overwrite: return print("Please wait...") from .lastfmconverter import convert convert(filename,datadir("scrobbles/lastfmimport.tsv")) #os.system("python3 -m maloja.lastfmconverter " + filename + " " + datadir("scrobbles/lastfmimport.tsv")) print("Successfully imported your Last.FM scrobbles!")
def loadlastfm(filename): if not os.path.exists(filename): print("File could not be found.") return if os.path.exists(data_dir['scrobbles']("lastfmimport.tsv")): overwrite = ask("Already imported Last.FM data. Overwrite?", default=False) if not overwrite: return print("Please wait...") from .lastfmconverter import convert convert(filename, data_dir['scrobbles']("lastfmimport.tsv")) print("Successfully imported your Last.FM scrobbles!")
def convert_mod(srcfolder, trgtfolder, trgtfile, modname=None, force=False): if modname == None: modname = os.path.basename(srcfolder) # check if mod already exists (hostile user :P) if os.path.exists(trgtfolder) and not os.path.exists( os.path.join(trgtfolder, METAFILE)) and not ask( "Mod " + modname + " already exists, but is not managed by Suvorov. Overwrite?"): print("Mod was not created.") return False # gather mod data srcmodfile = os.path.join(srcfolder, "modinfo.yml") modmetadata = {} if os.path.exists(srcmodfile): with open(srcmodfile) as modfile: modmetadata.update(yaml.safe_load(modfile.read())) if "name" not in modmetadata: modmetadata["name"] = modname modmetadata["path"] = os.path.join(os.path.basename(VANILLAMODFOLDER), os.path.basename(trgtfolder)) # write modfile with open(trgtfile, "w") as modfile: for e in modmetadata: modfile.write(e + ' = "' + modmetadata[e] + '"\n') os.chdir(srcfolder) # go to the folder instead of path.join so that all names we print / save are relative to it # find out if original source files are still the same try: with open(os.path.join(trgtfolder, METAFILE)) as suvorovfile: modinfo = yaml.safe_load(suvorovfile.read()) assert modinfo is not None and "times" in modinfo and "results" in modinfo if force: modinfo["times"] = {f: 0 for f in modinfo["times"]} except: modinfo = {"times": {}, "results": {}} # all source files with their modify date srcfiles = {} for (dirpath, dirnames, filenames) in os.walk("."): for f in filenames: if f.startswith("."): continue pth = os.path.join(dirpath, f) srcfiles[pth] = os.path.getmtime(pth) if "modinfo.yml" in srcfiles: srcfiles.pop("modinfo.yml") if "./modinfo.yml" in srcfiles: srcfiles.pop("./modinfo.yml") # check changed / removed files removed_files = [f for f in modinfo["results"] if f not in srcfiles] changed_files = [ f for f in srcfiles if f not in modinfo["times"] or modinfo["times"][f] != srcfiles[f] ] # has any data changed? data_changed = any((f.split(".")[-1].lower() in DATA_FILE_EXTENSIONS) for f in changed_files + removed_files) # clear data files from the file lists (we don't convert them directly into mod files) removed_files = [ f for f in removed_files if f.split(".")[-1].lower() not in DATA_FILE_EXTENSIONS ] changed_files = [ f for f in changed_files if f.split(".")[-1].lower() not in DATA_FILE_EXTENSIONS ] # we need to keep track if gfx files have been changed in order to recreate sprite definitions etc changed_gfx_files = {f: False for f in gfx_types} # if data has changed, all suv files need to be reevaluated if data_changed: changed_files = list( set(changed_files + [ f for f in srcfiles if f.split(".")[-1].lower() in SUV_FILE_EXTENSIONS ])) for f in removed_files: for rf in modinfo["results"][f]: print("Removing", col["red"](rf), "as its source", col["red"](f), "no longer exists.") try: os.remove(os.path.join(trgtfolder, rf)) except FileNotFoundError: pass modinfo["results"].pop(f) # if we evaluate any suv files (bc they have changed OR data itself has been changed), we need to load all data! if any( f.split(".")[-1].lower() in SUV_FILE_EXTENSIONS for f in changed_files): data = {} for f in srcfiles: if f.split(".")[-1].lower() in DATA_FILE_EXTENSIONS: with open(f, "r") as ymlfile: content = yaml.safe_load(ymlfile.read()) if "data" in content: data.update(content["data"]) print("Loaded data from", col["purple"](f)) modinfo["times"][f] = srcfiles[f] # parse all the files for f in changed_files: # f is the path relative to the (suvorov) mod folder # naming convention for below: # relativepath path relative to relevant suvorov-structural subfolder # target file to be written relative to result mod folder # targetfolder folder to be written inside relative to result mod folder # fulltarget absolute path of target file # name name of the file # rawname name of the file without extensions # # get all files that have been created from this file old_createdfiles = modinfo["results"].get(f) or [] new_createdfiles = [] #check if suvorov file fullpath = os.path.realpath(f) #suvdir = os.path.realpath(SRCFOLDER) vanilladir = os.path.realpath(VANILLASRCFOLDER) gfxdir = os.path.realpath(GFXSRCFOLDER) # VANILLA FILE - COPY AS IS if os.path.commonprefix([fullpath, vanilladir]) == vanilladir: relativepath = os.path.relpath(f, start=vanilladir) print("Copying", col["green"](relativepath)) target = os.path.join(trgtfolder, relativepath) os.makedirs(os.path.dirname(target), exist_ok=True) shutil.copyfile(f, target) new_createdfiles.append(f) # GFX FILE - convert, add gfx definition file elif os.path.commonprefix([fullpath, gfxdir]) == gfxdir: relativepath = os.path.relpath(f, start=gfxdir) print("Loading GFX file", col["yellow"](relativepath)) folder, name = relativepath.split("/") rawname = name.split(".")[0] targetfolder, gfxtype = gfx_types[folder] target = os.path.join(targetfolder, rawname + ".dds") fulltarget = os.path.join(trgtfolder, target) img = load_from_file(fullpath) img = crop(img, type=gfxtype) print("\tCreating", col["green"](target)) os.makedirs(os.path.join(trgtfolder, targetfolder), exist_ok=True) write_to_dds_file(img, fulltarget) new_createdfiles.append(target) changed_gfx_files[folder] = True # DATA FILE - interpret, write in correct folders else: identifier = os.path.relpath(os.path.splitext(f)[0], start=SUVOROVSRCFOLDER).replace( "/", ".") ext = f.split(".")[-1].lower() if ext in TXT_FILE_EXTENSIONS + SUV_FILE_EXTENSIONS: print("Parsing", col["yellow"](f)) if ext in TXT_FILE_EXTENSIONS: with open(f) as fh: entries, individual_files = process_txt_file(fh.read()) loc_keys = {} #elif ext in YML_FILE_EXTENSIONS: # with open(f) as fh: # entries,individual_files = process_svy_file(fh.read()) # loc_keys = {} if ext in SUV_FILE_EXTENSIONS: with open(f) as fh: entries, individual_files, loc_keys = process_suv_file( fh.read(), data) for folder in entries: os.makedirs(os.path.join(trgtfolder, folder), exist_ok=True) target = os.path.join(folder, identifier) + ".txt" fulltarget = os.path.join(trgtfolder, target) # check if we're allowed to edit this file or it has been created by someone else canown = (not os.path.exists(fulltarget)) or ( target in modinfo["results"].get(f, [])) if canown or ask("File " + col["yellow"]( target ) + " exists, but is not tied to this source file! Overwrite?" ): print("\tCreating", col["green"](target)) new_createdfiles.append(target) with open(fulltarget, "w", encoding=ENCODING) as tf: tf.write("\n".join(entries[folder])) else: print("\tDid not create", col["red"](target)) for target in individual_files: directory = os.path.dirname(target) os.makedirs(os.path.join(trgtfolder, directory), exist_ok=True) fulltarget = os.path.join(trgtfolder, target) # check if we're allowed to edit this file or it has been created by someone else canown = (not os.path.exists(fulltarget)) or ( target in modinfo["results"].get(f, [])) if canown or ask("File " + col["yellow"]( target ) + " exists, but is not tied to this source file! Overwrite?" ): print("\tCreating", col["green"](target)) new_createdfiles.append(target) with open(fulltarget, "w", encoding=ENCODING) as tf: tf.write(individual_files[target]) else: print("\tDid not create", col["red"](target)) if len(loc_keys) != 0: target = os.path.join("localisation", identifier + ".csv") fulltarget = os.path.join(trgtfolder, target) os.makedirs(os.path.join(trgtfolder, "localisation"), exist_ok=True) canown = (not os.path.exists(fulltarget)) or ( target in modinfo["results"].get(f, [])) if canown or ask("File " + col["yellow"]( target ) + " exists, but is not tied to this source file! Overwrite?" ): print("\tCreating", col["green"](target)) new_createdfiles.append(target) with open(fulltarget, "w", encoding=ENCODING) as tf: writer = csv.writer(tf, delimiter=";") for key in loc_keys: writer.writerow([key] + [loc_keys[key]] + [""] * 12 + ["x"]) else: print("\tDid not create", col["red"](target)) else: print("File", col["red"](f), "will be ignored...") modinfo["times"][f] = srcfiles[f] modinfo["results"][f] = list(set(new_createdfiles)) for fr in [ fil for fil in old_createdfiles if fil not in new_createdfiles ]: print("Removing", col["red"](fr), "as its no longer created by source", col["red"](f)) # GFX DEFINITIONS if changed_gfx_files["traits"]: sprites = [] for f in os.listdir(os.path.join(srcfolder, "gfx", "traits")): name = f.split(".")[0] sprites.append(("spriteType", "=", [ ("name", "=", "GFX_trait_" + name), ("texturefile", "=", "gfx\\\\traits\\\\" + name + ".dds"), ("noOfFrames", "=", 1), ("norefcount", "=", "yes"), ("effectFile", "=", "gfx/FX/buttonstate.lua") ])) defin = CK2Definition([("spriteTypes", "=", sprites)]) gfxfilefolder = os.path.join("interface") gfxfile = os.path.join(gfxfilefolder, modname + "_svrvtraits.gfx") os.makedirs(os.path.join(trgtfolder, gfxfilefolder), exist_ok=True) print("Creating", col["green"](gfxfile)) defin.write(os.path.join(trgtfolder, gfxfile), format="ck2") # print suvorov info data with open(os.path.join(trgtfolder, METAFILE), "w") as suvorovfile: yaml.dump(modinfo, suvorovfile)
def setup(): copy_initial_local_files() SKIP = settings.get_settings("SKIP_SETUP") print("Various external services can be used to display images. If not enough of them are set up, only local images will be used.") for k in apikeys: key = settings.get_settings(k) if key is None: print("\t" + "Currently not using a " + apikeys[k] + " for image display.") elif key == "ASK": print("\t" + "Please enter your " + apikeys[k] + ". If you do not want to use one at this moment, simply leave this empty and press Enter.") key = prompt("",types=(str,),default=None,skip=SKIP) settings.update_settings(data_dir['settings']("settings.ini"),{k:key},create_new=True) else: print("\t" + apikeys[k] + " found.") # OWN API KEY if os.path.exists(data_dir['clients']("authenticated_machines.tsv")): pass else: answer = ask("Do you want to set up a key to enable scrobbling? Your scrobble extension needs that key so that only you can scrobble tracks to your database.",default=True,skip=SKIP) if answer: key = randomstring(64) print("Your API Key: " + col["yellow"](key)) with open(data_dir['clients']("authenticated_machines.tsv"),"w") as keyfile: keyfile.write(key + "\t" + "Default Generated Key") else: pass # PASSWORD defaultpassword = settings.get_settings("DEFAULT_PASSWORD") forcepassword = settings.get_settings("FORCE_PASSWORD") # this is mainly meant for docker, supply password via environment variable if forcepassword is not None: # user has specified to force the pw, nothing else matters auth.defaultuser.setpw(forcepassword) print("Password has been set.") elif auth.defaultuser.checkpw("admin"): # if the actual pw is admin, it means we've never set this up properly (eg first start after update) if defaultpassword is None: # non-docker installation or user didn't set environment variable defaultpassword = randomstring(32) newpw = prompt("Please set a password for web backend access. Leave this empty to generate a random password.",skip=SKIP,secret=True) if newpw is None: newpw = defaultpassword print("Generated password:"******"Please set a password for web backend access. Leave this empty to use the default password.",skip=SKIP,default=defaultpassword,secret=True) auth.defaultuser.setpw(newpw) if settings.get_settings("NAME") is None: name = prompt("Please enter your name. This will be displayed e.g. when comparing your charts to another user. Leave this empty if you would not like to specify a name right now.",default="Generic Maloja User",skip=SKIP) settings.update_settings(data_dir['settings']("settings.ini"),{"NAME":name},create_new=True) if settings.get_settings("SEND_STATS") is None: answer = ask("I would like to know how many people use Maloja. Would it be okay to send a daily ping to my server (this contains no data that isn't accessible via your web interface already)?",default=True,skip=SKIP) if answer: settings.update_settings(data_dir['settings']("settings.ini"),{"SEND_STATS":True,"PUBLIC_URL":None},create_new=True) else: settings.update_settings(data_dir['settings']("settings.ini"),{"SEND_STATS":False},create_new=True)
def setup(): copy_initial_local_files() SKIP = settings.get_settings("SKIP_SETUP") print( "Various external services can be used to display images. If not enough of them are set up, only local images will be used." ) for k in apikeys: key = settings.get_settings(k) if key is None: print("\t" + "Currently not using a " + apikeys[k] + " for image display.") elif key == "ASK": print( "\t" + "Please enter your " + apikeys[k] + ". If you do not want to use one at this moment, simply leave this empty and press Enter." ) key = prompt("", types=(str, ), default=None, skip=SKIP) settings.update_settings(datadir("settings/settings.ini"), {k: key}, create_new=True) else: print("\t" + apikeys[k] + " found.") # OWN API KEY if os.path.exists(datadir("clients/authenticated_machines.tsv")): pass else: answer = ask( "Do you want to set up a key to enable scrobbling? Your scrobble extension needs that key so that only you can scrobble tracks to your database.", default=True, skip=SKIP) if answer: import random key = "" for i in range(64): key += str( random.choice( list(range(10)) + list("abcdefghijklmnopqrstuvwxyz") + list("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))) print("Your API Key: " + col["yellow"](key)) with open(datadir("clients/authenticated_machines.tsv"), "w") as keyfile: keyfile.write(key + "\t" + "Default Generated Key") else: pass if settings.get_settings("NAME") is None: name = prompt( "Please enter your name. This will be displayed e.g. when comparing your charts to another user. Leave this empty if you would not like to specify a name right now.", default="Generic Maloja User", skip=SKIP) settings.update_settings(datadir("settings/settings.ini"), {"NAME": name}, create_new=True) if settings.get_settings("SEND_STATS") is None: answer = ask( "I would like to know how many people use Maloja. Would it be okay to send a daily ping to my server (this contains no data that isn't accessible via your web interface already)?", default=True, skip=SKIP) if answer: settings.update_settings(datadir("settings/settings.ini"), { "SEND_STATS": True, "PUBLIC_URL": None }, create_new=True) else: settings.update_settings(datadir("settings/settings.ini"), {"SEND_STATS": False}, create_new=True)