#!/usr/bin/env python __doc__ = """A small, simple rc file library DEFAULT_RCFILE: multi-line string of the default rcfile RCPATH: name of the rcfile ("~/.(prog)rc") VALID_SETTINGS: list of valid strings for a setting """ import os, re, sys DEFAULT_RCFILE = """# default rcfile for pyarc/pyunarc # destination directory for decompression # %n = name of file, %e = extension destdir = %n # default compression mode # * 'flat' means extract only files to $destdir # * 'hierarchy' means preserve the hierarchy in the archive # * 'smart' detects a linear hierarchy and flattens it, # but extracts as a hierarchy otherwise mode = hierarchy # alternate executable paths # valid for %prog_path, where %prog is: # gzip, bzip2, tar, 7za, zip, rar # ex. #gzip_path = /usr/local/bin/gzip # alternate destdirs in form %prog_destdir # unextract gzip/bzip2 files to themselves gzip_destdir = . bzip2_destdir = . # alternate (from the default) compression modes zip_mode = smart rar_mode = flat """ # the path already has rc in it.. clever! RCPATH = "~/.pyunarc" VALID_SETTINGS = ['destdir', 'mode'] __progs = ['gzip', 'bzip2', 'tar', '7za', 'zip', 'rar'] VALID_SETTINGS += map(lambda x: x + '_path', __progs) VALID_SETTINGS += map(lambda x: x + '_destdir', __progs) VALID_SETTINGS += map(lambda x: x + '_mode', __progs) class rcFile: def __init__(self, path=RCPATH, settings=VALID_SETTINGS): self.path = os.path.expanduser(path) self.settings = settings self.values = {} self.init() def __str__(self): return str(self.values) def __getitem__(self, index): if self.values.has_key(index): return self.values[index] return None def init(self): "read in the rcfile (or write the default one), returns values dict" rcfile = self.readrc() if not rcfile: rcfile = DEFAULT_RCFILE.split("\n") # artificially add some \n's for after the split rcfile = map(lambda(x): x + "\n", rcfile) try: self.writerc() except: print >> sys.stderr, "Using default options for rcfile." self.values = self.parserc(rcfile) return self.values def readrc(self): "read rcfile and returns list of lines, or False on i/o error" try: rc = open(self.path, 'r') # the rcfile doesn't exist, we should write the default except IOError, (errno, strerror): print >> sys.stderr, "I/O error(%s): %s" % (errno, strerror) return False rcfile = rc.readlines() rc.close() return rcfile # returns a dictionary[key] = value def parserc(self, lines): "simple parser extracts 'setting = value', returns dict" vals = {} re_comment = re.compile(r"^\s*#.*") for line in lines: if re_comment.match(line): pass else: parts = line.split("=") setting = parts[0].strip() # for now, only "setting = value" is allowed if len(parts) != 2: continue if setting not in self.settings: continue vals[setting] = parts[1].strip() return vals # this will only ever write the default RC file def writerc(self): "write out value stored in DEFAULT_RCFILE to 'path'" global DEFAULT_RCFILE try: rc = open(self.path, 'w') except IOError, (errno, strerror): print >> sys.stderr, "I/O error(%s): %s" % (errno, strerror) raise rc.write(DEFAULT_RCFILE) return True