From: Oleg Broytman Date: Tue, 7 Jan 2014 10:16:55 +0000 (+0400) Subject: Open config in xsetbg_conf.py, open db in xsetbg_db.py X-Git-Tag: v4.1.0~16 X-Git-Url: https://git.phdru.name/?p=xsetbg.git;a=commitdiff_plain;h=a337e26c169baecb75197b50db5fd734c39f6187 Open config in xsetbg_conf.py, open db in xsetbg_db.py --- diff --git a/print-filename.py b/print-filename.py index 91088c2..6348f68 100755 --- a/print-filename.py +++ b/print-filename.py @@ -8,11 +8,13 @@ This file is a part of XSetBg. """ __author__ = "Oleg Broytman " -__copyright__ = "Copyright (C) 2004-2012 PhiloSoft Design" +__copyright__ = "Copyright (C) 2004-2014 PhiloSoft Design" __license__ = "GNU GPL" -import sys, os, shelve +import sys, os +from xsetbg_conf import xsetbg_conf +from xsetbg_db import xsetbg_db def usage(code=0): @@ -53,27 +55,18 @@ def get_args(): old, output_encoding = get_args() -xsetbg_dir = os.path.join(os.environ["HOME"], "lib", "xsetbg") -os.chdir(xsetbg_dir) - -global_db_name = "xsetbg.db" filename_key = "filename" old_filename_key = "old_filename" - -global_db = shelve.open(global_db_name, flag='r') if old: key = old_filename_key else: key = filename_key -filename = global_db[key] -global_db.close() -from ConfigParser import SafeConfigParser -config = SafeConfigParser() -config.read("xsetbg.conf") +filename = xsetbg_db[key] +xsetbg_db.close() -fs_encoding = config.get("images", "fs_encoding") +fs_encoding = xsetbg_conf.get("images", "fs_encoding") filename = unicode(filename, fs_encoding) print filename.encode(output_encoding) diff --git a/xsetbg.py b/xsetbg.py index db49a1b..f4426ee 100755 --- a/xsetbg.py +++ b/xsetbg.py @@ -8,50 +8,47 @@ using xli program. """ __author__ = "Oleg Broytman " -__copyright__ = "Copyright (C) 2000-2012 PhiloSoft Design" +__copyright__ = "Copyright (C) 2000-2014 PhiloSoft Design" __license__ = "GNU GPL" __all__ = ['host', 'port', 'change'] -import sys, os -from ConfigParser import SafeConfigParser - import anydbm +from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB from datetime import timedelta +import os import random import re import shelve import subprocess +import sys from time import time -from fcntl import flock, LOCK_EX, LOCK_UN, LOCK_NB + +from xsetbg_conf import xsetbg_dir, xsetbg_conf +from xsetbg_db import xsetbg_db_name, xsetbg_db + def error(error_str, error_code=1): sys.stderr.write("%s: Error: %s\n" % (sys.argv[0], error_str)) sys.exit(error_code) -xsetbg_dir = os.path.dirname(os.path.abspath(__file__)) -os.chdir(xsetbg_dir) - -config = SafeConfigParser() -config.read("xsetbg.conf") - -if config.has_option("images", "directory") or \ - config.has_option("images", "directory0") or \ - config.has_option("images", "directory1"): +if xsetbg_conf.has_option("images", "directory") or \ + xsetbg_conf.has_option("images", "directory0") or \ + xsetbg_conf.has_option("images", "directory1"): image_dirs = [] - if config.has_option("images", "directory"): - image_dirs.append(config.get("images", "directory")) - if config.has_option("images", "directory0"): - image_dirs.append(config.get("images", "directory0")) - if config.has_option("images", "directory1"): - image_dirs.append(config.get("images", "directory1")) + if xsetbg_conf.has_option("images", "directory"): + image_dirs.append(xsetbg_conf.get("images", "directory")) + if xsetbg_conf.has_option("images", "directory0"): + image_dirs.append(xsetbg_conf.get("images", "directory0")) + if xsetbg_conf.has_option("images", "directory1"): + image_dirs.append(xsetbg_conf.get("images", "directory1")) i = 2 while True: option = "directory%d" % i - if config.has_option("images", option): - image_dirs.append(config.get("images", option)) + if xsetbg_conf.has_option("images", option): + image_dirs.append(xsetbg_conf.get("images", option)) i += 1 else: break @@ -64,20 +61,20 @@ image_dirs = [os.path.join(xsetbg_dir, ] # minimum time in seconds between background image changes -if config.has_option("xsetbg", "min_pause"): - min_pause = config.getint("xsetbg", "min_pause") +if xsetbg_conf.has_option("xsetbg", "min_pause"): + min_pause = xsetbg_conf.getint("xsetbg", "min_pause") else: min_pause = 60 -borders = config.get("xsetbg", "borders").split(',') -if config.has_option("xsetbg", "borders"): - borders = [border.strip() for border in config.get("xsetbg", "borders").split(',')] +borders = xsetbg_conf.get("xsetbg", "borders").split(',') +if xsetbg_conf.has_option("xsetbg", "borders"): + borders = [border.strip() for border in xsetbg_conf.get("xsetbg", "borders").split(',')] else: borders = ["darkcyan", "steelblue", "midnightblue"] # minimum time in seconds between occurences of the same image -if config.has_option("xsetbg", "min_delay"): - min_delay = config.get("xsetbg", "min_delay") +if xsetbg_conf.has_option("xsetbg", "min_delay"): + min_delay = xsetbg_conf.get("xsetbg", "min_delay") # Borrowed from http://stackoverflow.com/a/2765366 td_re = re.compile('(?:(?P\d+)y)?(?:(?P\d+)m)?(?:(?P\d+)d)?(?:T(?:(?P\d+)h)?(?:(?P\d+)m)?(?:(?P\d+)s)?)?') @@ -96,22 +93,17 @@ else: min_delay = 3600*24 # 24 hours # httpd settings -if config.has_option("httpd", "host"): - host = config.get("httpd", "host") +if xsetbg_conf.has_option("httpd", "host"): + host = xsetbg_conf.get("httpd", "host") else: host = 'localhost' -if config.has_option("httpd", "port"): - port = config.getint("httpd", "port") +if xsetbg_conf.has_option("httpd", "port"): + port = xsetbg_conf.getint("httpd", "port") else: error("Config must specify a port to listen. Abort.") -os.umask(0066) # octal; -rw-------; make the global persistent dictionary - # readable only by the user -global_db_name = "xsetbg.db" - - # DB keys timestamp_key = "timestamp" filename_key = "filename" @@ -120,28 +112,31 @@ old_filename_key = "old_filename" # Create the database if it is not exists yet. +os.umask(0066) # octal; -rw-------; make the global persistent dictionary + # readable only by the user + try: - global_db = shelve.open(global_db_name, flag='c') + xsetbg_db = shelve.open(os.path.join(xsetbg_dir, xsetbg_db_name), flag='c') except anydbm.error, msg: if str(msg) == "db type could not be determined": - os.remove(global_db_name) - global_db = shelve.open(global_db_name, flag='c') + os.remove(xsetbg_db_name) + xsetbg_db = shelve.open(os.path.join(xsetbg_dir, xsetbg_db_name), flag='c') # Remove old filenames old_time = time() - min_delay to_delete = [timestamp_key] -for key in global_db.keys(): - if key.startswith('/') and global_db[key] < old_time: +for key in xsetbg_db.keys(): + if key.startswith('/') and xsetbg_db[key] < old_time: to_delete.append(key) for key in to_delete: try: - del global_db[key] + del xsetbg_db[key] except KeyError: pass -global_db.close() # Close DB in the parent process +xsetbg_db.close() # Close DB in the parent process images = [] @@ -166,12 +161,12 @@ def change(force=False): return - global_db = None + xsetbg_db = None try: # Reopen the global persistent dictionary - global_db = shelve.open(global_db_name, 'w') + xsetbg_db = shelve.open(os.path.join(xsetbg_dir, xsetbg_db_name), 'w') - timestamp = global_db.get(timestamp_key) + timestamp = xsetbg_db.get(timestamp_key) current_time = time() if not force and timestamp is not None and \ @@ -179,24 +174,24 @@ def change(force=False): return # Save current time - global_db[timestamp_key] = current_time + xsetbg_db[timestamp_key] = current_time # Select a random image and check if we've seen it recently; # loop until we can find a new image (never seen before) or old enough. for i in xrange(len(images)): # ensure the loop is not infinite image_name = random.choice(images) - if global_db.has_key(image_name): - image_time = global_db[image_name] + if xsetbg_db.has_key(image_name): + image_time = xsetbg_db[image_name] if current_time - image_time > min_delay: break else: break - global_db[image_name] = current_time + xsetbg_db[image_name] = current_time # Save filename - if global_db.has_key(filename_key): - global_db[old_filename_key] = global_db[filename_key] - global_db[filename_key] = image_name + if xsetbg_db.has_key(filename_key): + xsetbg_db[old_filename_key] = xsetbg_db[filename_key] + xsetbg_db[filename_key] = image_name program_options = ["xli", "-onroot", "-quiet"] + \ ["-center", "-border", random.choice(borders), "-zoom", "auto", @@ -211,4 +206,4 @@ def change(force=False): flock(lock_file, LOCK_UN) lock_file.close() # Flush and close the global persistent dictionary - if global_db: global_db.close() + if xsetbg_db: xsetbg_db.close() diff --git a/xsetbg_conf.py b/xsetbg_conf.py new file mode 100755 index 0000000..a77d678 --- /dev/null +++ b/xsetbg_conf.py @@ -0,0 +1,18 @@ +#! /usr/bin/env python +"""XSetBg config + +""" + +__author__ = "Oleg Broytman " +__copyright__ = "Copyright (C) 2014 PhiloSoft Design" +__license__ = "GNU GPL" + +__all__ = ['xsetbg_conf'] + +import os +from ConfigParser import SafeConfigParser + +xsetbg_dir = os.path.dirname(os.path.abspath(__file__)) + +xsetbg_conf = SafeConfigParser() +xsetbg_conf.read(os.path.join(xsetbg_dir, 'xsetbg.conf')) diff --git a/xsetbg_db.py b/xsetbg_db.py new file mode 100755 index 0000000..4853afb --- /dev/null +++ b/xsetbg_db.py @@ -0,0 +1,22 @@ +#! /usr/bin/env python +"""XSetBg database + +""" + +__author__ = "Oleg Broytman " +__copyright__ = "Copyright (C) 2014 PhiloSoft Design" +__license__ = "GNU GPL" + +__all__ = ['xsetbg_db'] + +import anydbm +import os +import shelve +from xsetbg_conf import xsetbg_dir, xsetbg_conf + +xsetbg_db_name = "xsetbg.db" + +try: + xsetbg_db = shelve.open(os.path.join(xsetbg_dir, xsetbg_db_name), 'r') +except anydbm.error: + xsetbg_db = None