def test_duplicate_error(self): text = ("key1: value1\n" "key1: value2\n") with StringIO(text) as stream: with self.assertRaises(RFC822SyntaxError) as call: load_rfc822_records(stream) self.assertEqual( call.exception.msg, "Job has a duplicate key 'key1' with old value 'value1'" " and new value 'value2'")
def test_duplicate_error(self): text = ( "key1: value1\n" "key1: value2\n" ) with StringIO(text) as stream: with self.assertRaises(RFC822SyntaxError) as call: load_rfc822_records(stream) self.assertEqual(call.exception.msg, ( "Job has a duplicate key 'key1' with old value 'value1'" " and new value 'value2'"))
def _plugin_resource(self, job): proc = self._run_command(job) if proc.returncode == 127: logging.warning("Unable to find command: %s", job.command) return line_list = [] for byte_line in proc.stdout.splitlines(): try: line = byte_line.decode("UTF-8") except UnicodeDecodeError as exc: logger.warning("resource script %s returned invalid UTF-8 data" " %r: %s", job, byte_line, exc) else: line_list.append(line) with StringIO("\n".join(line_list)) as stream: try: record_list = load_rfc822_records(stream) except RFC822SyntaxError as exc: logger.warning("resource script %s returned invalid RFC822" " data: %s", job, exc) else: for record in record_list: logger.info("Storing resource record %s: %s", job.name, record) resource = Resource(record) self._context.add_resource(job.name, resource)
def test_many_records(self): text = ("key1:value1\n" "\n" "key2:value2\n" "\n" "key3:value3\n") with StringIO(text) as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 3) self.assertEqual(records[0], {'key1': 'value1'}) self.assertEqual(records[1], {'key2': 'value2'}) self.assertEqual(records[2], {'key3': 'value3'})
def test_relevant_whitespace(self): text = ( "key:\n" " value\n" ) with StringIO(text) as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 1) self.assertEqual(records[0].data, {'key': 'value'})
def test_relevant_whitespace(self): text = ( "key:\n" " value\n" ) with StringIO(text) as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 1) self.assertEqual(records[0], {'key': 'value'})
def test_multiline_value(self): text = ( "key:\n" " longer\n" " value\n" ) with StringIO(text) as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 1) self.assertEqual(records[0].data, {'key': 'longer\nvalue'})
def test_multiline_value(self): text = ( "key:\n" " longer\n" " value\n" ) with StringIO(text) as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 1) self.assertEqual(records[0], {'key': 'longer\nvalue'})
def test_many_multiline_values(self): text = ("key1:initial\n" " longer\n" " value 1\n" "\n" "key2:\n" " longer\n" " value 2\n") with StringIO(text) as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 2) self.assertEqual(records[0], {'key1': 'initial\nlonger\nvalue 1'}) self.assertEqual(records[1], {'key2': 'longer\nvalue 2'})
def test_many_records(self): text = ( "key1:value1\n" "\n" "key2:value2\n" "\n" "key3:value3\n" ) with StringIO(text) as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 3) self.assertEqual(records[0].data, {'key1': 'value1'}) self.assertEqual(records[1].data, {'key2': 'value2'}) self.assertEqual(records[2].data, {'key3': 'value3'})
def test_many_multiline_values(self): text = ( "key1:initial\n" " longer\n" " value 1\n" "\n" "key2:\n" " longer\n" " value 2\n" ) with StringIO(text) as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 2) self.assertEqual(records[0].data, {'key1': 'initial\nlonger\nvalue 1'}) self.assertEqual(records[1].data, {'key2': 'longer\nvalue 2'})
def load(self, somewhere): if isinstance(somewhere, str): # Load data from a file with the given name filename = somewhere with open(filename, 'rt', encoding='UTF-8') as stream: return load(stream) if isinstance(somewhere, TextIOWrapper): stream = somewhere logger.debug("Loading jobs definitions from %r...", stream.name) record_list = load_rfc822_records(stream) job_list = [] for record in record_list: job = JobDefinition.from_rfc822_record(record) logger.debug("Loaded %r", job) job_list.append(job) return job_list else: raise TypeError("Unsupported type of 'somewhere': {!r}".format( type(somewhere)))
def load(self, somewhere): if isinstance(somewhere, str): # Load data from a file with the given name filename = somewhere with open(filename, 'rt', encoding='UTF-8') as stream: return load(stream) if isinstance(somewhere, TextIOWrapper): stream = somewhere logger.debug("Loading jobs definitions from %r...", stream.name) record_list = load_rfc822_records(stream) job_list = [] for record in record_list: job = JobDefinition.from_rfc822_record(record) logger.debug("Loaded %r", job) job_list.append(job) return job_list else: raise TypeError( "Unsupported type of 'somewhere': {!r}".format( type(somewhere)))
def test_empty(self): with StringIO("") as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 0)
def test_syntax_error(self): text = "key1 = value1" with StringIO(text) as stream: with self.assertRaises(RFC822SyntaxError) as call: load_rfc822_records(stream) self.assertEqual(call.exception.msg, "Unexpected non-empty line")
def test_bad_multiline(self): text = " extra value" with StringIO(text) as stream: with self.assertRaises(RFC822SyntaxError) as call: load_rfc822_records(stream) self.assertEqual(call.exception.msg, "Unexpected multi-line value")
def test_garbage(self): text = "garbage" with StringIO(text) as stream: with self.assertRaises(RFC822SyntaxError) as call: load_rfc822_records(stream) self.assertEqual(call.exception.msg, "Unexpected non-empty line")
def test_bad_multilie(self): text = " extra value" with StringIO(text) as stream: with self.assertRaises(RFC822SyntaxError) as call: load_rfc822_records(stream) self.assertEqual(call.exception.msg, "Unexpected multi-line value")
def test_single_record(self): with StringIO("key:value") as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 1) self.assertEqual(records[0].data, {'key': 'value'})
def test_single_record(self): with StringIO("key:value") as stream: records = load_rfc822_records(stream) self.assertEqual(len(records), 1) self.assertEqual(records[0], {'key': 'value'})