Ejemplo n.º 1
0
class Scheduler:
    frequency_help = trim_docstring(frequency_help)

    def __init__(self):
        try:
            self._sys_scheduler = get_scheduler()
        except PlatformError as e:
            raise SchedulerError('scheduler error: ' + str(e))

    def set_frequency(self, freq):
        assert type(freq) == str
        if freq is None:
            return

        cmd = const.scheduler_cmdline

        taskname = const.scheduler_task_name
        try:
            self._sys_scheduler.parse(freq)
            if self._sys_scheduler.exists(taskname):
                self._sys_scheduler.delete(taskname)

            r = self._sys_scheduler.schedule(freq, cmd, taskname)
            print('schedule creation %s..' % ('succeeded' if r else 'failed'))
        except FrequencyError as e:
            help = trim_docstring(self._sys_scheduler.parse.__doc__)
            raise SchedulerError('scheduler error: ' + str(e) + '\n' + help)

    def remove(self):
        taskname = const.scheduler_task_name
        if self._sys_scheduler.exists(taskname):
            r = self._sys_scheduler.delete(taskname)
            print('schedule deletion %s..' % ('succeeded' if r else 'failed'))
        else:
            print('no schedule exists..')
Ejemplo n.º 2
0
    def set_frequency(self, freq):
        assert type(freq) == str
        if freq is None:
            return

        cmd = const.scheduler_cmdline

        taskname = const.scheduler_task_name
        try:
            self._sys_scheduler.parse(freq)
            if self._sys_scheduler.exists(taskname):
                self._sys_scheduler.delete(taskname)

            r = self._sys_scheduler.schedule(freq, cmd, taskname)
            print('schedule creation %s..' % ('succeeded' if r else 'failed'))
        except FrequencyError as e:
            help = trim_docstring(self._sys_scheduler.parse.__doc__)
            raise SchedulerError('scheduler error: ' + str(e) + '\n' + help)
Ejemplo n.º 3
0
	def _format_usage(self, usage, actions, groups, prefix):
		help = ''
		usage = 'usage: ' + self._prog + ' '

		optionals = []
		positionals = []
		col1 = 0

		for action in actions:
			if action.option_strings:
				optionals.append(action)
				names_len = len(', '.join(action.option_strings)) + 2
				col1 = names_len if names_len > col1 else col1
			else:
				positionals.append(action)
				names_len = 0
				if action.__class__ == _SubParsersAction:
					names_len = max([len(n) for n in action.choices.keys()]) + 2
				else:
					names_len = len(action.dest) + 2
				col1 = names_len if names_len > col1 else col1

		terminal_width, _ = get_terminal_size()
		col2 = terminal_width - col1
		
		def format_help_lines(lines):
			out = ''
			first_line = True
			at_least_one_line = False

			for line in lines:
				if first_line:
					out += line
					first_line = False
				else:
					out += ('{0:<%d}{1}'%col1).format('', line)
				out += os.linesep
				at_least_one_line = True

			if not at_least_one_line:
				out += os.linesep
			return out

		def format_action(name, helptext, choices, default, is_action, hidden):
			if hidden:
				return ''

			out = ''
			def wrap(text):
				lines = []
				if text is not None and len(text) > 0 :
					lines = textwrap.wrap(text, col2)
				return lines

			help_lines = []
			if helptext is not None:
				for help_line in helptext.split('\n'):
					help_lines += wrap(help_line.strip())

			if choices is not None:
				choices = 'choices: ' + ', '.join(choices)

			choices_lines = wrap(choices)

			default_lines = []

			if not is_action:
				if not default in [None, SUPPRESS] :
					default = 'default: ' + str(default)
				else:
					default = None
				
				default_lines = wrap(default)

			out += ('{0:<%d}'%(col1)).format(name)			
			out += format_help_lines(help_lines + choices_lines + default_lines)

			return out

		pos_usage = ''
		has_subcmd = False
		for p in positionals:
			if p.__class__ == _SubParsersAction:
				has_subcmd = True
				help += os.linesep + 'subcommands:' + os.linesep
				for subcmd in p.choices.keys():
					help += format_action(subcmd, p.choices[subcmd].description, None, None, False, False)

				usage += 'subcommand [args...]'
		
			else:
				hidden = getattr(p, const.action_hidden_attr, False) or False
				help += format_action(p.dest, p.help, p.choices, p.default, False, hidden)
				pos_usage += '%s '%p.dest

		if len(optionals) > 0:
			usage += '[options] ' if not has_subcmd else ''
			help += os.linesep + 'options:' + os.linesep

		usage += pos_usage

		for o in optionals:
			name = ', '.join(o.option_strings)
			is_action = True if issubclass(o.__class__, Action) else False
			hidden = getattr(o, const.action_hidden_attr, False) or False

			help += format_action(name, o.help, o.choices, o.default, is_action, hidden)


		if self._extrahelp is not None:
			help += os.linesep + trim_docstring(self._extrahelp)

		usage += os.linesep + os.linesep
		if help == '':
			return None

		return usage + help
Ejemplo n.º 4
0
	def _format_text(self, text):
		return trim_docstring(text) + os.linesep
Ejemplo n.º 5
0
    def _format_usage(self, usage, actions, groups, prefix):
        help = ''
        usage = 'usage: ' + self._prog + ' '

        optionals = []
        positionals = []
        col1 = 0

        for action in actions:
            if action.option_strings:
                optionals.append(action)
                names_len = len(', '.join(action.option_strings)) + 2
                col1 = names_len if names_len > col1 else col1
            else:
                positionals.append(action)
                names_len = 0
                if action.__class__ == _SubParsersAction:
                    names_len = max([len(n)
                                     for n in action.choices.keys()]) + 2
                else:
                    names_len = len(action.dest) + 2
                col1 = names_len if names_len > col1 else col1

        terminal_width, _ = get_terminal_size()
        col2 = terminal_width - col1

        def format_help_lines(lines):
            out = ''
            first_line = True
            at_least_one_line = False

            for line in lines:
                if first_line:
                    out += line
                    first_line = False
                else:
                    out += ('{0:<%d}{1}' % col1).format('', line)
                out += os.linesep
                at_least_one_line = True

            if not at_least_one_line:
                out += os.linesep
            return out

        def format_action(name, helptext, choices, default, is_action, hidden):
            if hidden:
                return ''

            out = ''

            def wrap(text):
                lines = []
                if text is not None and len(text) > 0:
                    lines = textwrap.wrap(text, col2)
                return lines

            help_lines = []
            if helptext is not None:
                for help_line in helptext.split('\n'):
                    help_lines += wrap(help_line.strip())

            if choices is not None:
                choices = 'choices: ' + ', '.join(choices)

            choices_lines = wrap(choices)

            default_lines = []

            if not is_action:
                if not default in [None, SUPPRESS]:
                    default = 'default: ' + str(default)
                else:
                    default = None

                default_lines = wrap(default)

            out += ('{0:<%d}' % (col1)).format(name)
            out += format_help_lines(help_lines + choices_lines +
                                     default_lines)

            return out

        pos_usage = ''
        has_subcmd = False
        for p in positionals:
            if p.__class__ == _SubParsersAction:
                has_subcmd = True
                help += os.linesep + 'subcommands:' + os.linesep
                for subcmd in p.choices.keys():
                    help += format_action(subcmd,
                                          p.choices[subcmd].description, None,
                                          None, False, False)

                usage += 'subcommand [args...]'

            else:
                hidden = getattr(p, const.action_hidden_attr, False) or False
                help += format_action(p.dest, p.help, p.choices, p.default,
                                      False, hidden)
                pos_usage += '%s ' % p.dest

        if len(optionals) > 0:
            usage += '[options] ' if not has_subcmd else ''
            help += os.linesep + 'options:' + os.linesep

        usage += pos_usage

        for o in optionals:
            name = ', '.join(o.option_strings)
            is_action = True if issubclass(o.__class__, Action) else False
            hidden = getattr(o, const.action_hidden_attr, False) or False

            help += format_action(name, o.help, o.choices, o.default,
                                  is_action, hidden)

        if self._extrahelp is not None:
            help += os.linesep + trim_docstring(self._extrahelp)

        usage += os.linesep + os.linesep
        if help == '':
            return None

        return usage + help
Ejemplo n.º 6
0
 def _format_text(self, text):
     return trim_docstring(text) + os.linesep