Skip to content
Browse files

first import

  • Loading branch information...
0 parents commit 4285d06ddae3ad35eb9d1fa6ed98a8e1666cebbd ametaireau committed
Showing with 258 additions and 0 deletions.
  1. +22 −0 LICENSE
  2. +34 −0 README
  3. +61 −0 builders.py
  4. +65 −0 config.py
  5. +76 −0 genbackground.py
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2010 Alexis Metaireau
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
34 README
@@ -0,0 +1,34 @@
+Gnome animated background generator
+###################################
+
+Gnome background can use XML files to make an animated background.
+This little script allows you to generate automaticaly your own XML background
+changer file, easily, using the command line.
+
+To generate the XML file, use:
+
+Options
+========
+
+Here is the --help output::
+
+ $ python genbackground --help
+ -h, --help show this help message and exit
+ -d, --debug display debug messages on the standard output
+ -p PATH, --path=PATH set the path to look for pictures.
+ -t TRANSITION_TIME, --transition_time=TRANSITION_TIME
+ set the transition time, overwriting the configuration
+ value
+ --display-time=DISPLAY_TIME
+ set the display time for a picture, overwriting the
+ configuration value
+
+Exemple
+========
+
+Here is an exemple to creates a XML file from a path containing all wallpapers
+you want::
+
+ python genbackground.py --path /path/to/wallpapers/directory/ > dynamicwallpapers.xml
+
+And that's it !
61 builders.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+#-*- coding: utf8 -*-
+
+# Copyright (c) 2010 Alexis Metaireau
+
+from xml.dom.minidom import Document
+
+class XmlBackgroundBuilder():
+ """Build the xml file from a configuration object and a path.
+
+ """
+ def __init__(self, transition_time, display_time):
+ """Constructor. Initialize path and conf.
+
+ """
+ self.transition_time = transition_time
+ self.display_time = display_time
+
+ def build_xml(self, pictures_list):
+ """Return the backgroun.xml file.
+
+ """
+ doc = Document()
+ background = doc.createElement('background')
+ doc.appendChild(background)
+
+ for pic in pictures_list:
+ try:
+ next = pictures_list[pictures_list.index(pic)+1]
+ except IndexError:
+ next = pictures_list[0]
+
+ static = doc.createElement('static')
+
+ duration = doc.createElement('duration')
+ duration.appendChild(doc.createTextNode("%s.0" % self.display_time))
+ static.appendChild(duration)
+
+ file = doc.createElement('file')
+ file.appendChild(doc.createTextNode(pic))
+ static.appendChild(file)
+
+ background.appendChild(static)
+
+ transition = doc.createElement('transition')
+
+ duration = doc.createElement('duration')
+ duration.appendChild(doc.createTextNode("%s.0" % self.transition_time))
+ transition.appendChild(duration)
+
+ fromel = doc.createElement('from')
+ fromel.appendChild(doc.createTextNode(pic))
+ transition.appendChild(fromel)
+
+ to = doc.createElement('to')
+ to.appendChild(doc.createTextNode(next))
+ transition.appendChild(to)
+
+ background.appendChild(transition)
+
+ print doc.toxml()
65 config.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+#-*- coding: utf8 -*-
+
+# Copyright (c) 2010 Alexis Metaireau
+# Part of this file are (c) 2009 Bruno Bord
+
+import os
+from xdg.BaseDirectory import xdg_config_home
+from ConfigParser import SafeConfigParser
+
+class Configuration():
+ """Configuration file"""
+
+ def __init__(self, options=None):
+ self.debug = False
+ if options:
+ self.debug = options.debug
+
+ config_dir = xdg_config_home
+ self.conf_dir = os.path.join(config_dir,self.project_name)
+ self.config = SafeConfigParser()
+ self.conf_file = os.path.join(self.conf_dir, '%s.conf' % self.project_name)
+
+ # variable definition here
+ for (option, value) in self.available_options.items():
+ setattr(self, option, value)
+
+ if not os.path.isfile(self.conf_file):
+ if self.debug:
+ print "Configuration file not found, creating it"
+ self.write()
+ self.read()
+ #overwrite configuration with options if present.
+ for option in self.available_options:
+ if hasattr(options, option):
+ attr = getattr(options, option, False)
+ if attr:
+ setattr(self, option, attr)
+
+ def write(self):
+ "Write settings in the configuration file"
+ if self.debug:
+ print "Writing configuration file"
+
+ if not os.path.isdir(self.conf_dir):
+ os.makedirs(self.conf_dir)
+
+ if not self.config.has_section('general'):
+ self.config.add_section('general')
+ # setting Feed URL
+ for option in self.available_options:
+ self.config.set('general', option, getattr(self, option))
+
+ # finally writing the file.
+ with open(self.conf_file, 'wb') as configfile:
+ self.config.write(configfile)
+ configfile.close()
+
+ def read(self):
+ "Reading settings from configuration file"
+ if self.debug:
+ print "Reading configuration file", self.conf_file
+ self.config.read(self.conf_file)
+ for option in self.available_options:
+ setattr(self, option, self.config.get('general', option))
76 genbackground.py
@@ -0,0 +1,76 @@
+#!/usr/bin/env python
+#-*- coding: utf8 -*-
+
+# Copyright (c) 2010 Alexis Metaireau
+# Part of this file are Copyrighted (c) 2009 Bruno Bord
+
+"Generates a .XML file for gnome wallpapers"
+
+from config import Configuration
+from builders import XmlBackgroundBuilder
+
+from optparse import OptionParser
+import os
+
+class GenBackgroundConfiguration(Configuration):
+ project_name = 'genbackground'
+ available_options = {
+ 'transition_time': '15', #15 seconds for a transition
+ 'display_time': '900', #15mn per picture
+ }
+ authorized_extensions = ('jpg', 'png', 'jpeg')
+
+class Main(object):
+ "Main class. Handles the whole process"
+
+ def __init__(self):
+ # first things first, retreive options
+ self.options, self.args = self.get_options()
+ # configuration reading / writing
+ self.configuration = GenBackgroundConfiguration(options=self.options)
+
+ def get_options(self):
+ "Retrieves the options using OptionParser"
+ usage = '%prog [options]'
+ parser = OptionParser(usage=usage)
+ parser.add_option('-d', '--debug', action='store_true', default=False,
+ help="display debug messages on the standard output")
+ parser.add_option('-p', '--path', action='store', default=".",
+ help="set the path to look for pictures."""
+ )
+ # options that may overwrite the configuration
+ parser.add_option('-t', '--transition_time', default=None, type="int",
+ help="set the transition time, overwriting the configuration value"
+ )
+ parser.add_option('--display-time', default=None, type="int",
+ help="""set the display time for a picture, overwriting the
+ configuration value"""
+ )
+
+ return parser.parse_args()
+
+ def generate_xml(self):
+ "Generate the XML, according to preferences"
+ builder = XmlBackgroundBuilder(
+ self.configuration.transition_time,
+ self.configuration.display_time,
+ )
+
+ builder.build_xml(self.get_pictures_list(
+ self.options.path,
+ self.configuration.authorized_extensions
+ ))
+
+ def get_pictures_list(self, path, authorized_extensions):
+ """Return the list of pictures in a specific directory.
+
+ """
+ pictures_list = []
+ for filename in os.listdir(path):
+ if filename.split('.')[-1] in authorized_extensions:
+ pictures_list.append('%s%s' % (path, filename))
+ return pictures_list
+
+if __name__ == "__main__":
+ main = Main()
+ main.generate_xml()

0 comments on commit 4285d06

Please sign in to comment.
Something went wrong with that request. Please try again.