Skip to content

Commit

Permalink
automatically try to load file in caller's folder to bypass some work…
Browse files Browse the repository at this point in the history
…ing folder issue
  • Loading branch information
wjo1212 committed Dec 4, 2018
1 parent c503300 commit 8299399
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion aliyun/log/etl_core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class SettingError(LogException):
def __init__(self, ex=None, settings="", msg=""):
if msg and settings:
msg += 'Invalid Settings "{0}"'.format(settings)
msg += '\nInvalid Settings "{0}"'.format(settings)
else:
msg = msg or 'Invalid Settings "{0}"'.format(settings or 'unknown')

Expand Down
11 changes: 9 additions & 2 deletions aliyun/log/etl_core/trans_comp/trans_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import csv
from collections import Iterable
import logging
import inspect

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -87,8 +88,14 @@ def __init__(self, data, output_fields, sep=',', quote='"', lstrip=True, case_in
type_path = data.split("://")
file_type = type_path[0] if len(type_path) == 2 else 'file'
file_path = type_path[1] if len(type_path) == 2 else data
if file_type != 'file' or not os.path.isfile(file_path):
raise SettingError(msg="trans_comp_lookup: cannot parse file path", settings=data)
if file_type != 'file':
raise SettingError(msg="trans_comp_lookup: unsupported file type", settings=data)
if not os.path.isfile(file_path):
caller_frame_record = inspect.stack()[1]
module = inspect.getmodule(caller_frame_record[0])
file_path = os.path.sep.join([os.path.dirname(module.__file__), file_path])
if not os.path.isfile(file_path):
raise SettingError(msg="trans_comp_lookup: cannot locate the file path", settings=data)

with open(file_path) as file:
reader = csv.DictReader(file, skipinitialspace=lstrip, delimiter=sep, quotechar=quote)
Expand Down

0 comments on commit 8299399

Please sign in to comment.