diff --git a/overviewer_core/assetmanager.py b/overviewer_core/assetmanager.py index ed71fb8..c393e3e 100644 --- a/overviewer_core/assetmanager.py +++ b/overviewer_core/assetmanager.py @@ -27,6 +27,7 @@ import world import util from files import FileReplacer, mirror_dir, get_fs_caps + class AssetManager(object): """\ These objects provide an interface to metadata and persistent data, and at the @@ -36,9 +37,9 @@ There should only be one instances of these per execution. def __init__(self, outputdir, custom_assets_dir=None): """\ -Initializes the AssetManager with the top-level output directory. -It can read/parse and write/dump the overviewerConfig.js file into this top-level -directory. +Initializes the AssetManager with the top-level output directory. +It can read/parse and write/dump the overviewerConfig.js file into this +top-level directory. """ self.outputdir = outputdir self.custom_assets_dir = custom_assets_dir @@ -47,13 +48,16 @@ directory. self.fs_caps = get_fs_caps(self.outputdir) # look for overviewerConfig in self.outputdir + config_loc = os.path.join(self.outputdir, "overviewerConfig.js") try: - with open(os.path.join(self.outputdir, "overviewerConfig.js")) as c: - overviewerConfig_str = "{" + "\n".join(c.readlines()[1:-1]) + "}" - self.overviewerConfig = json.loads(overviewerConfig_str) + with open(config_loc) as c: + ovconf_str = "{" + "\n".join(c.readlines()[1:-1]) + "}" + self.overviewerConfig = json.loads(ovconf_str) except Exception, e: - if os.path.exists(os.path.join(self.outputdir, "overviewerConfig.js")): - logging.warning("A previous overviewerConfig.js was found, but I couldn't read it for some reason. Continuing with a blank config") + if os.path.exists(config_loc): + logging.warning("A previous overviewerConfig.js was found, " + "but I couldn't read it for some reason." + "Continuing with a blank config") logging.debug(traceback.format_exc()) self.overviewerConfig = dict(tilesets=dict()) @@ -73,7 +77,6 @@ directory. if conf['path'] == name: return conf return dict() - def initialize(self, tilesets): """Similar to finalize() but calls the tilesets' get_initial_data() @@ -92,11 +95,14 @@ directory. def _output_assets(self, tilesets, initial): if not initial: - get_data = lambda tileset: tileset.get_persistent_data() + def get_data(tileset): + return tileset.get_persistent_data() else: - get_data = lambda tileset: tileset.get_initial_data() + def get_data(tileset): + return tileset.get_initial_data() - # dictionary to hold the overviewerConfig.js settings that we will dumps + # dictionary to hold the overviewerConfig.js settings that we will dump + # to JSON using dumps dump = dict() dump['CONST'] = dict(tileSize=384) dump['CONST']['image'] = { @@ -109,15 +115,15 @@ directory. 'queryMarker2x': 'icons/marker_location_2x.png' } dump['CONST']['mapDivId'] = 'mcmap' - dump['CONST']['UPPERLEFT'] = world.UPPER_LEFT + dump['CONST']['UPPERLEFT'] = world.UPPER_LEFT dump['CONST']['UPPERRIGHT'] = world.UPPER_RIGHT - dump['CONST']['LOWERLEFT'] = world.LOWER_LEFT + dump['CONST']['LOWERLEFT'] = world.LOWER_LEFT dump['CONST']['LOWERRIGHT'] = world.LOWER_RIGHT dump['CONST']['image']['compass'] = { - world.UPPER_LEFT:'compass_upper-left.png', - world.UPPER_RIGHT:'compass_upper-right.png', - world.LOWER_LEFT:'compass_lower-left.png', - world.LOWER_RIGHT:'compass_lower-right.png' + world.UPPER_LEFT: 'compass_upper-left.png', + world.UPPER_RIGHT: 'compass_upper-right.png', + world.LOWER_LEFT: 'compass_lower-left.png', + world.LOWER_RIGHT: 'compass_lower-right.png' } # based on the tilesets we have, group them by worlds @@ -131,7 +137,7 @@ directory. dump['map'] = dict() dump['map']['debug'] = True dump['map']['cacheTag'] = str(int(time.time())) - dump['map']['north_direction'] = 'lower-left' # only temporary + dump['map']['north_direction'] = 'lower-left' # only temporary dump['map']['center'] = [-314, 67, 94] dump['map']['controls'] = { 'pan': True, @@ -143,51 +149,55 @@ directory. 'coordsBox': True, } - dump['tilesets'] = [] - for tileset in tilesets: dump['tilesets'].append(get_data(tileset)) # write a blank image - blank = Image.new("RGBA", (1,1), tileset.options.get('bgcolor')) - blank.save(os.path.join(self.outputdir, tileset.options.get('name'), "blank." + tileset.options.get('imgformat'))) + blank = Image.new("RGBA", (1, 1), tileset.options.get('bgcolor')) + blank.save(os.path.join(self.outputdir, + tileset.options.get('name'), "blank." + + tileset.options.get('imgformat'))) # write out config jsondump = json.dumps(dump, indent=4) - with FileReplacer(os.path.join(self.outputdir, "overviewerConfig.js"), capabilities=self.fs_caps) as tmpfile: + with FileReplacer(os.path.join(self.outputdir, "overviewerConfig.js"), + capabilities=self.fs_caps) as tmpfile: with codecs.open(tmpfile, 'w', encoding='UTF-8') as f: f.write("var overviewerConfig = " + jsondump + ";\n") - #Copy assets, modify index.html - self.output_noconfig() - + # Copy assets, modify index.html + self.output_noconfig() def output_noconfig(self): - # copy web assets into destdir: - global_assets = os.path.join(util.get_program_path(), "overviewer_core", "data", "web_assets") + global_assets = os.path.join(util.get_program_path(), + "overviewer_core", "data", "web_assets") if not os.path.isdir(global_assets): global_assets = os.path.join(util.get_program_path(), "web_assets") mirror_dir(global_assets, self.outputdir, capabilities=self.fs_caps) if self.custom_assets_dir: - # Could have done something fancy here rather than just overwriting - # the global files, but apparently this what we used to do pre-rewrite. - mirror_dir(self.custom_assets_dir, self.outputdir, capabilities=self.fs_caps) - - # write a dummy baseMarkers.js if none exists - if not os.path.exists(os.path.join(self.outputdir, "baseMarkers.js")): - with open(os.path.join(self.outputdir, "baseMarkers.js"), "w") as f: - f.write("// if you wants signs, please see genPOI.py\n"); + # We could have done something fancy here rather than just + # overwriting the global files, but apparently this what we used to + # do pre-rewrite. + mirror_dir(self.custom_assets_dir, self.outputdir, + capabilities=self.fs_caps) + # write a dummy baseMarkers.js if none exists + basemarkers_path = os.path.join(self.outputdir, "baseMarkers.js") + if not os.path.exists(basemarkers_path): + with open(basemarkers_path, "w") as f: + f.write("// if you wants signs, please see genPOI.py\n") # create overviewer.js from the source js files - js_src = os.path.join(util.get_program_path(), "overviewer_core", "data", "js_src") + js_src = os.path.join(util.get_program_path(), + "overviewer_core", "data", "js_src") if not os.path.isdir(js_src): js_src = os.path.join(util.get_program_path(), "js_src") - with FileReplacer(os.path.join(self.outputdir, "overviewer.js"), capabilities=self.fs_caps) as tmpfile: + with FileReplacer(os.path.join(self.outputdir, "overviewer.js"), + capabilities=self.fs_caps) as tmpfile: with open(tmpfile, "w") as fout: # first copy in js_src/overviewer.js with open(os.path.join(js_src, "overviewer.js"), 'r') as f: @@ -195,16 +205,20 @@ directory. # now copy in the rest for js in os.listdir(js_src): if not js.endswith("overviewer.js") and js.endswith(".js"): - with open(os.path.join(js_src,js)) as f: + with open(os.path.join(js_src, js)) as f: fout.write(f.read()) - + # Add time and version in index.html indexpath = os.path.join(self.outputdir, "index.html") index = codecs.open(indexpath, 'r', encoding='UTF-8').read() index = index.replace("{title}", "Minecraft Overviewer") - index = index.replace("{time}", time.strftime("%a, %d %b %Y %H:%M:%S %Z", time.localtime()).decode(self.preferredencoding)) - versionstr = "%s (%s)" % (util.findGitVersion(), util.findGitHash()[:7]) + index = index.replace("{time}", + time.strftime("%a, %d %b %Y %H:%M:%S %Z", + time.localtime()) + .decode(self.preferredencoding)) + versionstr = "%s (%s)" % (util.findGitVersion(), + util.findGitHash()[:7]) index = index.replace("{version}", versionstr) with FileReplacer(indexpath, capabilities=self.fs_caps) as indexpath: