/Misc/TextMate/Python-Dev.tmbundle/Commands/2 to 3 - Module Deletion.tmCommand

http://unladen-swallow.googlecode.com/ · Unknown · 64 lines · 54 code · 10 blank · 0 comment · 0 complexity · ca9d3e59393af9680d96aac24f4b8758 MD5 · raw file

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <key>beforeRunningCommand</key>
  6. <string>nop</string>
  7. <key>command</key>
  8. <string>#!/usr/bin/python
  9. """Generate code to warn about a module's removal in Python 3.0.
  10. XXX Not supported:
  11. - Module's in a package do not have their full name generated.
  12. - Package's __init__ module; should detect and use the package's name instead.
  13. """
  14. py_template = """from warnings import warnpy3k
  15. warnpy3k("the ${1:%s} module has been removed in Python 3.0", stacklevel=2)
  16. del warnpy3k$0"""
  17. c_template = """
  18. if (PyErr_WarnPy3k("the ${1:%s} module has been removed in "
  19. "Python 3.0", 2) &lt; 0)
  20. return;$0"""
  21. import imp
  22. import os
  23. file_name = os.path.split(os.environ['TM_FILEPATH'])[1]
  24. py_suffixes = reversed(sorted((suffix[0] for suffix in imp.get_suffixes() if suffix[2] == imp.PY_SOURCE), key=len))
  25. c_suffixes = reversed(sorted((os.path.splitext(suffix[0])[0] + '.c'
  26. for suffix in imp.get_suffixes() if suffix[2] == imp.C_EXTENSION), key=len))
  27. pairings = ((py_suffixes, py_template), (c_suffixes, c_template))
  28. def create_template(suffixes, template):
  29. for suffix in suffixes:
  30. if not file_name.endswith(suffix):
  31. continue
  32. module_name = file_name[:-len(suffix)]
  33. return template % module_name
  34. else:
  35. return None
  36. for template in (create_template(*pair) for pair in pairings):
  37. if not template:
  38. continue
  39. print template,
  40. break
  41. else:
  42. print 'XXX Could not generate code.'</string>
  43. <key>input</key>
  44. <string>none</string>
  45. <key>name</key>
  46. <string>2 to 3 - Module Deletion</string>
  47. <key>output</key>
  48. <string>insertAsSnippet</string>
  49. <key>tabTrigger</key>
  50. <string>2to3moddel</string>
  51. <key>uuid</key>
  52. <string>9519C22B-6AB8-41A1-94F6-079E0B45C147</string>
  53. </dict>
  54. </plist>