Skip to content

Commit

Permalink
ENH: read_mode
Browse files Browse the repository at this point in the history
- rU for python 2, r for python 3
- minor tweaks
  • Loading branch information
jeremygray committed Feb 13, 2014
1 parent f846c48 commit 417e363
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
15 changes: 10 additions & 5 deletions pyfilesec/__init__.py
Expand Up @@ -57,6 +57,11 @@
# import _pyperclip # if use --clipboard from commandline
# import _getpass # for no-display passphrase entry during RSA key-gen

if sys.version < '3':
read_mode = 'rU' # univeral newlines, for cross-platform transparency
else:
read_mode = 'r' # 3.x file open does universal newlines by default

"""TO-DO:
- move _enc, _dec into files in new dir codec/, sha256() and execfile() them
- use SecStr consistently throughout: in RsaKeys, generate, etc
Expand Down Expand Up @@ -308,15 +313,15 @@ def read(self, lines=0):
"""Return lines from self.file as a string; 0 means all lines.
"""
if not self.file:
return '(no file)'
return ''

if int(lines) < 1:
contents = open(self.file, 'rb').read() # all
contents = open(self.file, read_mode).read() # all
else:
if self.is_encrypted:
contents = open(self.file, 'rb').read(lines * 60) # .tgz file
contents = open(self.file, read_mode).read(lines * 60) # .tgz file
else:
contents = ''.join(open(self.file, 'rb').readlines()[:lines])
contents = ''.join(open(self.file, read_mode).readlines()[:lines])
if self.is_encrypted:
return b64encode(contents)
return contents
Expand Down Expand Up @@ -2610,7 +2615,7 @@ def _parse_args():
OPENSSL, openssl_version = set_openssl(args and args.openssl)
DESTROY_EXE, DESTROY_OPTS = set_destroy()

py64bit = (sys.maxsize == 9223372036854775807)
py64bit = bool(sys.maxsize == 2 ** 63 - 1)

# Register the default codec, runs auto-test
default_codec = {'_encrypt_rsa_aes256cbc': _encrypt_rsa_aes256cbc,
Expand Down
10 changes: 5 additions & 5 deletions which.py
Expand Up @@ -309,17 +309,17 @@ def main(argv):
try:
optlist, args = getopt.getopt(argv[1:], 'haVvqp:e:',
['help', 'all', 'version', 'verbose', 'quiet', 'path=', 'exts='])
except getopt.GetoptError, msg:
except getopt.GetoptError as msg:
sys.stderr.write("which: error: %s. Your invocation was: %s\n"\
% (msg, argv))
sys.stderr.write("Try 'which --help'.\n")
return 1
for opt, optarg in optlist:
if opt in ('-h', '--help'):
print _cmdlnUsage
print(_cmdlnUsage)
return 0
elif opt in ('-V', '--version'):
print "which %s" % __version__
print("which %s" % __version__)
return 0
elif opt in ('-a', '--all'):
all = 1
Expand Down Expand Up @@ -347,9 +347,9 @@ def main(argv):
nmatches = 0
for match in whichgen(arg, path=altpath, verbose=verbose, exts=exts):
if verbose:
print "%s (%s)" % match
print("%s (%s)" % match)
else:
print match
print(match)
nmatches += 1
if not all:
break
Expand Down

0 comments on commit 417e363

Please sign in to comment.