/usr/lib/python2.7/dist-packages/preggy/assertions/types/file.py is in python-preggy 1.1.3-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 | # -*- coding: utf-8 -*-
'''preggy file assertions. For use with `expect()` (see `preggy.core`).
For these assertions, "file" can be either a string or a file object. Since
a string is required to create a file object in the first place, these
assertions provide a convenient, flexible way to test whether a topic is
a "file" in your tests.
'''
# preggy assertions
# https://github.com/heynemann/preggy
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2013 Bernardo Heynemann heynemann@gmail.com
from __future__ import absolute_import
try:
import six
except ImportError: # pragma: no cover
import warnings
warnings.warn("Ignoring six. Probably setup.py installing package.")
try:
import io
except ImportError: # pragma: no cover
pass ## FIXME: explain using "pass" here
from os.path import isfile
import types
from preggy import assertion
#-------------------------------------------------------------------------------------------------
# Helpers
#-------------------------------------------------------------------------------------------------
_is_file = lambda topic: isfile(topic)
_is_string = lambda topic: isinstance(topic, (six.string_types, six.text_type))
def _is_file_obj(topic):
try:
return isinstance(topic, types.FileType)
except AttributeError: # pragma: no cover
# FIXME: add comment...
# what is this for?
return isinstance(topic, io.IOBase)
#-------------------------------------------------------------------------------------------------
# Assertions
#-------------------------------------------------------------------------------------------------
@assertion
def to_be_a_file(topic):
'''Asserts that `topic` is a file.
If `topic` is a string, this asserts that `os.path.isfile()`
returns `True`.
Otherwise, this asserts whether `topic` is an instance of the
built-in `file` type.
'''
if _is_string(topic) and _is_file(topic):
return True
elif _is_file_obj(topic):
return True
msg = 'Expected topic({0}) to be a file, but it was {1!r}'.format(topic, type(topic))
raise AssertionError(msg)
@assertion
def not_to_be_a_file(topic):
'''Asserts that `topic` is NOT a file.
If `topic` is a string, this asserts that `os.path.isfile()`
returns `False`.
Otherwise, this asserts whether `topic` is NOT an instance of the
built-in `file` type.
'''
try:
to_be_a_file(topic)
except AssertionError:
return True
msg = 'Expected topic({0}) not to be a file, but it was'.format(topic)
raise AssertionError(msg)
|