Skip to content

Commit

Permalink
Simplify ZopeWSGIOptions and drop dependency on zdaemon.
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Aug 7, 2016
1 parent 95d7a66 commit 04ab51d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 38 deletions.
1 change: 0 additions & 1 deletion setup.py
Expand Up @@ -67,7 +67,6 @@ def _read_file(filename):
'setuptools',
'transaction',
'waitress',
'zdaemon',
'zExceptions >= 3.2',
'zope.browser',
'zope.browsermenu',
Expand Down
25 changes: 20 additions & 5 deletions src/Zope2/Startup/options.py
Expand Up @@ -15,9 +15,8 @@
import os
import xml.sax

from ZConfig.loader import SchemaLoader
from ZConfig.loader import ConfigLoader, SchemaLoader
from ZConfig.schema import SchemaParser
from zdaemon.zdoptions import ZDOptions
from zope.deferredimport import deprecated

# BBB Zope 5.0
Expand Down Expand Up @@ -48,13 +47,19 @@ def start_import(self, attrs):
SchemaParser.start_import(self, attrs)


class ZopeWSGIOptions(ZDOptions):
"""zdaemon based ZopeWSGIOptions to parse a ZConfig schema.
class ZopeWSGIOptions(object):
"""ZopeWSGIOptions parses a ZConfig schema and config file.
"""

configfile = None
confighandlers = None
configroot = None
schema = None
schemadir = os.path.dirname(os.path.abspath(__file__))
schemafile = 'wsgischema.xml'

def __init__(self, configfile=None):
self.configfile = configfile

def load_schema(self):
if self.schema is None:
# Load schema
Expand All @@ -75,3 +80,13 @@ def _conditional_load(self):
self.schema = parser._schema
finally:
resource.close()

def load_configfile(self):
loader = ConfigLoader(self.schema)
self.configroot, self.confighandlers = loader.loadURL(
self.configfile)

def __call__(self):
self.load_schema()
self.load_configfile()
return self
12 changes: 2 additions & 10 deletions src/Zope2/Startup/run.py
Expand Up @@ -43,13 +43,7 @@ def _set_wsgi_config(configfile=None):
Optionally accept a configfile argument (string path) in order
to specify where the configuration file exists. """
from Zope2.Startup import options, handlers
opts = options.ZopeWSGIOptions()
if configfile:
opts.configfile = configfile
opts.realize(raise_getopt_errs=0)
else:
opts.realize()

opts = options.ZopeWSGIOptions(configfile=configfile)()
handlers.handleWSGIConfig(opts.configroot, opts.confighandlers)
import App.config
App.config.setConfiguration(opts.configroot)
Expand All @@ -63,9 +57,7 @@ def make_wsgi_app(global_config, zope_conf):
from Zope2.Startup.options import ZopeWSGIOptions
from ZPublisher.WSGIPublisher import publish_module
starter = get_wsgi_starter()
opts = ZopeWSGIOptions()
opts.configfile = zope_conf
opts.realize(args=(), progname='Zope2WSGI', raise_getopt_errs=False)
opts = ZopeWSGIOptions(configfile=zope_conf)()
handleWSGIConfig(opts.configroot, opts.confighandlers)
setConfiguration(opts.configroot)
starter.setConfiguration(opts.configroot)
Expand Down
18 changes: 6 additions & 12 deletions src/Zope2/Startup/tests/test_schema.py
Expand Up @@ -21,38 +21,32 @@

from Zope2.Startup.options import ZopeWSGIOptions

_SCHEMA = {}
_SCHEMA = None
TEMPNAME = tempfile.mktemp()
TEMPVAR = os.path.join(TEMPNAME, "var")


def getSchema(schemafile):
def getSchema():
global _SCHEMA
if schemafile not in _SCHEMA:
if _SCHEMA is None:
opts = ZopeWSGIOptions()
opts.schemafile = schemafile
opts.load_schema()
_SCHEMA[schemafile] = opts.schema
return _SCHEMA[schemafile]
_SCHEMA = opts.schema
return _SCHEMA


class WSGIStartupTestCase(unittest.TestCase):

@property
def schema(self):
return getSchema('wsgischema.xml')

def load_config_text(self, text):
# We have to create a directory of our own since the existence
# of the directory is checked. This handles this in a
# platform-independent way.
schema = self.schema
sio = cStringIO.StringIO(
text.replace("<<INSTANCE_HOME>>", TEMPNAME))
os.mkdir(TEMPNAME)
os.mkdir(TEMPVAR)
try:
conf, handler = ZConfig.loadConfigFile(schema, sio)
conf, handler = ZConfig.loadConfigFile(getSchema(), sio)
finally:
os.rmdir(TEMPVAR)
os.rmdir(TEMPNAME)
Expand Down
9 changes: 2 additions & 7 deletions src/Zope2/Startup/tests/test_starter.py
Expand Up @@ -27,22 +27,17 @@
_SCHEMA = None


def getSchema(schemafile):
def getSchema():
global _SCHEMA
if _SCHEMA is None:
opts = ZopeWSGIOptions()
opts.schemafile = schemafile
opts.load_schema()
_SCHEMA = opts.schema
return _SCHEMA


class WSGIStarterTestCase(unittest.TestCase):

@property
def schema(self):
return getSchema('wsgischema.xml')

def setUp(self):
self.TEMPNAME = tempfile.mktemp()

Expand All @@ -66,7 +61,7 @@ def load_config_text(self, text):
if why == 17:
# already exists
pass
conf, self.handler = ZConfig.loadConfigFile(self.schema, sio)
conf, self.handler = ZConfig.loadConfigFile(getSchema(), sio)
self.assertEqual(conf.instancehome, self.TEMPNAME)
return conf

Expand Down
4 changes: 1 addition & 3 deletions src/Zope2/utilities/finder.py
Expand Up @@ -30,9 +30,7 @@ def get_app(self, config_file=None):
from Zope2.Startup import options, handlers
import App.config
import Zope2
opts = options.ZopeWSGIOptions()
opts.configfile = config_file
opts.realize(args=[], doc="", raise_getopt_errs=0)
opts = options.ZopeWSGIOptions(configfile=config_file)()
handlers.handleWSGIConfig(opts.configroot, opts.confighandlers)
App.config.setConfiguration(opts.configroot)
app = Zope2.app()
Expand Down

0 comments on commit 04ab51d

Please sign in to comment.