def check_file(self, path): """ Check one regular file with pylint for py syntax errors. :param path: Path to a regular file. :return: False, if pylint found syntax problems, True, if pylint didn't find problems, or path is not a python module or script. """ inspector = PathInspector(path) if not inspector.is_python(): return True opt_obj = pep8.StyleGuide().options ignore_list = self.ignored_errors.split(',') + list(opt_obj.ignore) opt_obj.ignore = tuple(set(ignore_list)) runner = pep8.Checker(filename=path, options=opt_obj) status = runner.check_all() if status != 0: log.error('PEP8 check fail: %s', path) self.failed_paths.append(path) log.error('Trying to fix errors with autopep8') try: opt_obj = autopep8.parse_args( [path, '--ignore', self.ignored_errors, '--in-place']) autopep8.fix_file(path, opt_obj) except Exception, details: log.error('Not able to fix errors: %s', details)
def __format_file(self, path): autopep8.fix_file( path, options=autopep8.parse_args( ['--in-place', '--aggressive', path] ) )
def check_file(self, path): """ Check one regular file with pylint for py syntax errors. :param path: Path to a regular file. :return: False, if pylint found syntax problems, True, if pylint didn't find problems, or path is not a python module or script. """ inspector = PathInspector(path) if not inspector.is_python(): return True opt_obj = pep8.StyleGuide().options ignore_list = self.ignored_errors.split(',') + list(opt_obj.ignore) opt_obj.ignore = tuple(set(ignore_list)) runner = pep8.Checker(filename=path, options=opt_obj) status = runner.check_all() if status != 0: log.error('PEP8 check fail: %s', path) self.failed_paths.append(path) log.error('Trying to fix errors with autopep8') try: opt_obj = autopep8.parse_args([path, '--ignore', self.ignored_errors, '--in-place']) autopep8.fix_file(path, opt_obj) except Exception, details: log.error('Not able to fix errors: %s', details)
def autopep8(args): """Format code according to PEP8 """ try: import autopep8 except: error('autopep8 not found! Run "paver install_devtools".') sys.exit(1) if any(x not in args for x in ['-i', '--in-place']): args.append('-i') args.append('--ignore=E261,E265,E402,E501') args.insert(0, 'dummy') cmd_args = autopep8.parse_args(args) ext_libs_basename = os.path.basename(options.plugin.ext_libs) ext_src_basename = os.path.basename(options.plugin.ext_src) excludes = (ext_libs_basename, ext_src_basename) for p in options.plugin.source_dir.walk(): if any(exclude in p for exclude in excludes): continue if p.fnmatch('*.py'): autopep8.fix_file(p, options=cmd_args)
def load_file(file, newfilename): outfile = open(newfilename, 'w+') autopep8.fix_file(file, None, outfile) outfile.close() print("Code formatted successfully and saved to", newfilename) if input("Press 'y' if you want to print the output to console: ") == 'y': output(newfilename)
def _inner_setup(self, line): f = open(self.tempfile[1], 'w') f.write(line) f.close() opts, _ = autopep8.parse_args([self.tempfile[1]]) sio = StringIO() autopep8.fix_file(filename=self.tempfile[1], opts=opts, output=sio) self.result = sio.getvalue()
def _inner_setup(self, line, options=()): with open(self.tempfile[1], 'w') as temp_file: temp_file.write(line) opts, _ = autopep8.parse_args([self.tempfile[1]] + list(options)) sio = StringIO() autopep8.fix_file(filename=self.tempfile[1], opts=opts, output=sio) self.result = sio.getvalue()
def pram2mesa(sim: Simulation, name: str, autopep: bool = True) -> None: """ Converts a PyPRAM simulation object to equivalent Mesa Agent and Model classes. This function should be the only function a user must call. :param sim: The PyPRAM Simulation to translate :param name: The prefix of the files to be created. :param autopep: Should the files be run through autopep8 to clean the code? If autopep evaluates to False, autopep8 will not be used. If custom autopep8 usage is desired, set autopep to False and do so manually :return: None. Creates two Python files containing the new Mesa Agent and Model classes and three JSON data files. From there, instantiate one of these Models and proceed using standard Mesa tools. """ directory = _make_filename(name, extension='') os.mkdir(directory) os.chdir(directory) # model relies on make_python_identifier so we pack it up shutil.copy(inspect.getsourcefile(mpi), '.') group_file, site_file, rule_file = create_json_data(sim, name) rw = RuleWriter() new_rules, rule_imports = translate_rules([type(r) for r in sim.rules], rw) top_level_rules = [type(r).__name__ for r in sim.rules] group_setup = sim.fn.group_setup or '' if group_setup: tree = ast.parse(textwrap.dedent(inspect.getsource(group_setup))) tree.body[0].name = '_group_setup' # tree.body[0].args.args[0].arg = 'self' # FIXME: this is hacky tree.body[0].decorator_list.append('staticmethod') group_setup = astor.to_source(rw.visit(tree)) agent_file = create_agent_class(name, new_rules, top_level_rules, rule_file, used_functions=rw.used, custom_imports='\n'.join(rule_imports)) model_file = create_model_class(name, group_file, site_file, agent_file, top_level_rules, group_setup, used_functions=rw.used) if autopep: autopep8.fix_file(agent_file, options=autopep8.parse_args( ['--in-place', agent_file])) autopep8.fix_file(model_file, options=autopep8.parse_args( ['--in-place', model_file]))
def run_autopep(filename): cmd_args = ['dummy', '-d'] args = autopep8.parse_args(cmd_args) args.aggressive = 2 args.in_place = True args.diff = False args.max_line_length = 66 autopep8.fix_file(filename, args) if not any(x in filename for x in import_fixed) or "internal" in filename: isort.SortImports(filename) run_autoflake(filename)
def check_file(self, path): """ Check one regular file with pylint for py syntax errors. :param path: Path to a regular file. :return: False, if pylint found syntax problems, True, if pylint didn't find problems, or path is not a python module or script. """ inspector = PathInspector(path=path, args=self.args) if inspector.is_toignore(): return True if not inspector.is_python(): return True try: opt_obj = pep8.StyleGuide().options ignore_list = self.args.disable.split(',') + list(opt_obj.ignore) opt_obj.ignore = tuple(set(ignore_list)) # pylint: disable=E1123 runner = pep8.Checker(filename=path, options=opt_obj) except Exception: opts = ['--ignore'] + self.args.disable.split(',') pep8.process_options(opts) runner = pep8.Checker(filename=path) try: status = runner.check_all() except: log.error('Unexpected exception while checking %s', path) exc_info = sys.exc_info() stacktrace.log_exc_info(exc_info, 'inspektor.style') status = 1 if status != 0: log.error('PEP8 check fail: %s', path) self.failed_paths.append(path) if AUTOPEP8_CAPABLE: if self.args.fix: log.info('Trying to fix errors with autopep8') try: auto_args = [path, '--in-place', ('--max-line-length=%s' % self.args.max_line_length)] if self.args.disable: auto_args.append("--ignore='%s'" % self.args.disable) opt_obj = autopep8.parse_args(auto_args) autopep8.fix_file(path, opt_obj) except Exception: log.error('Unable to fix errors') exc_info = sys.exc_info() stacktrace.log_exc_info(exc_info, 'inspektor.style') return status == 0
def format_file(self, file_to_format_path, setup_path): if setup_path is None: options = None else: options = autopep8.parse_args(('--global-config='+setup_path.__str__(), file_to_format_path.__str__()), apply_config=True) temp_fd, temp_path = tempfile.mkstemp(prefix=f'{file_to_format_path.stem}_', suffix=file_to_format_path.suffix, text=True) with os.fdopen(temp_fd, 'w') as file: autopep8.fix_file(file_to_format_path.__str__(), output=file, options=options) return Path(temp_path)
def _inner_setup(self, line, options=""): f = open(self.tempfile[1], 'w') f.write(line) f.close() opts, _ = autopep8.parse_args(options.split() + [self.tempfile[1]]) sio = StringIO() # Monkey patch pep8 to trigger spawning original_pep8 = autopep8.pep8 try: autopep8.pep8 = None autopep8.fix_file(filename=self.tempfile[1], opts=opts, output=sio) finally: autopep8.pep8 = original_pep8 self.result = sio.getvalue()
def check(expected_filename, input_filename, aggressive): """Test and compare output. Return True on success. """ got = autopep8.fix_file( input_filename, options=autopep8.parse_args([''] + aggressive * ['--aggressive'])) try: with autopep8.open_with_encoding(expected_filename) as expected_file: expected = expected_file.read() except IOError: expected = None if expected == got: return True else: got_filename = expected_filename + '.err' encoding = autopep8.detect_encoding(input_filename) with autopep8.open_with_encoding(got_filename, encoding=encoding, mode='w') as got_file: got_file.write(got) print('{begin}{got} does not match expected {expected}{end}'.format( begin=RED, got=got_filename, expected=expected_filename, end=END), file=sys.stdout) return False
def format_pep8(file_path): try: return fix_file(abspath(file_path)) except FileNotFoundError: print("The " + file_path + " is not available") except Exception as e: print(e)
def test_pep8(self): # verify all files are nicely styled python_filepaths = get_python_filepaths() style_guide = get_style_guide() fake_stdout = io.StringIO() with patch('sys.stdout', fake_stdout): report = style_guide.check_files(python_filepaths) # if flake8 didnt' report anything, we're done if report.total_errors == 0: return # grab on which files we have issues flake8_issues = fake_stdout.getvalue().split('\n') broken_filepaths = { item.split(':')[0] for item in flake8_issues if item } # give hints to the developer on how files' style could be improved options = autopep8.parse_args(['']) options.aggressive = 1 options.diff = True options.max_line_length = 99 issues = [] for filepath in broken_filepaths: diff = autopep8.fix_file(filepath, options=options) if diff: issues.append(diff) report = ["Please fix files as suggested by autopep8:"] + issues report += ["\n-- Original flake8 reports:"] + flake8_issues self.fail("\n".join(report))
def _inner_setup(self, line, options=""): with open(self.tempfile[1], 'w') as temp_file: temp_file.write(line) opts, _ = autopep8.parse_args(options.split() + [self.tempfile[1]]) sio = StringIO() # Monkey patch pep8 to trigger spawning original_pep8 = autopep8.pep8 try: autopep8.pep8 = None autopep8.fix_file(filename=self.tempfile[1], opts=opts, output=sio) finally: autopep8.pep8 = original_pep8 self.result = sio.getvalue()
def _fix_file(parameters): """Helper function for optionally running fix_file() in parallel.""" try: result = autopep8.fix_file(*parameters) if result: print("%s %s" % (parameters[1].select[0], parameters[0])) return result except IOError as error: print(unicode(error))
def validate_file(self): try: validated_file = pep8.fix_file(abspath(self)) return validated_file except FileExistsError: print("The file in " + self + "does not seem to exist") except FileNotFoundError: print("The file " + self + " Could not be found in that file path") except Exception as e: print(e)
def format_file(path, with_meld=True, cwd='.'): _logger.info( f'Format the file: {path} using {settings.Tools.FILE_FORMATTER} ' f'with merge mode in {settings.Tools.MERGE_TOOL}') path = Path(cwd) / path formatted_file_descriptor, formated_file_path = tempfile.mkstemp( prefix=f'{path.stem}_', suffix='.py', text=True) os.close(formatted_file_descriptor) formated_file_path = Path(formated_file_path) setup_file_path = (Path(cwd) / settings.FileName.SETUP_CFG).resolve() if path.is_file(): with open(formated_file_path, 'w') as file: options = autopep8.parse_args( ['--global-config=' + str(setup_file_path), str(path)], apply_config=True) autopep8.fix_file(str(path), output=file, options=options) else: raise exceptions.NotAFileError('Path must point to a file!', _logger) if with_meld: utils.execute_cmd([ 'meld', str(path), str(path), str(formated_file_path), '-o', str(path) ], str(cwd)) formated_file_path.unlink() else: _logger.info(f'Formatted file has ben written to {formated_file_path}') _logger.info('Lint formatted file and show report') try: utils.execute_cmd( [settings.Tools.LINTER, str(path), f'--config={setup_file_path}'], cwd) except exceptions.ExecuteCmdError as e: print(e) else: _logger.info('Linter report is empty - file ok')
def style_fixer(in_fname, out_fname=None, in_place=False): if in_place: out_fname = in_fname if out_fname is None: raise ValueError("out_fname must be set if in_place not True.") out = autopep8.fix_file(input_fname) out = autopep8.fix_code(out, options={"select": ["E2"]}) with open(out_fname, "w") as f: f.write(out)
def autopep8(args): """Format code according to PEP8""" try: import autopep8 except: error('autopep8 not found! Run "paver install_devtools".') sys.exit(1) if any(x not in args for x in ['-i', '--in-place']): args.append('-i') args.insert(0, 'dummy') cmd_args = autopep8.parse_args(args)[0] excludes = ('ext-lib', 'ext-src') for p in options.plugin.source_dir.walk(): if any(exclude in p for exclude in excludes): continue if p.fnmatch('*.py'): autopep8.fix_file(p, options=cmd_args)
def run_autopep8(source, options=()): """Return source code formatted with autopep8.""" import tempfile temp = tempfile.mkstemp(dir='.') with open(temp[1], 'w') as temp_file: temp_file.write(source) try: import autopep8 opts, _ = autopep8.parse_args(list(options) + [temp[1]]) sio = StringIO() autopep8.fix_file(filename=temp[1], opts=opts, output=sio) output = sio.getvalue() if source.strip() and not output.strip(): raise ValueError('autopep8 failed') return output finally: import os os.remove(temp[1])
def main() -> None: """ main Driver function for PyFlex program """ # The first thing to do is get the lines of the PyFlex file we are given. parser = Parser(filename=sys.argv[1]) parsed_data = parser.ParseFile() # Upon retrieving the Parsed Data, assign the parsed data to the # Symbol Table. SymbolTable.RULESET = parsed_data['ruleset'] SymbolTable.INSTRUCTIONS = parsed_data['instructions'] SymbolTable.CODE = parsed_data['code'] # SymbolTable.PrintTable() # Using the Generator backend, we can build the generated script generator = Generator() generator.GenerateNewScript() autopep8.fix_file(filename=generator.file_main) print("Generated Script can be found in {}".format(generator.file_main))
def translate(filename): file = open(filename, 'r').read()\ .replace(' Then', ':')\ .replace('End If', '')\ .replace('CStr', 'str')\ .replace(' Mod ', ' % ') trans = open("trans.py", 'w') if file.__contains__('Rnd'): trans.write("from random import randrange\n\n") file = file.replace('Rnd', 'randrange').split( '\n' ) # переводим некоторые функции, не требующие дополнительного вмешательства current = "" for line in file: # тут простой перевод line = line.replace(' And ', ' and ').replace(' Or ', ' or ') if line.__contains__('End Function'): trans.write('\n') current = '' elif line.__contains__('Function'): trans.write(line.replace('Function', 'def') + ':' + '\n') current = line.split(' ')[1].split('(')[0] elif line.__contains__("'"): trans.write(line.replace("'", '#') + '\n') elif line.__contains__('ElseIf'): trans.write(MakeOperands(line.replace('ElseIf', 'elif')) + '\n') elif line.__contains__("If"): trans.write(MakeOperands(line.replace("If", 'if')) + '\n') elif line.__contains__("Else"): kek = MakeOperands(line.replace("Else", 'else')) + ':' + '\n' trans.write( MakeOperands(line.replace("Else", 'else').replace(':', '')) + ':' + '\n') elif line.__contains__(current): if line.__contains__('&'): # конкатинация строк if current == 'GetHyperState': line = line.replace('&', '+') if line.__contains__('Array'): line = __solveBracketsInArray__(line) trans.write(line.replace(current + ' =', 'return') + '\n') elif line.__contains__('&'): if current == 'GetHyperState': trans.write(line.replace('&', '+') + '\n') else: trans.write(line + '\n') trans.close() lll = autopep8.fix_file('trans.py') # приводим код к pep8 fff = open('transpep.py', 'w') fff.write(lll) fff.close()
def autopep8(args): """Format code according to PEP8""" # noinspection PyBroadException try: import autopep8 except: error('autopep8 not found! Set up conda dev environ.') sys.exit(1) if any(x not in args for x in ['-i', '--in-place']): args.append('-i') args.append('--ignore=E261,E265,E402,E501') args.insert(0, 'dummy') cmd_args = autopep8.parse_args(args) excludes = ('ext-lib', 'ext-src') for p in options.plugin.source_dir.walk(): if any(exclude in p for exclude in excludes): continue if p.fnmatch('*.py'): autopep8.fix_file(p, options=cmd_args)
def fix_files(filenames, options, output=None): """Fix list of files.""" results = [] current_error = options.select[0] if len(options.select) == 1 else "All" for current, name in enumerate(filenames): try: result = autopep8.fix_file(name, options) if result: print("\r\033[K%s %s" % (current_error, name)) results.append(result) _display_progess_bar(current, len(filenames), current_error) except IOError as error: print(unicode(error)) return results
def generate(dir_path, f_out): file_names = tuple( sorted(filter(lambda x: x.endswith('.csv'), os.listdir(dir_path)))) file_paths = tuple(map(lambda x: os.path.join(dir_path, x), file_names)) var_names = tuple(map(lambda x: x.split('.csv')[0], file_names)) def round2(x): return tuple(map(lambda y: round(y, 2), x)) context_dict = dict() for i in range(len(var_names)): file_path = file_paths[i] var_name = var_names[i] var_name_split = var_name.split('_') taxid = var_name_split[0].split('taxid')[1] side = var_name_split[1] context = None if side == 'L': context = read_l(file_path) elif side == 'R': context = read_r(file_path) else: continue context = tuple(map(round2, context)) context_dict[taxid + '_' + side] = context f = open(f_out, 'w') print('"""Start Codon Context."""\n\n', end='', file=f) print('contexts' + ' = ', end='', file=f) pprint(context_dict, f) f.close() txt = autopep8.fix_file(f_out) f = open(f_out, 'w') f.write(txt) f.close()
def check(expected_filename, input_filename): """Test and compare output. Return True on success. """ got = autopep8.fix_file( input_filename, options=autopep8.parse_args(['', '--aggressive'])[0]) try: with autopep8.open_with_encoding(expected_filename) as expected_file: expected = expected_file.read() except IOError: expected = None if expected == got: return True else: got_filename = expected_filename + '.err' encoding = autopep8.detect_encoding(input_filename) with autopep8.open_with_encoding(got_filename, encoding=encoding, mode='w') as got_file: got_file.write(got) print( '{begin}{got} does not match expected {expected}{end}'.format( begin=RED, got=got_filename, expected=expected_filename, end=END), file=sys.stdout) return False
def gq_fix_file(): fix_file(vim.current.buffer.name, Gq_Options)
def fix_current_file(): fix_file(vim.current.buffer.name, Options)
def format_file(self): """ This formats the output file """ autopep8.fix_file(self.output_file)
# fix ident: space -> tab ident = 0 while line.startswith(" "): ident += 1 line = line[4:] line = "\t" * ident + line.rstrip() # done prevline = line lines.append(line) return "\n".join(lines).strip() + "\n" files = [(root, f) for root, dirs, files in os.walk(os.path.dirname(os.path.realpath(__file__))) for f in files] for root, fname in files: path = os.path.join(root, fname) source = None if fname.endswith(".py"): tmppath = prepare(path) source = autopep8.fix_file(tmppath, autopep8.parse_args([tmppath, "--ignore=E123,E124,E127,E128,E501"])[0]) source = lint(source, root) os.remove(tmppath) elif fname.endswith(".html") or fname.endswith(".css") or fname.endswith(".js"): source = text_lint(path) if source != None: with open(path, "w") as f: f.write(source.encode("utf8"))
def indent_python(fname): import autopep8 autopep8.fix_file(fname, options=autopep8.parse_args([fname, '-i']))
def pep8_format(fn): contents = fix_file(fn) with open(fn, 'w') as f: f.write(contents)
ident = 0 while line.startswith(' '): ident += 1 line = line[4:] line = '\t' * ident + line.rstrip() # done prevline = line lines.append(line) return '\n'.join(lines).strip() + '\n' files = [(root, f) for root, dirs, files in os.walk(os.path.dirname(os.path.realpath(__file__))) for f in files] for root, fname in files: path = os.path.join(root, fname) source = None if fname.endswith('.py'): tmppath = prepare(path) source = autopep8.fix_file(tmppath, autopep8.parse_args([tmppath, '--ignore=E123,E124,E127,E128,E501'])[0]) source = lint(source, root) os.remove(tmppath) elif fname.endswith('.html') or fname.endswith('.css') or fname.endswith('.js'): source = text_lint(path) if source != None: with open(path, 'w') as f: f.write(source.encode('utf8'))