2 '''Python interface to the wire-cell command. 6 * WC file is a wire-cell configuration file in JSON format. 8 * WC JSON is the contents of a WC file. 10 * A configlist is the Python data structure representing a WC JSON 11 string. It is a list of dictionaries with keys "type", "name" and 12 "data" which holds a type-specific data structure. 14 * A configdict is a dictionary keyed by tuple(type,name) with a value 15 that of the corresponding "data" value. 17 The C++ Wire Cell components which are configurable provide a default, 18 hard-coded configuration "data" structure. 21 from collections
import defaultdict
27 formats =
'wcjson wcfile configlist configdict'.
split()
30 self.
_cfg = defaultdict(dict)
35 obj = kwds.get(form,
None)
37 meth = getattr(self,
'load_'+form)
41 cl = json.loads(string)
49 self.
load_one(d[
'type'], d.get(
'name',
''), d[
'data'])
52 for (t,n),d
in self._cfg.items():
55 def get(self, type, name=''):
56 return self.
_cfg[(type,name)]
59 val = self.
get(type,name)
61 self.
_cfg[(type,name)] = val
66 Merge other into copy of self and return new Config object 69 for (t,n),d
in self._cfg.items():
72 for (t,n),d
in other._cfg.items():
78 Return wire-cell compatible JSON representation. 81 for (t,n),d
in self._cfg.items():
82 dat.append(dict(type=t, name=n, data=d))
83 return json.dumps(dat)
89 '''Python interface to wire-cell executable. 91 Beware, this trusts its input! In particular do not let 92 <executable> be set by untrusted sources. 95 default_plugins = [
'WireCellAlg',
'WireCellGen',
'WireCellApps',
'WireCellTbb']
97 def __init__(self, executable='wire-cell', **kwds):
101 self.
app = kwds.get(
'app',
None)
105 Return command line string 107 parts = [
'%s -c /dev/stdin' % self.
prog]
108 parts += [
'-p %s'%p
for p
in self.
plugins]
109 app = app
or self.
app 111 parts.append(
'-a %s' % app)
112 return ' '.join(parts)
117 If app is not given then fall back to one given to constructor. 119 If any config is given it is merged with any given to constructor. 124 cfg = self.config.merge(config).wcjson()
126 proc = subprocess.Popen(cmd, shell=
True,
127 stdin = subprocess.PIPE,
128 stdout = subprocess.PIPE,
129 stderr = subprocess.PIPE)
130 out,err= proc.communicate(cfg)
137 Return a config list giving configuration for components 140 config.load_one(
"ConfigDumper",
"", data=dict(components = components))
141 o,e = self(
"ConfigDumper", config=config)
def load_wcfile(self, filename)
def load_one(self, type, name, data)
def __call__(self, app=None, config=None)
def load_configlist(self, cfglist)
int open(const char *, int)
Opens a file descriptor.
def load_configdict(self, cfgdict)
def cmdline(self, app=None)
def get(self, type, name='')
def load_wcjson(self, string)
int read(int, char *, size_t)
Read bytes from a file descriptor.
void split(std::string const &s, char c, OutIter dest)
def component_configlist(self, components)
def __init__(self, executable='wire-cell', kwds)