Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bkprt py3 csvfile #37665

Merged
merged 2 commits into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/py3-csvfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- Fix csvfile lookup plugin when used on Python3 https://github.com/ansible/ansible/pull/37625
23 changes: 16 additions & 7 deletions lib/ansible/plugins/lookup/csvfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

from ansible.errors import AnsibleError, AnsibleAssertionError
from ansible.plugins.lookup import LookupBase
from ansible.module_utils.six import PY2
from ansible.module_utils._text import to_bytes, to_native, to_text


Expand All @@ -66,8 +67,10 @@ def __init__(self, f, encoding='utf-8'):
def __iter__(self):
return self

def next(self):
return self.reader.next().encode("utf-8")
def __next__(self):
return next(self.reader).encode("utf-8")

next = __next__ # For Python 2


class CSVReader:
Expand All @@ -77,13 +80,19 @@ class CSVReader:
"""

def __init__(self, f, dialect=csv.excel, encoding='utf-8', **kwds):
f = CSVRecoder(f, encoding)
if PY2:
f = CSVRecoder(f, encoding)
else:
f = codecs.getreader(encoding)(f)

self.reader = csv.reader(f, dialect=dialect, **kwds)

def next(self):
row = self.reader.next()
def __next__(self):
row = next(self.reader)
return [to_text(s) for s in row]

next = __next__ # For Python 2

def __iter__(self):
return self

Expand All @@ -93,8 +102,8 @@ class LookupModule(LookupBase):
def read_csv(self, filename, key, delimiter, encoding='utf-8', dflt=None, col=1):

try:
f = open(filename, 'r')
creader = CSVReader(f, delimiter=to_bytes(delimiter), encoding=encoding)
f = open(filename, 'rb')
creader = CSVReader(f, delimiter=to_native(delimiter), encoding=encoding)

for row in creader:
if row[0] == key:
Expand Down