/Mac/Demo/resources/listres.py

http://unladen-swallow.googlecode.com/ · Python · 60 lines · 51 code · 8 blank · 1 comment · 9 complexity · b294fbdfcefa061a3ff5eafe922354f4 MD5 · raw file

  1. # List all resources
  2. from Carbon import Res
  3. from Carbon.Resources import *
  4. def list1resources():
  5. ntypes = Res.Count1Types()
  6. for itype in range(1, 1+ntypes):
  7. type = Res.Get1IndType(itype)
  8. print "Type:", repr(type)
  9. nresources = Res.Count1Resources(type)
  10. for i in range(1, 1 + nresources):
  11. Res.SetResLoad(0)
  12. res = Res.Get1IndResource(type, i)
  13. Res.SetResLoad(1)
  14. info(res)
  15. def listresources():
  16. ntypes = Res.CountTypes()
  17. for itype in range(1, 1+ntypes):
  18. type = Res.GetIndType(itype)
  19. print "Type:", repr(type)
  20. nresources = Res.CountResources(type)
  21. for i in range(1, 1 + nresources):
  22. Res.SetResLoad(0)
  23. res = Res.GetIndResource(type, i)
  24. Res.SetResLoad(1)
  25. info(res)
  26. def info(res):
  27. print res.GetResInfo(), res.SizeResource(), decodeattrs(res.GetResAttrs())
  28. attrnames = {
  29. resChanged: 'Changed',
  30. resPreload: 'Preload',
  31. resProtected: 'Protected',
  32. resLocked: 'Locked',
  33. resPurgeable: 'Purgeable',
  34. resSysHeap: 'SysHeap',
  35. }
  36. def decodeattrs(attrs):
  37. names = []
  38. for bit in range(16):
  39. mask = 1<<bit
  40. if attrs & mask:
  41. if attrnames.has_key(mask):
  42. names.append(attrnames[mask])
  43. else:
  44. names.append(hex(mask))
  45. return names
  46. def test():
  47. print "=== Local resourcess ==="
  48. list1resources()
  49. print "=== All resources ==="
  50. listresources()
  51. if __name__ == '__main__':
  52. test()