~t-w-/backtestground/packaging

« back to all changes in this revision

Viewing changes to backtestground-0.4-0ubuntu1/bin/extract-background-context-gnome

  • Committer: Thorsten Wilms
  • Date: 2011-01-23 17:01:32 UTC
  • Revision ID: t_w_@freenet.de-20110123170132-f9ghojtxruz6pkme
Copy from trunk r163, prepare 0.4.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""
 
4
Background Context Extractor commandline tool, automatic version for GNOME
 
5
 
 
6
Automatically takes one screenshot with white, and one with shadow-color
 
7
(usually black), background. Puts out a PNG image with transparent desktop.
 
8
 
 
9
This is usefull for evaluationg wallpapers that should go well with panels
 
10
and themes other than the one you are currently using.
 
11
 
 
12
Based on a bash script by Sergey Davidoff
 
13
 
 
14
Copyright 2010, 2011 Thorsten Wilms, Sergey Davidoff
 
15
License: GPLv3, see docs/COPYING
 
16
 
 
17
This program is free software: you can redistribute it and/or modify it under
 
18
the terms of the GNU General Public License as published by the Free Software
 
19
Foundation, either version 3 of the License, or (at your option) any later
 
20
version.
 
21
 
 
22
This program is distributed in the hope that it will be useful, but WITHOUT
 
23
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
24
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
25
 
 
26
You should have received a copy of the GNU General Public License
 
27
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
28
 
 
29
"""
 
30
import argparse # Added with Python 2.7, not present in 3.0 but back in 3.2
 
31
from os import remove
 
32
from os.path import isdir, exists
 
33
from sys import stdout, stderr, exit
 
34
from time import sleep
 
35
import gconf
 
36
from subprocess import call
 
37
 
 
38
# Custom modules
 
39
from backtestground.cli_input import try_parse
 
40
import backtestground.extract_background_context as extract_background_context
 
41
 
 
42
 
 
43
def main():
 
44
    """Run program and handle command line options"""
 
45
 
 
46
    # Default output filename
 
47
    filename = "context.png"
 
48
    help_text = """destination directory or filename. \".png\" will be appended
 
49
                to given filename, if missing. Defaults to %s.""" % filename
 
50
 
 
51
    # Parse commandline
 
52
    parser = argparse.ArgumentParser(description="""Create image with
 
53
        transparent desktop from 2 screenshots with white and shadow-color
 
54
        (usually black) desktops.""")
 
55
    parser.add_argument('-v', '--version',
 
56
                        action='version',
 
57
                        version='%(prog)s 1.3',
 
58
                        help="print program version")
 
59
    parser.add_argument('-o',
 
60
                        # no type=file, as that would require
 
61
                        # the file to exist beforehand.
 
62
                        dest='output',
 
63
                        metavar='path/filename',
 
64
                        help=help_text,
 
65
                        default=filename)
 
66
    parser.add_argument('-f', '--force',
 
67
                        action='store_true',
 
68
                        dest='overwrite',
 
69
                        help="""overwrite existing files,
 
70
                             do not creat a backup.""")
 
71
 
 
72
    # Catch IOErrors to put out a _short_ error message
 
73
    args = try_parse(parser)
 
74
 
 
75
    # Special care for 'output'
 
76
    if isdir(args.output):
 
77
        # args.output specifies a directory, so append the default filename.
 
78
        output = ''.join([args.output, "/", filename])
 
79
    else:
 
80
        # args.output specifies a file,
 
81
        # so make sure the name ends with ".png" (or ".PNG").
 
82
        if args.output.endswith(".png") or args.output.endswith(".PNG"):
 
83
            output = args.output
 
84
        else:
 
85
            output = ''.join([args.output, ".png"])
 
86
 
 
87
    try:
 
88
        client = gconf.client_get_default()
 
89
    except:
 
90
        stderr.write("Can't access gconf. Maybe not running from within a GNOME session?\n")
 
91
        exit(1)
 
92
    else:
 
93
        # Announce countdown
 
94
        stdout.write("Taking screenshots in 3 seconds\n")
 
95
        stdout.flush()
 
96
        for i in range(3, 0, -1):
 
97
            stdout.write(''.join([str(i), "\n"]))
 
98
            stdout.flush()
 
99
            sleep(1)
 
100
 
 
101
        gconfs = ('/desktop/gnome/background/picture_filename',
 
102
                  '/desktop/gnome/background/color_shading_type',
 
103
                  '/desktop/gnome/background/primary_color')
 
104
 
 
105
        # Save initial settings
 
106
        before = map(client.get_string, gconfs)
 
107
 
 
108
        # Find unused filenames
 
109
        def unused(name):
 
110
            n = 0
 
111
            while True:
 
112
                name = ''.join([name, str(n), '.png'])
 
113
                if  not exists(name): break
 
114
                n += 1
 
115
 
 
116
            return name
 
117
 
 
118
        filename_seeds = ('on_white', 'on_black')
 
119
        files = map(unused, filename_seeds)
 
120
 
 
121
        # Switch to no-picture/solid/white, wait for transition,
 
122
        # take screenshot
 
123
        values = ('', 'solid', '#ffffffffffff')
 
124
        map(client.set_string, gconfs, values)
 
125
        sleep(2)
 
126
        call("import -window root %s" % files[0], shell=True)
 
127
 
 
128
        # Switch to black, wait for transition, take screenshot
 
129
        client.set_string(gconfs[2], '#000000000000')
 
130
        sleep(2)
 
131
        call("import -window root %s" % files[1], shell=True)
 
132
 
 
133
        # Revert settings
 
134
        map(client.set_string, gconfs, before)
 
135
 
 
136
        # Extract
 
137
        extract_background_context.extract(files[0], files[1],
 
138
                                           output, args.overwrite)
 
139
 
 
140
        # Remove screenshots
 
141
        map(remove, files)
 
142
 
 
143
 
 
144
if __name__ == '__main__':
 
145
    main()