/usr/share/pyshared/synaptiks/util.py is in kde-config-touchpad 0.8.1-2.
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 112 113 114 115 116 117 118 119 120 121 122 123 | # -*- coding: utf-8 -*-
# Copyright (C) 2010, 2011 Sebastian Wiesner <lunaryorn@googlemail.com>
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""
synaptiks.util
==============
General utilities used throughout synaptiks.
.. moduleauthor:: Sebastian Wiesner <lunaryorn@googlemail.com>
"""
from __future__ import (print_function, division, unicode_literals,
absolute_import)
import os
import sys
import errno
import json
def ensure_byte_string(s):
"""
Convert ``s`` to a byte string.
If ``s`` is already a byte string, it is returned unchanged. If ``s``
is a unicode string, it is converted according to
:func:`sys.getfilesystemencoding()`.
"""
if isinstance(s, unicode):
return s.encode(sys.getfilesystemencoding())
return s
def ensure_unicode_string(s):
"""
Convert ``s`` to a unicode string.
If ``s`` is already a unicode string, it is returned unchanged. If
``s`` is a byte string, it is converted according to
:func:`sys.getfilesystemencoding()`.
"""
if isinstance(s, str):
return s.decode(sys.getfilesystemencoding())
return s
def ensure_directory(directory):
"""
Ensure, that the given ``directory`` exists. If the ``directory`` does not
yet exist, it is created.
Return ``directory`` again.
Raise :exc:`~exceptions.EnvironmentError`, if directory creation fails.
"""
try:
os.makedirs(directory)
return directory
except EnvironmentError as error:
if error.errno == errno.EEXIST:
return directory
else:
raise
def save_json(filename, obj):
"""
Save the given ```obj`` in JSON format to the given ``filename``.
``obj`` must either be a primitive type such as integer or string, or a
list or a dict. Any other type will raise a :exc:`~exceptions.TypeError`.
``filename`` is a string containing the path to the file to which the
object is saved.
Raise :exc:`~exceptions.TypeError`, if ``obj`` has an unsupported path.
Raise :exc:`~exceptions.EnvironmentError`, if ``filename`` could not be
opened or not be written to.
"""
with open(filename, 'w') as stream:
json.dump(obj, stream, indent=2)
def load_json(filename, default=None):
"""
Load the given JSON file.
If the given file does not exist and ``default`` is not ``None``, then the
given ``default`` is returned.
Raise :exc:`~exceptions.EnvironmentError`, if the file could not be read,
or if ``default`` is ``None`` and the file was not found.
"""
try:
with open(filename, 'r') as stream:
return json.load(stream)
except EnvironmentError as error:
if default is not None and error.errno == errno.ENOENT:
return default
raise
|