Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self,savedata,filename,ident,filesystem,save_type=SaveType.workshop):
log=tts.logger()
self.data = savedata
self.ident=ident
if self.data['SaveName']:
self.save_name=self.data['SaveName']
else:
self.save_name=self.ident
self.save_type=save_type
self.filesystem = filesystem
self.filename=filename
#strip the local part off.
fileparts=self.filename.split(os.path.sep)
while fileparts[0]!='Saves' and fileparts[0]!='Mods':
fileparts=fileparts[1:]
self.basename=os.path.join(*fileparts)
log.debug("filename: {},save_name: {}, basename: {}".format(self.filename,self.save_name,self.basename))
self.urls = [ Url(url,self.filesystem) for url in get_save_urls(savedata) ]
def download(self):
log=tts.logger()
if self.exists:
return True
url=self.url
protocols=url.split('://')
if len(protocols)==1:
log.warn("Missing protocol for {}. Assuming http://.".format(url))
url = "http://" + url
log.info("Downloading data for %s." % url)
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
request=urllib.request.Request(url,headers=headers)
try:
response=urllib.request.urlopen(request)
except urllib.error.URLError as e:
log.error("Error downloading %s (%s)" % (url,e))
return False
def get_filenames_in(self,search_path):
if not os.path.isdir(search_path):
tts.logger().warn("Tried to search non-existent path {}.".format(search_path))
return []
return [os.path.splitext(file)[0] for file in os.listdir(search_path) if os.path.splitext(file)[1].lower()=='.json']
def __init__(self,root):
self.log=tts.logger()
self.log.setLevel(logging.WARN)
self.preferences=tts.preferences.Preferences()
self.root=root
if self.preferences.firstRun:
messagebox.showinfo("TTS Manager","First run detected.\nOpening preferences pane.")
self.showPreferences()
if not self.preferences.validate():
messagebox.showwarning("TTS Manager","Invalid preferences detected.\nOpening preferences pane.")
self.showPreferences()
self.reload_filesystem()
mode_notebook = ttk.Notebook(root)
list_frame = ttk.Frame(mode_notebook)
self.populate_list_frame(list_frame)
def change_log_level(self,event):
levels=[logging.DEBUG,logging.INFO,logging.WARN,logging.ERROR]
tts.logger().info("Setting log level to %s" % levels[self.loggerLevel.current()])
tts.logger().setLevel(levels[self.loggerLevel.current()])
def __init__(self,root):
self.log=tts.logger()
self.log.setLevel(logging.WARN)
self.preferences=tts.preferences.Preferences()
self.root=root
Tk.Grid.rowconfigure(self.root,0,weight=1)
Tk.Grid.columnconfigure(self.root,0,weight=1)
self.log_window=tts.TTS_LOGGER(root)
if self.preferences.firstRun:
messagebox.showinfo("TTS Manager","First run detected.\nOpening preferences pane.")
self.showPreferences()
if not self.preferences.validate():
messagebox.showwarning("TTS Manager","Invalid preferences detected.\nOpening preferences pane.")
self.showPreferences()
self.filesystem=self.preferences.get_filesystem()
def check_dirs(self):
"""Do all the directories exist?"""
for dir in [ self._saves, self._chest, self._mods, self._images, self._models, self._workshop ]:
if not os.path.isdir(dir):
tts.logger().warn("TTS Dir missing: {}".format(dir))
return False
return True
if not data:
return 1, "Unable to load data for file %s" % json_filename
save=tts.Save(savedata=data,
filename=json_filename,
ident=args.id,
save_type=args.save_type,
filesystem=self.filesystem)
if not save.isInstalled:
if not args.download:
return 1, "Unable to find all urls required by %s. Rerun with -d to try and download them or open it within TTS.\n%s" % (args.id,save)
else:
tts.logger().info("Downloading missing files...")
successful = save.download()
if successful:
tts.logger().info("Files downloaded successfully.")
else:
return 1, "Some files failed to download"
if os.path.isfile(filename) and not args.force:
return 1,"%s already exists. Please specify another file or use '-f'" % filename
tts.logger().info("Exporting json file %s to %s" % (args.id,filename))
save.export(filename)
# TODO: exception handling
return 0,"Exported %s to %s" % (args.id,filename)
def get_save_urls(savedata):
'''
Iterate over all the values in the json file, building a (key,value) set of
all the values whose key ends in "URL"
'''
log=tts.logger()
def parse_list(data):
urls=set()
for item in data:
urls |= get_save_urls(item)
return urls
def parse_dict(data):
urls=set()
if not data:
return urls
for key in data:
if type(data[key]) is not str or key=='PageURL' or key=='Rules':
# If it isn't a string, it can't be an url.
# Also don't save tablet state / rulebooks
continue
if key.endswith('URL') and data[key]!='':
log.debug("Found {}:{}".format(key,data[key]))
def importPak(filesystem,filename):
log=tts.logger()
log.debug("About to import {} into {}.".format(filename,filesystem))
if not os.path.isfile(filename):
log.error("Unable to find mod pak {}".format(filename))
return False
if not zipfile.is_zipfile(filename):
log.error("Mod pak {} format appears corrupt.".format(filename))
return False
try:
with zipfile.ZipFile(filename,'r') as zf:
bad_file=zf.testzip()
if bad_file:
log.error("At least one corrupt file found in {} - {}".format(filename,bad_file))
return False
if not zf.comment:
# TODO: allow overrider
log.error("Missing pak header comment in {}. Aborting import.".format(filename))