Пример #1
0
def template(func: Union['Kernel', 'Function']) -> str:
    body = ''.join(func.body)
    tpl = func.header + '\n{' + body + '}\n'
    args = [value.to_string(key) + ',' for key, value in func.args.items()]
    args = '{}'.format('\n'.join(args))
    args = args[:-1]  # remove last comma
    replacements = {
        'name': func.name,
        'args': args,
        'returns': c_name_from_dtype(func.returns)
    }
    for key, value in func.replacements.items():
        replacements[key] = str(value)
    try:  # todo: e.g. if replacement has been forgotten, still save template as file
        tpl = Template(tpl).render(**replacements)
    except:
        raise ValueError(exceptions.text_error_template().render())

    defines = '\n'.join([
        '#define {} {}'.format(key, str(value))
        for key, value in func.defines.items()
    ])
    tpl_formatted = pyastyle.format('{}\n\n{}'.format(defines, tpl),
                                    '--style=allman --indent=spaces=4')
    return tpl_formatted
Пример #2
0
def test_format_simple_code():
    code = '''\
int main(void)
{
  }'''
    expected = '''\
int main(void)
{
}'''
    assert expected == pyastyle.format(code, '--style=allman')
Пример #3
0
 def run_whole_file(self, edit, options):
     view = self.view
     region = sublime.Region(0, view.size())
     code = view.substr(region)
     # Performing astyle formatter
     formatted_code = pyastyle.format(code, options)
     # Replace to view
     _, err = merge_code(view, edit, code, formatted_code)
     if err:
         sublime.error_message('%s: Merge failure: "%s"' % (PLUGIN_NAME, err))
Пример #4
0
def test_format_simple_code():
    code = '''\
int main(void)
{
  }'''
    expected = '''\
int main(void)
{
}'''
    assert expected == pyastyle.format(code, '--style=allman')
Пример #5
0
def format_code(content):
    args = ('pad-oper', 'pad-first-paren-out', 'add-brackets', 'close-templates')
    kwargs = {
        'style':            'java',
        'align-pointer':    'type',
        'align-reference':  'type'
    }
    options = []
    for arg in args:
        options.append("--%s" % arg)

    for kwarg in kwargs.items():
        options.append("--%s=%s" % kwarg)

    return pyastyle.format(content, ' '.join(options))
Пример #6
0
    def write_file(file_path, content):
        if not os.path.exists(os.path.dirname(file_path)):
            os.makedirs(os.path.dirname(file_path))
        f = codecs.open(file_path, "w", constants.UTF8)

        try:
            import pyastyle

            content = re.sub("(?![ ]*\/)[ ]{2,}", " ", content)
            content = pyastyle.format(content, "")
        except ImportError:
            FileHandling.warn_pyastyle = True

        f.write(FileHandling.license)
        f.write("\n")
        f.write(content)
        f.close()
        return
Пример #7
0
    def write_file(file_path, content):
        if not os.path.exists(os.path.dirname(file_path)):
            os.makedirs(os.path.dirname(file_path))
        f = codecs.open(file_path, "w", constants.UTF8)

        try:
            import pyastyle

            content = re.sub("(?![ ]*\/)[ ]{2,}", " ", content)
            content = pyastyle.format(content, "")
        except ImportError:
            FileHandling.warn_pyastyle = True

        f.write(FileHandling.license)
        f.write("\n")
        f.write(content)
        f.close()
        return
Пример #8
0
    def __print_source(self, filename):
        try:
            lexer = pygments.lexers.get_lexer_for_filename(filename)
        except pygments.util.ClassNotFound as e:
            lexer = pygments.lexers.get_lexer_for_filename("hello.c")

        code = self.source.read(filename)
        mode = os.path.splitext(filename)[1].lower()[1:]

        if mode not in settings.ASTYLE_BLACKLIST_EXTENSIONS:
            if mode not in ['c', 'cs', 'java']:
                mode = 'c'

            opts = settings.ASTYLE_OPTIONS.format(mode=mode)
            code = pyastyle.format(code, opts)

        return doughnut.fixer.fix_highlight(
            pygments.highlight(
                code, lexer,
                pygments.formatters.HtmlFormatter(
                    linenos=True,
                    linespans=settings.PYGMENT_HTML_LINE_SPAN_PREFIX)))
Пример #9
0
    def generateCode(self):
        impl = ''
        api = ''
        api_sm_include = ''
        pyapi = ''

        pyapi += "# Note: This file is an autogenerated API of " + self.states[
            0].sid + " statemachine\n"
        pyapi += "# and should not be edited manually.\n\n"

        pyapi = '#!/usr/bin/python3'
        pyapi += '\n\n\n'
        pyapi += 'class PySmAPI():'
        pyapi += '\n\n'

        num = 1
        for event in self.eventDict.keys():
            nmeStr = 4 * ' ' + event
            pyapi += nmeStr + ' ' * (40 -
                                     len(nmeStr)) + ' = ' + str(num) + '\n'
            num += 1

        pyapi += '\n\n'
        pyapi += 4 * ' ' + 'def __init__(self):'
        pyapi += '\n'
        pyapi += 8 * ' ' + 'return'
        pyapi += '\n'

        impl += "// Note: This file is an autogenerated API/Implementation of statemachine\n"
        impl += "// and should only be edited (if needed) within the START/END tags.\n"
        impl += "// Adding an event or state is not allowed as it will break the consistency\n"
        impl += "// with statemachine diagram.\n\n\n"

        impl += "#include " + '"SM' + self.states[0].sid + '.h"\n\n'

        impl += "// @ACSTAG:INCLUDES:UCODE:START\n"
        for include in self.includes:
            if 'ACStates.h' in include:
                api_sm_include = include
            else:
                impl += "#include " + include + '\n'
        impl += "// @ACSTAG:INCLUDES:UCODE:END\n\n"

        impl += 'ScheduleEventPtr_t scheduleEvent;\n\n'

        sids = []
        for state in self.states:
            sids.append(state.sid)
        impl += 'State_t ' + ', '.join(sids) + ';\n'
        impl += 'StateMachine_t sm;\n\n\n\n\n'

        for state in self.states:
            sid = state.sid
            impl += '// ' + sid + '\n'
            impl += 'EventState_t ' + sid + '_entry(Event_t ev)\n'
            impl += '{\n'
            if state.entry[0] == True:
                impl += "// @ACSTAG:ENTRY:" + sid + ":UCODE:START\n"
                # Script node exits but is blank. So insert a newline here.
                if None == state.entry[1]:
                    impl += '\n'
                else:
                    impl += 4 * ' ' + state.entry[1].replace(
                        '\n', '\n' + 4 * ' ').strip() + '\n'
                impl += "// @ACSTAG:ENTRY:" + sid + ":UCODE:END\n"
            impl += 4 * ' ' + 'return EVENT_HANDLED;\n'
            impl += '}\n'

            impl += 'EventState_t ' + sid + '_inprogress(Event_t ev)\n'
            impl += '{\n'

            for inttran in state.internal:
                impl += 4 * ' ' + 'if (ev.eventId==' + inttran[1] + ')\n'
                impl += 4 * ' ' + '{\n'
                code = inttran[2]
                impl += "// @ACSTAG:INTTRAN:" + sid + ":" + inttran[
                    1] + ":UCODE:START\n"
                impl += 8 * ' ' + code.replace('\n',
                                               '\n' + 8 * ' ').strip() + '\n'
                impl += "// @ACSTAG:INTTRAN:" + sid + ":" + inttran[
                    1] + ":UCODE:END\n"
                impl += 8 * ' ' + 'return EVENT_HANDLED;\n'
                impl += 4 * ' ' + '}\n'

            impl += 4 * ' ' + 'return EVENT_NOT_HANDLED;\n'
            impl += '}\n'

            impl += 'EventState_t ' + sid + '_exit(Event_t ev)\n'
            impl += '{\n'
            if state.exit[0] == True:
                impl += "// @ACSTAG:EXIT:" + sid + ":UCODE:START\n"
                # Script node exits but is blank. So insert a newline here.
                if None == state.exit[1]:
                    impl += '\n'
                else:
                    impl += 4 * ' ' + state.exit[1].replace(
                        '\n', '\n' + 4 * ' ').strip() + '\n'
                impl += "// @ACSTAG:EXIT:" + sid + ":UCODE:END\n"
            impl += 4 * ' ' + 'return EVENT_HANDLED;\n'
            impl += '}\n\n\n\n\n'

        impl += 'void initSM_' + self.states[0].sid + '()\n'
        impl += '{\n'
        state_count = 0
        for state in self.states:
            if state_count == 0:
                impl += 4 * ' ' + 'prepareState(&sm, &' + state.sid + ', ' + state.sid + '_entry, ' + state.sid + '_inprogress, ' + state.sid + '_exit, NULL);\n'
            else:
                impl += 4 * ' ' + 'prepareState(&sm, &' + state.sid + ', ' + state.sid + '_entry, ' + state.sid + '_inprogress, ' + state.sid + '_exit, &' + state.parent.sid + ');\n'
            state_count += 1
        impl += '\n'

        for state in self.states:
            for exttran in state.external:
                impl += 4 * ' ' + 'addTransition(&' + state.sid + ', ' + exttran[
                    1] + ', &' + exttran[2] + ');\n'
        impl += '\n'

        for state in self.states:
            if state.isInitial == True:
                impl += 4 * ' ' + 'setInitialState(&' + state.sid + ');\n'
        impl += '}\n\n'

        impl += 'void installSM_' + self.states[
            0].sid + 'ScheduleEventHandle(ScheduleEventPtr_t ptr)\n'
        impl += '{\n'
        impl += 4 * ' ' + 'scheduleEvent = ptr;\n'
        impl += '}\n\n'

        impl += 'void startSM_' + self.states[0].sid + '()\n'
        impl += '{\n'
        impl += 4 * ' ' + 'startMachine(&sm);\n'
        impl += '}\n\n'

        impl += 'void triggerSM_' + self.states[0].sid + 'Event(Event_t ev)\n'
        impl += '{\n'
        impl += 4 * ' ' + 'triggerEvent(&sm, ev);\n'
        impl += '}\n'

        api += "// Note: This file is an autogenerated API of " + self.states[
            0].sid + " statemachine\n"
        api += "// and should not be edited manually.\n\n"

        api += "#ifndef SM_" + self.states[0].sid.upper() + "_H\n"
        api += "#define SM_" + self.states[0].sid.upper() + "_H\n"

        if api_sm_include != '':
            api += "#include " + api_sm_include + "\n\n\n"

        api += 'enum EVENTS\n'
        api += '{\n'
        api += 4 * ' ' + 'EV_DEFAULT_ENTRY' + 8 * ' ' + '= EV_DEFAULT_ENTRY_LIB,\n'
        for event in self.eventDict.keys():
            api += 4 * ' ' + event + ',\n'
        api += '};\n\n'

        api += 4 * '\n'
        api += "// Install callback handle to schedule a delayed event.\n"
        api += 'typedef void (*ScheduleEventPtr_t)(Event_t);\n'
        api += 'void installSM_' + self.states[
            0].sid + 'ScheduleEventHandle(ScheduleEventPtr_t ptr);\n'

        api += 2 * '\n'
        api += "// Initilizes statemachine.\n"
        api += 'void initSM_' + self.states[0].sid + '();\n'

        api += 2 * '\n'
        api += "// Starts statemachine.\n"
        api += 'void startSM_' + self.states[0].sid + '();\n'

        api += 2 * '\n'
        api += "// Trigger event in statemachine.\n"
        api += 'void triggerSM_' + self.states[0].sid + 'Event(Event_t ev);\n'

        api += 4 * '\n'
        api += "#endif    // " + "SM_" + self.states[0].sid.upper() + "_H\n"

        impl = pyastyle.format(impl, '--style=allman --indent=spaces=4')
        api = pyastyle.format(api, '--style=allman --indent=spaces=4')

        print("API:")
        print(api)
        print("IMPLEMENTATION:")
        print(impl)
        print("PYAPI:")
        print(pyapi)

        return (impl, api, pyapi)
Пример #10
0
    def run_selection_only(self, edit, options):
        def get_line_indentation_pos(view, point):
            line_region = view.line(point)
            pos = line_region.a
            end = line_region.b
            while pos < end:
                ch = view.substr(pos)
                if ch != ' ' and ch != '\t':
                    break
                pos += 1
            return pos

        def get_indentation_count(view, start):
            indent_count = 0
            i = start - 1
            while i > 0:
                ch = view.substr(i)
                scope = view.scope_name(i)
                # Skip preprocessors, strings, characaters and comments
                if 'string.quoted' in scope or 'comment' in scope or 'preprocessor' in scope:
                    extent = view.extract_scope(i)
                    i = extent.a - 1
                    continue
                else:
                    i -= 1

                if ch == '}':
                    indent_count -= 1
                elif ch == '{':
                    indent_count += 1
            return indent_count

        view = self.view
        regions = []
        for sel in view.sel():
            start = get_line_indentation_pos(view, min(sel.a, sel.b))
            region = sublime.Region(
                view.line(start).a,  # line start of first line
                view.line(max(sel.a, sel.b)).b)  # line end of last line
            indent_count = get_indentation_count(view, start)
            # Add braces for indentation hack
            text = '{' * indent_count
            if indent_count > 0:
                text += '\n'
            text += view.substr(region)
            # Performing astyle formatter
            formatted_code = pyastyle.format(text, options)
            if indent_count > 0:
                for _ in range(indent_count):
                    index = formatted_code.find('{') + 1
                    formatted_code = formatted_code[index:]
                formatted_code = re.sub(r'[ \t]*\n([^\r\n])', r'\1', formatted_code, 1)
            else:
                # HACK: While no identation, a '{' will generate a blank line, so strip it.
                search = '\n{'
                if search not in text:
                    formatted_code = formatted_code.replace(search, '{', 1)
            # Applying formatted text
            view.replace(edit, region, formatted_code)
            # Region for replaced text
            if sel.a <= sel.b:
                regions.append(sublime.Region(region.a, region.a + len(formatted_code)))
            else:
                regions.append(sublime.Region(region.a + len(formatted_code), region.a))
        view.sel().clear()
        # Add regions of formatted text
        [view.sel().add(region) for region in regions]
Пример #11
0
    print("Enter the struct's name: ")
    struct_name = input()
    conditional_print(" [++] Attempting to automatically get structure...",
                      quiet_mode_set)

    sleep(0.5)
    if (returned_struct := automatically_resolve_struct(
            struct_name, length_type=False)) is None:
        returned_struct = resolve_struct(struct, options).struct

    conditional_print(
        "[==] Finished calculating offsets and formatting.\n(if there were any errors please submit an issue to my github)\n",
        quiet_mode_set)

    formatted = pyastyle.format(returned_struct, '--style=allman')
    line_max_length = lambda lines: max(
        len(line[:line.find("//")]) for line in lines.split("\n"))
    formatted = formatted.expandtabs(line_max_length(formatted) + 4)

    conditional_print("[++] Writing formatted string to output / file...\n",
                      quiet_mode_set)
    struct_out_file = options_dict["of"]
    if struct_out_file is not None:
        with open(struct_out_file, "w") as f:
            f.write(formatted)
    else:
        print(formatted)
    conditional_print(
        "\n[==] Finished writing output, thank you for using this program :)",
        quiet_mode_set)
Пример #12
0
def do_format(text):
    opt = options()
    #print('AStyle options:', opt)
    return pyastyle.format(text, opt)
Пример #13
0
def test_format_invalid_options():
    with pytest.raises(pyastyle.error) as exc:
        pyastyle.format('int main() {}', '--invalid-options-hereXXXXXX')
    assert '[130]' in str(exc)
Пример #14
0
def format_code(text):
    fcode = pyastyle.format(text, "--style=java --delete-empty-lines")
    html_string = highlight(fcode, JavaLexer(),
                            HtmlFormatter(style='friendly'))
    return html_string
Пример #15
0
    def rendered_template(self):
        all_funcs = _get_list_with_unique_functions(self.functions,
                                                    self.kernels)

        functions = [f.template
                     for f in all_funcs] + [k.template for k in self.kernels]
        functions = '\n'.join(functions)

        if 'double' in functions:
            _preamble_precision = preamble_precision('double')
        else:
            _preamble_precision = preamble_precision('single')

        if 'cfloat_t' in functions:
            _preamble_generic_operations = preamble_generic_type_operations(
                'complex', 'single')
        elif 'cdouble_t' in functions:
            _preamble_generic_operations = preamble_generic_type_operations(
                'complex', 'double')
        else:
            _preamble_generic_operations = preamble_generic_type_operations(
                'real')
        preamble_buff_t = f'{_preamble_precision}\n{_preamble_generic_operations}'

        # join program typedefs with typedefs from kernels and functions
        # todo: consider replacing type strings directly to avoid name conflicts
        def update_and_checks_for_duplicates_same_type(items: dict, dic: dict):
            for key, value in items.items():
                if key in dic:
                    if not dic[key] == value:
                        raise ValueError(
                            'Same type def name for different types')
                else:
                    dic[key] = value

        [
            update_and_checks_for_duplicates_same_type(func.type_defs,
                                                       self.type_defs)
            for func in self.functions
        ]
        [
            update_and_checks_for_duplicates_same_type(func.type_defs,
                                                       self.type_defs)
            for func in self.kernels
        ]

        # remove since defines are inserted before function/kernels
        # [update_and_checks_for_duplicates_same_type(func.defines, self.defines) for func in self.functions]
        # [update_and_checks_for_duplicates_same_type(func.defines, self.defines) for func in self.kernels]

        defines = '\n'.join([
            '#define {} {}'.format(key, str(value))
            for key, value in self.defines.items()
        ])

        type_defs = '\n'.join([
            'typedef {c_name} {new_name};\n#define convert_{new_name}(x) convert_{c_name}(x)'
            .format(c_name=c_name_from_dtype(value), new_name=str(key))
            for key, value in self.type_defs.items()
        ])

        tpl_all = self._get_tpl(preamble_buff_t, defines, type_defs, functions)

        tpl_formatted = pyastyle.format(tpl_all,
                                        '--style=allman --indent=spaces=4')
        return tpl_formatted
Пример #16
0
def ParseCode(scxml_file, src_file):

    ET.register_namespace('', "http://www.w3.org/2005/07/scxml")
    ET.register_namespace('qt', "http://www.qt.io/2015/02/scxml-ext")

    src = ''
    with open(src_file) as f:
        src = f.readlines()

    tree = ET.parse(scxml_file)
    root = tree.getroot()

    all_states = []
    nodes = []
    nodes.append(root)

    content = ''

    while len(nodes) > 0:
        curr_node = nodes[0]
        nodes = nodes[1:]

        read = False
        script_node = None

        if 'state' in curr_node.tag:

            for line in src:

                # Parsing entry code.
                if line == '// @ACSTAG:ENTRY:' + curr_node.attrib[
                        'id'] + ':UCODE:START\n':
                    read = True
                    # Find the script tag in this state for entry.
                    for node in curr_node:
                        if 'onentry' in node.tag:
                            for sn in node:
                                if 'script' in sn.tag:
                                    script_node = sn
                                    script_node.text = ''

                elif line == '// @ACSTAG:ENTRY:' + curr_node.attrib[
                        'id'] + ':UCODE:END\n':
                    if script_node != None:
                        script_node.text = pyastyle.format(
                            script_node.text,
                            '--style=allman --indent=spaces=4')
                        script_node = None
                    read = False

                # Parsing exit code.
                elif line == '// @ACSTAG:EXIT:' + curr_node.attrib[
                        'id'] + ':UCODE:START\n':
                    read = True
                    # Find the script tag in this state for exit.
                    for node in curr_node:
                        if 'onexit' in node.tag:
                            for sn in node:
                                if 'script' in sn.tag:
                                    script_node = sn
                                    script_node.text = ''

                elif line == '// @ACSTAG:EXIT:' + curr_node.attrib[
                        'id'] + ':UCODE:END\n':
                    if script_node != None:
                        script_node.text = pyastyle.format(
                            script_node.text,
                            '--style=allman --indent=spaces=4')
                        script_node = None
                    read = False

                # Parsing internal transition code.
                elif ('// @ACSTAG:INTTRAN:' + curr_node.attrib['id'] +
                      ':') in line and (':UCODE:START\n') in line:
                    read = True
                    internal = line.replace(
                        '// @ACSTAG:INTTRAN:' + curr_node.attrib['id'] + ':',
                        '').replace(':UCODE:START\n', '')
                    # Find the script tag in this state for this particular internal transition.
                    for node in curr_node:
                        if 'transition' in node.tag and node.attrib[
                                'type'] == 'internal' and node.attrib[
                                    'event'] == internal:
                            for sn in node:
                                if 'script' in sn.tag:
                                    script_node = sn
                                    script_node.text = ''

                elif ('// @ACSTAG:INTTRAN:' + curr_node.attrib['id'] +
                      ':') in line and (':UCODE:END\n') in line:
                    if script_node != None:
                        script_node.text = pyastyle.format(
                            script_node.text,
                            '--style=allman --indent=spaces=4')
                        script_node = None
                    read = False

                elif read == True:
                    if script_node != None:
                        script_node.text += line

        for node in curr_node:
            nodes.append(node)

    scxml = ET.tostring(root, encoding='utf8').decode('utf8')
    print(scxml)
    return scxml
Пример #17
0
	def read_file(self, query_file, results_file):
		# Read data
		with open(results_file) as infile:
			finalresult = json.load(infile)
		with open(query_file) as infile:
			queryresults = {}
			for page in json.load(infile):
				queryresults[page["url"]] = {}
				for segment in page["segments"]:
					queryresults[page["url"]][segment["in_page_order"]] = segment
		
		for fcluster in finalresult["clusters"]:
			fcluster["Score"] = []
			fcluster["imports"] = {}
			top_apis = fcluster["top_apis_by_cluster"]
			new_top_apis = []
			for fsnippet in fcluster["cluster_snippets"]:
				# Get stats and method invocations
				snip = queryresults[fsnippet["Url"]][fsnippet["In_Page_Order"]]
				fsnippet["API_Ratio"] = snip["API_Ratio"]
				fsnippet["readability"] = snip["readability"] if snip["readability"] == True else False
				MethodInvocationsFull = fsnippet["MethodInvocations"]
				MethodInvocations = []
				for mi in MethodInvocationsFull:
					if mi.endswith('__init__'):
						MethodInvocations.append(mi.split('.')[0])
					else:
						if len(mi.split('.')) > 1:
							MethodInvocations.append(mi.split('.')[1])
				fsnippet["MethodInvocationsFull"] = MethodInvocationsFull
				fsnippet["MethodInvocations"] = MethodInvocations

				# Compute snippet API score
				for mi in MethodInvocationsFull:
					fsnippet["API_Score"] = snip["APIsProjects"][mi] / 1000 if mi in snip["APIsProjects"] else 0

				# Find API qualified names
				for api in top_apis:
					for m in MethodInvocationsFull:
						if api == m.lower() and m not in new_top_apis:
							new_top_apis.append(m)

				# Format code
				code = fsnippet["Code"]
				fsnippet["Code"] = ';\n'.join(code.split(';')) if '\n' not in code else code 
				fsnippet["formatted_code"] = pyastyle.format(fsnippet["Code"], "--style=java --delete-empty-lines")
				fsnippet["full_code"] = highlight(fsnippet["formatted_code"], JavaLexer(), HtmlFormatter())

				# Find imports
				fsnippet["imports"] = p.findall(fsnippet["formatted_code"])
				fsnippet["imports"] += ['.'.join(snip["APIsQualifiedNames"][m].split('.')[:-1]) for m in MethodInvocationsFull if m in snip["APIsQualifiedNames"]]
				for im in fsnippet["imports"]:
					if len(im) <= 100:# and not im.endswith("Exception"):
						fcluster["imports"][im] = 1 if im not in fcluster["imports"] else fcluster["imports"][im] + 1

				# Highlight api calls
				lines = fsnippet["full_code"].split('\n')
				hapicalls = 0
				for mi in MethodInvocations:
					for l, line in enumerate(lines):
						if 'import' not in line:
							while '<span class="na">' + mi + '</span>' in line:
								line = line.replace('<span class="na">' + mi + '</span>', '<span class="na nana">' + mi + '</span>')
								hapicalls += 1
						while '<span class="n">' + mi + '</span>' in line:
							line = line.replace('<span class="n">' + mi + '</span>', '<span class="n nana">' + mi + '</span>')
						lines[l] = line
				fsnippet["Num_Highlighted_API_Calls"] = hapicalls
				fsnippet["full_code"] = '\n'.join(lines)
				fcluster["Score"].append(fsnippet["Score"])

				# Get important lines
				first = fsnippet["full_code"][0:28]
				lines = fsnippet["full_code"][28:-14].split('\n')
				last = fsnippet["full_code"][-13:]
				newlineids = []
				for l, line in enumerate(lines):
					if any('<span class="na nana">' + mi + '</span>' in line for mi in MethodInvocations):
						newlineids.append(l)
					if any('<span class="n nana">' + mi + '</span>' in line for mi in MethodInvocations):
						newlineids.append(l)
				newlines = []
				used_ids = set()
				for newlineid in newlineids:
					for k in [-1, 0, 1]:
						if newlineid + k >= 0 and newlineid + k < len(lines) and newlineid + k not in used_ids:
							newlines.append(lines[newlineid + k])
							used_ids.add(newlineid + k)
				fsnippet["invocations_code"] = first + '\n'.join(newlines) + last
				fsnippet["LOC"] = len([line for line in lines if line.strip()])
			# Create score
			scores = fcluster["Score"]
			fcluster["Score"] = sum(scores[:min(10, len(scores))]) / len(scores[:min(10, len(scores))])
			
			if len(fcluster["imports"]) < 5:
				for j, _ in enumerate(fcluster["cluster_snippets"]):
					if "API_Qualified_Names" in fsnippet:
						fsnippet["API_Qualified_Names"] = [im.split('.')[-1] for im in fsnippet["API_Qualified_Names"]]
						for im in fsnippet["API_Qualified_Names"]:
							if im not in fcluster["imports"]:
								if len(im) <= 100:
									fcluster["imports"][im] = 1

			fcluster["top_apis_by_cluster"] = new_top_apis

			fcluster["API_Score"] = sum([fsnippet["API_Score"] for fsnippet in fcluster["cluster_snippets"]]) / len(fcluster["cluster_snippets"])
			fcluster["cluster_snippets"].sort(reverse = True, key = lambda x: (x["API_Score"], x["Score"]))
		finalresult["clusters"].sort(reverse = True, key = lambda x: x["API_Score"])
		return finalresult
Пример #18
0
def do_format(text):
    if pyastyle is None:
        return
    opt = options()
    #print('AStyle options:', opt)
    return pyastyle.format(text, opt)
Пример #19
0
def test_format_invalid_options():
    with pytest.raises(pyastyle.error) as exc:
        pyastyle.format('int main() {}', '--invalid-options-hereXXXXXX')
    assert '[130]' in str(exc)