All pastes #913389 Raw Edit

ode.pxd formatter

public python v1 · immutable
·published 2008-02-22 00:38 UTC
rendered paste body
from __future__ import with_statementimport osfilenames = ['compatibility.h', 'odecpp.h', 'odecpp_collision.h', 'odecpp_old.h', \             'ode.h'] #files to skipfilenames = set(filenames)next_line = 'false'functions = []#deletes any values out of values_list that matches valuedef delete_value (value, values_list) :  for ind_value in values_list :    if ind_value == value :      values_list.remove(ind_value)  return values_listdef wrap(text, width):  """  A word-wrap function that preserves existing line breaks  and most spaces in the text. Expects that existing line  breaks are posix newlines (\n).  """  return reduce(lambda line, word, width=width: '%s%s%s' %                (line,                 ' \n'[(len(line)-line.rfind('\n')-1                       + len(word.split('\n',1)[0]                            ) >= width)],                 word),                text.split(' ')               )cruft = open('cruft.txt','w')for root, dirs, files in os.walk('/usr/include/ode') :  for name in files :    if name in filenames:      #skip      pass    else :      with open('/usr/include/ode/'+name) as f :        for line in f:          if line[0:7] == 'ODE_API' or next_line == 'true' :#or line[0:7] == 'typedef':            if next_line != 'true' :              line = line[8:]              function = line[:line.find('(')].split(' ')              args = line[line.find('('):]              #next 6 lines format pointers correctly (var*, not var *)              if function[1] == '*' or function[1][:1] == '*' :                function[0] = function[0] + '*'                if function[1] == '*' :                  item = function.pop(1)                else :                  function[1] = function[1][1:]              function = delete_value('const',function)              function[0] = function[0].ljust(8)              function = ' '.join(function)              function = function.ljust(44)              functions.append(function + args)            else :              #args broken into several lines, so strip all whitespace on the left               functions[len(functions)-1] = functions[len(functions)-1][:-3] + line.lstrip()            next_line = 'false'            if line.find(';') == -1 :              #args broken into several lines              next_line = 'true'            else :              #get rid of the ;              functions[len(functions)-1] = functions[len(functions)-1][:-2] + '\n'          else :            cruft.write(line)cruft.close()ode_file = open('ode.pxd','w')for __index, function in enumerate(functions):  args = function[function.find('(')+1:-2].split(',')  for index, arg in enumerate(args) :    arg_chunks = arg.split(' ')    arg_chunks = delete_value('const',arg_chunks)    arg_chunks = delete_value('',arg_chunks)    for _index, _arg in enumerate(arg_chunks) :      arg_chunks[_index] = _arg.strip()    if len(arg_chunks) > 1 :      if arg_chunks[1][:1] == '*' :        arg_chunks[0] = arg_chunks[0] + '*'    #get every other list entry, starting at [0]    arg_chunks = arg_chunks[::2]    args[index] = ' '.join(arg_chunks)  ode_file.write(wrap('cdef ' + function[:function.find('(')] +  '( ' + ', '.join(args) + ' )', 80) + '\n')ode_file.close()