Skip to content

Commit

Permalink
Merge pull request #3 from abulgher/utf-post
Browse files Browse the repository at this point in the history
Capability to post and read back messages / attributes with special characters
  • Loading branch information
abulgher authored Sep 4, 2022
2 parents 2176d62 + af5165d commit 1867059
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
24 changes: 22 additions & 2 deletions elog/logbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,21 @@ def post(self, message, msg_id=None, reply=False, attributes=None, attachments=N

# Make requests module think that Text is a "file". This is the only way to force requests to send data as
# multipart/form-data even if there are no attachments. Elog understands only multipart/form-data
files_to_attach.append(('Text', ('', message)))
files_to_attach.append(('Text', ('', message.encode('iso-8859-1'))))

# Base attributes are common to all messages
self._add_base_msg_attributes(attributes_to_edit)

# Keys in attributes cannot have certain characters like whitespaces or dashes for the http request
attributes_to_edit = _replace_special_characters_in_attribute_keys(attributes_to_edit)

# All string values in the attributes must be encoded in latin1
attributes_to_edit = _encode_values(attributes_to_edit)

try:
response = requests.post(self._url, data=attributes_to_edit, files=files_to_attach, allow_redirects=False,
verify=False)

# Validate response. Any problems will raise an Exception.
resp_message, resp_headers, resp_msg_id = _validate_response(response)

Expand Down Expand Up @@ -258,7 +262,7 @@ def read(self, msg_id):
attributes = dict()
attachments = list()

returned_msg = resp_message.decode('utf-8', 'ignore').splitlines()
returned_msg = resp_message.decode('iso-8859-1', 'ignore').splitlines()
delimiter_idx = returned_msg.index('========================================')

message = '\n'.join(returned_msg[delimiter_idx + 1:])
Expand Down Expand Up @@ -513,6 +517,22 @@ def _remove_reserved_attributes(attributes):
attributes.pop('Attachment', None)
attributes.pop('Text', None) # Remove this one because it will be send attachment like

def _encode_values(attributes):
"""
prepares a dictionary of the attributes with latin1 encoded string values.
:param attributes: dictionary of attributes to ve encoded
:return: dictionary with encoded string attributes
"""

encoded_attributes = {}
for key, value in attributes.items():
if isinstance(value, str):
encoded_attributes[key] = value.encode('iso-8859-1')
else:
encoded_attributes[key] = value
return encoded_attributes


def _replace_special_characters_in_attribute_keys(attributes):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
README = open(os.path.join(here, 'README.md')).read()

setup(name='py_elog',
version='1.3.13',
version='1.3.14',
description="Python library to access Elog.",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
46 changes: 25 additions & 21 deletions tests/test_logbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,62 @@




class TestClass(unittest.TestCase):

# TODO add test for delete
# TODO add description how to run the test docker container for testing

elog_hostname = 'https://elog.psi.ch/elogs/Linux+Demo/'

message = 'This message text is new'

def test_get_message_ids(self):
logbook = elog.open(self.elog_hostname)
message_ids = logbook.get_message_ids()
print(len(message_ids))
print(message_ids)


def test_get_last_message_id(self):

logbook = elog.open(self.elog_hostname)
msg_id = logbook.post('This is message text is new', attributes={'Author':'AB', 'Type':'Routine'})
msg_id = logbook.post(self.message, attributes={'Author':'AB', 'Type':'Routine'})
message_id = logbook.get_last_message_id()

print(msg_id)
print(message_id)
self.assertEqual(msg_id, message_id, "Created message does not show up as last edited message")


def test_read(self):

logbook = elog.open(self.elog_hostname)
message, attributes, attachments = logbook.read(logbook.get_last_message_id())
print(message)
self.assertEqual(message, 'This is message text is new', "Unable to retrieve message")


def test_edit(self):
logbook = elog.open(self.elog_hostname)
logbook.post('hehehehehe', msg_id=logbook.get_last_message_id(), attributes={"Subject": 'py_elog test [mod]'})

def test_search(self):
logbook = elog.open(self.elog_hostname)
ids = logbook.search("message")
print(ids)

def test_search_empty(self):
logbook = elog.open(self.elog_hostname)
ids = logbook.search("")
print(ids)

def test_search_dict(self):
logbook = elog.open(self.elog_hostname)
ids = logbook.search({"Category": "Hardware"})
print(ids)

def test_post_special_characters(self):
logbook = elog.open(self.elog_hostname)
attributes = { 'Author' : 'Me', 'Type' : 'Other', 'Category' : 'General',
'Subject' : 'This is a test of UTF-8 characters like èéöä'}
message = 'Just to be clear this is a general test using UTF-8 characters like èéöä.'
msg_id = logbook.post(message, reply=False, attributes=attributes,encoding='HTML')
read_msg, read_attr, read_att = logbook.read(msg_id)

mess_ok = message == read_msg

attr_ok = True
for key in attributes:
if attributes[key] == read_attr[key]:
attr_ok = attr_ok and True
else:
attr_ok = attr_ok and False

whole_test = attr_ok and mess_ok

self.assertTrue(whole_test)

if __name__ == '__main__':
unittest.main()

0 comments on commit 1867059

Please sign in to comment.