Paste number 307448: experimental press generator

Paste number 307448: experimental press generator
Pasted by: phf
When:6 years, 4 months ago
Share:Tweet this! | http://paste.lisp.org/+6L88
Channel:None
Paste contents:
Raw Source | XML | Display As
comment = ''' /****************************\\
*      EXPERIMENTAL BRANCH.    *
*    FOR LABORATORY USE ONLY.  *
********************************
          ************
         **************
        ****************
       ****   ****   ****
       ***     ***    ***
       ***     ***    ***
        ***    * *    **
        ******** ********
        *******   ******
            ***   **
          *  ******* **
          ** * * * * *
    **     *         *     ***
   ****    * *     * *    ****
   ****    *** * * **     ***
    ****    *********   ******
   *******    *****    *******
   *********        ****** **
    **   ******   ******
           **  *******       **
   **       *******         ***
  ****   ********  ************
  ************    ************
   ********             *******
  ******                   ****
   ***                      ***
********************************'''

from os import walk, remove
from sys import argv
from os.path import splitext, join, dirname
from tempfile import NamedTemporaryFile
from shutil import copyfileobj, move

def exact(filename): return lambda fn: filename==fn
def byext(ext): return lambda filename: splitext(filename)[1] == ext
def bybase(base): return lambda filename: splitext(filename)[0] == base

def check(filename, text):
    with open(filename, 'r') as inp:
        return text == inp.read(len(text))

def prepend(filename, text):
    tmp = NamedTemporaryFile(dir=dirname(filename), delete=False)
    tmp.write(text)
    with open(filename, 'r') as inp:
        copyfileobj(inp, tmp)
    tmp.close()
    move(tmp.name, filename)

def prepending(text, prefix=None):
    if prefix:
        text = prefix + ('\n{}'.format(prefix).join(text.split('\n')))
    text = text + '\n'
    def _(filename):
        if not check(filename, text):
            prepend(filename, text)
    return _

def pc(prefix=None): return prepending(comment, prefix)

commenters = [
    (exact('COPYING')    , pc()),
    (exact('README')     , pc()),
    (byext('.txt')       , pc()),
    (byext('.h')         , pc('// ')),
    (byext('.cpp')       , pc('// ')),
    (exact('.gitignore') , pc('# ')),
    (bybase('makefile')  , pc('# ')),
]

for dirpath, dirnames, filenames in walk(argv[1]):
    for filename in filenames:
        for test, handler in commenters:
            if test(filename):
                handler(join(dirpath, filename))
                break
        else:
            raise Exception('missing handler for %s' % filename)

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.