Skip to content

Commit

Permalink
[qacode] hot fix #195
Browse files Browse the repository at this point in the history
  • Loading branch information
netzulo committed Jul 27, 2018
1 parent 2236ee3 commit 54f5d8a
Showing 1 changed file with 63 additions and 4 deletions.
67 changes: 63 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,69 @@
from os import path
from setuptools import setup, find_packages
from sys import version_info
try:
from qautils.files import read
except ImportError:
raise Exception("Please, install 'qautils' package: pip install qautils")


def path_format(file_path=None, file_name=None, is_abspath=False,
ignore_raises=False):
"""
Get path joined checking before if path and filepath exist,
if not, raise an Exception
if ignore_raise it's enabled, then file_path must include '/' at end lane
"""
path_formatted = "{}{}".format(file_path, file_name)
if ignore_raises:
return path_formatted
if file_path is None or not path.exists(file_path):
raise IOError("Path '{}' doesn't exists".format(file_path))
if file_name is None or not path.exists(path_formatted):
raise IOError(
"File '{}{}' doesn't exists".format(file_path, file_name))
if is_abspath:
return path.abspath(path.join(file_path, file_name))
else:
return path.join(file_path, file_name)

def read_file(is_json=False, file_path=None, encoding='utf-8',
is_encoding=True, ignore_raises=False):
"""Returns file object from file_path,
compatible with all py versiones
optionals:
can be use to return dict from json path
can modify encoding used to obtain file
"""
text = None
try:
if file_path is None:
raise Exception("File path received it's None")
if version_info.major >= 3:
if not is_encoding:
encoding = None
with open(file_path, encoding=encoding) as buff:
text = buff.read()
if version_info.major <= 2:
with open(file_path) as buff:
if is_encoding:
text = buff.read().decode(encoding)
else:
text = buff.read()
if is_json:
return json.loads(text)
except Exception as err:
if not ignore_raises:
raise Exception(err)
return text

def read(file_path='./', file_name=None, is_encoding=True, ignore_raises=False):
"""Read file"""
if file_name is None:
raise Exception("File name not provided")
return read_file(
is_encoding=is_encoding,
ignore_raises=ignore_raises,
file_path=path_format(
file_path=file_path,
file_name=file_name,
ignore_raises=ignore_raises))


VERSION = "0.5.5"
Expand Down

0 comments on commit 54f5d8a

Please sign in to comment.