Skip to content
This repository
tree: 3f42950656
Fetching contributors…

Cannot retrieve contributors at this time

file 154 lines (106 sloc) 3.061 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
#!/usr/bin/python

import ApiCodeFilter

# Import attribute <name> from module <name>

def importAttr(name):

  attr = getattr(__import__(name), name)
  return attr

# Output code to output file.

def outputCode(filename, code):

  code = codeJoin(code)

  code = ApiCodeFilter.expandtabs(code)
  code = ApiCodeFilter.trim(code)
  code = ApiCodeFilter.foldIfs(code)
  code = ApiCodeFilter.foldPreprocessorIfs(code)
  code = ApiCodeFilter.foldEmptyLines(code)
  code = ApiCodeFilter.foldImpossibleBreak(code)
  code = ApiCodeFilter.foldImpossibleReturn(code)
  code = ApiCodeFilter.foldRedundantCase(code)

  # Compare code with current file.

  try:

    # Read current file.

    current = open(filename, 'r')
    currentCode = current.read()
    current.close()

    # Compare code.

    if currentCode == code:
      print 'File skipped: %s' % filename
      return

  except IOError:

    # Current file does not exist.

    print 'File created: %s' % filename

  # Output code to file.

  try:

    output = open(filename, 'w')
    output.write(code)
    output.close()

    print 'File updated: %s' % filename

  except IOError:

    # File not found or file is read-only.

    print 'File read-only: %s' % filename

# Check if <version> is valid wrt <outputVersion>

def validVersion(version, outputVersion):

  if isinstance(version, int) or isinstance(version, float):
    if float(version) > outputVersion:
      return False

  return True

# Determine the maximum of some aspect of a collection of items

def maxLength(collection, aspect):
  ret = 0
  if collection is not None:
    for i in collection:
      if not i is None:
        if not aspect(i) is None:
          ret = max(ret,aspect(i))
  return ret

# Tidy-up code in list-of-strings format
# - Break multi-line strings into seperate strings
# - Strip extraneous whitespace from the end of each line
# - Collapse multiple empty lines into one

def codeTidy(lines):

  # In the first pass, deal with multi-line strings
  tmp = []
  for i in lines:
    for j in i.split('\n'):
      tmp.append(j.rstrip())

  # In the second pass, fold consecutive empty lines
  ret = []
  skipping = False
  for i in tmp:
    if not len(i):
      if skipping:
        continue
      else:
        skipping = True
    else:
      skipping = False
    ret.append(i)

  return ret

# Join list-of-strings code into a string

def codeJoin(lines):
  if lines==None or len(lines)==0:
    return ''

  if not isinstance(lines,list):
    lines = [lines]

  return '\n'.join(codeTidy(lines))

# Value as hex string, if possible

def hexValue(value,format = '0x%x'):

  if isinstance(value, str) or isinstance(value, unicode):
    try:
      value = long(value)
    except:
      pass

  if isinstance(value, int) or isinstance(value, long):
    return format%(value)

  return value

# Value to long, if possible, otherwise None

def toLong(value):
  tmp = 0
  try:
    tmp = long(value)
  except:
    return None
  return tmp

# void type?

def typeIsVoid(t):
  t = t.strip()
  return t=='void' or t=='VOID'
Something went wrong with that request. Please try again.