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_title_casing(self): options = Options() options.casing_type = CasingTypeEnum.Title ceh = ExpressionDescriptor("* * * * *", options) self.assertEqual( "Every Minute", ceh.get_description(DescriptionTypeEnum.FULL))
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 parse_expression(self): options = Options() options.throw_exception_on_parse_error = True options.casing_type = CasingTypeEnum.Sentence options.use_24hour_time_format = True return ExpressionDescriptor(self.expression, options).get_description( DescriptionTypeEnum.FULL)
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_lower_casing(self): options = Options() options.casing_type = CasingTypeEnum.LowerCase ceh = ExpressionDescriptor("* * * * *", options) self.assertEqual("every minute", 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 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())