def test_pseudo_parens(self): # # Check the second branch in `reformatter._FormatFinalLines()`. # # Ensure that `reformatted_lines` are correcly formed when # when the following conditions hold: # # if tok.is_pseudo_paren: # if (not tok.next_token.whitespace_prefix.startswith('\n') and # not tok.next_token.whitespace_prefix.startswith(' ')): # if (tok.previous_token.value == ':' or # tok.next_token.value not in ',}])'): # ... # style.SetGlobalStyle( style.CreateStyleFromConfig( f'{{based_on_style: pep8, ' f'indent_dictionary_value: true}}')) input_text = textwrap.dedent("""\ {'a':1} """) # should not raise any exception FormatCode(input_text, lines=[(10,10)])[0]
def test_files_format(self): for file in list_all_py_files(): try: print(file) code = _read_utf_8_file(file) # https://pypi.python.org/pypi/yapf/0.20.2#example-as-a-module diff, changed = FormatCode(code, filename=file, style_config='setup.cfg', print_diff=True) if changed: print(diff) self.badly_formatted_files.append(file) except Exception as e: print("Error while processing file: `%s`\n" "Error: %s" % (file, str(e))) with self.assertNotRaises(Exception): str_err = "" if self.badly_formatted_files: for filename in self.badly_formatted_files: str_err += 'yapf -i --style=setup.cfg %s\n' % filename str_err = "\n======================================================================================\n" \ "Bad Coding Style: %d file(s) need to be formatted, run the following commands to fix: \n%s" \ "======================================================================================" % ( len(self.badly_formatted_files), str_err) raise Exception(str_err)
def _compile(self, widj, ev): fname = "ADV_{}.py".format(self.mytree.raw_vars["unique_id"]) # First, check for errors. myerrors = self.mytree.get_errors() if myerrors: pbge.BasicNotification("{} has errors. Check the console.".format(fname)) for e in myerrors: print(e) return myprog = self.mytree.compile() fname = "ADV_{}.py".format(self.mytree.raw_vars["unique_id"]) if FormatCode: fullprog, changed = FormatCode(myprog["main"]) else: fullprog = myprog["main"] with open(pbge.util.user_dir("content", fname), 'wt') as fp: fp.write(fullprog) pbge.BasicNotification("{} has been written. You can start the scenario from the main menu.".format(fname)) game.content.ghplots.reload_plot_module(fname.rpartition('.')[0]) for k, v in myprog.items(): if k != "main": print("Leftover section: {}".format(k))
def test_thing(t): assume('\n' not in t) assume(t != '\\') formatted = FormatCode(t)[0][:-1] assert '\n' not in formatted
def generate(self, filename, data, style_config='pep8'): self.mod_name = data["name"] # at first, sort data to avoid generating large diff sorted_data = sorted(data["data"], key=lambda x: (x.type(), x.name())) with open(filename, "w", encoding="utf-8") as file: self.print_header(file) code_data = "" for mod in data["child_modules"]: code_data += "from . import {}\n".format(mod) if len(data["child_modules"]) >= 1: code_data += "\n\n" for info in sorted_data: # if "type" not in s: # continue if info.type() == "function": code_data += self._gen_function_code(info) elif info.type() == "class": code_data += self._gen_class_code(info) elif info.type() == "constant": code_data += self._gen_constant_code(info) if style_config != "none": file.write(FormatCode(code_data, style_config=style_config)[0]) else: file.write(code_data)
def format_nb(notebook_filename, dry_run=False): print('Formatting {}...'.format(notebook_filename), end='') with open(notebook_filename, 'r') as f: notebook = nbformat.read(f, as_version=nbformat.NO_CONVERT) nbformat.validate(notebook) changed = False for cell in notebook.cells: if cell['cell_type'] != 'code': continue src = cell['source'] lines = src.split('\n') if len(lines) <= 0 or '# noqa' in lines[0]: continue formatted_src, did_change = FormatCode(src, style_config=style_file) if did_change: cell['source'] = formatted_src changed = True if changed: if not dry_run: with open(notebook_filename, 'w') as f: nbformat.write(notebook, f, version=nbformat.NO_CONVERT) print(' (reformatted)') else: print()
def _format(document, lines=None): new_source, changed = FormatCode( document.source, lines=lines, filename=document.filename, style_config=file_resources.GetDefaultStyleForDir( os.path.dirname(document.filename))) if not changed: return [] # I'm too lazy at the moment to parse diffs into TextEdit items # So let's just return the entire file... return [{ 'range': { 'start': { 'line': 0, 'character': 0 }, # End char 0 of the line after our document 'end': { 'line': len(document.lines), 'character': 0 } }, 'newText': new_source }]
def _postprocess_python(self, code): # while re.search('\n\n\n', code): # code = re.sub('\n\n\n', '\n\n', code) warning = '\n'.join(self._config.Generation['warning']) + '\n' code = warning + code code, _ = FormatCode(code, style_config='facebook') return code
def _rename_func(self, func, func_name): """ Rename the function name and return source code. This function does not check for name conflicts. Install `yapf` for optional code reformatting (takes extra processing time). """ import inspect if func is None: return f"# empty {func_name}\n" src = inspect.getsource(func) src = src.replace("def _lambdifygenerated(", f"def {func_name}(") # remove `Indicator` src = src.replace("Indicator", "") if self.parent.system.config.yapf_pycode: try: from yapf.yapflib.yapf_api import FormatCode src = FormatCode( src, style_config='pep8')[0] # drop the encoding `None` except ImportError: logger.warning( "`yapf` not installed. Skipped code reformatting.") src += '\n' return src
def update_jupyterhub_config(self, file_path: PathLike, default_config: str, **kwargs: t.Any) -> None: '''Short summary. Args: file_path (PathLike): . default_config (str): . **kwargs (t.Any): . Returns: None: . ''' if os.path.exists(file_path): logger.warning(f'Old {file_path!r} will be overwritten.') with open(file_path, 'w') as jupyterhub_config: jupyterhub_config.write( FormatCode(Config.template_warning + '\n\n\n' + default_config + Config.users.format(**kwargs), style_config='pep8')[0]) logger.info(f'Successfully updated {file_path!r}')
def output(self): self._thrift2pyi() try: o = FormatCode(compose(self.pyi))[0] except: o = compose(self.pyi) print("format failed") if not self.out or self.prefix == self.out: with open("%s.pyi" % self.filename.replace(".thrift", "_thrift"), "w") as f: f.write(o) return if self.prefix: filename = self.filename.replace(self.prefix, self.out) else: filename = os.path.join(self.out, self.filename) dirname = os.path.dirname(filename) if not os.path.exists(dirname): os.makedirs(dirname, mode=0o755, exist_ok=True) with open("%s/__init__.py" % dirname, "w") as f: pass shutil.copyfile(self.filename, filename) with open("%s.pyi" % filename.replace(".thrift", "_thrift"), "w") as f: f.write(o)
def _rename_func(self, func, func_name, yapf_pycode=False): """ Rename the function name and return source code. This function does not check for name conflicts. Install `yapf` for optional code reformatting (takes extra processing time). It also patches function argument list for select. """ if func is None: return f"# empty {func_name}\n" src = inspect.getsource(func) src = src.replace("def _lambdifygenerated(", f"def {func_name}(") # remove `Indicator` src = src.replace("Indicator", "") # append additional arguments for select if 'select' in src: src = src.replace("):", ", __zeros, __ones, __falses, __trues):") if yapf_pycode: try: from yapf.yapflib.yapf_api import FormatCode src = FormatCode( src, style_config='pep8')[0] # drop the encoding `None` except ImportError: logger.warning( "`yapf` not installed. Skipped code reformatting.") src += '\n' return src
def format_code(source: str): if 'import ' in source: config = isort.settings.Config(no_lines_before=[ isort.settings.FUTURE, isort.settings.STDLIB, isort.settings. THIRDPARTY, isort.settings.FIRSTPARTY, isort.settings.LOCALFOLDER ]) source = isort.code(source, config=config) # fix the bug that yapf cannot handle jupyter magic for l in source.splitlines(): if l.startswith('%') or l.startswith('!'): return source # fix the bug that yapf remove the tailling ; has_tailling_semicolon = source.rstrip().endswith(';') style = { 'DISABLE_ENDING_COMMA_HEURISTIC': True, 'SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET': False, 'SPLIT_BEFORE_CLOSING_BRACKET': False, 'SPLIT_BEFORE_DICT_SET_GENERATOR': False, 'SPLIT_BEFORE_LOGICAL_OPERATOR': False, 'SPLIT_BEFORE_NAMED_ASSIGNS': False, 'COLUMN_LIMIT': 78, 'BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION': 1, } source = FormatCode(source, style_config=style)[0].strip() if has_tailling_semicolon: source += ';' return source
def code_gen(asts: list): relations = RichList() tables = RichList() for each in asts: if isinstance(each, Relation): relations.append(each) elif isinstance(each, Table): tables.append(each) elif isinstance(each, (Python, Engine)): pass else: raise TypeError(type(each)) ctx = Context('', tables, relations) proc = Proc([ 'from auto_orm.database.types import *', 'import auto_orm.database.infrastructure as db', 'import builtins', ]) for each in asts: proc, ctx = emit(each, proc, ctx) proc += '__base__.metadata.create_all(bind=engine)' proc += 'session = __session__' return FormatCode(dedent(dumps(proc.codes)))[0]
def exported_function_info(mod, mod_name, func_name) -> Dict[str, Any]: func = getattr(mod, func_name) sig = inspect.signature(func) activity_type = "" mod_lastname = mod_name.rsplit(".", 1)[1] if mod_lastname == "actions": activity_type = "action" elif mod_lastname == "probes": activity_type = "probe" args = build_signature_info(sig) return_type = build_return_type_info(sig) as_json = called_without_args_info(args, mod_name, func_name, activity_type) return { "type": activity_type, "module": mod_name, "name": func_name, "doc": inspect.getdoc(func), "return": return_type, "signature": FormatCode("def {}{}:pass".format(func_name, str(sig)))[0], "arguments": args, "as_json": json.dumps(as_json, indent=2), "as_yaml": yaml.dump(as_json, default_flow_style=False) }
def test_warn_location(self): style.SetGlobalStyle( style.CreateStyleFromConfig( f'{{based_on_style: pep8, ' f'check_var_naming_style: snake_case, ' f'column_limit: 30}}')) input_source = textwrap.dedent("""\ if left > right or right > left: pass SomeVariable = 0 def fn(): ''' fn description ''' pass OtherVar = fn() """) FormatCode(input_source) # one line added becaus the `if` statement was wrapped self.assertWarnMessage(warns.Warnings.VAR_NAMING_STYLE, pattern='.*SomeVariable', lineno=3) # two more line added as the style requires two blank lines # around top-level functions self.assertWarnMessage(warns.Warnings.VAR_NAMING_STYLE, pattern='.*OtherVar', lineno=13)
def format_source(validator_source): formatted_source, _ = FormatCode(validator_source, style_config={ 'based_on_style': 'google', 'DEDENT_CLOSING_BRACKETS': True, 'COLUMN_LIMIT': 119 }) return formatted_source
def test_first_line(self): self.__setup_import_splitter(False) input_source = textwrap.dedent("""\ VAR = 0 """) FormatCode(input_source)[0] self.assertFalse(self._stderr.messages)
def format_code(cls, data, style_config: Dict = None): """ # Format Code Format python code using yapf style conventions """ if not style_config: style_config = {} res = FormatCode(data, style_config=style_config) return res[0]
def user_given(request, data, col, code): try: x = FormatCode(code) exec(x[0]) except Exception as e: messages.error( request, "Invalid Argument parsing or go to previous page and rerun")
def _format_code(content): """ Takes a string outputs a PEP8 version of it :param content: string :return: content formatted according to PEP8 :rtype: string """ content, _ = FormatCode(content, style_config='pep8') return content
def main(): args = parser.parse_args() full_path = args.full_path force = args.force if full_path is None: full_path = os.path.join(BASE_PATH, '..', 'examples', 'notebooks') notebook = args.notebook if notebook is None: notebooks = glob.glob(os.path.join(full_path, '*.ipynb')) else: if not notebook.endswith('.ipynb'): notebook = notebook + '.ipynb' notebook = os.path.abspath(os.path.join(full_path, notebook)) if not os.path.exists(notebook): raise FileNotFoundError('Notebook {0} not found.'.format(notebook)) notebooks = [notebook] if not notebooks: import warnings warnings.warn('No notebooks found', UserWarning) for nb in notebooks: nb_full_name = os.path.split(nb)[1] nb_name = os.path.splitext(nb_full_name)[0] py_name = nb_name + '.py' # Get base directory to notebook out_file = os.path.split(nb)[0] # Write to ../python out_file = os.path.join(out_file, '..', 'python', py_name) if is_newer(out_file, nb) and not force: logger.info('Skipping {0}, exported version newer than ' 'notebook'.format(nb_name)) continue logger.info('Converting {0}'.format(nb_name)) with open(nb, 'r') as nb_file: converter = nbconvert.PythonExporter() python = converter.from_file(nb_file) code = python[0].split('\n') code_out = [] for i, block in enumerate(code): if 'get_ipython' in block: continue elif block.startswith('# In[ ]:'): continue if block.startswith('#'): # Wrap comments from Markdown block = textwrap.fill(block, width=74) block = block.replace('\n', '\n# ') code_out.append(block) if not code_out[0]: code_out = code_out[1:] loc = 0 if 'coding' in code_out[0]: loc = 1 code_out.insert(loc, DO_NOT_EDIT.format(notebook=nb_full_name)) code_out = '\n'.join(code_out) code_out, success = FormatCode(code_out, style_config='pep8') with open(out_file, 'w') as of: of.write(code_out)
def test_placeholder(self): self.__setup('snake_case') input_source = textwrap.dedent("""\ _, a = fn() """) FormatCode(input_source) self.assertWarnCount(warns.Warnings.VAR_NAMING_STYLE, 0)
def test_bare_star(self): self.__setup('snake_case') input_source = textwrap.dedent("""\ def fn(*, var): pass """) FormatCode(input_source) self.assertWarnCount(warns.Warnings.VAR_NAMING_STYLE, 0)
def test_func_calls(self): self.__setup('snake_case') input_source = textwrap.dedent("""\ Func(Arg) = 1 """) FormatCode(input_source) self.assertWarnCount(warns.Warnings.VAR_NAMING_STYLE, 0)
def _test_non_empty_raise(self): self.__setup(True) input_text = textwrap.dedent("""\ if True: raise Exception() """) FormatCode(input_text) self.assertWarnCount(warns.Warnings.MISPLACED_BARE_RAISE, 0)
def __setup(self, enable): style.SetGlobalStyle( style.CreateStyleFromConfig( f'{{based_on_style: pep8, ' f'should_have_encoding_header: {enable}}}')) input_source = textwrap.dedent("""\ def foo(): pass """) FormatCode(input_source)
def test_continuation(self): self.__setup(True) input_text = textwrap.dedent("""\ value = default_value \\ if other_value is None else other_value """) output_text = FormatCode(input_text)[0] self.assertCodeEqual(input_text, output_text)
def test_is_none(self): self.__setup(True) input_source = textwrap.dedent("""\ a is None b is not None """) FormatCode(input_source) self.assertWarnCount(warns.Warnings.COMP_WITH_NONE, 0)
def get_setup(self): # render template with self.setup_path.open(encoding='utf-8') as f: document = f.read() template = Environment().from_string(document) document = template.render( package=self.package, format_vcs=self._format_vcs, ) # format by yapf style = CreateGoogleStyle() document, _changed = FormatCode(document, style_config=style) # remove empty strings while '\n\n' in document: document = document.replace('\n\n', '\n') # format by autopep8 document = fix_code(document) return document
def ipynb_format(fname, style=None): f = open(fname, "r") nb = nbf.read(f, "ipynb") f.close() if style is None: style = GetDefaultStyleForDir(os.path.dirname(fname)) modified = False for worksheet in nb["worksheets"]: for cell in worksheet["cells"]: if cell["cell_type"] != "code": continue if cell["language"] != "python": continue if len(cell["input"]) == 0: continue modcell = False out = [] q = [] for i in cell["input"].splitlines(True): if i.startswith("%"): if len(q) > 0: qf, mod = FormatCode("".join(q), style_config=style) if mod: modcell = True out += qf.splitlines(True) else: out += q q = [] out += [i] continue q.append(i) if len(q) > 0: qf, mod = FormatCode("".join(q), style_config=style) if mod: modcell = True out += qf.splitlines(True) else: out += q if len(out[-1]) == 0: modcell = True out = out[:-1] if out[-1][-1] == "\n": modcell = True out[-1] = out[-1][:-1] if modcell: cell["input"] = out modified = True if modified: f = open(fname, "w") nbf.write(nb, f, "ipynb") f.close() return modified