wplayer

Artifact Content
Login

Artifact 987a37b4f0118810e7a92058a6a254b4ec788e59c5e69efe0231669daf2f0cd4:


#
# utils.py
#	Various utilities
#
import threading, string, os

# A "with" serialization mechanism
class Exclusion(object):
    def __init__(self):
	self.mutex = threading.Lock()
    def __enter__(self):
	self.mutex.acquire()
    def __exit__(self, typ, val, traceback):
	self.mutex.release()

# Filter out to just simple ASCII chars
def toascii(s):
    return str(filter(lambda c: c in string.printable, s))

# Load file index into memory
# This should be a flat text file, sorted, which we will use
#  to provide search
# Return (file-name, cur-st_mtime, ( (line, line.lower()), ...))
def load_index(fn):
    f = open(fn, 'r')

    # Ugh, we need both the original path, and the case-flattened
    #  variant for loose search matching
    idx = tuple( (l.strip(), l.strip().lower()) for l in f )
    tup = (fn, os.fstat(f.fileno()).st_mtime, idx)

    f.close()
    return tup