Skip to content

Commit

Permalink
fixed style issues in file utils, although many of these are deprecat…
Browse files Browse the repository at this point in the history
…ed and will go away soon
  • Loading branch information
pjuren committed Dec 16, 2015
1 parent f0829ce commit e349c73
Showing 1 changed file with 47 additions and 28 deletions.
75 changes: 47 additions & 28 deletions src/pyokit/util/fileUtils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Date of Creation: 22nd May 2011
Description: functions for manupulating files, filehandles and filenames
Description: functions for manupulating files, filehandles and filenames
Copyright (C) 2011-2014
Philip J. Uren
Expand All @@ -22,58 +22,71 @@
"""


import sys, os
import sys
import os
import random
from copy import copy
from pyokit.util.progressIndicator import ProgressIndicator


def genericFileIterator(fn, verbose=False):
"""
@summary: iterate over a file, returning non-blank lines
@param fn: either a string representing the name of the file or a file object
@param fn: either a string representing the name of the file or a file
object
@param verbose: if True, output status messages to stderr
"""
if type(fn).__name__ == "str" : fh = open(fn)
else : fh = fn

if verbose :
try :
pind = ProgressIndicator(totalToDo = os.path.getsize(fh.name),
messagePrefix = "completed",
messageSuffix = "of processing " +\
fh.name)
if type(fn).__name__ == "str":
fh = open(fn)
else:
fh = fn

if verbose:
try:
pind = ProgressIndicator(totalToDo=os.path.getsize(fh.name),
messagePrefix="completed",
messageSuffix="of processing " + fh.name)
junk = fh.tell()
except :
sys.stderr.write("Cannot show progress for stream.. doesn't behave like a file")
except:
sys.stderr.write("Cannot show progress for stream.. doesn't behave "
"like a file")
verbose = False

for line in fh :
if verbose :
for line in fh:
if verbose:
pind.done = fh.tell()
pind.showProgress()
line = line.strip()
if line == "" : continue
if line == "":
continue
yield line

def getUniqueFilename(dir = None, base = None):

def getUniqueFilename(dir=None, base=None):
"""
DESCRP: Generate a filename in the directory <dir> which is
unique (i.e. not in use at the moment)
PARAMS: dir -- the directory to look in. If None, use CWD
base -- use this as the base name for the filename
RETURN: string -- the filename generated
"""
while True :
fn = str(random.randint(0,100000)) + ".tmp"
if not os.path.exists(fn) : break
while True:
fn = str(random.randint(0, 100000)) + ".tmp"
if not os.path.exists(fn):
break
return fn


def linesInFile(fd):
if type(fd).__name__ == "str" : f = open(fd)
else : f = fd
if type(fd).__name__ == "str":
f = open(fd)
else:
f = fd
t = sum(1 for line in f)
f.close()
return t


def openFD(fd):
"""
@summary: given a descriptor for a file (e.g. path or file stream), will
Expand All @@ -83,14 +96,20 @@ def openFD(fd):
@raise IOError: if a new stream reset to the begining of the file cannot
be constructed
"""
if type(fd).__name__ == "str" : return open(fd)
if type(fd).__name__ == "file" : return open(fd.name)
if type(fd).__name__ == "mmap" : return fd
if type(fd).__name__ == "str":
return open(fd)
if type(fd).__name__ == "file":
return open(fd.name)
if type(fd).__name__ == "mmap":
return fd
nfd = copy(fd)
nfd.reset()
return nfd


def getFDName(fd):
if type(fd).__name__ == "str" : return fd
if type(fd).__name__ == "file" : return fd.name
if type(fd).__name__ == "str":
return fd
if type(fd).__name__ == "file":
return fd.name
return fd.name

0 comments on commit e349c73

Please sign in to comment.