Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if in_place:
if USING_PYTHON2:
sys.exit("{}: -i/--in-place is not compatible with Python 2".format(program_name))
if args.output_format not in {"yaml", "annotated_yaml"}:
sys.exit("{}: -i/--in-place can only be used with -y/-Y".format(program_name))
input_streams = yq_args.pop("input_streams")
if len(input_streams) == 1 and input_streams[0].name == "":
msg = "{}: -i/--in-place can only be used with filename arguments, not on standard input"
sys.exit(msg.format(program_name))
for i, input_stream in enumerate(input_streams):
def exit_handler(arg=None):
if arg:
sys.exit(arg)
if i < len(input_streams):
yq_args["exit_func"] = exit_handler
yq(input_streams=[input_stream], output_stream=DeferredOutputStream(input_stream.name), **yq_args)
else:
yq(**yq_args)
data = yaml.load(input)
try:
for item in op.apply([data]):
yield item
except MatchError as ex:
print ex
if __name__ == '__main__':
if len(sys.argv) < 2:
print >> sys.stderr, 'Usage: {} '.format(sys.argv[0])
sys.exit(2)
op = sys.argv[1]
input = sys.stdin.read()
for item in main(op, input):
print output(item)
jq_filter_arg_loc = len(jq_args)
if "--args" in jq_args:
jq_filter_arg_loc = jq_args.index('--args') + 1
elif "--jsonargs" in jq_args:
jq_filter_arg_loc = jq_args.index('--jsonargs') + 1
jq_args.insert(jq_filter_arg_loc, args.jq_filter)
delattr(args, "jq_filter")
in_place = args.in_place
delattr(args, "in_place")
if sys.stdin.isatty() and not args.input_streams:
return parser.print_help()
yq_args = dict(input_format=input_format, program_name=program_name, jq_args=jq_args, **vars(args))
if in_place:
if USING_PYTHON2:
sys.exit("{}: -i/--in-place is not compatible with Python 2".format(program_name))
if args.output_format not in {"yaml", "annotated_yaml"}:
sys.exit("{}: -i/--in-place can only be used with -y/-Y".format(program_name))
input_streams = yq_args.pop("input_streams")
if len(input_streams) == 1 and input_streams[0].name == "":
msg = "{}: -i/--in-place can only be used with filename arguments, not on standard input"
sys.exit(msg.format(program_name))
for i, input_stream in enumerate(input_streams):
def exit_handler(arg=None):
if arg:
sys.exit(arg)
if i < len(input_streams):
yq_args["exit_func"] = exit_handler
yq(input_streams=[input_stream], output_stream=DeferredOutputStream(input_stream.name), **yq_args)
else:
yq(**yq_args)
from yq.operators.base import Operator
from yq.operators.match_error import MatchError
class Comprehension(Operator):
def __init__(self, op):
self.op = op
def _apply_item(self, data):
if not isinstance(data, list):
raise MatchError(self, data, 'tried to apply comprehension %s to non-array' % self)
retval = []
for item in data:
retval.append(self.op._apply_item(item))
return retval
def __repr__(self):
return '[%s]' % self.op
from yq.operators.base import Operator
class Sequence(Operator):
def __init__(self, operators):
self.operators = operators
def apply(self, data):
for operator in self.operators:
data = operator.apply(data)
return data
def __repr__(self):
return ''.join([repr(operator) for operator in self.operators])
from yq.operators.base import Operator
class Subscript(Operator):
def __init__(self, indices):
self.indices = indices
def apply(self, data):
retval = []
for i in self.indices:
try:
retval.append(data[0][i])
except IndexError:
retval.append(None)
return retval
def __repr__(self):
return '[%s]' % ','.join(self.indices)
from yq.operators.base import Operator
from yq.operators.dot import Dot
from yq.operators.match_error import MatchError
class ProjectionItem(object):
def __init__(self, key, op):
self.key = key
self.op = op
class Projection(Operator):
def __init__(self, items):
self.items = items
def _apply_item(self, data):
retval = {}
for item in self.items:
retval[item.key] = item.op._apply_item(data)
return retval
def __repr__(self):
str_items = []
for item in self.items:
if isinstance(item.op, Dot) and item.op.key == item.key:
str_items.append(item.key)
else:
str_items.append('%s: %s' % (item.key, item.op))
from yq.operators.base import Operator
from yq.operators.match_error import MatchError
class Dot(Operator):
def __init__(self, key=''):
self.key = key
def _apply_item(self, data):
if self.key == '':
return data
if not isinstance(data, dict):
raise MatchError(self, data, 'tried to access field %s on a non-object' % self)
try:
return data[self.key]
except KeyError:
return None
def __repr__(self):
return '.%s' % self.key
#!/usr/bin/env python
import yaml
import sys
from yq import parser
from yq.operators.match_error import MatchError
from yq.output import output
op = parser.parse(sys.argv[1])
input = sys.stdin.read()
data = yaml.load(input)
try:
print output(op.apply(data))
except MatchError as ex:
print ex
def main(op_str, input):
op = parser.parse(op_str)
data = yaml.load(input)
try:
for item in op.apply([data]):
yield item
except MatchError as ex:
print ex