/usr/share/pyshared/swiftsc/utils.py is in python-swiftsc 0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | # -*- coding: utf-8 -*-
"""
Copyright (C) 2013 Kouhei Maeda <mkouhei@palmtb.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import magic
import inspect
import sys
from io import BytesIO
def return_json(response_json):
if isinstance(response_json, dict) or isinstance(response_json, list):
return response_json
elif inspect.ismethod(response_json):
# support requests 1.0 over
return response_json()
def generate_url(partial_uri_list):
"""
Argument:
partial_uri_list: patial string of generating URL
ex. ["https://swift.example.org", "auth", "v1.0"]
Return: URL
ex. "https://swift.example.org/auth/v1.0"
"""
url = ""
for i, partial_uri in enumerate(partial_uri_list):
if i + 1 == len(partial_uri_list):
url += partial_uri
else:
url += partial_uri + "/"
return url
def check_mimetype(filepath):
"""check mimetype of file
Argument:
filename: target filename path
"""
if 'open' in dir(magic):
# for python-magic package of Debian Wheezy/Sid, Ubuntu 12.04
m = magic.open(magic.MAGIC_MIME)
m.load()
mimetype = m.file(filepath).split('; ')[0]
elif 'from_file' in dir(magic):
# for pip install python-magic
mimetype = magic.from_file(filepath, mime=True)
else:
raise RuntimeError("Not support python-magic in this environment")
if sys.version_info > (3, 0) and isinstance(mimetype, bytes):
mimetype = mimetype.decode('utf-8')
return mimetype
def check_mimetype_buffer(fileobj):
"""check mimetype of file
Argument:
filename: target filename path
"""
if 'open' in dir(magic):
# for python-magic package of Debian Wheezy/Sid, Ubuntu 12.04
m = magic.open(magic.MAGIC_MIME)
m.load()
mimetype = m.buffer(fileobj.read()).split('; ')[0]
elif 'from_file' in dir(magic):
# for pip install python-magic
mimetype = magic.from_buffer(fileobj.read(), mime=True)
else:
raise RuntimeError("Not support python-magic in this environment")
if sys.version_info > (3, 0) and isinstance(mimetype, bytes):
mimetype = mimetype.decode('utf-8')
return mimetype
def retrieve_info_from_buffer(file_object):
"""check mimetype of file object
Argument:
file_object: target file object
"""
bio = BytesIO()
bio.write(file_object.read())
bio.seek(0)
mimetype = check_mimetype_buffer(bio)
bio.seek(0)
content_length = len(bio.read())
bio.seek(0)
data = bio.read()
bio.close()
return (mimetype, content_length, data)
|