Commit 58402542 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Remove use of the 'uenc' and 'udec'

Replace use of 'uenc' and 'udec' functions from 'synnefo.util.text'
module with the 'smart_decode' and 'smart_unicode' functions from
'django.utils.encoding' module.

The uenc/udec functions depend on the locale category LC_CTYPE, which is not
appropriate for the Synenefo API. We explicitly use the
smart_unicode/smart_str functions to decode to unicode and encode to
bytestring objects, always using the utf-8 encoding.
parent 633eb5ac
......@@ -33,7 +33,7 @@ from synnefo.db.models import VirtualMachine, Network
from snf_django.lib.api import faults
from django.conf import settings
from copy import deepcopy
from synnefo.util.text import uenc
from django.utils.encoding import smart_unicode
def id_from_instance_name(name):
......@@ -222,5 +222,6 @@ def hide_pass(kw):
def check_name_length(name, max_length, message):
"""Check if a string is within acceptable value length"""
if len(uenc(name)) > max_length:
name = smart_unicode(name, encoding="utf-8")
if len(name) > max_length:
raise faults.BadRequest(message)
......@@ -59,12 +59,12 @@ from time import time, gmtime, strftime
from functools import wraps
from operator import itemgetter
from collections import namedtuple
from copy import deepcopy
from django.conf import settings
from django.utils import importlib
from django.utils.encoding import smart_unicode
from pithos.backends.base import NotAllowedError, VersionNotExists, QuotaError
from synnefo.util.text import uenc
from copy import deepcopy
from snf_django.lib.api import faults
Location = namedtuple("ObjectLocation", ["account", "container", "path"])
......@@ -217,7 +217,7 @@ class PlanktonBackend(object):
prefixed = {}
for k, v in _prefixed_metadata.items():
# Encode to UTF-8
k, v = uenc(k), uenc(v)
k, v = smart_unicode(k), smart_unicode(v)
# Check the length of key/value
if len(k) > 128:
raise faults.BadRequest('Metadata keys should be less than %s'
......
......@@ -39,10 +39,10 @@ from urllib import unquote
from django.conf import settings
from django.http import HttpResponse
from django.utils.encoding import smart_unicode
from snf_django.lib import api
from snf_django.lib.api import faults
from synnefo.util.text import uenc
from synnefo.plankton.backend import PlanktonBackend
from synnefo.plankton.backend import split_url
......@@ -94,7 +94,7 @@ def _create_image_response(image):
if key == 'properties':
for k, v in image.get('properties', {}).items():
name = 'x-image-meta-property-' + k.replace('_', '-')
response[name] = uenc(v)
response[name] = smart_unicode(v, encoding="utf-8")
else:
if key == "status":
img_status = image.get(key, "").upper()
......@@ -103,7 +103,8 @@ def _create_image_response(image):
response["x-image-meta-status"] = status
else:
name = 'x-image-meta-' + key.replace('_', '-')
response[name] = uenc(image.get(key, ''))
response[name] = smart_unicode(image.get(key, ''),
encoding="utf-8")
return response
......@@ -122,10 +123,12 @@ def _get_image_headers(request):
for key, val in request.META.items():
if key.startswith(META_PROPERTY_PREFIX):
name = normalize(key[META_PROPERTY_PREFIX_LEN:])
headers['properties'][unquote(name)] = unquote(uenc(val))
headers['properties'][unquote(name)] = \
unquote(smart_unicode(val, encoding='utf-8'))
elif key.startswith(META_PREFIX):
name = normalize(key[META_PREFIX_LEN:])
headers[unquote(name)] = unquote(uenc(val))
headers[unquote(name)] = \
unquote(smart_unicode(val, encoding='utf-8'))
is_public = headers.get('is_public', None)
if is_public is not None:
......@@ -165,7 +168,7 @@ def add_image(request):
name = params.pop('name', None)
if name is None:
raise faults.BadRequest("Image 'name' parameter is required")
elif len(uenc(name)) == 0:
elif len(smart_unicode(name, encoding="utf-8")) == 0:
raise faults.BadRequest("Invalid image name")
location = params.pop('location', None)
if location is None:
......
......@@ -38,8 +38,7 @@ import operator
from datetime import datetime
from django.utils.timesince import timesince, timeuntil
from django.db.models.query import QuerySet
from synnefo.util.text import uenc, udec
from django.utils.encoding import smart_unicode
def parse_bool(value, strict=True):
......@@ -179,15 +178,9 @@ def pprint_table(out, table, headers=None, output_format='pretty',
sep = separator if separator else " "
def stringnify(obj):
if isinstance(obj, (unicode, str)):
return udec(obj)
else:
return str(obj)
if headers:
headers = map(stringnify, headers)
table = [map(stringnify, row) for row in table]
headers = map(smart_unicode, headers)
table = [map(smart_unicode, row) for row in table]
if output_format == "json":
assert(headers is not None), "json output format requires headers"
......@@ -198,7 +191,6 @@ def pprint_table(out, table, headers=None, output_format='pretty',
cw = csv.writer(out)
if headers:
table.insert(0, headers)
table = map(functools.partial(map, uenc), table)
cw.writerows(table)
elif output_format == "pretty":
if vertical:
......@@ -207,8 +199,7 @@ def pprint_table(out, table, headers=None, output_format='pretty',
max_key = max(map(len, headers))
for row in table:
for (k, v) in zip(headers, row):
k = uenc(k.ljust(max_key))
v = uenc(v)
k = k.ljust(max_key)
out.write("%s: %s\n" % (k, v))
else:
# Find out the max width of each column
......@@ -223,14 +214,14 @@ def pprint_table(out, table, headers=None, output_format='pretty',
out.write("-" * t_length + "\n")
if headers:
# pretty print the headers
line = sep.join(uenc(v.rjust(w))
line = sep.join(v.rjust(w)
for v, w in zip(headers, widths))
out.write(line + "\n")
out.write("-" * t_length + "\n")
# print the rest table
for row in table:
line = sep.join(uenc(v.rjust(w)) for v, w in zip(row, widths))
line = sep.join(v.rjust(w) for v, w in zip(row, widths))
out.write(line + "\n")
else:
raise ValueError("Unknown output format '%s'" % output_format)
......@@ -35,7 +35,7 @@
from contextlib import contextmanager
from django.test import TestCase
from django.utils import simplejson as json
from synnefo.util import text
from django.utils.encoding import smart_unicode
from mock import patch
......@@ -138,14 +138,14 @@ def astakos_user(user):
"expires": "2013-06-19T15:23:59.975572+00:00",
"id": "DummyToken",
"tenant": {
"id": text.udec(user, 'utf8'),
"id": smart_unicode(user, encoding='utf-8'),
"name": "Firstname Lastname"
}
},
"serviceCatalog": [],
"user": {
"roles_links": [],
"id": text.udec(user, 'utf8'),
"id": smart_unicode(user, encoding='utf-8'),
"roles": [{"id": 1, "name": "default"}],
"name": "Firstname Lastname"}}
}
......@@ -205,6 +205,7 @@ def mocked_quotaholder(success=True):
return (len(astakos.return_value.issue_one_commission.mock_calls) +
serial)
astakos.return_value.issue_one_commission.side_effect = foo
def resolve_mock(*args, **kwargs):
return {"failed": [],
"accepted": args[0],
......
......@@ -47,13 +47,13 @@ from pithos.backends.migrate import initialize_db
from synnefo.lib.services import get_service_path
from synnefo.lib import join_urls
from synnefo.util import text
from django.test import TestCase
from django.test.client import Client, MULTIPART_CONTENT, FakePayload
from django.test.simple import DjangoTestSuiteRunner
from django.conf import settings
from django.utils.http import urlencode
from django.utils.encoding import smart_unicode
from django.db.backends.creation import TEST_DATABASE_PREFIX
import django.utils.simplejson as json
......@@ -230,7 +230,9 @@ class PithosAPITest(TestCase):
mock_validate_token = self.create_patch(
'astakosclient.AstakosClient.validate_token')
mock_validate_token.return_value = {
'access': {'user': {'id': text.udec(self.user, 'utf8')}}}
'access': {
'user': {'id': smart_unicode(self.user, encoding='utf-8')}}
}
# patch astakosclient.AstakosClient.get_token
mock_get_token = self.create_patch(
......
......@@ -32,6 +32,7 @@
# or implied, of GRNET S.A.
from django.http import HttpResponse
from django.utils.encoding import smart_str
import gd
import os
......@@ -46,7 +47,6 @@ from hashlib import sha256
from synnefo_stats import settings
from synnefo.util.text import uenc
from snf_django.lib.api import faults, api_method
from logging import getLogger
......@@ -252,12 +252,12 @@ available_graph_types = {'cpu-bar': draw_cpu_bar,
@api_method(http_method='GET', token_required=False, user_required=False,
format_allowed=False, logger=log)
def grapher(request, graph_type, hostname):
hostname = decrypt(uenc(hostname))
fname = uenc(os.path.join(settings.RRD_PREFIX, hostname))
hostname = decrypt(smart_str(hostname))
fname = smart_str(os.path.join(settings.RRD_PREFIX, hostname))
if not os.path.isdir(fname):
raise faults.ItemNotFound('No such instance')
outfname = uenc(os.path.join(settings.GRAPH_PREFIX, hostname))
outfname = smart_str(os.path.join(settings.GRAPH_PREFIX, hostname))
draw_func = available_graph_types[graph_type]
response = HttpResponse(draw_func(fname, outfname),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment