Python yaml.CSafeDumper() Examples
The following are 10
code examples of yaml.CSafeDumper().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
yaml
, or try the search function
.
Example #1
Source File: utils.py From fusesoc with BSD 2-Clause "Simplified" License | 5 votes |
def yaml_fwrite(filepath, content, preamble=""): with open(filepath, "w") as f: f.write(preamble) f.write(yaml.dump(content, Dumper=YamlDumper))
Example #2
Source File: tpm_abstract.py From keylime with BSD 2-Clause "Simplified" License | 5 votes |
def __write_tpm_data(self): os.umask(0o077) if os.geteuid() != 0 and common.REQUIRE_ROOT: logger.warning("Creating tpm metadata file without root. Sensitive trust roots may be at risk!") with open('tpmdata.yml', 'w') as f: yaml.dump(self.global_tpmdata, f, Dumper=SafeDumper)
Example #3
Source File: ca_util.py From keylime with BSD 2-Clause "Simplified" License | 5 votes |
def write_private(inp): priv = inp[0] salt = inp[1] global global_password priv_encoded = yaml.dump(priv, Dumper=SafeDumper) key = crypto.kdf(global_password,salt) ciphertext = crypto.encrypt(priv_encoded,key) towrite = {'salt':salt,'priv':ciphertext} with os.fdopen(os.open('private.yml',os.O_WRONLY | os.O_CREAT,0o600), 'w') as f: yaml.dump(towrite,f, Dumper=SafeDumper)
Example #4
Source File: default_handlers.py From python-frontmatter with MIT License | 5 votes |
def export(self, metadata, **kwargs): """ Export metadata as YAML. This uses yaml.SafeDumper by default. """ kwargs.setdefault("Dumper", SafeDumper) kwargs.setdefault("default_flow_style", False) kwargs.setdefault("allow_unicode", True) metadata = yaml.dump(metadata, **kwargs).strip() return u(metadata) # ensure unicode
Example #5
Source File: file_utils.py From mlflow with Apache License 2.0 | 5 votes |
def write_yaml(root, file_name, data, overwrite=False): """ Write dictionary data in yaml format. :param root: Directory name. :param file_name: Desired file name. Will automatically add .yaml extension if not given :param data: data to be dumped as yaml format :param overwrite: If True, will overwrite existing files """ if not exists(root): raise MissingConfigException("Parent directory '%s' does not exist." % root) file_path = os.path.join(root, file_name) yaml_file_name = file_path if file_path.endswith(".yaml") else file_path + ".yaml" if exists(yaml_file_name) and not overwrite: raise Exception("Yaml file '%s' exists as '%s" % (file_path, yaml_file_name)) try: with codecs.open(yaml_file_name, mode='w', encoding=ENCODING) as yaml_file: yaml.dump(data, yaml_file, default_flow_style=False, allow_unicode=True, Dumper=YamlSafeDumper) except Exception as e: raise e
Example #6
Source File: yamlwrapper.py From treadmill with Apache License 2.0 | 5 votes |
def dump(*args, **kwargs): """Delegate to yaml dumps. """ if kwargs is None: kwargs = {} kwargs['Dumper'] = Dumper return yaml.dump(*args, **kwargs)
Example #7
Source File: test_storage.py From operator with Apache License 2.0 | 5 votes |
def test_is_c_dumper(self): dumper = storage._SimpleDumper(io.StringIO('')) if getattr(yaml, 'CSafeDumper', None) is not None: self.assertIsInstance(dumper, yaml.CSafeDumper) else: self.assertIsInstance(dumper, yaml.SafeDumper)
Example #8
Source File: custom_yaml.py From INGInious with GNU Affero General Public License v3.0 | 4 votes |
def dump(data, stream=None, **kwds): """ Serialize a Python object into a YAML stream. If stream is None, return the produced string instead. Dict keys are produced in the order in which they appear in OrderedDicts. Safe version. If objects are not "conventional" objects, they will be dumped converted to string with the str() function. They will then not be recovered when loading with the load() function. """ # Display OrderedDicts correctly class OrderedDumper(SafeDumper): pass def _dict_representer(dumper, data): return dumper.represent_mapping( original_yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, list(data.items())) # Display long strings correctly def _long_str_representer(dumper, data): if data.find("\n") != -1: # Drop some unneeded data # \t are forbidden in YAML data = data.replace("\t", " ") # empty spaces at end of line are always useless in INGInious, and forbidden in YAML data = "\n".join([p.rstrip() for p in data.split("\n")]) return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|') else: return dumper.represent_scalar('tag:yaml.org,2002:str', data) # Default representation for some odd objects def _default_representer(dumper, data): return _long_str_representer(dumper, str(data)) OrderedDumper.add_representer(str, _long_str_representer) OrderedDumper.add_representer(str, _long_str_representer) OrderedDumper.add_representer(OrderedDict, _dict_representer) OrderedDumper.add_representer(None, _default_representer) s = original_yaml.dump(data, stream, OrderedDumper, encoding='utf-8', allow_unicode=True, default_flow_style=False, indent=4, **kwds) if s is not None: return s.decode('utf-8') else: return
Example #9
Source File: storage.py From rednotebook with GNU General Public License v2.0 | 4 votes |
def _save_month_to_disk(month, journal_dir): """ When overwriting 2014-12.txt: write new content to 2014-12.new.txt cp 2014-12.txt 2014-12.old.txt mv 2014-12.new.txt 2014-12.txt rm 2014-12.old.txt """ content = {} for day_number, day in month.days.items(): if not day.empty: content[day_number] = day.content def get_filename(infix): year_and_month = format_year_and_month(month.year_number, month.month_number) return os.path.join(journal_dir, "{}{}.txt".format(year_and_month, infix)) old = get_filename(".old") new = get_filename(".new") filename = get_filename("") # Do not save empty month files. if not content and not os.path.exists(filename): return False with codecs.open(new, "wb", encoding="utf-8") as f: # Write readable unicode and no Python directives. yaml.dump(content, f, Dumper=Dumper, allow_unicode=True) if os.path.exists(filename): mtime = os.path.getmtime(filename) if mtime != month.mtime: conflict = get_filename(".CONFLICT_BACKUP" + str(mtime)) logging.debug( "Last edit time of %s conflicts with edit time at file load\n" "--> Backing up to %s" % (filename, conflict) ) shutil.copy2(filename, conflict) shutil.copy2(filename, old) shutil.move(new, filename) if os.path.exists(old): os.remove(old) try: # Make file readable and writable only by the owner. os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR) except OSError: pass month.edited = False month.mtime = os.path.getmtime(filename) logging.info("Wrote file %s" % filename) return True
Example #10
Source File: ddyaml.py From integrations-core with BSD 3-Clause "New" or "Revised" License | 4 votes |
def safe_yaml_dump_all( documents, stream=None, Dumper=yDumper, default_style=None, default_flow_style=None, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding='utf-8', explicit_start=None, explicit_end=None, version=None, tags=None, ): if Dumper != yDumper: stream_name = get_stream_name(stream) log.debug("Unsafe dumping of YAML has been disabled - using safe dumper instead in %s", stream_name) if pyyaml_dump_all: return pyyaml_dump_all( documents, stream, yDumper, default_style, default_flow_style, canonical, indent, width, allow_unicode, line_break, encoding, explicit_start, explicit_end, version, tags, ) return yaml.dump_all( documents, stream, yDumper, default_style, default_flow_style, canonical, indent, width, allow_unicode, line_break, encoding, explicit_start, explicit_end, version, tags, )