__init__.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #
  2. # Copyright (C) 2008 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import os
  16. all_commands = {}
  17. my_dir = os.path.dirname(__file__)
  18. for py in os.listdir(my_dir):
  19. if py == '__init__.py':
  20. continue
  21. if py.endswith('.py'):
  22. name = py[:-3]
  23. clsn = name.capitalize()
  24. while clsn.find('_') > 0:
  25. h = clsn.index('_')
  26. clsn = clsn[0:h] + clsn[h + 1:].capitalize()
  27. mod = __import__(__name__,
  28. globals(),
  29. locals(),
  30. ['%s' % name])
  31. mod = getattr(mod, name)
  32. try:
  33. cmd = getattr(mod, clsn)()
  34. except AttributeError:
  35. raise SyntaxError('%s/%s does not define class %s' % (
  36. __name__, py, clsn))
  37. name = name.replace('_', '-')
  38. cmd.NAME = name
  39. all_commands[name] = cmd
  40. if 'help' in all_commands:
  41. all_commands['help'].commands = all_commands