PyGTK Time Data de gerenciamento no loop

0

Estou tentando criar um Snoozer de desligamento para o Ubuntu, mas ainda estou lutando com o Python em geral.

O PyGTK é baseado, e eu não sei como fazer o gerenciamento de data / hora por causa disso. Alguém tem algum recurso para hora ou data ou até mesmo gerenciamento de calandras no PyGTK.

Eu preciso verificar a data de hoje e compará-la com o que o usuário deseja. Qualquer ajuda seria appreaciated.

Aqui está o código por trás do snoozer:

import gettext
from gettext import gettext as _
gettext.textdomain('snooze')

from gi.repository import Gtk # pylint: disable=E0611
import logging, time, datetime

logger = logging.getLogger('snooze')

import ConfigParser, os # We need to be able to store and recal settings

from snooze_lib import Window
from snooze.AboutSnoozeDialog import AboutSnoozeDialog
from snooze.PreferencesSnoozeDialog import PreferencesSnoozeDialog

#first try to read the config.cfg file
config = ConfigParser.RawConfigParser()
configFile = 'data/config.cfg'
monState = False
tueState = False
wedState = False
thurState = False
friState = False
satState = False
sunState = False

# Creating the Config file
def createConfigFile(fileName):
    print "CREATING CONFIG"
    config.add_section('Preferences')
    config.set('Preferences', 'mon', False)
    config.set('Preferences', 'tues', False)
    config.set('Preferences', 'wed', False)
    config.set('Preferences', 'thur', False)
    config.set('Preferences', 'fri', False)
    config.set('Preferences', 'sat', False)
    config.set('Preferences', 'sun', False)
    rewriteConfigFile(filename)

# Writing our configuration file to the failename as specifeid
def rewriteConfigFile(filename):    
    with open(filename, 'wb') as configfile:
        config.write(configfile)
# Reading the config file 
def readConfigFile(fileName):
    print "READING CONFIG"
    global monState, tueState, wedState, thurState, friState, satState, sunState
    monState = config.getboolean('Preferences', 'mon')
    tueState = config.getboolean('Preferences', 'tues')
    wedState = config.getboolean('Preferences', 'wed')
    thurState = config.getboolean('Preferences', 'thur')
    friState = config.getboolean('Preferences', 'fri')
    satState = config.getboolean('Preferences', 'sat')
    sunState = config.getboolean('Preferences', 'sun')

# If the config does not exist, create it then read it. Otherwise read it
if not config.read(configFile):    
    createConfigFile(configFile)
    readConfigFile(configFile)    
else:
    readConfigFile(configFile)

# See snooze_lib.Window.py for more details about how this class works
class SnoozeWindow(Window):
    __gtype_name__ = "SnoozeWindow"

    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the main window"""
        super(SnoozeWindow, self).finish_initializing(builder)

        self.AboutDialog = AboutSnoozeDialog
        self.PreferencesDialog = PreferencesSnoozeDialog

        # Code for other initialization actions should be added here.
        self.daymon = self.builder.get_object("daymon")
        self.daytues = self.builder.get_object("daytues")
        self.daywed = self.builder.get_object("daywed")
        self.daythur = self.builder.get_object("daythur")
        self.dayfri = self.builder.get_object("dayfri")
        self.daysat = self.builder.get_object("daysat")
        self.daysun = self.builder.get_object("daysun")
        self.statusBar = self.builder.get_object("statusBar")
        self.count = 0

        # Set the values based on the config file        
        if monState == True:
            self.daymon.activate()

        if tueState == True:
            self.daytues.activate()

        if wedState == True:
            self.daywed.activate()

        if thurState == True:
            self.daythur.activate()

        if friState == True:
            self.dayfri.activate()

        if satState == True:
            self.daysat.activate()

        if sunState == True:
           self.daysun.activate()

        self.daymon.connect('notify::active', self.toggle_day, "mon")
        self.daytues.connect('notify::active', self.toggle_day, "tues")
        self.daywed.connect('notify::active', self.toggle_day, "wed")
        self.daythur.connect('notify::active', self.toggle_day, "thur")
        self.dayfri.connect('notify::active', self.toggle_day, "fri")
        self.daysat.connect('notify::active', self.toggle_day, "sat")
        self.daysun.connect('notify::active', self.toggle_day, "sun")

    # Toggle the setting and store the information in the config file
    def toggle_day(self, widget, active, day):
        state = widget.get_active()

        # Set the config option and update the config file
        global configFile        
        config.set('Preferences', day, state)        
        rewriteConfigFile(configFile)
        self.statusBar.set_text("Saved Config")        
    
por Rudi Strydom 12.11.2012 / 22:41

1 resposta

1

Você provavelmente vai querer usar o módulo datetime , mais especificamente a classe datetime.date se você quiser apenas a data, não as vezes.

>>> from datetime import date
>>> today = date.today()
>>> today
datetime.date(2012, 11, 12)
>>> today.isoformat()
'2012-11-12'
>>>

Editar Você poderia usar um timer GObject para verificar a cada x segundos. Algo parecido com isto (não testado):

import time
from gi.repository import GObject

def _check_time_timer(self):
    if time.time() >= USER_TIME:
        # Do your action here
        print "Time has been reached!"
        # Return False to stop timer
        return False
    # Return True to keep the timer going
    return True

# Check the user time every 4 seconds, change as needed
GObject.timeout_add_seconds(4, _check_time_timer)
    
por Timo 12.11.2012 / 22:53