Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_instance_invalid_yaml(self, tmpdir):
p = tmpdir.join("config.yaml")
p.write(
"""
I a(m) no yaml:
- keks
cookie
"""
)
with pytest.raises(InvalidConfigError):
e = Environment(str(p))
name: "bar"
should be transformed to
resources: # or drivers
- cls: "FooPort"
- cls: "BarPort"
name: "bar"
"""
# resolve syntactic sugar (list of dicts each containing a dict of key -> args)
result = []
if isinstance(data, list):
for item in data:
if not isinstance(item, dict):
raise InvalidConfigError(
"invalid list item type {} (should be dict)".format(type(item)))
if not item:
raise InvalidConfigError("invalid empty dict as list item")
if len(item) > 1:
if 'cls' in item:
item = item.copy()
else:
raise InvalidConfigError("missing 'cls' key in {}".format(item))
else:
# only one pair left
(key, value), = item.items()
if key == 'cls':
item = item.copy()
else:
item = {'cls': key}
if value is None:
def on_activate(self):
assert self.wrapper is None
self.wrapper = AgentWrapper(self.tmc.host)
match = (self.tmc.vendor_id, self.tmc.model_id)
if match == (0x0957, 0x1798):
model = 'keysight_dsox2000'
elif match == (0x0699, 0x0368):
model = 'tektronix_tds2000'
else:
raise InvalidConfigError("Unkown USB TMC device {:04x}:{:04x}".format(*match))
# TODO: allow backends to register models with other names
self.backend = import_module(
".usbtmc.{}".format(model),
__package__
)
resources: # or drivers
- cls: "FooPort"
- cls: "BarPort"
name: "bar"
"""
# resolve syntactic sugar (list of dicts each containing a dict of key -> args)
result = []
if isinstance(data, list):
for item in data:
if not isinstance(item, dict):
raise InvalidConfigError(
"invalid list item type {} (should be dict)".format(type(item)))
if not item:
raise InvalidConfigError("invalid empty dict as list item")
if len(item) > 1:
if 'cls' in item:
item = item.copy()
else:
raise InvalidConfigError("missing 'cls' key in {}".format(item))
else:
# only one pair left
(key, value), = item.items()
if key == 'cls':
item = item.copy()
else:
item = {'cls': key}
if value is None:
raise InvalidConfigError("invalid list item, add empty dict for no arguments") # pylint: disable=line-too-long
item.update(value)
result.append(item)
def get_screenshot(self):
png = getattr(self.backend, 'get_screenshot_png', None)
tiff = getattr(self.backend, 'get_screenshot_tiff', None)
if png:
return 'png', png(self) # pylint: disable=not-callable
if tiff:
return 'tiff', tiff(self) # pylint: disable=not-callable
raise InvalidConfigError("get_screenshot_png/_tiff not implemented")
if not item:
raise InvalidConfigError("invalid empty dict as list item")
if len(item) > 1:
if 'cls' in item:
item = item.copy()
else:
raise InvalidConfigError("missing 'cls' key in {}".format(item))
else:
# only one pair left
(key, value), = item.items()
if key == 'cls':
item = item.copy()
else:
item = {'cls': key}
if value is None:
raise InvalidConfigError("invalid list item, add empty dict for no arguments") # pylint: disable=line-too-long
item.update(value)
result.append(item)
elif isinstance(data, dict):
for cls, args in data.items():
args.setdefault('cls', cls)
result.append(args)
else:
raise InvalidConfigError("invalid type {} (should be dict or list)".format(type(data)))
for item in result:
item.setdefault('name', None)
assert 'cls' in item
return result
def get_caps(self):
match = (self.video.vendor_id, self.video.model_id)
if match == (0x046d, 0x082d):
return ("mid", [
("low", "video/x-h264,width=640,height=360,framerate=5/1"),
("mid", "video/x-h264,width=1280,height=720,framerate=15/2"),
("high", "video/x-h264,width=1920,height=1080,framerate=10/1"),
])
if match == (0x046d, 0x0892):
return ("mid", [
("low", "image/jpeg,width=640,height=360,framerate=5/1"),
("mid", "image/jpeg,width=1280,height=720,framerate=15/2"),
("high", "image/jpeg,width=1920,height=1080,framerate=10/1"),
])
raise InvalidConfigError("Unkown USB video device {:04x}:{:04x}".format(*match))
session.loop.close()
except NoResourceFoundError as e:
if args.debug:
traceback.print_exc()
else:
print("{}: error: {}".format(parser.prog, e), file=sys.stderr)
print("This may be caused by disconnected exporter or wrong match entries.\nYou can use the 'show' command to review all matching resources.", file=sys.stderr) # pylint: disable=line-too-long
exitcode = 1
except NoDriverFoundError as e:
if args.debug:
traceback.print_exc()
else:
print("{}: error: {}".format(parser.prog, e), file=sys.stderr)
print("This is likely caused by an error or missing driver in the environment configuration.", file=sys.stderr) # pylint: disable=line-too-long
exitcode = 1
except InvalidConfigError as e:
if args.debug:
traceback.print_exc()
else:
print("{}: error: {}".format(parser.prog, e), file=sys.stderr)
print("This is likely caused by an error in the environment configuration or invalid\nresource information provided by the coordinator.", file=sys.stderr) # pylint: disable=line-too-long
exitcode = 1
except ConnectionError as e:
print("Could not connect to coordinator: {}".format(e))
exitcode = 1
except Error as e:
if args.debug:
traceback.print_exc()
else:
print("{}: error: {}".format(parser.prog, e), file=sys.stderr)
exitcode = 1
except KeyboardInterrupt:
def make_driver(self, target, driver, name, args):
assert isinstance(args, dict)
if not driver in self.drivers:
raise InvalidConfigError("unknown driver class {}".format(driver))
try:
cls = self.drivers[driver]
args = filter_dict(args, cls, warn=True)
d = cls(target, name, **args)
except TypeError as e:
raise InvalidConfigError(
"failed to create {} for target '{}' using {} ".format(
driver, target, args)) from e
return d
"""
# resolve syntactic sugar (list of dicts each containing a dict of key -> args)
result = []
if isinstance(data, list):
for item in data:
if not isinstance(item, dict):
raise InvalidConfigError(
"invalid list item type {} (should be dict)".format(type(item)))
if not item:
raise InvalidConfigError("invalid empty dict as list item")
if len(item) > 1:
if 'cls' in item:
item = item.copy()
else:
raise InvalidConfigError("missing 'cls' key in {}".format(item))
else:
# only one pair left
(key, value), = item.items()
if key == 'cls':
item = item.copy()
else:
item = {'cls': key}
if value is None:
raise InvalidConfigError("invalid list item, add empty dict for no arguments") # pylint: disable=line-too-long
item.update(value)
result.append(item)
elif isinstance(data, dict):
for cls, args in data.items():
args.setdefault('cls', cls)
result.append(args)
else: