Created
March 14, 2017 18:29
-
-
Save epmoyer/e4e9b09e9af38478e8df1c9506222626 to your computer and use it in GitHub Desktop.
Reproduce Unicode encoding error with PythonConfluenceAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding_test.py | |
# This script reproduces a Unicode encoding error with PythonConfluenceAPI for the | |
# purpose of demonstrating the bug and validating a pull request to correct it. | |
# | |
# Prerequisites: | |
# 1: pip install configparser | |
# 2: Create a config.ini file of the form: | |
# [confluence] | |
# url=https://your_confluence_url.com | |
# user=<Confluence username> | |
# pass=<Confluence account password> | |
# space=<Confluence space containing a test page> | |
# page=<A Confluence page containing a cent sign> | |
import sys | |
import configparser | |
from PythonConfluenceAPI import ConfluenceAPI, ConfluenceFuturesAPI | |
def test_api(api): | |
response = api.get_content( | |
content_type='page', | |
space_key=config_dict['space'], | |
title=config_dict['page']) | |
if type(response) is dict: | |
# Not using futures | |
result = response | |
else: | |
# Using futures. Get the result. | |
result = response.result() | |
if result['size'] == 0: | |
print('ERROR: Page "{}" not found in space "{}".'.format( | |
config_dict['page'], | |
config_dict['space'])) | |
sys.exit() | |
page_id = result['results'][0]['id'] | |
print('Found page id: {}'.format(page_id)) | |
response = api.get_content_by_id( | |
content_id=page_id, | |
expand='body.storage') | |
if type(response) is dict: | |
# Not using futures | |
result = response | |
else: | |
# Using futures. Get the result. | |
result = response.result() | |
body = result['body']['storage']['value'] | |
print('--- Raw page body ----------------------------') | |
print(body) | |
print('--- Page body codepoints ---------------------') | |
codepoints_text = '' | |
for character in body: | |
#codepoints_text += '0x{:x} '.format(ord(character)) | |
value = ord(character) | |
if value < 127: | |
codepoints_text += character | |
else: | |
codepoints_text += r'\x{:x}'.format(value) | |
print(codepoints_text) | |
print('----------------------------------------------') | |
if __name__ == "__main__": | |
config = configparser.ConfigParser() | |
config.read('./config.ini') | |
config_dict = config._sections['confluence'] | |
# Convert config dict values from Unicode to str for Python 2.7 compatibility | |
for key in config_dict: | |
config_dict[key] = str(config_dict[key]) | |
print('Testing ConfluenceAPI...') | |
test_api( | |
ConfluenceAPI( | |
config_dict['user'], | |
config_dict['pass'], | |
config_dict['url']) | |
) | |
print('Testing ConfluenceFuturesAPI...') | |
test_api( | |
ConfluenceFuturesAPI( | |
config_dict['user'], | |
config_dict['pass'], | |
config_dict['url']) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment