ZConfigInterpreter with data from STDIN
Alexey Smishlayev <[email protected]> Wed, 15 Apr 2015 15:49:51 +0300
| Newsgroups | gmane.comp.python.peak |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--------------000800080608010107040903
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Hello!
I am trying to refactor existing peak application. I need to be able to
load ZConfig file from data incoming by STDIN. Now, the loader included
in peak is using files, so I implemented a subclass which should
simulate a file resource (in file zconfig.py). I also searched peak.ini
for any lines regarding zconfig. I copied all settings and changed them
to point to my custom classes (in file custom.ini; "ank.BBS" is my
project package).
In the python script, ZConfigInperpreter was used to get the application
root component in the following way:
> _argv=["ZConfig", scheme, cfgName]
> _argv.extend(sys.argv[1:])
> return ZConfigInterpreter(
> config.makeRoot(),
> argv=_argv,
> componentName="MYAPP"
> )
Now I want to pass no cfgName and get interpreter to process the STDIN.
But the PEAK seem to not find settings which I've put in my .ini file
and I'm stuck.
I've narrowed it down - the
ZConfigInterpreter.lookupComponent('zconfig.schema:'+str(url)) returns
peak.config.load_zconfig.ConfigLoader, instead of my custom class
(STDINConfigLoader from my file). I tried to trace down the stack with
pdb, and it seems that some lookup for
"zconfig.schema:/path/to/schema/file.xml" returns
peak.config.load_zconfig.ZConfigSchemaContext.
I've subclassed that one too, but have no success.
Can anyone, please, help me, how to replace the standard ZConfig classes?
Best regards,
Alexey
--------------000800080608010107040903
Content-Type: text/x-python-script;
name="zconfig.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="zconfig.py"
import protocols
from StringIO import StringIO
from peak.api import binding, commands, config, naming
from peak.config.load_zconfig import SchemaLoader, ConfigLoader, ConfigRunner, ZConfigSchemaContext
from peak.running.commands import ICmdLineAppFactory
class STDINConfigLoader(ConfigLoader):
"""Load ZConfig file from the stdid instead of file resource"""
def openResource(self, url):
"""Create C{StringIO} from the stdin contents"""
_file = StringIO(sys.stdin.read())
return self.createResource(_file, url)
class STDINSchemaLoader(SchemaLoader):
def loadResource(self, resource):
result = super(SchemaLoader,self).loadResource(resource)
# use custom ConfigLoader
return STDINConfigLoader(self.getParentComponent(), schema=result)
class STDINZConfigSchemaContext(ZConfigSchemaContext):
def _get(self, name, retrieve=1):
# use custom SchemaLoader
return STDINSchemaLoader(self.creationParent).getObjectInstance(
self, naming.Reference('',[name.body]), name
)
class STDINConfigRunner(ConfigRunner):
def interpret(self, filename):
factory = config.getStreamFactory(self,filename)
print "hello there"
#ob, handler = self.loadFile(factory.open('t'), factory.address)
binding.suggestParentComponent(self.getCommandParent(),None,ob)
return self.getSubcommand(ob)
def STDINloaderAsRunner(ob):
schema, parent = ob.schema, ob.getParentComponent()
def STDINfactory(**kw):
kw.setdefault('schema', schema)
kw.setdefault('parentComponent', parent)
return STDINConfigRunner(**kw)
protocols.adviseObject(STDINfactory, provides=[ICmdLineAppFactory])
return STDINfactory
protocols.declareAdapter(
STDINloaderAsRunner,
provides = [ICmdLineAppFactory],
forTypes = [STDINConfigLoader]
)
class ZConfigInterpreter(commands.ZConfigInterpreter):
def __init__(self,*args,**kw):
super(ZConfigInterpreter, self).__init__(*args, **kw)
self.cmdParent
cmdConfig = binding.Make(lambda: ["pkgfile:ank/BBS/BBS.ini"])
def cmdParent(self):
if self.cmdConfig:
parent = config.ServiceArea(self)
config.loadConfigFiles(parent,self.cmdConfig)
return parent
cmdParent = binding.Make(cmdParent)
def getCommandParent(self):
"""Get or create a component to be used as the subcommand's parent"""
# Default is to use the interpreter as the parent
parent = self.cmdParent
if parent is None:
return self
return parent
--------------000800080608010107040903
Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0";
name="custom.ini"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="custom.ini"
IyBbLi4uXQpbcGVhay5uYW1pbmcuZmFjdG9yaWVzXQp6Y29uZmlnLnNjaGVtYSA9IGltcG9y
dFN0cmluZygKICAgICAgICAiYW5rLkJCUy56Y29uZmlnOlNURElOU2NoZW1hTG9hZGVyIgog
ICAgKSh0YXJnZXRPYmopCgpbcGVhay5uYW1pbmcuc2NoZW1lc10KemNvbmZpZy5zY2hlbWEg
PSAiYW5rLkJCUy56Y29uZmlnOlNURElOWkNvbmZpZ1NjaGVtYUNvbnRleHQiCgojWy4uLl0=
--------------000800080608010107040903
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
PEAK mailing list
[email protected]
http://www.eby-sarna.com/mailman/listinfo/peak
--------------000800080608010107040903--