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

Ensure UTF8 encoding #7

Merged
merged 1 commit into from
Sep 26, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
except ImportError:
MySQLdb = None

import chardet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PB here :)


from shinken.basemodule import BaseModule
from shinken.log import logger

Expand Down Expand Up @@ -96,6 +98,17 @@ def init(self):
raise
logger.info("[MySQLImport]: Connection opened")

def ensure_encoding(self, value):
try:
value.decode('utf-8')
except UnicodeDecodeError, e:
encoding = chardet.detect(value)['encoding'] or 'latin1'
logger.warning("[MySQLImport]: Error decoding string " + \
"(value='%s', encoding='%s')" % (value, encoding) + \
". Failing back to utf-8")
value = value.decode(encoding).encode('utf-8')
return value

# Main function that is called in the CONFIGURATION phase
def get_objects(self):
if not hasattr(self, 'conn'):
Expand Down Expand Up @@ -126,7 +139,8 @@ def get_objects(self):
h = {}
for column in row:
if row[column]:
h[column] = str(row[column])
value = str(row[column])
h[column] = self.ensure_encoding(value)
r[k].append(h)

cursor.close()
Expand Down