From e3c05fcb73c5a1081167d73278785bf18d652dab Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Thu, 17 Sep 2015 17:56:45 +0200 Subject: Remove uses of the `types` module In Python 3, the types module no longer provide alternate names for built-in types, e.g. `types.StringType` can just be spelled `str`. NoneType is also removed; it needs to be replaced with type(None) Reviewed-By: David Kupka Reviewed-By: Jan Cholasta Reviewed-By: Martin Basti --- ipapython/ipautil.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'ipapython') diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py index 960c9d5ea..b6fd11338 100644 --- a/ipapython/ipautil.py +++ b/ipapython/ipautil.py @@ -29,7 +29,6 @@ import stat import shutil import socket import struct -from types import * import re import datetime import netaddr @@ -426,18 +425,18 @@ def backup_file(fname): if file_exists(fname): os.rename(fname, fname + ".orig") +def _ensure_nonempty_string(string, message): + if not isinstance(string, str) or not string: + raise ValueError(message) + # uses gpg to compress and encrypt a file def encrypt_file(source, dest, password, workdir = None): - if type(source) is not StringType or not len(source): - raise ValueError('Missing Source File') + _ensure_nonempty_string(source, 'Missing Source File') #stat it so that we get back an exception if it does no t exist os.stat(source) - if type(dest) is not StringType or not len(dest): - raise ValueError('Missing Destination File') - - if type(password) is not StringType or not len(password): - raise ValueError('Missing Password') + _ensure_nonempty_string(dest, 'Missing Destination File') + _ensure_nonempty_string(password, 'Missing Password') #create a tempdir so that we can clean up with easily tempdir = tempfile.mkdtemp('', 'ipa-', workdir) @@ -458,16 +457,12 @@ def encrypt_file(source, dest, password, workdir = None): def decrypt_file(source, dest, password, workdir = None): - if type(source) is not StringType or not len(source): - raise ValueError('Missing Source File') + _ensure_nonempty_string(source, 'Missing Source File') #stat it so that we get back an exception if it does no t exist os.stat(source) - if type(dest) is not StringType or not len(dest): - raise ValueError('Missing Destination File') - - if type(password) is not StringType or not len(password): - raise ValueError('Missing Password') + _ensure_nonempty_string(dest, 'Missing Destination File') + _ensure_nonempty_string(password, 'Missing Password') #create a tempdir so that we can clean up with easily tempdir = tempfile.mkdtemp('', 'ipa-', workdir) -- cgit