Пример #1
0
def parse_bpmn(
    bpmn_path: str, json_path: str, template_path: str, model_path: str
) -> None:
    """
    this method reads from a given (camunda-modeler) bpmn/xml file and
    completes a given skeleton template by initialized blocks and
    their connections
    """

    bpmn = _to_json_dict(bpmn_path, json_path)

    elements = {
        value: _get_elements_of_type(bpmn, key) for key, value in MAPPING.items()
    }

    components = _get_components_from_elements(elements)

    _add_class_and_id_names(components + elements["resources"])

    insertions = _generate_insertions(bpmn, elements, components)
    model_components = _generate_model_components(components)
    model_graph_names = _generate_model_graph_names(elements, components)

    _write_model_file(
        model_path, template_path, insertions, model_components, model_graph_names
    )

    path = pathlib.Path(model_path)
    file_mode = black.FileMode()
    black.format_file_in_place(path, False, file_mode, write_back=black.WriteBack.YES)

    print("\nBPMN parsed.\n")
Пример #2
0
 def _format_with_black(output_file_name):
     black.format_file_in_place(
         Path(output_file_name),
         mode=black.FileMode(line_length=110),
         fast=False,
         write_back=black.WriteBack.YES,
     )
Пример #3
0
def _code_style(dir: Path) -> None:
    for file in dir.rglob('**/*.py'):
        black.format_file_in_place(
            src=file.absolute(),
            fast=False,
            mode=black.Mode(line_length=100,
                            target_versions={black.TargetVersion.PY38}),
            write_back=black.WriteBack.YES)
Пример #4
0
def format(path):
    black.format_file_in_place(
        src=path,
        fast=False,
        mode=black.Mode(target_versions={black.TargetVersion.PY38}),
        write_back=black.WriteBack.YES,
    )
    isort.file(path)
Пример #5
0
def cmd_black(*args, **kwargs):
    import black

    if args:
        editedfile = Path(args[0])
        black.format_file_in_place(
            editedfile, fast=True, mode=black.FileMode(), write_back=black.WriteBack(1)
        )
    else:
        runCommand("python -m black src tests")
Пример #6
0
    def write_model(self):
        app = apps.get_app_config(self.app_name)
        file_path = Path(app.models_module.__file__)
        with file_path.open("a") as model_file:
            model_file.write(f"\n\n{self.render()}")

        # Format file with black to make stuff nicer (a.k.a. I'm lazy)
        format_file_in_place(file_path,
                             fast=False,
                             mode=FileMode(),
                             write_back=WriteBack.YES)
Пример #7
0
def cmd_black(*args, **kwargs):
    import black

    if args:
        editedfile = Path(args[0])
        print(f"##### black formatting  {editedfile} #####")
        black.format_file_in_place(
            editedfile, fast=True, mode=black.FileMode(), write_back=black.WriteBack(1)
        )
    else:
        runCommand(f"python -m black {PACKAGE_NAME} tests")
Пример #8
0
def format_file(filename):
    try:
        import black

        black.format_file_in_place(
            pathlib.Path(filename).absolute(),
            88,  # black default line length is 88
            fast=True,
            write_back=black.WriteBack.YES,
        )
    except:
        pass
 def create_dag_file(self, workflow: Workflow):
     """
     Writes to a file the Apache Oozie parsed workflow in Airflow's DAG format.
     """
     file_name = self.output_dag_name
     with open(file_name, "w") as file:
         logging.info(f"Saving to file: {file_name}")
         dag_content = self.render_workflow(workflow)
         file.write(dag_content)
     black.format_file_in_place(Path(file_name),
                                mode=black.FileMode(line_length=110),
                                fast=False,
                                write_back=black.WriteBack.YES)
Пример #10
0
def format_blueprint_command(bp_file):
    path = pathlib.Path(bp_file)
    LOG.debug("Formatting blueprint {} using black".format(path))
    if format_file_in_place(
        path, fast=False, mode=FileMode(), write_back=WriteBack.DIFF
    ):
        LOG.info("Patching above diff to blueprint - {}".format(path))
        format_file_in_place(
            path, fast=False, mode=FileMode(), write_back=WriteBack.YES
        )
        LOG.info("All done!")
    else:
        LOG.info("Blueprint {} left unchanged.".format(path))
Пример #11
0
def handler(event, context):
    if "body" in event and event["body"]:
        params = json.loads(event["body"])
    else:
        params = event
    try:
        git_repo = params["git_repo"]
        git_branch = params[
            "git_branch"] if "git_branch" in params else "master"
    except KeyError:
        return {
            "body": json.dumps({"message": "git_repo is not provided"}),
            "headers": {
                "Content-Type": "application/json"
            },
            "statusCode": 401,
        }

    try:
        r = requests.get(f"{git_repo}/archive/{git_branch}.zip")
        open(f"{LOCAL_PATH}/{git_branch}.zip", 'wb').write(r.content)

        with ZipFile(f"{LOCAL_PATH}/{git_branch}.zip", "r") as zipObj:
            zipObj.extractall(f"{LOCAL_PATH}")

        for root, dirs, files in os.walk(LOCAL_PATH):
            for file in files:
                if file.endswith(".py"):
                    format_file_in_place(Path(f"{root}/{file}"), False,
                                         FileMode(), WriteBack.DIFF)

        return {
            "body": {
                "message": "result in logs"
            },
            "headers": {
                "Content-Type": "application/json"
            },
            "statusCode": 200,
        }
    except Exception as e:
        message = e.message if hasattr(e, 'message') else e
        return {
            "body": {
                "message": message
            },
            "headers": {
                "Content-Type": "application/json"
            },
            "statusCode": 200,
        }
Пример #12
0
    def format_python_file(filename):
        try:
            import black

            black.format_file_in_place(
                pathlib.Path(filename).absolute(),
                fast=True,
                mode=black.FileMode(
                    line_length=88, target_versions={black.TargetVersion.PY33}
                ),
                write_back=black.WriteBack.YES,
            )
        except:
            pass
Пример #13
0
 def test_black(self):
     for p in self.files:
         not_changed = black.format_file_in_place(p, False,
                                                  black.FileMode(),
                                                  black.WriteBack.NO)
         self.assertFalse(not_changed,
                          "Found files not formatted by Black.")
Пример #14
0
    def _run_black(self):
        has_change = False
        if not self._migration._use_black:
            return

        file_mode = black.FileMode()
        file_mode.line_length = _BLACK_LINE_LENGTH

        for root, directories, filenames in os.walk(
                self._module_path.resolve()):
            for filename in filenames:
                # Skip useless file
                if os.path.splitext(filename)[1] != ".py":
                    continue

                absolute_file_path = os.path.join(root, filename)

                has_change = has_change or black.format_file_in_place(
                    pathlib.Path(absolute_file_path), False, file_mode,
                    black.WriteBack.YES)

        # Commit changes
        if has_change:
            commit_name = "[REF] %s: Black python code" % (self._module_name)

            logger.info("Commit changes for %s. commit name '%s'" %
                        (self._module_name, commit_name))

            _execute_shell(
                "cd %s && git add . --all && git commit -m '%s'" %
                (str(self._migration._directory_path.resolve()), commit_name))
Пример #15
0
 def handle(self, *args, **options):
     root = pathlib.Path(settings.BASE_DIR)
     g_args = ["--exclude-standard", "-X", root.joinpath(".lintignore")]
     if not options["all"]:
         g_args.append("-m")
     files_str = git.Git(settings.BASE_DIR).ls_files(g_args)
     del_files_str = git.Git(settings.BASE_DIR).ls_files("-d", "-k")
     files = set(
         pathlib.Path(p)
         for p in files_str.splitlines() if p[-3:] == ".py") - set(
             pathlib.Path(p) for p in del_files_str.splitlines())
     num_lint_errors = 0
     num_black_formats = 0
     for p in files:
         if not options["no_fix"]:
             changed = black.format_file_in_place(p, False,
                                                  black.FileMode(),
                                                  black.WriteBack.YES)
             if changed:
                 num_black_formats += 1
                 self.stdout.write("%s: Black formatted file" % str(p))
         if not options["no_lint"]:
             num_lint_errors += pyflakes.checkPath(str(p))
     if num_black_formats:
         raise CommandError("%i file(s) formatted by Black" %
                            num_black_formats)
     if num_lint_errors:
         raise CommandError("%i liniting error(s) found" % num_lint_errors)
Пример #16
0
def black_reformat(source_path: Path) -> bool:
    return black.format_file_in_place(
        source_path,
        fast=False,
        mode=black.FileMode(),
        write_back=black.WriteBack.YES,
    )
Пример #17
0
def format_file(file: str) -> None:
    file_path = Path(file)
    if file_path.suffix in PYTHON_FILE_EXTENSIONS:
        mode = black.FileMode(
            target_versions={black.TargetVersion.PY38},
            line_length=LINE_LENGTH,
            is_pyi=file_path.suffix == ".pyi",
            string_normalization=True,
        )
        isort.SortImports(file_path)
        black.format_file_in_place(file_path, False, mode, black.WriteBack.YES)
    elif file_path.suffix in CXX_FILE_EXTENSIONS:
        subprocess.run(["clang-format", "-i", "-style=file", file_path],
                       cwd=WORKSPACE)
    elif file_path.suffix == ".bzl" or file_path.name in {
            "BUILD", "WORKSPACE"
    }:
        subprocess.run([BUILDIFIER, "-lint", "fix", file_path], cwd=WORKSPACE)
Пример #18
0
    def _run_black(self):
        if not self._migration._use_black:
            return

        file_mode = black.FileMode()
        file_mode.line_length = _BLACK_LINE_LENGTH

        for root, directories, filenames in os.walk(
                self._module_path.resolve()):
            for filename in filenames:
                # Skip useless file
                if os.path.splitext(filename)[1] != ".py":
                    continue

                absolute_file_path = os.path.join(root, filename)

                black.format_file_in_place(pathlib.Path(absolute_file_path),
                                           False, file_mode,
                                           black.WriteBack.YES)
Пример #19
0
def main():
    with open(output_path, "w") as fd:
        fd.write("# autogenerated by %s\n" % __file__)
        fd.write("import re\n")

    print("build backend_url_patterns")
    index = build_backend_url_pattern_index()
    with open(output_path, "a") as fd:
        fd.write("backend_url_patterns = ")
        pprint.pprint(index, fd)
        fd.write(os.linesep)

    print("format with black")
    black.format_file_in_place(
        Path(output_path),
        fast=False,
        mode=black.FileMode(),
        write_back=black.WriteBack.YES,
    )
Пример #20
0
        def format_file(file: str, isortconfig: isort.settings.Config) -> None:
            """
            Formats the given file using black.

            :param file:
                The file to format.
            :parm isortconfig:
                The configuration to apply while sorting the imports.
            """

            isort.api.sort_file(pathlib.Path(file), config=isortconfig)

            black.format_file_in_place(
                pathlib.Path(file),
                fast=False,
                mode=black.Mode(),
                write_back=black.WriteBack.YES,
            )

            PyFunceble.facility.Logger.info("Update format of %r", file)
Пример #21
0
def cleanup_code():
    """
    Clean up all files of a given extension under a directory
    """
    for filepath in glob.iglob("**/*.py", recursive=True):
        path = pathlib.Path(os.getcwd(), filepath)
        if black.format_file_in_place(path, False, black.FileMode(),
                                      black.WriteBack.YES):
            print("Formatted file: ", filepath)
        else:
            print(f"Skipping file {filepath} as it is already formatted")
Пример #22
0
def format_all_files(top_directory: pathlib.Path) -> None:
    for file_dir, _, files in os.walk(top_dir):
        for file in files:
            file = pathlib.Path(file_dir) / file
            if file.suffix != ".py":
                continue
            if black.format_file_in_place(file,
                                          fast=True,
                                          mode=black.FileMode(),
                                          write_back=black.WriteBack.YES):
                print(f"Reformatted {file}")
Пример #23
0
def _format_py_file(target: Path):
    black.format_file_in_place(target,
                               fast=False,
                               mode=black.FileMode(),
                               write_back=WriteBack.YES)
Пример #24
0
def main(event, context):

    print(" Starting LambdaOpt backend")
    print(
        "\n███████╗ █████╗ ██████╗  ██████╗ ██████╗ ████████╗\n██╔════╝██╔══██╗██╔══██╗██╔═══██╗██╔══██╗╚══██╔══╝\n█████╗  ███████║██████╔╝██║   ██║██████╔╝   ██║   \n██╔══╝  ██╔══██║██╔══██╗██║   ██║██╔═══╝    ██║   \n██║     ██║  ██║██║  ██║╚██████╔╝██║        ██║   \n╚═╝     ╚═╝  ╚═╝╚═╝  ╚═╝ ╚═════╝ ╚═╝        ╚═╝   \n\n"
    )
    print("---------------------------------------------------------------")
    print('Downloading source')

    print('Looking for source.zip in ', event['s3bucket'], '/',
          event['s3key'] + '/source.zip')

    s3 = boto3.client('s3')
    try:
        s3.download_file(event['s3bucket'], event['s3key'] + '/source.zip',
                         '/tmp/source.zip')
        print('Downloaded source.zip! uncompressing')
    except:
        print('Could not download source. Please check :', event['s3bucket'],
              '/', event['s3key'] + '/source.zip')

    print("Setting env variables for micro environment")
    os.environ['s3key'] = event['s3key']
    os.environ['s3bucket'] = event['s3bucket']

    with zipfile.ZipFile('/tmp/source.zip', 'r') as zip_ref:
        zip_ref.extractall('/tmp/')

    print("---------------------------------------------------------------")

    # Add utils file

    f = open("/var/task/utils.py", 'r')
    utilsstr = f.read()
    f.close()

    # Rewrite to tmp
    f = open("/tmp/utils.py", 'w')
    f.write(utilsstr)
    f.close()

    listfiles = os.listdir("/tmp/")
    print("reformatting with black")
    for f in listfiles:
        if f[-2:] == 'py':
            print(f)
            format_file_in_place(src=Path('/tmp/' + f),
                                 fast=True,
                                 mode=FileMode())

    os.environ["PYTHONPATH"] = "/opt/python/lib/python3.7/site-packages"
    process = subprocess.Popen([sys.executable, "/tmp/main.py"],
                               env=os.environ,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT)
    output, error = process.communicate()

    print(output.decode())
    print("----")
    print(error)

    return {'statusCode': 200, 'body': json.dumps({'error': error})}
Пример #25
0
def test_unparseable_notebook() -> None:
    path = DATA_DIR / "notebook_which_cant_be_parsed.ipynb"
    msg = rf"File '{re.escape(str(path))}' cannot be parsed as valid Jupyter notebook\."
    with pytest.raises(ValueError, match=msg):
        format_file_in_place(path, fast=True, mode=JUPYTER_MODE)
Пример #26
0
 def _format_code(self, py_filename):
     _dargs = dict(fast=False, mode=black.FileMode(), write_back=black.WriteBack.YES)
     assert black.format_file_in_place(pathlib.Path(py_filename), **_dargs), py_filename
Пример #27
0
        ]
    )

    # Needs to define the MVariant and GameFeatureType type:
    writer._print("MoVariant = {}".format(Type.MO_VARIANT))
    writer._print('GameFeatureType = TypeVar("GameFeatureType")')
    writer._print()

    # This is a class to represent interface not implemented:
    writer.print_class(Class("InterfaceNotImplemented", [], []))
    writer._print()

    for n, o in objects:

        # Get the corresponding object:
        c = MOBASE_REGISTER.get_object(n)

        if isinstance(c, Class):
            writer.print_class(c)

        elif isinstance(c, list) and isinstance(c[0], Function):
            for fn in c:
                writer.print_function(fn)

black.format_file_in_place(
    args.output,
    fast=False,
    mode=black.Mode(is_pyi=args.output.name.endswith("pyi")),
    write_back=black.WriteBack.YES,
)
Пример #28
0
from pathlib import Path
import sys
import black

output = Path(sys.argv[2])
output.mkdir(exist_ok=True)

for py in Path(sys.argv[1]).rglob("*.py"):
    print(py)

    content = Parser(py.open(), inline=False, module_name=py.name)
    output_file = output / f"test_{py.name}"
    with output_file.open("w") as fp:
        for i, block in enumerate(content.parse().content):
            if isinstance(block, Python):
                fp.write(str(block))
                fp.write("\n\n")

            if isinstance(block, Docstring):
                fp.write(f"def test_block_{i:000d}():\n")

                for line in block.content:
                    fp.write(" " * 4 + line)

                fp.write('    """\n    pass\n\n')

    black.format_file_in_place(
        output_file, fast=True, mode=black.FileMode(), write_back=black.WriteBack.YES
    )
Пример #29
0
    writer.write("\n")
    writer.write("    def accept(self, visitor):\n")
    writer.write(f"        return visitor.visit{className}{baseName}(self)\n")
    writer.write("\n\n")


if __name__ == "__main__":
    main()

    p = pathlib.Path(__file__).resolve().parent.parent / "expr.py"
    print(p)
    print(
        black.format_file_in_place(
            src=p,
            fast=False,
            mode=black.Mode(target_versions={black.TargetVersion.PY38}),
            write_back=black.WriteBack.YES,
        ))
    print(isort.file(p))

    p = pathlib.Path(__file__).resolve().parent.parent / "stmt.py"
    print(p)
    print(
        black.format_file_in_place(
            src=p,
            fast=False,
            mode=black.Mode(target_versions={black.TargetVersion.PY38}),
            write_back=black.WriteBack.YES,
        ))
    print(isort.file(p))