Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 2 additions & 32 deletions pyipptool/__init__.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,8 @@
import ConfigParser
import os

from .config import get_config
from .core import IPPToolWrapper


def read_config(paths=('/etc/opt/pyipptool/pyipptool.cfg',
os.path.join(os.path.expanduser('~'),
'.pyipptool.cfg'))):
config = {}
fs_config = ConfigParser.ConfigParser()
fs_config.read(paths)
config['ipptool_path'] = fs_config.get('main', 'ipptool_path')
try:
config['login'] = fs_config.get('main', 'login')
except ConfigParser.NoOptionError:
pass
try:
config['password'] = fs_config.get('main', 'password')
except ConfigParser.NoOptionError:
pass
try:
config['graceful_shutdown_time'] = fs_config.getint(
'main',
'graceful_shutdown_time')
except ConfigParser.NoOptionError:
config['graceful_shutdown_time'] = 2
try:
config['timeout'] = fs_config.getint('main', 'timeout')
except ConfigParser.NoOptionError:
config['timeout'] = 10
return config


config = read_config()
config = get_config()

wrapper = IPPToolWrapper(config)
create_job_subscriptions = wrapper.create_job_subscriptions
Expand Down
46 changes: 46 additions & 0 deletions pyipptool/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import ConfigParser
import os


def read_config(paths=()):
config = {}
fs_config = ConfigParser.ConfigParser()
fs_config.read(paths)
config['ipptool_path'] = fs_config.get('main', 'ipptool_path')
try:
config['login'] = fs_config.get('main', 'login')
except ConfigParser.NoOptionError:
pass
try:
config['password'] = fs_config.get('main', 'password')
except ConfigParser.NoOptionError:
pass
try:
config['graceful_shutdown_time'] = fs_config.getint(
'main',
'graceful_shutdown_time')
except ConfigParser.NoOptionError:
config['graceful_shutdown_time'] = 2
try:
config['timeout'] = fs_config.getint('main', 'timeout')
except ConfigParser.NoOptionError:
config['timeout'] = 10
return config


class LazyConfig(dict):
def __init__(self, paths):
self.paths = paths
self.loaded = False

def __getitem__(self, key):
if not self.loaded:
self.update(read_config(self.paths))
self.loaded = True
return super(LazyConfig, self).__getitem__(key)


def get_config(paths=('/etc/opt/pyipptool/pyipptool.cfg',
os.path.join(os.path.expanduser('~'),
'.pyipptool.cfg'))):
return LazyConfig(paths)
2 changes: 1 addition & 1 deletion tests/test_highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
"""

def do_POST(self):
time.sleep(.5)
time.sleep(.2)
assassin = threading.Thread(target=self.server.shutdown)
assassin.daemon = True
assassin.start()
Expand Down