本文整理汇总了Python中redbaron.RedBaron类的典型用法代码示例。如果您正苦于以下问题:Python RedBaron类的具体用法?Python RedBaron怎么用?Python RedBaron使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RedBaron类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
"""Rewrite Thrift-generated Python clients to handle recursive structs. For
more details see: https://issues.apache.org/jira/browse/THRIFT-2642.
Requires package `RedBaron`, available via pip:
$ pip install redbaron
To use:
$ thrift -gen py mapd.thrift
$ mv gen-py/mapd/ttypes.py gen-py/mapd/ttypes-backup.py
$ python fix_recursive_structs.py gen-py/mapd/ttypes-backup.py gen-py/mapd/ttypes.py
"""
in_file = open(sys.argv[1], 'r')
out_file = open(sys.argv[2], 'w')
red_ast = RedBaron(in_file.read())
thrift_specs = [ts.parent for ts in red_ast.find_all(
'name', 'thrift_spec') if ts.parent.type == 'assignment' and ts.parent.parent.name in ['TDatumVal', 'TColumnData']]
nodes = []
for ts in thrift_specs:
node = ts.copy()
node.target = ts.parent.name + '.' + str(node.target)
nodes.append(node)
ts.value = 'None'
red_ast.extend(nodes)
out_file.write(red_ast.dumps())
示例2: test_node_elif_ifelseblock_next
def test_node_elif_ifelseblock_next():
red = RedBaron("if a:\n pass\nelif a:\n pass")
assert red.elif_.next is None
red = RedBaron("if a:\n pass\nelif a:\n pass\nelse:\n pass")
assert red.elif_.next is red.else_
red = RedBaron("if a:\n pass\nelif a:\n pass\nchocolat")
assert red.elif_.next is red.find("name", "chocolat")
示例3: _cleanupPyLintComments
def _cleanupPyLintComments(filename, abort):
from baron.parser import ( # pylint: disable=I0021,import-error,no-name-in-module
ParsingError, # @UnresolvedImport
)
from redbaron import ( # pylint: disable=I0021,import-error,no-name-in-module
RedBaron, # @UnresolvedImport
)
old_code = getFileContents(filename)
try:
red = RedBaron(old_code)
# red = RedBaron(old_code.rstrip()+'\n')
except ParsingError:
if abort:
raise
my_print("PARSING ERROR.")
return 2
for node in red.find_all("CommentNode"):
try:
_updateCommentNode(node)
except Exception:
my_print("Problem with", node)
node.help(deep=True, with_formatting=True)
raise
new_code = red.dumps()
if new_code != old_code:
with open(filename, "w") as source_code:
source_code.write(red.dumps())
示例4: insert_output_start_stop_indicators
def insert_output_start_stop_indicators(src):
"""
Insert identifier strings so that output can be segregated from input.
Parameters
----------
src : str
String containing input and output lines.
Returns
-------
str
String with output demarked.
"""
rb = RedBaron(src)
# find lines with trailing comments so we can preserve them properly
lines_with_comments = {}
comments = rb.findAll('comment')
for c in comments:
if c.previous and c.previous.type != 'endl':
lines_with_comments[c.previous] = c
input_block_number = 0
# find all nodes that might produce output
nodes = rb.findAll(lambda identifier: identifier in ['print', 'atomtrailers'])
for r in nodes:
# assume that whatever is in the try block will fail and produce no output
# this way we can properly handle display of error messages in the except
if hasattr(r.parent, 'type') and r.parent.type == 'try':
continue
# Output within if/else statements is not a good idea for docs, because
# we don't know which branch execution will follow and thus where to put
# the output block. Regardless of which branch is taken, though, the
# output blocks must start with the same block number.
if hasattr(r.parent, 'type') and r.parent.type == 'if':
if_block_number = input_block_number
if hasattr(r.parent, 'type') and r.parent.type in ['elif', 'else']:
input_block_number = if_block_number
if is_output_node(r):
# if there was a trailing comment on this line, output goes after it
if r in lines_with_comments:
r = lines_with_comments[r] # r is now the comment
# find the correct node to 'insert_after'
while hasattr(r, 'parent') and not hasattr(r.parent, 'insert'):
r = r.parent
r.insert_after('print(">>>>>%d")\n' % input_block_number)
input_block_number += 1
# curse you, redbaron! stop inserting endl before trailing comments!
for l, c in lines_with_comments.items():
if c.previous and c.previous.type == 'endl':
c.previous.value = ''
return rb.dumps()
示例5: compare
def compare(s1, s2, decay_factor = DEFAULT_DECAY_FACTOR):
red1 = RedBaron(s1)
red2 = RedBaron(s2)
result = []
for ast_f2 in red2.find_all('def'):
ast_f1 = red1.find('def', name = ast_f2.name)
if ast_f1 is not None:
additions, deletions = preprocess_files(ast_f1.dumps(),
ast_f2.dumps())
comments, exceptions = preprocess_comments(ast_f2, additions)
for a in additions:
for c in comments:
line, _ = c.left_bounds
distance = math.fabs(line - a)
score = int(c.score() - float(decay_factor) / (distance * distance))
c.setScore(score if score > 0 else 0)
for d in deletions:
for c in comments:
line, _ = c.left_bounds
line = line + 1 if line >= d else line
distance = math.fabs(line - d)
score = int(c.score() - float(decay_factor) / (distance * distance))
c.setScore(score if score > 0 else 0)
result.extend(comments)
result.extend(exceptions)
else:
result.extend(preprocess_comments(ast_f2, []))
return result
示例6: test_find_empty
def test_find_empty():
red = RedBaron("")
assert red.find("stuff") is None
assert red.find("something_else") is None
assert red.find("something_else", useless="pouet") is None
with pytest.raises(AttributeError):
red.will_raises
示例7: readCode
def readCode(self,modulepath=None):
result=Bag()
modulepath=modulepath or sys.modules[self.__module__].__file__
with open(modulepath, "r") as source_code:
red = RedBaron(source_code.read())
result.fromJson(red.fst())
return result
示例8: test_node_if_ifelseblock_previous_intuitive
def test_node_if_ifelseblock_previous_intuitive():
red = RedBaron("if a:\n pass")
assert red.if_.previous_intuitive is None
red = RedBaron("chocolat\nif a:\n pass")
assert red.if_.previous_intuitive is red.find("endl")
red = RedBaron("pouet\nif a:\n pass\nelif a:\n pass\nelse:\n pass")
assert red.else_.previous_intuitive is red.elif_
assert red.if_.previous is None
示例9: test_node_else_elseelseblock_next_generator
def test_node_else_elseelseblock_next_generator():
red = RedBaron("if a:\n pass\nelse:\n pass")
assert len(list(red.else_.next_generator())) == 0
red = RedBaron("if a:\n pass\nelse:\n pass\nchocolat")
assert list(red.else_.next_generator())[0] is red.find("name", "chocolat")
red = RedBaron("if a:\n pass\nelse:\n pass\nchocolat")
assert list(red.else_.next_generator()) == [red.find("name", "chocolat")]
示例10: main
def main(meetup, tc=(255, 255, 255), bg=None, *tags):
target_url = meetup
soup = BeautifulSoup(requests.get(target_url).content, "html.parser")
description = soup.find("div", id="groupDesc")
description = (" " * 4).join(map(lambda x: str(x), description.contents)) + (" " * 4)
description = "\n".join(map(lambda x: x.rstrip(), description.split("\n")))
target_meetup_name = target_url.split("/")[-2]
target = target_url.split("/")[-2].lower().replace("-", "_")
if re.match("^\d", target):
target = "_" + target
logo_url = soup.find("img", "photo")["src"] if soup.find("img", "photo") else None
if bg == None:
if logo_url:
palette = extract_colors(Image.open(BytesIO(requests.get(logo_url).content)))
colors = palette.colors
background_color = colors[0].value
text_color = tc
else:
h = (random.randint(1, 100) * 0.618033988749895) % 1
background_color = hsv_to_rgb(h, .5, .95)
text_color = "#000000"
h, s, v = rgb_to_hsv(background_color)
else:
background_color = bg
text_color = tc
# background_color = map(lambda x: (x + 255)/2, background_color)
red = RedBaron(open("agendas/be.py", "r").read())
for i in red("def", recursive=False):
if target < i.name:
break
i.insert_before(template % {
"background_color": rgb_to_hex(background_color) if not (isinstance(background_color, basestring) and background_color.startswith("#")) else background_color,
"text_color": rgb_to_hex(text_color) if not (isinstance(text_color, basestring) and text_color.startswith("#")) else text_color,
"url": target_url,
"tags": ", ".join(map(repr, tags)),
"function_name": target,
"description": description,
"meetup_name": target_meetup_name,
})
red.dumps()
open("agendas/be.py", "w").write(red.dumps())
os.system("python manage.py fetch_events %s" % target)
示例11: test_comma_proxy_list_indented_set_item
def test_comma_proxy_list_indented_set_item():
red = RedBaron("[\n 1,\n]")
comma_proxy_list = red[0].value
comma_proxy_list[0] = "42"
assert comma_proxy_list[0].type == "int"
assert comma_proxy_list[0].value == "42"
comma_proxy_list[0] = "plop"
assert comma_proxy_list[0].type == "name"
assert comma_proxy_list[0].value == "plop"
assert red.dumps() == "[\n plop,\n]"
示例12: test_comma_proxy_list_set_item
def test_comma_proxy_list_set_item():
red = RedBaron("[1]")
comma_proxy_list = red[0].value
comma_proxy_list[0] = "42"
assert comma_proxy_list[0].type == "int"
assert comma_proxy_list[0].value == 42
comma_proxy_list[0] = "plop"
assert comma_proxy_list[0].type == "name"
assert comma_proxy_list[0].value == "plop"
assert red.dumps() == "[plop]"
示例13: replace_argument
def replace_argument(txt, pos, new):
"""
"""
red = RedBaron(txt)
fst = red.fst()[0]
args = fst['arguments']
args = filter(arg_type_no_comma, args)
args.pop(pos)
args.insert(pos, new)
res = reform_input(args, method=fst['name'])
return res
示例14: remove_raise_skip_tests
def remove_raise_skip_tests(src):
"""
Remove from the code any raise unittest.SkipTest lines since we don't want those in
what the user sees.
"""
rb = RedBaron(src)
raise_nodes = rb.findAll("RaiseNode")
for rn in raise_nodes:
# only the raise for SkipTest
if rn.value[:2].dumps() == 'unittestSkipTest':
rn.parent.value.remove(rn)
return rb.dumps()
示例15: get_method_body
def get_method_body(method_code):
'''Using the RedBaron module, get the body of a method.
Do not want the definition signature line
'''
method_code = '\n' + method_code # For some reason RedBaron has problems with this if
# if it does not start with an empty line
rb = RedBaron(method_code)
def_node = rb.findAll("DefNode")[0] # Look for the 'def' node. Should only be one!
def_node.value.decrease_indentation(8) # so that the code is all the way to the left
return def_node.value.dumps()