def test_lower_casing(self): options = Options() options.casing_type = CasingTypeEnum.LowerCase ceh = ExpressionDescriptor("* * * * *", options) self.assertEqual( "every minute", ceh.get_description(DescriptionTypeEnum.FULL))
def test_invalid_cron_expression_error(self): options = Options() options.throw_exception_on_parse_error = False ceh = ExpressionDescriptor("INVALID CRON", options) self.assertEqual( "Error: Expression only has 2 parts. At least 5 part are required.", ceh.get_description(DescriptionTypeEnum.FULL))
def test_title_casing(self): options = Options() options.casing_type = CasingTypeEnum.Title ceh = ExpressionDescriptor("* * * * *", options) self.assertEqual( "Every Minute", ceh.get_description(DescriptionTypeEnum.FULL))
def test_inline_import(self): from cron_descriptor import Options, DescriptionTypeEnum, ExpressionDescriptor options = Options() options.use_24hour_time_format = True ceh = ExpressionDescriptor("* * * * *", options) self.assertEqual("Every minute", ceh.get_description(DescriptionTypeEnum.FULL))
def test_none_cron_expression_error(self): options = Options() options.throw_exception_on_parse_error = False ceh = ExpressionDescriptor(None, options) self.assertEqual( "Field 'ExpressionDescriptor.expression' not found.", ceh.get_description(DescriptionTypeEnum.FULL))
def test_full(self): options = Options() options.use_24hour_time_format = True ceh = ExpressionDescriptor("* * * * *", options) self.assertEqual( "Every minute", ceh.get_description(DescriptionTypeEnum.FULL))
def get_pretty(expression): import sys # By default the module is installed in the 2.7 branch, pyotherside uses python 3 # We use a custom location sys.path.append("/usr/share/harbour-sailcron/python/") # https://github.com/Salamek/cron-descriptor from cron_descriptor import Options, CasingTypeEnum, DescriptionTypeEnum, ExpressionDescriptor import subprocess # get proper 24/12 hour notation and strip output output = subprocess.check_output("/usr/bin/dconf read /sailfish/i18n/lc_timeformat24h", shell=True) output = str(output).replace("'", "").replace('b"', "").replace('\\n"', "").strip() if output == "24": is24h = True else: is24h = False # set options options = Options() options.throw_exception_on_parse_error = False options.casing_type = CasingTypeEnum.Sentence options.use_24hour_time_format = is24h descripter = ExpressionDescriptor(expression, options) human_format = descripter.get_description(DescriptionTypeEnum.FULL) return human_format
def test_inline_import(self): from cron_descriptor import Options, DescriptionTypeEnum, ExpressionDescriptor options = Options() options.use_24hour_time_format = True ceh = ExpressionDescriptor("* * * * *", options) self.assertEqual( "Every minute", ceh.get_description(DescriptionTypeEnum.FULL))
def test_full_import(self): from cron_descriptor.Options import Options from cron_descriptor.DescriptionTypeEnum import DescriptionTypeEnum from cron_descriptor.ExpressionDescriptor import ExpressionDescriptor options = Options() options.use_24hour_time_format = True ceh = ExpressionDescriptor("* * * * *", options) self.assertEqual('Every minute', ceh.get_description(DescriptionTypeEnum.FULL))
def test_full_import(self): from cron_descriptor.Options import Options from cron_descriptor.DescriptionTypeEnum import DescriptionTypeEnum from cron_descriptor.ExpressionDescriptor import ExpressionDescriptor options = Options() options.use_24hour_time_format = True ceh = ExpressionDescriptor("* * * * *", options) self.assertEqual( 'Every minute', ceh.get_description(DescriptionTypeEnum.FULL))
def description(self, **kw): """ Returns a description of the crontab's schedule (if available) **kw - Keyword arguments to pass to cron_descriptor (see docs) """ try: from cron_descriptor import ExpressionDescriptor except ImportError: raise ImportError("cron_descriptor not available. Please install"\ "cron_descriptor python module via pip or your package manager") exdesc = ExpressionDescriptor(self.slices.clean_render(), **kw) return exdesc.get_description()
def cronIntervalToDescription(interval): """ Convert a crontab interval to a human readable format :param interval: crontab interval :type interval: str :return: readable format or None :rtype: str|None """ try: descriptor = ExpressionDescriptor(interval, use_24hour_time_format=True) return descriptor.get_description() except: return None
def __init__(self, cron: str, timezone: Timezone) -> None: self._expression = cron_presets.get(cron, cron) self._timezone = timezone descriptor = ExpressionDescriptor(expression=self._expression, casing_type=CasingTypeEnum.Sentence, use_24hour_time_format=True) try: # checking for more than 5 parameters in Cron and avoiding evaluation for now, # as Croniter has inconsistent evaluation with other libraries if len(croniter(self._expression).expanded) > 5: raise FormatException() interval_description = descriptor.get_description() except (CroniterBadCronError, FormatException, MissingFieldException): interval_description = "" self.description = interval_description
def get_pretty(line_nbr, expression): """Get a human readable string for the cron expression""" import sys import subprocess import pyotherside # By default the module is installed in the 2.7 branch, pyotherside uses python 3 # We use a custom location sys.path.append("/usr/share/harbour-sailcron/python/") # https://github.com/Salamek/cron-descriptor from cron_descriptor import Options, CasingTypeEnum, DescriptionTypeEnum, ExpressionDescriptor # get proper 24/12 hour notation and strip output output = subprocess.check_output( "/usr/bin/dconf read /sailfish/i18n/lc_timeformat24h", shell=True) output = str(output).replace("'", "").replace('b"', "").replace('\\n"', "").strip() is24h = bool(output == "24") SPECIALS = { "reboot": '@reboot', "hourly": '0 * * * *', "daily": '0 0 * * *', "weekly": '0 0 * * 0', "monthly": '0 0 1 * *', "yearly": '0 0 1 1 *', "annually": '0 0 1 1 *', "midnight": '0 0 * * *' } key = expression.lstrip('@').rstrip(' ').lower() if key in SPECIALS.keys(): expression = SPECIALS[key] # set options options = Options() options.throw_exception_on_parse_error = False options.casing_type = CasingTypeEnum.Sentence options.use_24hour_time_format = is24h if expression == "@reboot": pyotherside.send('result', line_nbr, expression) human_format = "reboot" else: descripter = ExpressionDescriptor(expression, options) human_format = descripter.get_description(DescriptionTypeEnum.FULL) pyotherside.send('result', line_nbr, human_format) return human_format
def test_none_cron_expression_error(self): self.options.throw_exception_on_parse_error = False ceh = ExpressionDescriptor(None, self.options) self.assertEqual("Field 'ExpressionDescriptor.expression' not found.", ceh.get_description(DescriptionTypeEnum.FULL))
def test_sentence_casing(self): options = Options() options.casing_type = CasingTypeEnum.Sentence ceh = ExpressionDescriptor("* * * * *", options) self.assertEqual("Every minute", ceh.get_description(DescriptionTypeEnum.FULL))
def test_title_casing(self): self.options.casing_type = CasingTypeEnum.Title ceh = ExpressionDescriptor("* * * * *", self.options) self.assertEqual("Every Minute", ceh.get_description(DescriptionTypeEnum.FULL))
def get_descricao(self, cron_expressao): descripter = ExpressionDescriptor(cron_expressao, self.options) return descripter.get_description()
def test_lower_casing(self): self.options.casing_type = CasingTypeEnum.LowerCase ceh = ExpressionDescriptor("* * * * *", self.options) self.assertEqual("every minute", ceh.get_description(DescriptionTypeEnum.FULL))
def register_job(self, workspace, job_class, log_in_db=False): jobkey = "" workspace_name = None if workspace is not None: if type(workspace) is str: jobkey += workspace + '/' workspace_name = workspace.name else: jobkey += workspace.name + '/' workspace_name = workspace.name jobInstance = job_class() jobkey += jobInstance.name jobInstance.job_key = jobkey if workspace is not None: jobInstance.workspace = workspace.name job = { 'job_class': job_class, 'name': jobInstance.name, 'workspace': workspace_name, 'description': jobInstance.description, 'parameters': jobInstance.parameters, 'trigger': 'Internal', 'log_in_db': log_in_db, 'cron': jobInstance.cron, 'day': jobInstance.day, 'week': jobInstance.week, 'day_of_week': jobInstance.day_of_week, 'hour': jobInstance.hour, 'minute': jobInstance.minute, 'second': jobInstance.second } if jobInstance.cron is True: cron_list = [] if job_class.minute is None: cron_list.append("*") else: cron_list.append(job_class.minute) if job_class.hour is None: cron_list.append("*") else: cron_list.append(job_class.hour) if job_class.day is None: cron_list.append("*") else: cron_list.append(job_class.day) cron_list.append("*") if job_class.day_of_week is None: cron_list.append("*") else: cron_list.append(job_class.day_of_week) cron_string = " ".join(cron_list) options = Options() options.throw_exception_on_parse_error = False options.day_of_week_start_index_zero = True options.use_24hour_time_format = True options.casing_type = CasingTypeEnum.LowerCase descripter = ExpressionDescriptor(cron_string, options) logManager.info("Register repetitive job '{}' triggered {}".format( jobkey, descripter.get_description())) self.scheduler.add_job(jobInstance.start_job, kwargs=({ "job_id": str(jobkey) }), id=(str(jobkey)), trigger='cron', replace_existing=True, day=job_class.day, day_of_week=job_class.day_of_week, week=job_class.week, hour=job_class.hour, minute=job_class.minute, second=job_class.second) job['trigger'] = descripter.get_description() self.jobs[str(jobkey)] = ObjDict(job.copy())