コード例 #1
0
def _exists(filename,create=_E):
	A=filename;B=Path(A)
	if create:
		try:B.touch(exist_ok=_D)
		except OSError as C:raise BoxError(f"Could not create file {A} - {C}")
		else:return
	if not B.exists():raise BoxError(f'File "{A}" does not exist')
	if not B.is_file():raise BoxError(f"{A} is not a file")
コード例 #2
0
ファイル: converters.py プロジェクト: zejr/dynaconf
def _exists(filename, create=False):
    path = Path(filename)
    if create:
        try:
            path.touch(exist_ok=True)
        except OSError as err:
            raise BoxError(f'Could not create file {filename} - {err}')
        else:
            return
    if not path.exists():
        raise BoxError(f'File "{filename}" does not exist')
    if not path.is_file():
        raise BoxError(f'{filename} is not a file')
コード例 #3
0
def _from_toml(toml_string=_A,filename=_A,encoding=_B,errors=_C):
	B=toml_string;A=filename
	if A:
		_exists(A)
		with open(A,_G,encoding=encoding,errors=errors)as D:C=toml.load(D)
	elif B:C=toml.loads(B)
	else:raise BoxError('from_toml requires a string or filename')
	return C
コード例 #4
0
def _to_csv(box_list,filename,encoding=_B,errors=_C):
	B=filename;A=box_list;C=list(A[0].keys())
	for E in A:
		if list(E.keys())!=C:raise BoxError('BoxList must contain the same dictionary structure for every item to convert to csv')
	if B:
		_exists(B,create=_D)
		with open(B,_F,encoding=encoding,errors=errors,newline='')as F:
			D=csv.DictWriter(F,fieldnames=C);D.writeheader()
			for G in A:D.writerow(G)
コード例 #5
0
def _from_yaml(yaml_string=_A,filename=_A,encoding=_B,errors=_C,**A):
	F='Loader';C=yaml_string;B=filename
	if F not in A:A[F]=yaml.SafeLoader
	if B:
		_exists(B)
		with open(B,_G,encoding=encoding,errors=errors)as E:D=yaml.load(E,**A)
	elif C:D=yaml.load(C,**A)
	else:raise BoxError('from_yaml requires a string or filename')
	return D
コード例 #6
0
def _from_json(json_string=_A,filename=_A,encoding=_B,errors=_C,multiline=_E,**B):
	D=json_string;A=filename
	if A:
		_exists(A)
		with open(A,_G,encoding=encoding,errors=errors)as E:
			if multiline:C=[json.loads(A.strip(),**B)for A in E if A.strip()and not A.strip().startswith('#')]
			else:C=json.load(E,**B)
	elif D:C=json.loads(D,**B)
	else:raise BoxError('from_json requires a string or filename')
	return C
コード例 #7
0
ファイル: converters.py プロジェクト: zejr/dynaconf
def _from_toml(toml_string=None,
               filename=None,
               encoding="utf-8",
               errors="strict"):
    if filename:
        _exists(filename)
        with open(filename, 'r', encoding=encoding, errors=errors) as f:
            data = toml.load(f)
    elif toml_string:
        data = toml.loads(toml_string)
    else:
        raise BoxError('from_toml requires a string or filename')
    return data
コード例 #8
0
ファイル: converters.py プロジェクト: zejr/dynaconf
def _from_yaml(yaml_string=None,
               filename=None,
               encoding="utf-8",
               errors="strict",
               **kwargs):
    if 'Loader' not in kwargs:
        kwargs['Loader'] = yaml.SafeLoader
    if filename:
        _exists(filename)
        with open(filename, 'r', encoding=encoding, errors=errors) as f:
            data = yaml.load(f, **kwargs)
    elif yaml_string:
        data = yaml.load(yaml_string, **kwargs)
    else:
        raise BoxError('from_yaml requires a string or filename')
    return data
コード例 #9
0
ファイル: converters.py プロジェクト: zejr/dynaconf
def _to_csv(box_list, filename, encoding="utf-8", errors="strict"):
    csv_column_names = list(box_list[0].keys())
    for row in box_list:
        if list(row.keys()) != csv_column_names:
            raise BoxError(
                'BoxList must contain the same dictionary structure for every item to convert to csv'
            )

    if filename:
        _exists(filename, create=True)
        with open(filename, 'w', encoding=encoding, errors=errors,
                  newline='') as csv_file:
            writer = csv.DictWriter(csv_file, fieldnames=csv_column_names)
            writer.writeheader()
            for data in box_list:
                writer.writerow(data)
コード例 #10
0
ファイル: converters.py プロジェクト: zejr/dynaconf
def _from_json(json_string=None,
               filename=None,
               encoding="utf-8",
               errors="strict",
               multiline=False,
               **kwargs):
    if filename:
        _exists(filename)
        with open(filename, 'r', encoding=encoding, errors=errors) as f:
            if multiline:
                data = [
                    json.loads(line.strip(), **kwargs) for line in f
                    if line.strip() and not line.strip().startswith("#")
                ]
            else:
                data = json.load(f, **kwargs)
    elif json_string:
        data = json.loads(json_string, **kwargs)
    else:
        raise BoxError('from_json requires a string or filename')
    return data