示例#1
0
def build_nltk_wrappers():
    imports = _walk(nltk)
    imports += _walk(nltk.cluster)
    imports += _walk(gensim.models)
    # imports += _walk(nltk.chunk.named_entity)
    imports += _walk(nltk.tag)

    manager = enlighten.get_manager()
    counter = manager.counter(total=len(imports), unit="classes")
    path = Path(__file__).parent / "_generated.py"

    imports = set(imports)

    with open(path, "w") as fp:
        fp.write(
            textwrap.dedent(f"""
            # AUTOGENERATED ON {datetime.datetime.now()}
            ## DO NOT MODIFY THIS FILE MANUALLY

            from autogoal.grammar import Continuous, Discrete, Categorical, Boolean
            from autogoal.contrib.nltk._builder import NltkStemmer, NltkTokenizer, NltkLemmatizer, NltkTagger, NltkTrainedTagger
            from autogoal.kb import *
            from autogoal.utils import nice_repr
            from numpy import inf, nan
            """))

        for cls in imports:
            counter.update()
            _write_class(cls, fp)

    black.reformat_one(path, True, black.WriteBack.YES, black.FileMode(),
                       black.Report())

    counter.close()
    manager.stop()
示例#2
0
def build_sklearn_wrappers():
    imports = _walk(sklearn)

    manager = enlighten.get_manager()
    counter = manager.counter(total=len(imports), unit="classes")

    path = Path(__file__).parent / "_generated.py"

    with open(path, "w") as fp:
        fp.write(
            textwrap.dedent(
                f"""
            # AUTOGENERATED ON {datetime.datetime.now()}
            ## DO NOT MODIFY THIS FILE MANUALLY

            from numpy import inf, nan

            from autogoal.grammar import Continuous, Discrete, Categorical, Boolean
            from autogoal.contrib.sklearn._builder import SklearnEstimator, SklearnTransformer
            from autogoal.kb import *
            """
            )
        )

        for cls in imports:
            counter.update()
            _write_class(cls, fp)

    black.reformat_one(
        path, True, black.WriteBack.YES, black.FileMode(), black.Report()
    )

    counter.close()
    manager.stop()
示例#3
0
文件: black.py 项目: dizballanze/gray
 def process(self, file_path: Path):
     # Black does not provide an official public API yet:
     # https://github.com/psf/black/issues/779
     black.reformat_one(
         file_path,
         fast=False,
         write_back=black.WriteBack.YES,
         mode=self._mode,
         report=black.Report(),
     )
示例#4
0
 def transform(self, filepaths: List[str]):
     report = Black.CollectingReport()
     for filepath in filepaths:
         reformat_one(
             src=Path(filepath),
             line_length=MAX_LINE_LENGTH,
             fast=False,
             write_back=WriteBack.YES,
             mode=self._mode,
             report=report,
         )
     return report
示例#5
0
def reformat_code(filename):
    SortImports(filename)

    src = Path(filename)
    report = black.Report()
    black.reformat_one(
        src=src,
        line_length=88,
        fast=False,
        write_back=black.WriteBack.YES,
        mode=black.FileMode.AUTO_DETECT,
        report=report,
    )
def reformat_code(filename):

    config = isort.api.Config()
    isort.api.sort_file(filename, config=config)

    src = Path(filename)
    report = black.Report()
    black.reformat_one(
        src=src,
        line_length=88,
        fast=False,
        write_back=black.WriteBack.YES,
        mode=black.FileMode.AUTO_DETECT,
        report=report,
    )
示例#7
0
def render_file_to_disk(file, txt):
    with open(file, "w") as f:
        f.write(txt)
    # end with
    if use_back:
        black.reformat_one(
            src=black.Path(file),
            write_back=black_settings['write_back'],
            fast=False,
            mode=black_settings['mode'],
            report=black_settings['report'],
        )
    # end if
    if use_yapf:
        try:
            FormatFile(file,
                       in_place=True,
                       style_config=yapf_settings['style'])
        except:
            logger.exception(
                "Formatting file {file} failed.".format(file=file))
示例#8
0
def reformat_many(sources, *args, **kwargs):
    """Monkeypatched to reformat multiple files using ``black.reformat_one``."""
    for src in sources:
        black.reformat_one(src, *args, **kwargs)
示例#9
0
async def api(
    *,
    src: Iterable[str],
    work_dir: str,
    line_length: int = black.DEFAULT_LINE_LENGTH,
    check: bool = False,
    diff: bool = False,
    fast: bool = False,
    pyi: bool = False,
    py36: bool = False,
    skip_string_normalization: bool = False,
    quiet: bool = False,
    verbose: bool = False,
    include: str = black.DEFAULT_INCLUDES,
    exclude: str = black.DEFAULT_EXCLUDES,
    config: Optional[str] = None,
) -> int:
    """The uncompromising code formatter."""
    src = tuple(src)
    work_dir = Path(work_dir)
    loop = asyncio.get_event_loop()
    write_back = black.WriteBack.from_configuration(check=check, diff=diff)
    mode = black.FileMode.from_configuration(
        py36=py36, pyi=pyi, skip_string_normalization=skip_string_normalization
    )
    if config and verbose:
        black.out(f"Using configuration from {config}.", bold=False, fg="blue")
    try:
        include_regex = black.re_compile_maybe_verbose(include)
    except re.error:
        black.err(f"Invalid regular expression for include given: {include!r}")
        return 2
    try:
        exclude_regex = black.re_compile_maybe_verbose(exclude)
    except re.error:
        black.err(f"Invalid regular expression for exclude given: {exclude!r}")
        return 2
    report = black.Report(check=check, quiet=quiet, verbose=verbose)
    root = black.find_project_root((work_dir,))
    sources: Set[Path] = set()
    for s in src:
        p = work_dir / Path(s)
        if p.is_dir():
            sources.update(
                black.gen_python_files_in_dir(
                    p, root, include_regex, exclude_regex, report
                )
            )
        elif p.is_file() or s == "-":
            # if a file was explicitly given, we don't care about its extension
            sources.add(p)
        else:
            black.err(f"invalid path: {s}")
    if len(sources) == 0:
        if verbose or not quiet:
            black.out("No paths given. Nothing to do 😴")
        return 0

    if len(sources) == 1:
        black.reformat_one(
            src=sources.pop(),
            line_length=line_length,
            fast=fast,
            write_back=write_back,
            mode=mode,
            report=report,
        )
    else:
        await black.schedule_formatting(
            sources=sources,
            line_length=line_length,
            fast=fast,
            write_back=write_back,
            mode=mode,
            report=report,
            executor=PROCESS_POOL,
            loop=loop,
        )
    if verbose or not quiet:
        bang = "💥 💔 💥" if report.return_code else "✨ 🍰 ✨"
        black.out(f"All done! {bang}")
        black.secho(str(report), err=True)
    return report.return_code