hello_cmd.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """DataLad demo command"""
  2. __docformat__ = 'restructuredtext'
  3. from os.path import curdir
  4. from os.path import abspath
  5. from datalad.interface.base import Interface
  6. from datalad.interface.base import build_doc
  7. from datalad.support.param import Parameter
  8. from datalad.distribution.dataset import datasetmethod
  9. from datalad.interface.utils import eval_results
  10. from datalad.support.constraints import EnsureChoice
  11. from datalad.interface.results import get_status_dict
  12. import logging
  13. lgr = logging.getLogger('datalad.helloworld.hello_cmd')
  14. # decoration auto-generates standard help
  15. @build_doc
  16. # all commands must be derived from Interface
  17. class HelloWorld(Interface):
  18. # first docstring line is used a short description in the cmdline help
  19. # the rest is put in the verbose help and manpage
  20. """Short description of the command
  21. Long description of arbitrary volume.
  22. """
  23. # parameters of the command, must be exhaustive
  24. _params_ = dict(
  25. # name of the parameter, must match argument name
  26. language=Parameter(
  27. # cmdline argument definitions, incl aliases
  28. args=("-l", "--language"),
  29. # documentation
  30. doc="""language to say "hello" in""",
  31. # type checkers, constraint definition is automatically
  32. # added to the docstring
  33. constraints=EnsureChoice('en', 'de')),
  34. )
  35. @staticmethod
  36. # decorator binds the command to the Dataset class as a method
  37. @datasetmethod(name='hello_cmd')
  38. # generic handling of command results (logging, rendering, filtering, ...)
  39. @eval_results
  40. # signature must match parameter list above
  41. # additional generic arguments are added by decorators
  42. def __call__(language='en'):
  43. if language == 'en':
  44. msg = 'Hello!'
  45. elif language == 'de':
  46. msg = 'Tachchen!'
  47. else:
  48. msg = ("unknown language: '%s'", language)
  49. # commands should be implemented as generators and should
  50. # report any results by yielding status dictionaries
  51. yield get_status_dict(
  52. # an action label must be defined, the command name make a good
  53. # default
  54. action='demo',
  55. # most results will be about something associated with a dataset
  56. # (component), reported paths MUST be absolute
  57. path=abspath(curdir),
  58. # status labels are used to identify how a result will be reported
  59. # and can be used for filtering
  60. status='ok' if language in ('en', 'de') else 'error',
  61. # arbitrary result message, can be a str or tuple. in the latter
  62. # case string expansion with arguments is delayed until the
  63. # message actually needs to be rendered (analog to exception
  64. # messages)
  65. message=msg)