def create_style(sync_job, task_metadata, task_status): """ This is not a critical task. """ try: output_name = os.path.join(CACHE_PATH, "{}.sld".format(sync_job["name"])) with open(output_name) as f: sld_data = f.read() # kludge to match for SLD 1.1 style_format = "sld10" if 'version="1.1.0"' in sld_data: style_format = "sld11" gs.create_style( name=sync_job["name"], data=sld_data, workspace=sync_job["workspace"], style_format=style_format ) s_gs = gs.get_style(name=sync_job["name"], workspace=sync_job["workspace"]) feature = get_feature(sync_job) feature.default_style = s_gs gs.save(feature) except: message = traceback.format_exc() task_status.set_message("message", message) logger.error("Create style failed ({0}) failed.{1}".format(task_style_name(sync_job), message))
def create_style(sync_job, task_metadata, task_status): """ This is not a critical task. """ default_style = None created_styles = [] failed_messages = [] style_name = None messages = [] default_style_name = geoserver_style_name(sync_job, sync_job.get("default_style", None)) # create styles for name, style in sync_job["styles"].iteritems(): style_name = geoserver_style_name(sync_job, name) try: with open(style["local_file"]) as f: sld_data = f.read() # kludge to match for SLD 1.1 style_format = "sld10" if 'version="1.1.0"' in sld_data: style_format = "sld11" gs.create_style(name=style_name, data=sld_data, workspace=sync_job["workspace"], style_format=style_format) s_gs = gs.get_style(name=style_name, workspace=sync_job["workspace"]) if s_gs.name == default_style_name: default_style = s_gs else: created_styles.append(s_gs) except: message = traceback.format_exc() logger.error("Create style failed ({}) failed. {}".format(task_style_name(sync_job), message)) failed_messages.append("Failed to create style {}. {}".format(style_name, message)) if not default_style and created_styles: # default style is not set, set the default style to the first created styles. default_style = created_styles[0] del created_styles[0] if default_style: if created_styles: messages.append( "Succeed to create styles ({}, {}).".format( default_style.name, ", ".join([s.name for s in created_styles]) ) ) else: messages.append("Succeed to create style ({}).".format(default_style.name)) # try to set feature's styles if default_style: try: feature = get_feature(sync_job) feature.default_style = default_style if created_styles: feature.styles = created_styles gs.save(feature) messages.append("Succeed to set default style ({}).".format(default_style.name)) if created_styles: messages.append( "Succeed to set alternative styles ({}).".format(", ".join([s.name for s in created_styles])) ) except: message = traceback.format_exc() logger.error( "Failed to set default style({}) and alternative styles ({}).{}".format( default_style.name, ", ".join([s.name for s in created_styles]), message ) ) failed_messages.append( "Failed to set default style ({}) and alternative styles ({}). {}".format( default_style.name, ", ".join([s.name for s in created_styles]), message ) ) # set messages if default_style: task_status.set_message("message", os.linesep.join(messages + failed_messages)) else: task_status.set_message("message", "No style are required to create.")