コード例 #1
0
    def __init__(self, schema_parser, schema_name):
        self.profile_opts = {
            # Task execution time
            'hour': self._set_hour,
            'minute': self._set_minute,
            'month': self._set_month,
            'day': self._set_day,
            'weekday': self._set_weekday,
        }

        self.options = {
            # Task scheduling profile
            'profile': self._set_profile,
            # Task command
            'command': self._set_command,
            # Task options
            'enabled': self._set_enabled,
            'addtoinv': self._set_addtoinv,
            'saveto': self._set_saveto,
            'mailto': self._set_mailto,
            'smtp': self._set_smtp
        }

        self.__schema_name = schema_name
        self.schema_parser = schema_parser
        self.last_check = None
        self.cron_parser = CronParser()
        self.profiles = Path.sched_profiles
        self.setup_schema(self.schema_name)
コード例 #2
0
ファイル: Scheduler.py プロジェクト: aregee/network-scanner
    def __init__(self, schema_parser, schema_name):
        self.profile_opts = {
            # Task execution time
            'hour':self._set_hour,
            'minute':self._set_minute,
            'month':self._set_month,
            'day':self._set_day,
            'weekday':self._set_weekday,
        }

        self.options = { 
            # Task scheduling profile
            'profile':self._set_profile,
            # Task command
            'command':self._set_command,
            # Task options
            'enabled':self._set_enabled,
            'addtoinv':self._set_addtoinv,
            'saveto':self._set_saveto,
            'mailto':self._set_mailto,
            'smtp':self._set_smtp
        }

        self.__schema_name = schema_name
        self.schema_parser = schema_parser
        self.last_check = None
        self.cron_parser = CronParser()
        self.profiles = Path.sched_profiles
        self.setup_schema(self.schema_name)
コード例 #3
0
class SchedSchema(object):
    """
    Parse scheduled schemas.
    """
    def __init__(self, schema_parser, schema_name):
        self.profile_opts = {
            # Task execution time
            'hour': self._set_hour,
            'minute': self._set_minute,
            'month': self._set_month,
            'day': self._set_day,
            'weekday': self._set_weekday,
        }

        self.options = {
            # Task scheduling profile
            'profile': self._set_profile,
            # Task command
            'command': self._set_command,
            # Task options
            'enabled': self._set_enabled,
            'addtoinv': self._set_addtoinv,
            'saveto': self._set_saveto,
            'mailto': self._set_mailto,
            'smtp': self._set_smtp
        }

        self.__schema_name = schema_name
        self.schema_parser = schema_parser
        self.last_check = None
        self.cron_parser = CronParser()
        self.profiles = Path.sched_profiles
        self.setup_schema(self.schema_name)

    def job_to_run(self):
        """
        Check if there is a scheduled job for now and if there is,
        return True, otherwise False.
        """
        cur_time = time.localtime()

        # check if we should check for job or not
        if self.last_check and cur_time[1] == self.last_check[1] and \
            cur_time[2] == self.last_check[2] and \
            cur_time[3] == self.last_check[3] and \
            cur_time[4] == self.last_check[4] and \
            cur_time[6] == self.last_check[6]:

            return False  # too early to run a job again!

        if cur_time[1] in self.month and cur_time[2] in self.day \
            and cur_time[3] in self.hour and cur_time[4] in self.minute \
            and cur_time[6] in self.weekday:

            self.last_check = cur_time
            return True  # there is a job to run!

    def setup_schema(self, name):
        """
        Setup schema.
        """
        if self.schema_parser.has_section(name):
            for opt in self.schema_parser.options(name):
                if opt in self.options:
                    self.options[opt](self.schema_parser.get(name, opt))

            self.load_profile()

    def load_profile(self):
        """
        Load scheduling profile for current schema.
        """
        p_cfg = ConfigParser()
        p_cfg.read(self.profiles)

        if p_cfg.has_section(self.profile):
            for opt in p_cfg.options(self.profile):
                if opt in self.profile_opts:
                    self.profile_opts[opt](p_cfg.get(self.profile, opt))

    def _get_schema_name(self):
        """
        Return schema name set.
        """
        return self.__schema_name

    def _get_last_check(self):
        """
        Return when last check happened.
        """
        return self.__lcheck

    def _set_last_check(self, when):
        """
        Set last check time.
        """
        self.__lcheck = when

    # Schema Profile
    def _get_profile(self):
        return self.__profile

    def _set_profile(self, profile):
        """
        Set scheduling profile for current schema.
        """
        self.__profile = profile

    # Job time
    def _get_month(self):
        return self.__month

    def _set_month(self, month):
        """
        Parse and set month.
        """
        self.__month = self.cron_parser.parse_month(month)

    def _get_day(self):
        return self.__day

    def _set_day(self, day):
        """
        Parse and set day.
        """
        self.__day = self.cron_parser.parse_day(day)

    def _get_weekday(self):
        return self.__weekday

    def _set_weekday(self, weekday):
        """
        Parse and set weekday.
        """
        self.__weekday = self.cron_parser.parse_weekday(weekday)

    def _get_hour(self):
        return self.__hour

    def _set_hour(self, hour):
        """
        Parse and set hour.
        """
        self.__hour = self.cron_parser.parse_hour(hour)

    def _get_minute(self):
        return self.__minute

    def _set_minute(self, minute):
        """
        Parse and set minute.
        """
        self.__minute = self.cron_parser.parse_minute(minute)

    # Job Command
    def _get_command(self):
        """
        Get job command.
        """
        return self.__command

    def _set_command(self, command):
        """
        Set command for job.
        """
        self.__command = command

    # Job Options
    def _get_enabled(self):
        """
        Returns True if job should run, otherwise, False.
        """
        return self.__enabled

    def _set_enabled(self, enable):
        """
        Set if job should run or not.
        """
        self.__enabled = enable

    def _get_addtoinv(self):
        """
        Returns True if job result should be stored in Inventory, otherwise,
        False.
        """
        return self.__addtoinv

    def _set_addtoinv(self, add):
        """
        Sets job result to be added to Inventory, or not.
        """
        self.__addtoinv = add

    def _get_saveto(self):
        """
        Get file that stores job results.
        """
        return self.__savefile

    def _set_saveto(self, save_dir):
        """
        Set a file to store job results.
        """
        if save_dir:
            self.__savefile = os.path.join(save_dir, self.schema_name + ".xml")
        else:
            self.__savefile = None

    def _get_mailto(self):
        """
        Get email set to receive job results.
        """
        return self.__mail

    def _set_mailto(self, mail):
        """
        Set an email to receive job results.
        """
        self.__mail = mail

    def _get_smtp(self):
        """
        Get smtp profile to use for sending email.
        """
        return self.__smtp

    def _set_smtp(self, smtp):
        """
        Set a smtp profile for sending email.
        """
        self.__smtp = smtp

    # Properties
    profile = property(_get_profile, _set_profile)
    month = property(_get_month, _set_month)
    day = property(_get_day, _set_day)
    weekday = property(_get_weekday, _set_weekday)
    hour = property(_get_hour, _set_hour)
    minute = property(_get_minute, _set_minute)
    command = property(_get_command, _set_command)
    enabled = property(_get_enabled, _set_enabled)
    addtoinv = property(_get_addtoinv, _set_addtoinv)
    saveto = property(_get_saveto, _set_saveto)
    mailto = property(_get_mailto, _set_mailto)
    smtp = property(_get_smtp, _set_smtp)

    last_check = property(_get_last_check, _set_last_check)
    schema_name = property(_get_schema_name)
コード例 #4
0
    def _save_profile(self, event):
        """
        Save scheduling profile.
        """
        pname = self.schedp_name.get_active_text()
        if not len(pname):
            dlg = HIGAlertDialog(
                self,
                message_format=_(
                    "Scheduling Profile - Error \
while saving"
                ),
                secondary_text=_(
                    "You need to specify a name \
for Profile."
                ),
            )
            dlg.run()
            dlg.destroy()
            return

        parser = CronParser()
        minute = self.cron_minute.get_text()
        hour = self.cron_hour.get_text()
        day = self.cron_day.get_text()
        month = self.cron_month.get_text()
        weekday = self.cron_weekday.get_text()
        try:
            parser.parse_minute(minute)
            parser.parse_hour(hour)
            parser.parse_day(day)
            parser.parse_month(month)
            parser.parse_weekday(weekday)
        except Exception, e:
            dlg = HIGAlertDialog(
                self,
                message_format=_(
                    "Scheduling Profile - Error \
while saving"
                ),
                secondary_text=_(
                    "Check your cron syntax and \
try to save again."
                ),
            )
            dlg.run()
            dlg.destroy()
            return
    def _save_profile(self, event):
        """
        Save scheduling profile.
        """
        pname = self.schedp_name.get_active_text()
        if not len(pname):
            dlg = HIGAlertDialog(self,
                                 message_format=_('Scheduling Profile - Error \
while saving'),
                                 secondary_text=_("You need to specify a name \
for Profile."))
            dlg.run()
            dlg.destroy()
            return

        parser = CronParser()
        minute = self.cron_minute.get_text()
        hour = self.cron_hour.get_text()
        day = self.cron_day.get_text()
        month = self.cron_month.get_text()
        weekday = self.cron_weekday.get_text()
        try:
            parser.parse_minute(minute)
            parser.parse_hour(hour)
            parser.parse_day(day)
            parser.parse_month(month)
            parser.parse_weekday(weekday)
        except Exception, e:
            dlg = HIGAlertDialog(self,
                                 message_format=_('Scheduling Profile - Error \
while saving'),
                                 secondary_text=_("Check your cron syntax and \
try to save again."))
            dlg.run()
            dlg.destroy()
            return
コード例 #6
0
ファイル: Scheduler.py プロジェクト: aregee/network-scanner
class SchedSchema(object):
    """
    Parse scheduled schemas.
    """

    def __init__(self, schema_parser, schema_name):
        self.profile_opts = {
            # Task execution time
            'hour':self._set_hour,
            'minute':self._set_minute,
            'month':self._set_month,
            'day':self._set_day,
            'weekday':self._set_weekday,
        }

        self.options = { 
            # Task scheduling profile
            'profile':self._set_profile,
            # Task command
            'command':self._set_command,
            # Task options
            'enabled':self._set_enabled,
            'addtoinv':self._set_addtoinv,
            'saveto':self._set_saveto,
            'mailto':self._set_mailto,
            'smtp':self._set_smtp
        }

        self.__schema_name = schema_name
        self.schema_parser = schema_parser
        self.last_check = None
        self.cron_parser = CronParser()
        self.profiles = Path.sched_profiles
        self.setup_schema(self.schema_name)


    def job_to_run(self):
        """
        Check if there is a scheduled job for now and if there is,
        return True, otherwise False.
        """
        cur_time = time.localtime()

        # check if we should check for job or not
        if self.last_check and cur_time[1] == self.last_check[1] and \
            cur_time[2] == self.last_check[2] and \
            cur_time[3] == self.last_check[3] and \
            cur_time[4] == self.last_check[4] and \
            cur_time[6] == self.last_check[6]:

            return False # too early to run a job again!

        if cur_time[1] in self.month and cur_time[2] in self.day \
            and cur_time[3] in self.hour and cur_time[4] in self.minute \
            and cur_time[6] in self.weekday:
            
            self.last_check = cur_time
            return True # there is a job to run!


    def setup_schema(self, name):
        """
        Setup schema.
        """
        if self.schema_parser.has_section(name):
            for opt in self.schema_parser.options(name):
                if opt in self.options:
                    self.options[opt](self.schema_parser.get(name, opt))

            self.load_profile()


    def load_profile(self):
        """
        Load scheduling profile for current schema.
        """
        p_cfg = ConfigParser()
        p_cfg.read(self.profiles)

        if p_cfg.has_section(self.profile):
            for opt in p_cfg.options(self.profile):
                if opt in self.profile_opts:
                    self.profile_opts[opt](p_cfg.get(self.profile, opt))


    def _get_schema_name(self):
        """
        Return schema name set.
        """
        return self.__schema_name


    def _get_last_check(self):
        """
        Return when last check happened.
        """
        return self.__lcheck


    def _set_last_check(self, when):
        """
        Set last check time.
        """
        self.__lcheck = when


    # Schema Profile
    def _get_profile(self):
        return self.__profile


    def _set_profile(self, profile):
        """
        Set scheduling profile for current schema.
        """
        self.__profile = profile


    # Job time
    def _get_month(self):
        return self.__month


    def _set_month(self, month):
        """
        Parse and set month.
        """
        self.__month = self.cron_parser.parse_month(month)


    def _get_day(self): 
        return self.__day


    def _set_day(self, day):
        """
        Parse and set day.
        """
        self.__day = self.cron_parser.parse_day(day)


    def _get_weekday(self): 
        return self.__weekday


    def _set_weekday(self, weekday):
        """
        Parse and set weekday.
        """
        self.__weekday = self.cron_parser.parse_weekday(weekday)


    def _get_hour(self): 
        return self.__hour


    def _set_hour(self, hour):
        """
        Parse and set hour.
        """
        self.__hour = self.cron_parser.parse_hour(hour)


    def _get_minute(self): 
        return self.__minute


    def _set_minute(self, minute):
        """
        Parse and set minute.
        """
        self.__minute = self.cron_parser.parse_minute(minute)


    # Job Command
    def _get_command(self):
        """
        Get job command.
        """
        return self.__command


    def _set_command(self, command):
        """
        Set command for job.
        """
        self.__command = command


    # Job Options
    def _get_enabled(self):
        """
        Returns True if job should run, otherwise, False.
        """
        return self.__enabled


    def _set_enabled(self, enable):
        """
        Set if job should run or not.
        """
        self.__enabled = enable


    def _get_addtoinv(self):
        """
        Returns True if job result should be stored in Inventory, otherwise,
        False.
        """
        return self.__addtoinv


    def _set_addtoinv(self, add):
        """
        Sets job result to be added to Inventory, or not.
        """
        self.__addtoinv = add


    def _get_saveto(self):
        """
        Get file that stores job results.
        """
        return self.__savefile


    def _set_saveto(self, save_dir):
        """
        Set a file to store job results.
        """
        if save_dir:
            self.__savefile = os.path.join(save_dir, self.schema_name + ".xml")
        else:
            self.__savefile = None
        

    def _get_mailto(self):
        """
        Get email set to receive job results.
        """
        return self.__mail


    def _set_mailto(self, mail):
        """
        Set an email to receive job results.
        """
        self.__mail = mail

    
    def _get_smtp(self):
        """
        Get smtp profile to use for sending email.
        """
        return self.__smtp


    def _set_smtp(self, smtp):
        """
        Set a smtp profile for sending email.
        """
        self.__smtp = smtp


    # Properties
    profile = property(_get_profile, _set_profile)
    month = property(_get_month, _set_month)
    day = property(_get_day, _set_day)
    weekday = property(_get_weekday, _set_weekday)
    hour = property(_get_hour, _set_hour)
    minute = property(_get_minute, _set_minute)
    command = property(_get_command, _set_command)
    enabled = property(_get_enabled, _set_enabled)
    addtoinv = property(_get_addtoinv, _set_addtoinv)
    saveto = property(_get_saveto, _set_saveto)
    mailto = property(_get_mailto, _set_mailto)
    smtp = property(_get_smtp, _set_smtp)

    last_check = property(_get_last_check, _set_last_check)
    schema_name = property(_get_schema_name)