import os
import tempfile
from stat import S_IRUSR,S_IWUSR,S_IRGRP,S_IROTH

def extract_tags(dir):
    """
    Assume that tags appropriate to the entry are encoded in the path.
    Each subdirectory represents a tag to apply to the entry.
    """
    dir, tag = os.path.split(dir)
    if tag == '.': return []
    return [tag] + extract_tags(dir)

def tag_entry(dir, f):
    """
    Given a pyblosxom blog entry at dir/f, extract tags from the pathname
    and apply them to the entry as metadata compatible with the tags and 
    tagcloud plugins. Entry modification and access times are preserved.
    """
    if not f.endswith(".txt"):
        return

    tags = extract_tags(dir)
    if not tags:
        print "%s: No tags found." % dir
        return

    p = os.path.join(dir, f)

    # Save modifcation and access times from original entry.
    s = os.stat(p)
    t = (s.st_atime, s.st_mtime)

    entry = open(p, "r").readlines()
    for l in entry[1:]:
        if not l.startswith('#'): break
        if l.startswith('#tags'): 
            # entry already has tags; don't overwrite them.
            print "%s: already tagged (%s)" % (p, l.strip())
            return
    tags = "#tags %s\n\n" % ', '.join(tags)
    entry.insert(1, tags);

    outf,outp = tempfile.mkstemp()

    os.write(outf, ''.join(entry));
    os.close(outf)

    os.chmod(outp, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
    os.utime(outp, t)
    os.rename(outp, p);


def main():
    """
    Run script in directory containing subdirectories you wish to tag.
    Subdirectories you don't want to tag should be listed in 'ignore_dirs'.
    """
    ignore_dirs = ['./general']
    tree = os.walk('.')
    for e in tree:
        if e[0] in ignore_dirs:
            continue
        for f in e[2]:
            tag_entry(e[0], f)

if __name__=="__main__":
    main()
