commands.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. """
  3. plan.commands
  4. ~~~~~~~~~~~~~
  5. Command line tools for Plan.
  6. :copyright: (c) 2014 by Shipeng Feng.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import os
  10. import click
  11. from ._compat import get_binary_content
  12. class Echo(object):
  13. """Echo class for Plan. This is used to echo some common used content
  14. type in the command line.
  15. """
  16. @classmethod
  17. def echo(cls, message):
  18. click.echo(message)
  19. @classmethod
  20. def secho(cls, *args, **kwargs):
  21. click.secho(*args, **kwargs)
  22. @classmethod
  23. def message(cls, message):
  24. cls.secho("[message] %s" % message, fg="green")
  25. @classmethod
  26. def write(cls, message):
  27. cls.secho("[write] %s" % message, fg="green")
  28. @classmethod
  29. def fail(cls, message):
  30. cls.secho("[fail] %s" % message, fg="red")
  31. @classmethod
  32. def add(cls, message):
  33. cls.secho("[add] %s" % message, fg="green")
  34. @classmethod
  35. def done(cls, message=None):
  36. if message:
  37. cls.secho("[done] %s" % message, fg="green")
  38. else:
  39. cls.secho("[done]!", fg="green")
  40. SCHEDULE_TEMPLATE = """\
  41. # -*- coding: utf-8 -*-
  42. # Use this file to easily define all of your cron jobs.
  43. #
  44. # It's helpful to understand cron before proceeding.
  45. # http://en.wikipedia.org/wiki/Cron
  46. #
  47. # Learn more: http://github.com/fengsp/plan
  48. from plan import Plan
  49. cron = Plan()
  50. # register one command, script or module
  51. # cron.command('command', every='1.day')
  52. # cron.script('script.py', path='/web/yourproject/scripts', every='1.month')
  53. # cron.module('calendar', every='feburary', at='day.3')
  54. if __name__ == "__main__":
  55. cron.run()
  56. """
  57. @click.command()
  58. @click.option('--path', default='./schedule.py',
  59. help='The filepath for your schedule file.')
  60. def quickstart(path):
  61. """plan-quickstart"""
  62. write = True
  63. if os.path.isfile(path):
  64. write = click.confirm("'%s' already exists, override?" % path)
  65. if write:
  66. Echo.add("writing '%s'" % path)
  67. with open(path, 'wb') as f:
  68. f.write(get_binary_content(SCHEDULE_TEMPLATE))
  69. Echo.done()
  70. def prompt_choices(name, choices):
  71. """One wrapper function for click.prompt to show choices to the user.
  72. """
  73. return click.prompt(name + ' - (%s)' % ', '.join(choices),
  74. type=click.Choice(choices), default=choices[0])
  75. if __name__ == "__main__":
  76. quickstart()