def loadArtifact(self, path): data = super(self.__class__, self).loadArtifact(path) self.template_data = anymarkup.parse(data, force_types=None) if "kind" in self.template_data and self.template_data["kind"].lower() == "template": if "parameters" in self.template_data: return anymarkup.serialize(self.template_data["parameters"], format="json") return data
def saveArtifact(self, path, data): if self.template_data: if "kind" in self.template_data and self.template_data["kind"].lower() == "template": if "parameters" in self.template_data: passed_data = anymarkup.parse(data, force_types=None) self.template_data["parameters"] = passed_data data = anymarkup.serialize(self.template_data, format=os.path.splitext(path)[1].strip(".")) #FIXME super(self.__class__, self).saveArtifact(path, data)
def echo_json(dir_name, use_yaml): file_name = '{}/.coverage'.format(get_dir_name(dir_name)) data = open(file_name).read() idx = data.find('{') data = data[idx:] data = json.loads(data) if use_yaml: data = anymarkup.serialize(data, 'yaml') print('[COUT] CO_YAML_CONTENT {}'.format(str(data)[1:])) else: print('[COUT] CO_JSON_CONTENT {}'.format(json.dumps(data)))
def get_j_config(self): """ Retrieve a job config from Jenkins. """ logger.info("Getting %s job config", self.job_name) job_config = self.server.get_job_config(self.job_name) # Convert to YAML job_config_yaml = anymarkup.serialize(anymarkup.parse(job_config), 'yaml') with open(self.config_path, 'w') as f: f.write(job_config_yaml) logger.info(" %s job config has been saved to %s", self.job_name, self.config_path)
def echo_xml(use_yaml): file = '/tmp/output.xml' if use_yaml: data = anymarkup.parse_file(file) data = anymarkup.serialize(data, 'yaml') print('[COUT] CO_YAML_CONTENT {}'.format(str(data)[1:])) return with open(file, 'rb') as f: data = f.read() print('[COUT] CO_XML_CONTENT {}'.format(str(data)[1:])) return True
def saveArtifact(self, path, data): if self.template_data: if "kind" in self.template_data and self.template_data[ "kind"].lower() == "template": if "parameters" in self.template_data: passed_data = anymarkup.parse(data, force_types=None) self.template_data["parameters"] = passed_data data = anymarkup.serialize( self.template_data, format=os.path.splitext(path)[1].strip(".")) #FIXME super(self.__class__, self).saveArtifact(path, data)
def echo_json(dir_name, use_yaml): for root, dirs, files in os.walk('{}/{}'.format(REPO_PATH, dir_name)): for file_name in files: if file_name.endswith('.json'): data = json.load(open(os.path.join(root, file_name))) if use_yaml: data = anymarkup.serialize(data, 'yaml') print('[COUT] CO_YAML_CONTENT {}'.format(str(data)[1:])) else: print('[COUT] CO_JSON_CONTENT {}'.format(json.dumps(data))) return True
def convert(filename, from_format, to_format, interpolate): """Parses stdin or a file and converts to the specified format""" # Try to parse the file and output in the specified format data = anymarkup.parse(filename, format=from_format, interpolate=interpolate) serialized = anymarkup.serialize(data, format=to_format) click.echo(serialized) # Exit if all done sys.exit(0)
def apply_pointers(self, content, params): """ Let's apply all the json pointers! Valid params in Nulecule: param1: - /spec/containers/0/ports/0/hostPort - /spec/containers/0/ports/0/hostPort2 or param1: - /spec/containers/0/ports/0/hostPort, /spec/containers/0/ports/0/hostPort2 Args: content (str): content of artifact file params (dict): list of params with pointers to replace in content Returns: str: content with replaced pointers Todo: In the future we need to change this to detect haml, yaml, etc as we add more providers Blocked by: github.com/bkabrda/anymarkup-core/blob/master/anymarkup_core/__init__.py#L393 """ obj = anymarkup.parse(content) if type(obj) != dict: logger.debug("Artifact file not json/haml, assuming it's $VARIABLE substitution") return content if params is None: # Nothing to do here! return content for name, pointers in params.items(): if not pointers: logger.warning("Could not find pointer for %s" % name) continue for pointer in pointers: try: resolve_pointer(obj, pointer) set_pointer(obj, pointer, name) logger.debug("Replaced %s pointer with %s param" % (pointer, name)) except JsonPointerException: logger.debug("Error replacing %s with %s" % (pointer, name)) logger.debug("Artifact content: %s", obj) raise NuleculeException("Error replacing pointer %s with %s." % (pointer, name)) return anymarkup.serialize(obj, format="json")
def echo_xml(use_yaml): for root, dirs, files in os.walk('/tmp/output'): for file_name in files: file = os.path.join(root, file_name) if use_yaml: data = anymarkup.parse_file(file) data = anymarkup.serialize(data, 'yaml') print('[COUT] CO_YAML_CONTENT {}'.format(str(data)[1:])) continue if file_name.endswith('.xml'): with open(file, 'rb') as f: data = f.read() print('[COUT] CO_XML_CONTENT {}'.format(str(data)[1:])) return True
def get_vos_xml(): """ Returns the serailized xml (as a string) """ to_output = {"VOSummary": {"VO": []}} vos = [] for file in os.listdir("virtual-organizations"): vo = anymarkup.parse_file("virtual-organizations/{0}".format(file)) vos.append(vo) to_output["VOSummary"]["VO"] = vos return anymarkup.serialize(to_output, 'xml').decode()
def pycco(file_name, use_yaml): code = open(file_name, 'rb').read().decode('utf-8') language = pyccoLib.get_language(file_name, code) sections = pyccoLib.parse(code, language) pyccoLib.highlight(sections, language, outdir="docs") data = { 'file_path': trim_repo_path(file_name), 'sections': sections } if use_yaml: data = anymarkup.serialize(data, 'yaml') print('[COUT] CO_YAML_CONTENT {}'.format(str(data)[1:])) else: print('[COUT] CO_JSON_CONTENT {}'.format(json.dumps(data))) return True
def get_projects_xml(): """ Returns the serailized xml (as a string) """ to_output = {"Projects": {"Project": []}} projects = [] for file in os.listdir("projects"): project = anymarkup.parse_file("projects/{0}".format(file)) projects.append(project) to_output["Projects"]["Project"] = projects return anymarkup.serialize(to_output, 'xml').decode()
def echo_xml(dir_name, use_yaml): if dir_name and dir_name != '.': dir_name = '{}/{}'.format(REPO_PATH, dir_name) else: dir_name = REPO_PATH for root, dirs, files in os.walk('{}/target'.format(dir_name)): for file_name in files: if file_name.endswith('.xml'): if use_yaml: data = anymarkup.parse_file(os.path.join(root, file_name)) data = anymarkup.serialize(data, 'yaml') print('[COUT] CO_YAML_CONTENT {}'.format(str(data)[1:])) continue with open(os.path.join(root, file_name), 'rb') as f: data = f.read() print('[COUT] CO_XML_CONTENT {}'.format(str(data)[1:])) return True
def echo_json(use_yaml): for root, dirs, files in os.walk('/tmp/output'): for file_name in files: if file_name.endswith('.html'): with open(os.path.join(root, file_name), 'r') as f: data = f.read() soup = BeautifulSoup(data, 'html.parser') title = soup.find('title').text body = soup.find('body').renderContents() data = { "title": title, "body": str(body, 'utf-8', errors='ignore'), "file": file_name } if use_yaml: data = anymarkup.serialize(data, 'yaml') print('[COUT] CO_YAML_CONTENT {}'.format( str(data)[1:])) else: print('[COUT] CO_JSON_CONTENT {}'.format( json.dumps(data))) return True
def _writeFromSchema(self, elements): for element in elements: value = element["value"] if not element["contents"] and not value: continue if element["name"] == "application": value = self.app_id print("Writing %s" % element["name"]) if element["type"] == "directory": if value: os.mkdir(value) os.chdir(value) self._writeFromSchema(element["contents"]) os.chdir("..") else: logger.debug("No value for directory %s", element["name"]) elif element["type"] == "file": with open(value, "w") as fp: logger.debug(element) if element["contents"]: if isinstance(element["contents"], str) or isinstance(element["contents"], unicode): fp.write(element["contents"]) elif isinstance(element["contents"], collections.Mapping): fp.write(anymarkup.serialize(self._generateContents(element["contents"]), format='yaml'))
import anymarkup y = """ <?xml version="1.0" encoding="UTF-8"?><vxml version="2.1"> <form> <block> <prompt xml:lang="en-in-female"> <break time="2s"/> Welcome to You Report! Please tell us what district you live in. </prompt> </block> </form> </vxml> """ z = anymarkup.serialize(y, 'json') print z x = anymarkup.serialize({'foo': 'bar'}, 'xml') print x
# Handy check that will ensure xmlns and xmlns:xi is set. # If it is not set, it will automatically be set to a sane value. # This also removes the need for the user to include openbox_config as # a top-level. if not args.skip_check: if INPUT_CONFIG_OBJ.get('openbox_config', False): INPUT_CONFIG_OBJ['openbox_config']['@xmlns'] = \ INPUT_CONFIG_OBJ['openbox_config'].get('@xmlns', XMLNS_DEFAULT) INPUT_CONFIG_OBJ['openbox_config']['@xmlns:xi'] = \ INPUT_CONFIG_OBJ['openbox_config'].get('@xmlns:xi', XMLNSXI_DEFAULT) NEW_OBJ = INPUT_CONFIG_OBJ else: INPUT_CONFIG_OBJ['@xmlns'] = \ INPUT_CONFIG_OBJ.get('@xmlns', XMLNS_DEFAULT) INPUT_CONFIG_OBJ['@xmlns:xi'] = \ INPUT_CONFIG_OBJ.get('@xmlns:xi', XMLNSXI_DEFAULT) NEW_OBJ['openbox_config'] = INPUT_CONFIG_OBJ try: DATA = anymarkup.serialize(NEW_OBJ, args.ext) except Exception as e: print(e) exit(1) if args.stdout: print(DATA.decode('utf-8')) else: save_bytes(DATA, OUTPUT_CONFIG)
def get_projects_xml(indir="../projects"): """Returns the serialized XML as a string""" return anymarkup.serialize(get_projects(indir), 'xml').decode()
def render(key, value): return anymarkup.serialize({key: value}, "yaml").decode("utf-8", errors="replace").strip()
def to_xml(data): return anymarkup.serialize(data, "xml").decode("utf-8")
def _data_of_obj(self, obj): # format = obj['_format'] # del obj['_format'] # Note: The two lines above can be replaced by expression below: data_format = obj.pop('_format') return anymarkup.serialize(obj, data_format).decode("utf-8")
def _data_of_obj(self, obj): file_format = obj['_format'] del obj['_format'] return anymarkup.serialize(obj, file_format).decode("utf-8")