def effective_default(self, field):
"""
Returns a field's effective database default value
"""
if field.has_default():
default = field.get_default()
elif not field.null and field.blank and field.empty_strings_allowed:
if field.get_internal_type() == "BinaryField":
default = six.binary_type()
else:
default = six.text_type()
else:
default = None
# If it's a callable, call it
if six.callable(default):
default = default()
# Run it through the field's get_db_prep_save method so we can send it
# to the database.
default = field.get_db_prep_save(default, self.connection)
return default
python类binary_type()的实例源码
def test_sign_unsign(self):
"""sign/unsign should be reversible"""
signer = signing.BytesSigner('predictable-secret')
examples = [
b'q;wjmbk;wkmb',
b'3098247529087',
b'3098247:529:087:',
b'jkw osanteuh ,rcuh nthu aou oauh ,ud du',
b'\u2019',
]
if six.PY2:
examples.append(b'a byte string')
for example in examples:
signed = signer.sign(example)
self.assertIsInstance(signed, six.binary_type)
self.assertNotEqual(force_str(example), signed)
self.assertEqual(example, signer.unsign(signed))
def effective_default(self, field):
"""
Returns a field's effective database default value
"""
if field.has_default():
default = field.get_default()
elif not field.null and field.blank and field.empty_strings_allowed:
if field.get_internal_type() == "BinaryField":
default = six.binary_type()
else:
default = six.text_type()
else:
default = None
# If it's a callable, call it
if six.callable(default):
default = default()
# Run it through the field's get_db_prep_save method so we can send it
# to the database.
default = field.get_db_prep_save(default, self.connection)
return default
def effective_default(self, field):
"""
Returns a field's effective database default value
"""
if field.has_default():
default = field.get_default()
elif not field.null and field.blank and field.empty_strings_allowed:
if field.get_internal_type() == "BinaryField":
default = six.binary_type()
else:
default = six.text_type()
else:
default = None
# If it's a callable, call it
if six.callable(default):
default = default()
# Run it through the field's get_db_prep_save method so we can send it
# to the database.
default = field.get_db_prep_save(default, self.connection)
return default
def save(self):
"""
?? manifest ??
?? ?? Layers ?Links ??
:return:
"""
data_string = self.data_string
if not isinstance(data_string, six.binary_type):
data_string = data_string.encode("utf-8")
manifest_digest = storage.save_manifest(data_string)
# save tags
current_tag_path = storage.path_spec.get_tag_current_path(name=self.name, tag_name=self.reference)
storage.link(manifest_digest, current_tag_path)
tag_index_path = storage.path_spec.get_tag_index_path(name=self.name, tag_name=self.reference,
digest=manifest_digest)
storage.link(manifest_digest, tag_index_path)
# Save reference
reference_path = storage.path_spec.get_reference_path(name=self.name, digest=manifest_digest)
storage.link(manifest_digest, reference_path)
for layer in self.layers:
layer_path = storage.path_spec.get_layer_path(name=self.name, digest=layer)
storage.link(layer, layer_path)
def _decode(value):
"""
Base64 ?????"="
???????“?”???Docker?????????
:param value:
:return:
"""
length = len(value) % 4
if length in (2, 3,):
value += (4 - length) * "="
elif length != 0:
raise ValueError("Invalid base64 string")
if not isinstance(value, six.binary_type):
value = value.encode()
return base64.urlsafe_b64decode(value)
def effective_default(self, field):
"""
Returns a field's effective database default value
"""
if field.has_default():
default = field.get_default()
elif not field.null and field.blank and field.empty_strings_allowed:
if field.get_internal_type() == "BinaryField":
default = six.binary_type()
else:
default = six.text_type()
else:
default = None
# If it's a callable, call it
if six.callable(default):
default = default()
# Run it through the field's get_db_prep_save method so we can send it
# to the database.
default = field.get_db_prep_save(default, self.connection)
return default
def effective_default(self, field):
"""
Returns a field's effective database default value
"""
if field.has_default():
default = field.get_default()
elif not field.null and field.blank and field.empty_strings_allowed:
if field.get_internal_type() == "BinaryField":
default = six.binary_type()
else:
default = six.text_type()
else:
default = None
# If it's a callable, call it
if six.callable(default):
default = default()
# Run it through the field's get_db_prep_save method so we can send it
# to the database.
default = field.get_db_prep_save(default, self.connection)
return default
def test_read(self):
key_data = keys.read_key_file('test-2048.pem')
self.assertIsInstance(key_data, six.binary_type)
key_data_lines = key_data.decode('utf-8').split('\n')
self.assertIn('-----BEGIN PRIVATE KEY-----', key_data_lines)
self.assertIn('-----END PRIVATE KEY-----', key_data_lines)
self.assertIn('-----BEGIN PUBLIC KEY-----', key_data_lines)
self.assertIn('-----END PUBLIC KEY-----', key_data_lines)
def attach(self, filename=None, content=None, mimetype=None):
"""
Attaches a file with the given filename and content. The filename can
be omitted and the mimetype is guessed, if not provided.
If the first parameter is a MIMEBase subclass it is inserted directly
into the resulting message attachments.
For a text/* mimetype (guessed or specified), when a bytes object is
specified as content, it will be decoded as UTF-8. If that fails,
the mimetype will be set to DEFAULT_ATTACHMENT_MIME_TYPE and the
content is not decoded.
"""
if isinstance(filename, MIMEBase):
assert content is None
assert mimetype is None
self.attachments.append(filename)
else:
assert content is not None
if not mimetype:
mimetype, _ = mimetypes.guess_type(filename)
if not mimetype:
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
basetype, subtype = mimetype.split('/', 1)
if basetype == 'text':
if isinstance(content, six.binary_type):
try:
content = content.decode('utf-8')
except UnicodeDecodeError:
# If mimetype suggests the file is text but it's actually
# binary, read() will raise a UnicodeDecodeError on Python 3.
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
self.attachments.append((filename, content, mimetype))
def attach(self, filename=None, content=None, mimetype=None):
"""
Attaches a file with the given filename and content. The filename can
be omitted and the mimetype is guessed, if not provided.
If the first parameter is a MIMEBase subclass it is inserted directly
into the resulting message attachments.
For a text/* mimetype (guessed or specified), when a bytes object is
specified as content, it will be decoded as UTF-8. If that fails,
the mimetype will be set to DEFAULT_ATTACHMENT_MIME_TYPE and the
content is not decoded.
"""
if isinstance(filename, MIMEBase):
assert content is None
assert mimetype is None
self.attachments.append(filename)
else:
assert content is not None
if not mimetype:
mimetype, _ = mimetypes.guess_type(filename)
if not mimetype:
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
basetype, subtype = mimetype.split('/', 1)
if basetype == 'text':
if isinstance(content, six.binary_type):
try:
content = content.decode('utf-8')
except UnicodeDecodeError:
# If mimetype suggests the file is text but it's actually
# binary, read() will raise a UnicodeDecodeError on Python 3.
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
self.attachments.append((filename, content, mimetype))
def to_internal_value(self, data):
try:
if self.binary or getattr(data, 'is_json_string', False):
if isinstance(data, six.binary_type):
data = data.decode('utf-8')
return json.loads(data)
else:
json.dumps(data)
except (TypeError, ValueError):
self.fail('invalid')
return data
def unicode_http_header(value):
# Coerce HTTP header value to unicode.
if isinstance(value, six.binary_type):
return value.decode('iso-8859-1')
return value
def test_settings_has_key(self):
key = settings.CRYPTOGRAPHY_KEY
self.assertIsNotNone(key)
self.assertIsInstance(key, six.binary_type)
def _from_python(self, value):
"""Convert more Python data types to ES-understandable JSON."""
iso = self._iso_datetime(value)
if iso:
return iso
elif isinstance(value, six.binary_type):
# TODO: Be stricter.
return six.text_type(value, errors='replace')
elif isinstance(value, set):
return list(value)
return value
def unicode_http_header(value):
# Coerce HTTP header value to unicode.
if isinstance(value, six.binary_type):
return value.decode('iso-8859-1')
return value
def context(self):
ctx = {}
if six.PY2 and isinstance(self.raw_response, six.text_type):
self.raw_response = self.raw_response.encode('utf8')
for key, val in parse_qsl(self.raw_response):
if isinstance(key, six.binary_type):
key = key.decode('utf8')
if isinstance(val, six.binary_type):
val = val.decode('utf8')
ctx[key] = [val]
return ctx
def to_internal_value(self, data):
try:
if self.binary:
if isinstance(data, six.binary_type):
data = data.decode('utf-8')
return json.loads(data)
else:
json.dumps(data)
except (TypeError, ValueError):
self.fail('invalid')
return data
def unicode_http_header(value):
# Coerce HTTP header value to unicode.
if isinstance(value, six.binary_type):
return value.decode('iso-8859-1')
return value
def unicode_http_header(value):
# Coerce HTTP header value to unicode.
if isinstance(value, six.binary_type):
return value.decode('iso-8859-1')
return value
def attach(self, filename=None, content=None, mimetype=None):
"""
Attaches a file with the given filename and content. The filename can
be omitted and the mimetype is guessed, if not provided.
If the first parameter is a MIMEBase subclass it is inserted directly
into the resulting message attachments.
For a text/* mimetype (guessed or specified), when a bytes object is
specified as content, it will be decoded as UTF-8. If that fails,
the mimetype will be set to DEFAULT_ATTACHMENT_MIME_TYPE and the
content is not decoded.
"""
if isinstance(filename, MIMEBase):
assert content is None
assert mimetype is None
self.attachments.append(filename)
else:
assert content is not None
if not mimetype:
mimetype, _ = mimetypes.guess_type(filename)
if not mimetype:
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
basetype, subtype = mimetype.split('/', 1)
if basetype == 'text':
if isinstance(content, six.binary_type):
try:
content = content.decode('utf-8')
except UnicodeDecodeError:
# If mimetype suggests the file is text but it's actually
# binary, read() will raise a UnicodeDecodeError on Python 3.
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
self.attachments.append((filename, content, mimetype))
def effective_default(self, field):
"""
Returns a field's effective database default value
"""
if field.has_default():
default = field.get_default()
elif not field.null and field.blank and field.empty_strings_allowed:
if field.get_internal_type() == "BinaryField":
default = six.binary_type()
else:
default = six.text_type()
elif getattr(field, 'auto_now', False) or getattr(field, 'auto_now_add', False):
default = datetime.now()
internal_type = field.get_internal_type()
if internal_type == 'DateField':
default = default.date
elif internal_type == 'TimeField':
default = default.time
elif internal_type == 'DateTimeField':
default = timezone.now
else:
default = None
# If it's a callable, call it
if callable(default):
default = default()
# Run it through the field's get_db_prep_save method so we can send it
# to the database.
default = field.get_db_prep_save(default, self.connection)
return default
def _from_python(self, value):
"""Convert more Python data types to ES-understandable JSON."""
iso = self._iso_datetime(value)
if iso:
return iso
elif isinstance(value, six.binary_type):
# TODO: Be stricter.
return six.text_type(value, errors='replace')
elif isinstance(value, set):
return list(value)
return value
def attach(self, filename=None, content=None, mimetype=None):
"""
Attaches a file with the given filename and content. The filename can
be omitted and the mimetype is guessed, if not provided.
If the first parameter is a MIMEBase subclass it is inserted directly
into the resulting message attachments.
For a text/* mimetype (guessed or specified), when a bytes object is
specified as content, it will be decoded as UTF-8. If that fails,
the mimetype will be set to DEFAULT_ATTACHMENT_MIME_TYPE and the
content is not decoded.
"""
if isinstance(filename, MIMEBase):
assert content is None
assert mimetype is None
self.attachments.append(filename)
else:
assert content is not None
if not mimetype:
mimetype, _ = mimetypes.guess_type(filename)
if not mimetype:
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
basetype, subtype = mimetype.split('/', 1)
if basetype == 'text':
if isinstance(content, six.binary_type):
try:
content = content.decode('utf-8')
except UnicodeDecodeError:
# If mimetype suggests the file is text but it's actually
# binary, read() will raise a UnicodeDecodeError on Python 3.
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
self.attachments.append((filename, content, mimetype))
def effective_default(self, field):
"""
Returns a field's effective database default value
"""
if field.has_default():
default = field.get_default()
elif not field.null and field.blank and field.empty_strings_allowed:
if field.get_internal_type() == "BinaryField":
default = six.binary_type()
else:
default = six.text_type()
elif getattr(field, 'auto_now', False) or getattr(field, 'auto_now_add', False):
default = datetime.now()
internal_type = field.get_internal_type()
if internal_type == 'DateField':
default = default.date
elif internal_type == 'TimeField':
default = default.time
elif internal_type == 'DateTimeField':
default = timezone.now
else:
default = None
# If it's a callable, call it
if callable(default):
default = default()
# Run it through the field's get_db_prep_save method so we can send it
# to the database.
default = field.get_db_prep_save(default, self.connection)
return default
def __init__(self, data, delimiter=None):
if hasattr(data, 'seek'):
self.data = data
else:
if six.PY3:
if isinstance(data, six.binary_type):
self.data = io.StringIO(data.decode('utf-8'))
else:
self.data = io.StringIO(data)
else:
self.data = io.BytesIO(data)
if delimiter is None:
try:
self.dialect = csv.Sniffer().sniff(self.data.read(200),
delimiters=';:,')
except csv.Error:
self.dialect = None
self.delimiter = ':'
else:
self.delimiter = self.dialect.delimiter
finally:
self.data.seek(0)
self.reader = csv.DictReader(CommentStripper(self.data),
fieldnames=self.format,
delimiter=self.delimiter,
restkey=self.restkey)
self.line_num = 0
def _decode_as_utf8(row):
"""Decodes all unicode values in row as utf-8 strings"""
for key, value in row.items():
if isinstance(value, six.binary_type):
row[key] = value.decode('utf-8')
return row
def is_invalid_utf8(string):
"""Returns True if string is invalid UTF-8.
If string is not a an str object, or is decodeable as UTF-8, False is
returned.
"""
if isinstance(string, six.binary_type):
try:
string.decode('utf-8')
except UnicodeDecodeError:
return True
return False
def unicode_utf8(thing):
"""Casts thing to unicode, assuming utf-8 encoding if a binary string.
If the argument is None, it is returned unchanged.
"""
if isinstance(thing, six.binary_type):
return thing.decode('utf-8')
elif thing is not None:
return six.text_type(thing)
def scan(addresses):
"""Scan a list of ip-addresses for netbios names"""
_logger.debug('Scanning %s addresses', len(addresses))
proc = Popen(['nbtscan', '-f-', '-s', SPLITCHAR], stdin=PIPE, stdout=PIPE)
stdout, stderr = proc.communicate('\n'.join(addresses))
if stderr:
raise Exception(stderr)
if isinstance(stdout, six.binary_type):
stdout = stdout.decode('cp850') # cp850 seems like netbios' standard
_logger.debug('Result from scan:\n%s', stdout)
return stdout