| Store | Cart

Re: Using python to start programs after logging in

From: Cecil Westerhof <Cec...@decebal.nl>
Fri, 20 Jan 2017 05:34:45 +0100
On Friday 20 Jan 2017 00:36 CET, Cecil Westerhof wrote:

> I wrote a function for switching to the correct virtual desktop and> starting all the commands. I am also using with now:> def do_desktop(desktop, commands, seconds_to_wait = 10):> desktop_command = ('wmctrl -s ' + desktop).split()> check_call(tuple(desktop_command))> for command_arr in commands:> command         = command_arr[0].split()> log_directory   = command_arr[1]> directory       = command_arr[2]> if (directory != ''):> chdir(directory)> if (log_directory == 'NONE'):> Popen(tuple(command.split()))> else:> log_file_name = log_directory.replace('%T', strftime('%F_%R'))> with open(log_file_name, 'w') as log_file:> Popen(tuple(command), stdout = log_file, stderr = STDOUT)> if (directory != ''):> set_default_dir()> sleep(seconds_to_wait)


I wrote a little better version:
def do_desktop(desktop, commands, seconds_to_wait):
    desktop_command = (switch_desktop + desktop).split()
    if seconds_to_wait == 0:
        seconds_to_wait = 10
    check_call(desktop_command)
    for command_arr in commands:
        command         = command_arr[0].split()
        log_directory   = command_arr[1]
        directory       = command_arr[2]
        if not log_directory:
            log_directory = '/dev/null'
        log_file_name = log_directory.replace('%T', strftime('%F_%R'))
        with open(log_file_name, 'w') as log_file:
            Popen(command, stdout = log_file, cwd = directory, stderr = STDOUT)
    sleep(seconds_to_wait)

When there is no log_directory I set it to '/dev/null': that makes the
code a little bit cleaner.


And everything is now fetched from the database:
for desktop in cursor.execute(select_desktops).fetchall():
    desktop_name    = desktop[0]
    desktop_value   = desktop[1]
    desktop_wait    = desktop[2]
    commands        = cursor.execute(select_commands, [desktop_name]).fetchall()
    do_desktop(desktop_value, commands, desktop_wait)

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
-- 
https://mail.python.org/mailman/listinfo/python-list

Recent Messages in this Thread
Cecil Westerhof Jan 19, 2017 07:08 pm
John Gordon Jan 19, 2017 07:29 pm
Cecil Westerhof Jan 19, 2017 09:11 pm
John Gordon Jan 20, 2017 05:46 pm
MRAB Jan 19, 2017 08:12 pm
Cecil Westerhof Jan 19, 2017 09:21 pm
Cecil Westerhof Jan 19, 2017 11:36 pm
MRAB Jan 20, 2017 12:24 am
Cecil Westerhof Jan 20, 2017 04:26 am
Cecil Westerhof Jan 20, 2017 04:34 am
Cecil Westerhof Jan 23, 2017 11:53 am
Messages in this thread