mirror of https://github.com/m-labs/artiq.git
artiq_run: move parse_arguments and get_experiment to tools
This commit is contained in:
parent
2456e795dd
commit
5e046dc5ce
|
@ -10,11 +10,10 @@ import logging
|
||||||
import h5py
|
import h5py
|
||||||
|
|
||||||
from artiq.language.db import *
|
from artiq.language.db import *
|
||||||
from artiq.language.experiment import is_experiment, Experiment
|
from artiq.language.experiment import Experiment
|
||||||
from artiq.protocols import pyon
|
|
||||||
from artiq.protocols.file_db import FlatFileDB
|
from artiq.protocols.file_db import FlatFileDB
|
||||||
from artiq.master.worker_db import DBHub, ResultDB
|
from artiq.master.worker_db import DBHub, ResultDB
|
||||||
from artiq.tools import file_import, verbosity_args, init_logger
|
from artiq.tools import *
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -99,28 +98,6 @@ def get_argparser(with_file=True):
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def _parse_arguments(arguments):
|
|
||||||
d = {}
|
|
||||||
for argument in arguments:
|
|
||||||
name, eq, value = argument.partition("=")
|
|
||||||
d[name] = pyon.decode(value)
|
|
||||||
return d
|
|
||||||
|
|
||||||
|
|
||||||
def _get_experiment(module, experiment=None):
|
|
||||||
if experiment:
|
|
||||||
return getattr(module, experiment)
|
|
||||||
|
|
||||||
exps = [(k, v) for k, v in module.__dict__.items()
|
|
||||||
if is_experiment(v)]
|
|
||||||
if not exps:
|
|
||||||
logger.error("No experiments in module")
|
|
||||||
if len(exps) > 1:
|
|
||||||
logger.warning("Multiple experiments (%s), using first",
|
|
||||||
", ".join(k for (k, v) in exps))
|
|
||||||
return exps[0][1]
|
|
||||||
|
|
||||||
|
|
||||||
def _build_experiment(dbh, args):
|
def _build_experiment(dbh, args):
|
||||||
if hasattr(args, "file"):
|
if hasattr(args, "file"):
|
||||||
if args.file.endswith(".elf"):
|
if args.file.endswith(".elf"):
|
||||||
|
@ -136,8 +113,8 @@ def _build_experiment(dbh, args):
|
||||||
else:
|
else:
|
||||||
module = sys.modules["__main__"]
|
module = sys.modules["__main__"]
|
||||||
file = getattr(module, "__file__")
|
file = getattr(module, "__file__")
|
||||||
exp = _get_experiment(module, args.experiment)
|
exp = get_experiment(module, args.experiment)
|
||||||
arguments = _parse_arguments(args.arguments)
|
arguments = parse_arguments(args.arguments)
|
||||||
return exp(dbh,
|
return exp(dbh,
|
||||||
scheduler=DummyScheduler(),
|
scheduler=DummyScheduler(),
|
||||||
run_params=dict(file=file,
|
run_params=dict(file=file,
|
||||||
|
|
|
@ -7,6 +7,16 @@ import asyncio
|
||||||
import time
|
import time
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
|
from artiq.language.experiment import is_experiment
|
||||||
|
from artiq.protocols import pyon
|
||||||
|
|
||||||
|
|
||||||
|
def parse_arguments(arguments):
|
||||||
|
d = {}
|
||||||
|
for argument in arguments:
|
||||||
|
name, eq, value = argument.partition("=")
|
||||||
|
d[name] = pyon.decode(value)
|
||||||
|
return d
|
||||||
|
|
||||||
def format_arguments(arguments):
|
def format_arguments(arguments):
|
||||||
fmtargs = []
|
fmtargs = []
|
||||||
|
@ -41,6 +51,19 @@ def file_import(filename):
|
||||||
return module
|
return module
|
||||||
|
|
||||||
|
|
||||||
|
def get_experiment(module, experiment=None):
|
||||||
|
if experiment:
|
||||||
|
return getattr(module, experiment)
|
||||||
|
|
||||||
|
exps = [(k, v) for k, v in module.__dict__.items()
|
||||||
|
if is_experiment(v)]
|
||||||
|
if not exps:
|
||||||
|
raise ValueError("No experiments in module")
|
||||||
|
if len(exps) > 1:
|
||||||
|
raise ValueError("More than one experiment found in module")
|
||||||
|
return exps[0][1]
|
||||||
|
|
||||||
|
|
||||||
def verbosity_args(parser):
|
def verbosity_args(parser):
|
||||||
group = parser.add_argument_group("verbosity")
|
group = parser.add_argument_group("verbosity")
|
||||||
group.add_argument("-v", "--verbose", default=0, action="count",
|
group.add_argument("-v", "--verbose", default=0, action="count",
|
||||||
|
|
Loading…
Reference in New Issue