0

Carpet-bombing code style fixes

This commit is contained in:
Nicolas F
2016-07-19 21:23:26 +02:00
committed by Andrew Chin
parent 99817f786f
commit 843d9ddb00

View File

@@ -27,6 +27,7 @@ import world
import util import util
from files import FileReplacer, mirror_dir, get_fs_caps from files import FileReplacer, mirror_dir, get_fs_caps
class AssetManager(object): class AssetManager(object):
"""\ """\
These objects provide an interface to metadata and persistent data, and at the These objects provide an interface to metadata and persistent data, and at the
@@ -37,8 +38,8 @@ There should only be one instances of these per execution.
def __init__(self, outputdir, custom_assets_dir=None): def __init__(self, outputdir, custom_assets_dir=None):
"""\ """\
Initializes the AssetManager with the top-level output 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 It can read/parse and write/dump the overviewerConfig.js file into this
directory. top-level directory.
""" """
self.outputdir = outputdir self.outputdir = outputdir
self.custom_assets_dir = custom_assets_dir self.custom_assets_dir = custom_assets_dir
@@ -47,13 +48,16 @@ directory.
self.fs_caps = get_fs_caps(self.outputdir) self.fs_caps = get_fs_caps(self.outputdir)
# look for overviewerConfig in self.outputdir # look for overviewerConfig in self.outputdir
config_loc = os.path.join(self.outputdir, "overviewerConfig.js")
try: try:
with open(os.path.join(self.outputdir, "overviewerConfig.js")) as c: with open(config_loc) as c:
overviewerConfig_str = "{" + "\n".join(c.readlines()[1:-1]) + "}" ovconf_str = "{" + "\n".join(c.readlines()[1:-1]) + "}"
self.overviewerConfig = json.loads(overviewerConfig_str) self.overviewerConfig = json.loads(ovconf_str)
except Exception, e: except Exception, e:
if os.path.exists(os.path.join(self.outputdir, "overviewerConfig.js")): 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.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()) logging.debug(traceback.format_exc())
self.overviewerConfig = dict(tilesets=dict()) self.overviewerConfig = dict(tilesets=dict())
@@ -74,7 +78,6 @@ directory.
return conf return conf
return dict() return dict()
def initialize(self, tilesets): def initialize(self, tilesets):
"""Similar to finalize() but calls the tilesets' get_initial_data() """Similar to finalize() but calls the tilesets' get_initial_data()
instead of get_persistent_data() to compile the generated javascript instead of get_persistent_data() to compile the generated javascript
@@ -92,11 +95,14 @@ directory.
def _output_assets(self, tilesets, initial): def _output_assets(self, tilesets, initial):
if not initial: if not initial:
get_data = lambda tileset: tileset.get_persistent_data() def get_data(tileset):
return tileset.get_persistent_data()
else: 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 = dict()
dump['CONST'] = dict(tileSize=384) dump['CONST'] = dict(tileSize=384)
dump['CONST']['image'] = { dump['CONST']['image'] = {
@@ -143,51 +149,55 @@ directory.
'coordsBox': True, 'coordsBox': True,
} }
dump['tilesets'] = [] dump['tilesets'] = []
for tileset in tilesets: for tileset in tilesets:
dump['tilesets'].append(get_data(tileset)) dump['tilesets'].append(get_data(tileset))
# write a blank image # write a blank image
blank = Image.new("RGBA", (1, 1), tileset.options.get('bgcolor')) 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.save(os.path.join(self.outputdir,
tileset.options.get('name'), "blank." +
tileset.options.get('imgformat')))
# write out config # write out config
jsondump = json.dumps(dump, indent=4) 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: with codecs.open(tmpfile, 'w', encoding='UTF-8') as f:
f.write("var overviewerConfig = " + jsondump + ";\n") f.write("var overviewerConfig = " + jsondump + ";\n")
# Copy assets, modify index.html # Copy assets, modify index.html
self.output_noconfig() self.output_noconfig()
def output_noconfig(self): def output_noconfig(self):
# copy web assets into destdir: # 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): if not os.path.isdir(global_assets):
global_assets = os.path.join(util.get_program_path(), "web_assets") global_assets = os.path.join(util.get_program_path(), "web_assets")
mirror_dir(global_assets, self.outputdir, capabilities=self.fs_caps) mirror_dir(global_assets, self.outputdir, capabilities=self.fs_caps)
if self.custom_assets_dir: if self.custom_assets_dir:
# Could have done something fancy here rather than just overwriting # We could have done something fancy here rather than just
# the global files, but apparently this what we used to do pre-rewrite. # overwriting the global files, but apparently this what we used to
mirror_dir(self.custom_assets_dir, self.outputdir, capabilities=self.fs_caps) # do pre-rewrite.
mirror_dir(self.custom_assets_dir, self.outputdir,
capabilities=self.fs_caps)
# write a dummy baseMarkers.js if none exists # write a dummy baseMarkers.js if none exists
if not os.path.exists(os.path.join(self.outputdir, "baseMarkers.js")): basemarkers_path = os.path.join(self.outputdir, "baseMarkers.js")
with open(os.path.join(self.outputdir, "baseMarkers.js"), "w") as f: if not os.path.exists(basemarkers_path):
f.write("// if you wants signs, please see genPOI.py\n"); 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 # 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): if not os.path.isdir(js_src):
js_src = os.path.join(util.get_program_path(), "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: with open(tmpfile, "w") as fout:
# first copy in js_src/overviewer.js # first copy in js_src/overviewer.js
with open(os.path.join(js_src, "overviewer.js"), 'r') as f: with open(os.path.join(js_src, "overviewer.js"), 'r') as f:
@@ -203,8 +213,12 @@ directory.
index = codecs.open(indexpath, 'r', encoding='UTF-8').read() index = codecs.open(indexpath, 'r', encoding='UTF-8').read()
index = index.replace("{title}", "Minecraft Overviewer") 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)) index = index.replace("{time}",
versionstr = "%s (%s)" % (util.findGitVersion(), util.findGitHash()[:7]) 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) index = index.replace("{version}", versionstr)
with FileReplacer(indexpath, capabilities=self.fs_caps) as indexpath: with FileReplacer(indexpath, capabilities=self.fs_caps) as indexpath: