Exemplo n.º 1
0
def format_python(py_diff_list):
    if confirm('Review formatting changes? (Select no to approve all)'):

        for pyfile in py_diff_list:
            print 'Changes:\n' + FormatFile(pyfile, print_diff=True)[0]
            if confirm('Accept changes to %s?' % pyfile):
                FormatFile(pyfile, in_place=True)
    else:
        for pyfile in py_diff_list:
            FormatFile(pyfile, in_place=True)
Exemplo n.º 2
0
def getcode(secretKey, codeDescription):

    os.environ['OPENAI_API_KEY'] = secretKey
    openai.api_key = os.environ["OPENAI_API_KEY"]

    start_sequence = "\nA:"
    restart_sequence = "\n\nQ: "

    prompt = "I am a highly intelligent Python Bot and I can give you a simple code snippet in Python for your task. My code is \"properly indented\". I print only \"one line of code per line\". I \"don't use comments\". I \"import all libraries\" every time. I never use comments in the code. I never use \"#\" in my code.\n\nQ: Ask user for a number between 1 and 24th prime number. Test if it is a Fibonacci number.\nA: \nprint(\"Enter a number between 1 and 89: \")\nn = int(input())\nif n in [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]:\n    print(\"You entered: \", n)\nelse:\n    print(\"That is not a Fibonacci number.\")\n#Generated with Epsilon-Code\n\nQ: calculate the sine value of number stored in \"num\".\nA: \nimport math\nprint(\"Enter a number: \")\nnum = int(input())\nsin_value = math.sin(num) \nprint(\"The sine of your number is: \", sin_value, \".\") \n#Generated with Epsilon-Code\n\nQ: Print the top and bottom rows of the data frame\nA: \nimport pandas as pd\nimport numpy as np\ndf = pd.DataFrame(np.random.randint(1, 10, size=(5, 4)), columns=['a', 'b', 'c', 'd']) \nprint(\"The top row and bottom rows are:\n\", df.iloc[[0, -1]])\n#Generated with Epsilon-Code\n\nQ: make a decision tree classifier on the IRIS dataset.\nA:\nfrom sklearn import datasets\nfrom sklearn import metrics\nfrom sklearn.tree import DecisionTreeClassifier\ndataset = datasets.load_iris()\nmodel = DecisionTreeClassifier()\nmodel.fit(dataset.data, dataset.target)\nprint(model)\nexpected = dataset.target\npredicted = model.predict(dataset.data)\nprint(metrics.classification_report(expected, predicted))\n#Generated with Epsilon-Code\n\nQ: delete all vowels from input text.\nA:\nimport re\nprint(\"Enter some text (all vowels in it will be removed): \")\ntext = input() \nregexp = r'[aeiouAEIOU]'\nprint(re.sub('\b'.join(regexp), '', text))\n#Generated with Epsilon-Code\n\nQ: plot sin x\nA:\nimport matplotlib.pyplot as plt\nimport numpy as np\nx = np.linspace(-10, 10, 100)\ny = np.sin(x) \nplt.plot(x, y) \nplt.show()\n#Generated with Epsilon-Code\n\nQ: ask user to enter 3 numbers one by one. print the product.\nA:\nprint(\"Enter three numbers one by one: \")\nn1 = int(input()) \nn2 = int(input()) \nn3 = int(input()) \nproduct_number = n1 * n2 * n3\nprint(\"The product of your three numbers is: \", product_number, \".\")\n#Generated with Epsilon-Code\n\nQ: perform a google search of what the user wants and print the top result.\nA:\nimport requests\nfrom bs4 import BeautifulSoup\nsearch_url = \"https://www.google.com/search?q=\" + input() \nr = requests.get(search_url)\nhtml = r.text \nsoup = BeautifulSoup(html, 'lxml') \nprint(soup)\n#Generated with Epsilon-Code\n\nQ: Print what part of the day is going on right now.\nA:\nimport time\nmytime = time.localtime()\nif mytime.tm_hour < 6 or mytime.tm_hour > 18:\n    print ('It is night-time')\nelse:\n    print ('It is day-time')\n#Generated with Epsilon-Code\n\nQ: make a password generator\nA:\nimport random\ncharacters = 'abcdefghijklmnopqrstuvwxyz[];\',./{}:\"<>?\\|12345678980!@#$%^&*()-=_+~`'\ncharacters = list(characters)\npassword = ''\nfor i in range(0, random.randint(8, 13)):\n    char = random.choice(characters)\n    password+=char\nprint('Your password is:', password)\n#Generated with Epsilon-Code\n\nQ: delete first line of multi-line string. \nA:\nimport re\ntext = input() \nregexp = r'^\s*(.*)$'\nprint(re.sub('\b'.join(regexp), '', text))\n#Generated with Epsilon-Code\n\nQ: check if the year entered by user is a leap year\nA:\nimport datetime\nyear = int(input())\nif year % 4 == 0 and (year % 100 != 0 or year % 400 == 0): \n    print(\"It is a leap year\")\nelse:\n    print(\"It is not a leap year\")\n#Generated with Epsilon-Code\n\nQ: calculate factorial of number given by user\nA:\nimport math\nprint(\"Enter a number: \")\nnum = int(input())\nfactorial_number = 1 \nfor i in range(1, num + 1): \n    factorial_number *= i \nprint(factorial_number)\n#Generated with Epsilon-Code\n\nQ: "

    prompt += codeDescription

    response = openai.Completion.create(engine="davinci",
                                        prompt=prompt,
                                        temperature=0.1,
                                        max_tokens=512,
                                        top_p=0.5,
                                        frequency_penalty=1,
                                        presence_penalty=1,
                                        stop=["\n\n"])

    test_string = response['choices'][0]['text']
    spl_word = 'A:'
    res = test_string.partition(spl_word)[2]

    outF = open("EpsilonCodeOutput.py", "a")
    outF.writelines(res)
    outF.close()
    FormatFile("EpsilonCodeOutput.py", in_place=True)
Exemplo n.º 3
0
def main(argv: List[str]) -> int:
    """Short summary.

  Args:
      verbose (bool): Whether to print
      diff_args (Optional[List[str]]): arguments for git diff.

  """
    args = cli.parse_args(argv[1:])
    if args.from_git_diff:  # should always be true
        git_root = run('git rev-parse --show-toplevel'.split(' ')).strip()
        os.chdir(git_root)
        diff = getDiff(args.from_git_diff)
        changes = parseUDiff(diff, parent=git_root)
        for filename, lines in changes.items():
            if IsPythonFile(filename):
                results = FormatFile(filename,
                                     lines=lines,
                                     in_place=args.in_place,
                                     print_diff=args.diff)
                if args.diff:
                    sys.stdout.write(str(results[0]) or '')
        return 1 if bool(changes) and bool(args.diff) else 0
    else:
        return 1
Exemplo n.º 4
0
def _format_file(path):
    import platform
    from yapf.yapflib.yapf_api import FormatFile
    config = """{
column_limit : 120
}"""

    try:
        # It might be tempting to use the "inplace" option to
        # FormatFile, but it doesn't do an atomic replace, which
        # is dangerous, so don't use it unless you submit a fix to
        # yapf.
        (contents, encoding, changed) = FormatFile(path, style_config=config)
        if platform.system() == 'Windows':
            # yapf screws up line endings on windows
            with codecs.open(path, 'r', encoding) as file:
                old_contents = file.read()
            contents = contents.replace("\r\n", "\n")
            if len(old_contents) == 0:
                # windows yapf seems to force a newline? I dunno
                contents = ""
            changed = (old_contents != contents)
    except Exception as e:
        error = "yapf crashed on {path}: {error}".format(path=path, error=e)
        print(error, file=sys.stderr)
        return False

    if changed:
        atomic_replace(path, contents, encoding)
        print("Reformatted:     " + path)
        return False
    else:
        return True
Exemplo n.º 5
0
def main(argv):
    srcdir = argv[1]
    mode = argv[2]
    violation_detected = False
    for file_to_style in os.listdir(srcdir):

        if file_to_style.endswith('.py'):
            filepath = os.path.join(srcdir, file_to_style)

            if mode == "CHECK":
                if detect_formatting_violation(filepath):
                    print str(
                        argv[0]
                    ) + ": ERROR: formatting violation detected in " + str(
                        file_to_style)
                    violation_detected = True

            elif mode == "APPLY":
                if detect_formatting_violation(filepath):
                    print str(file_to_style) + ": found issue, reformatting..."
                    FormatFile(filepath,
                               in_place=True,
                               style_config='style.yapf')
                    violation_detected = True

            else:
                print "ERROR: invalid mode " + str(mode)
                exit(1)

    if mode == 'CHECK' and violation_detected:
        exit(1)
    elif not violation_detected:
        print str(argv[0]) + " INFO: all formatting for targets in " + str(
            argv[1]) + " OK!"
Exemplo n.º 6
0
def yapfWorker(path: str, stdout: TextIO) -> None:
    result = FormatFile(path,
                        style_config=configFile,
                        in_place=True,
                        logger=stdout)
    assert result[1] == "utf-8", "Wrong encoding {}, must be utf-8".format(
        result[1])
Exemplo n.º 7
0
    def wrapper(*args, **kwargs):
        path = kwargs['filepath']
        with path.open('r') as f:
            file_ = ['']
            file_.extend(f)
        kwargs['file_'] = file_

        # run original function
        new_file_, ext = fun(*args, **kwargs)

        alt_path = path.parent / f'{path.name}_backup'
        path.replace(alt_path)
        try:
            with path.open('w') as f:
                f.writelines(new_file_)

            sort_file(path)
            FormatFile(str(path), in_place=True)

            alt_path.unlink()

        except BaseException:
            alt_path.replace(path)
            raise

        return ext
Exemplo n.º 8
0
def format(all=False):
    import os
    import taichi as tc
    from yapf.yapflib.yapf_api import FormatFile
    repo = get_repo()

    print('Code formatting ...')
    if all:
        directories = ['taichi', 'tests', 'examples', 'misc', 'python']
        files = []
        for d in directories:
            files += list(
                Path(os.path.join(tc.get_repo_directory(), d)).rglob('*'))
    else:
        files = repo.index.diff('HEAD')
        files = list(
            map(lambda x: os.path.join(tc.get_repo_directory(), x.a_path),
                files))

    for fn in map(str, files):
        if fn.endswith('.py'):
            print(fn, '...')
            FormatFile(fn,
                       in_place=True,
                       style_config=os.path.join(tc.get_repo_directory(),
                                                 'misc', '.style.yapf'))
        if fn.endswith('.cpp') or fn.endswith('.h'):
            print(fn, '...')
            os.system('clang-format-6.0 -i -style=file {}'.format(fn))

    print('Formatting done!')
Exemplo n.º 9
0
def validateFormat(fix=False):
    """Check the format of python files in the tools directory.

    Arguments:
      fix: a flag to indicate if fixes should be applied.
  """
    fixes_required = False
    failed_update_files = set()
    successful_update_files = set()
    for python_file in collectFiles():
        reformatted_source, encoding, changed = FormatFile(
            python_file,
            style_config='tools/code_format/.style.yapf',
            in_place=fix,
            print_diff=not fix)
        if not fix:
            fixes_required = True if changed else fixes_required
            if reformatted_source:
                print(reformatted_source)
            continue
        file_list = failed_update_files if reformatted_source else successful_update_files
        file_list.add(python_file)
    if fix:
        displayFixResults(successful_update_files, failed_update_files)
        fixes_required = len(failed_update_files) > 0
    return not fixes_required
Exemplo n.º 10
0
def run_yapf(target_file, failure_count, error_count, yapf_config):
    """
    Run yapf on the target file.

    :param target_file: file to run against.
    :param failure_count: number of failures encountered.
    :param error_count: number of errors encountered.
    :param yapf_config: yapf config to use.
    :return: results of yapf run.
    """
    start_time = datetime.datetime.now()
    try:
        diff, encoding, needs_change = FormatFile(
            target_file, print_diff=True, style_config=yapf_config)
        if needs_change:
            end_time = datetime.datetime.now()
            failure_count += 1
            return JUnitFailure(target_file, diff, end_time - start_time)
    except ParseError as err:
        end_time = datetime.datetime.now()
        error_count += 1
        return JUnitError(target_file, str(err), end_time - start_time)

    end_time = datetime.datetime.now()
    return JUnitResult(target_file, end_time - start_time)
Exemplo n.º 11
0
def format():
    '''Format and commit python files committed on the current feature branch'''
    diff_list = git.diff_name_only()
    py_diff_list = [pyfile for pyfile in diff_list if pyfile.endswith('.py')]

    if confirm('Review formatting changes? (Select no to approve all)'):

        for pyfile in py_diff_list:
            print 'Changes:\n' + FormatFile(pyfile, print_diff=True)[0]
            if confirm('Accept changes to %s?' % pyfile):
                FormatFile(pyfile, in_place=True)
    else:
        for pyfile in py_diff_list:
            FormatFile(pyfile, in_place=True)
    print ui.info('Formatting complete')

    if len(local('git status --porcelain', capture=True)) > 0:
        local('git commit -am "Ran autoformatter"')
Exemplo n.º 12
0
def detect_formatting_violation(filepath):
    original = read_file_contents(filepath)
    reformatted = FormatFile(filepath, style_config='style.yapf')
    if original != reformatted[0]:
        print(
            FormatCode(original,
                       filename=filepath,
                       print_diff=True,
                       style_config='style.yapf')[0])
        return True
    return False
Exemplo n.º 13
0
def format(all=False, diff=None):
    import os
    import taichi as tc
    from yapf.yapflib.yapf_api import FormatFile
    repo = get_repo()

    print('Code formatting ...')
    if all:
        directories = [
            'taichi', 'tests', 'examples', 'misc', 'python', 'benchmarks',
            'docs', 'misc'
        ]
        files = []
        for d in directories:
            files += list(
                Path(os.path.join(tc.get_repo_directory(), d)).rglob('*'))
    else:
        if diff is None:
            files = repo.index.diff('HEAD')
        else:
            files = repo.index.diff(diff)
        files = list(
            map(lambda x: os.path.join(tc.get_repo_directory(), x.a_path),
                files))

    for fn in map(str, files):
        if os.path.isdir(fn):
            continue
        if fn.find('.pytest_cache') != -1:
            continue
        if fn.find('docs/build/') != -1:
            continue
        if re.match(r'.*examples\/[a-z_]+\d\d+\.py$', fn):
            print(f'Skipping example file {fn}...')
            continue
        if fn.endswith('.py'):
            print(fn, '...')
            FormatFile(fn,
                       in_place=True,
                       style_config=os.path.join(tc.get_repo_directory(),
                                                 'misc', '.style.yapf'))
        elif has_suffix(fn, ['cpp', 'h', 'cu', 'cuh']):
            os.system('clang-format-6.0 -i -style=file {}'.format(fn))
        elif has_suffix(fn, ['txt', 'md', 'rst', 'cfg', 'll', 'ptx']):
            format_plain_text(fn)
        elif has_suffix(fn, [
                'pyc', 'png', 'jpg', 'bmp', 'gif', 'gitignore', 'whl', 'mp4',
                'html'
        ]):
            pass
        else:
            print(f'Skipping {fn}...')

    print('Formatting done!')
Exemplo n.º 14
0
def write_file(file, content):
    # delete dictionary when not needed
    with open(f'../{file}', 'w') as f:
        print(content, file=f)
    FormatFile(f'../{file}', in_place=True)
    print('wrote: ', file)
    # change extension name and write the file to JSON
    file = file.replace('.py', '.json')
    # jsonify dictionary and write to file
    with open(f'../../data/{file}', 'w') as jf:
        json.dump(content, jf)
    print('wrote: ', file)
Exemplo n.º 15
0
def format(ctx, noimports=False, nostyle=False):
    if not noimports:
        from isort import SortImports

    if not nostyle:
        from yapf.yapflib.yapf_api import FormatFile

    for filename in glob.glob('**/*.py', recursive=True):
        if not noimports:
            SortImports(filename)
        if not nostyle:
            FormatFile(filename, in_place=True)
Exemplo n.º 16
0
def render_dags(manifest):
    templates = load_templates()
    for dag in manifest:
        template = templates.get_template(dag.get('template'))
        dag_name = dag.get('dag_name')
        filename = "%s%s.py" % (output_path, dag_name)
        print('Rendering %s' % (filename))
        rendered_dag = template.render(dag)

        with open(filename, "wb") as fh:
            fh.write(rendered_dag.encode())
            fh.close()
            FormatFile(filename, in_place=True, style_config="chromium")
Exemplo n.º 17
0
    def runtest(self):
        filename = self.path
        error = None
        try:
            diff, encoding, is_changed = FormatFile(self.path,
                                                    style_config=self.style,
                                                    print_diff=True)
        except BaseException as e:
            raise BaseException(e)
        if is_changed:
            file_lines = diff.split('\n')
            lines_added = len([x for x in file_lines if x.startswith('+')])
            lines_removed = len([x for x in file_lines if x.startswith('-')])

            message = "ERROR: %s Code formatting is not correct." % (
                filename, )
            message = "%s\n       Diff: -%s/+%s lines" % (
                message, lines_removed, lines_added)

            if self.show_diff:
                message = "%s\n\n%s" % (message, diff)

            raise YapfError(message)
Exemplo n.º 18
0
def generate_notebook(output_path):
    formated_script = FormatFile(output_path, style_config='facebook')

    os.remove(output_path)
    with open(output_path, "a") as outbook:
        outbook.write(formated_script[0].replace('\r', ''))
    outbook.close()

    os.system("python -m py2nb %s %s" %
              (output_path, output_path.replace('.py', '.ipynb')))
    os.system("ipython nbconvert --to=notebook --execute %s" %
              output_path.replace('.py', '.ipynb'))
    os.remove(output_path.replace('.py', '.ipynb'))
    os.system("mv %s %s" % (output_path.replace(
        '.py', '.nbconvert.ipynb'), output_path.replace('.py', '.ipynb')))
Exemplo n.º 19
0
    def runtest(self):
        """
        Run yapf with each file.

        Raise YapfError with messages if failed,
        or save the file mtime.
        """
        diff, _, changed = FormatFile(
            str(self.fspath),
            style_config=self.__style,
            print_diff=True,
        )
        if not changed:
            self.config.yapf_mtimes[self.nodeid] = self.__mtime
            return

        if self.show_diff:
            message = diff
        else:
            diff = diff.replace('\r', '\n')
            add = sum(1 for i in re.finditer(r'^\+', diff, re.MULTILINE)) - 1
            remove = sum(1 for i in re.finditer(r'^-', diff, re.MULTILINE)) - 1
            message = f'ERROR: {self.fspath} YAPF diff: +{add}/-{remove} lines'
        raise YapfError(message)
Exemplo n.º 20
0
def apply_to_file(fp, sp, in_place=False):
    """
    Apply the style to a file.
    :param fp: path to file
    :type fp: str
    :param sp: path to style
    :type sp: str
    :param in_place: format code in-place
    :type in_place: bool
    :return: the reformated code
    :rtype: str or None
    """
    rc, encoidng, changed = FormatFile(fp,
                                       style_config=sp,
                                       verify=True,
                                       in_place=in_place)
    return rc
Exemplo n.º 21
0
def generate_lbryd_wrapper(url=LBRY_API_RAW_JSON_URL,
                           read_file=__LBRYD_BASE_FPATH__,
                           write_file=LBRYD_FPATH):
    """ Generates the actual functions for lbryd_api.py based on lbry's documentation

    :param str url: URL to the documentation we need to obtain,
     pybry.constants.LBRY_API_RAW_JSON_URL by default
    :param str read_file: This is the path to the file from which we will be reading
    :param str write_file: Path from project root to the file we'll be writing to.
     """

    functions = get_lbry_api_function_docs(url)

    # Open the actual file for appending
    with open(write_file, 'w') as lbry_file:

        lbry_file.write(
            "# This file was generated at build time using the generator function\n"
        )
        lbry_file.write("# You may edit but do so with caution\n")

        with open(read_file, 'r') as template:
            header = template.read()

        lbry_file.write(header)

        # Iterate through all the functions we retrieved
        for func in functions:

            method_definition = generate_method_definition(func)

            # Write to file
            lbry_file.write(method_definition)

    try:
        from yapf.yapflib.yapf_api import FormatFile

        # Now we should format the file using the yapf formatter
        FormatFile(write_file, in_place=True)

    except ImportError as IE:
        print(
            "[Warning]: yapf is not installed, so the generated code will not follow an easy-to-read standard"
        )
        print(IE)
Exemplo n.º 22
0
def generate_notebook(output_path):
    formated_script = FormatFile(output_path, style_config="facebook")

    os.remove(output_path)
    with open(output_path, "a") as outbook:
        outbook.write(formated_script[0].replace("\r", ""))
    outbook.close()

    os.system("python -m py2nb %s %s" %
              (output_path, output_path.replace(".py", ".ipynb")))
    os.system(
        "ipython nbconvert --to=notebook --execute %s --ExecutePreprocessor.timeout=300"
        % output_path.replace(".py", ".ipynb"))
    os.remove(output_path.replace(".py", ".ipynb"))
    os.system("mv %s %s" % (
        output_path.replace(".py", ".nbconvert.ipynb"),
        output_path.replace(".py", ".ipynb"),
    ))
Exemplo n.º 23
0
def check_format(directories_to_check, files_to_skip):
    '''Check the formatting of every python file in the specified directories.

    :param list directories_to_check: list of directories to recursively look in for python files
    :param list files_to_skip: list of file names without ".py" that should be skipped by the
    formatter
    '''
    print("Check python files with yapf in directories: {}".format(
        directories_to_check))
    files_need_formatting = False
    diffs = []
    match_pattern = regex_match_pattern(files_to_skip)
    # We walk through given directories and subdirectories to find matching python files.
    for directory in directories_to_check:
        for root, _, file_names in os.walk(directory):
            for file_name in file_names:
                if re.search(match_pattern, file_name):
                    full_name = os.path.join(root, file_name)
                    # Documentation at https://github.com/google/yapf shows that the yapf diff
                    # output looks the same as git diff output looks. It shows the file name, path,
                    # and changed lines.
                    #
                    # diff is a tuple of:
                    #   1. diff output
                    #   2. encoding
                    #   3. bool indicating whether formatting is necessary
                    diff = FormatFile(filename=full_name,
                                      print_diff=True,
                                      style_config='.style.yapf')
                    needs_formatting = diff[2]
                    if needs_formatting:
                        files_need_formatting = True
                        diffs.append(diff[0])

    if files_need_formatting:
        print("Python files were formatted incorrectly")
        print("Run 'testscripts/fix-format-python.sh' on your local repo")
        for diff in diffs:
            print(diff)
        sys.exit(1)

    else:
        print("Python files are formatted correctly")
        sys.exit(0)
Exemplo n.º 24
0
def format():
    import os
    import taichi as tc
    from yapf.yapflib.yapf_api import FormatFile
    repo = get_repo()

    print('* Formatting code', end='')
    for item in repo.index.diff('HEAD'):
        fn = os.path.join(tc.get_repo_directory(), item.a_path)
        print(end='.')
        if fn.endswith('.py'):
            FormatFile(fn,
                       in_place=True,
                       style_config=os.path.join(tc.get_repo_directory(),
                                                 '.style.yapf'))
        if fn.endswith('.cpp'):
            os.system('clang-format -i -style=file {}'.format(fn))
        repo.git.add(item.a_path)

    print('* Done!')
Exemplo n.º 25
0
def format_file(fn):
    clang_format_bin = find_clang_format_bin()
    if fn.endswith('.py'):
        print('Formatting "{}"'.format(fn))
        FormatFile(fn,
                   in_place=True,
                   style_config=os.path.join(repo_dir, 'misc', '.style.yapf'))
        format_plain_text(fn)
        return True
    elif clang_format_bin and has_suffix(fn, ['cpp', 'h', 'cu', 'cuh']):
        print('Formatting "{}"'.format(fn))
        os.system('{} -i -style=file {}'.format(clang_format_bin, fn))
        format_plain_text(fn)
        return True
    elif has_suffix(fn, ['txt', 'md', 'rst', 'cfg', 'll', 'ptx']):
        print('Formatting "{}"'.format(fn))
        format_plain_text(fn)
        return True
    else:
        return False
Exemplo n.º 26
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))
Exemplo n.º 27
0
def format_file(fn):
    clang_format_bin = find_clang_format_bin()
    if fn.endswith('.py'):
        print('Formatting "{}"'.format(fn))
        FormatFile(fn,
                   in_place=True,
                   style_config=os.path.join(repo_dir, 'misc', '.style.yapf'))
        format_plain_text(fn)
        return True
    elif clang_format_bin and has_suffix(fn, ['cpp', 'h', 'c', 'cu', 'cuh']):
        print('Formatting "{}"'.format(fn))
        os.system('{} -i -style=file {}'.format(clang_format_bin, fn))
        format_plain_text(fn)
        return True
    elif has_suffix(fn, [
            'txt', 'md', 'rst', 'cfg', 'yml', 'ini', 'map', 'cmake'
    ]) or (os.path.basename(fn)[0].isupper()
           and fn.endswith('file')):  # E.g., Dockerfile and Jenkinsfile
        print('Formatting "{}"'.format(fn))
        format_plain_text(fn)
        return True
    else:
        return False
Exemplo n.º 28
0
'''format a file'''
from yapf.yapflib.yapf_api import FormatFile  # reformat a file
FormatFile('common.py', in_place=True)
FormatFile('loader1.py', in_place=True)
Exemplo n.º 29
0
def sample(
    context,
    analysis_type,
    install_config,
    sample_config,
    reference_config,
    panel_bed,
    output_config,
    normal,
    tumor,
    sample_id,
    analysis_dir,
    fastq_path,
    check_fastq,
    overwrite_config,
    create_dir,
    fastq_prefix,
):
    """
    Prepares a config file for balsamic run_analysis. For now it is just treating json as dictionary and merging them as
it is. So this is just a placeholder for future.

    """

    if normal and analysis_type == "single":
        analysis_type = "paired"

    if not output_config:
        output_config = sample_id + "_" + datetime.now().strftime("%Y%m%d") + ".json"

    analysis_config = get_config("analysis_" + analysis_type)

    click.echo("Reading analysis config file %s" % analysis_config)
    click.echo("Reading reference config file %s" % reference_config)

    read_prefix = ["1", "2"]

    if sample_config:
        click.echo("Reading sample config file %s" % sample_config)
        sample_config = os.path.abspath(sample_config)
    else:
        sample_config = get_config("sample")
        click.echo("Reading sample config file %s" % sample_config)
        with open(sample_config) as j:
            sample_config = json.load(j)
        sample_config["analysis"]["sample_id"] = sample_id
        sample_config["analysis"]["config_creation_date"] = datetime.now().strftime(
            "%Y-%m-%d %H:%M"
        )
        sample_config["analysis"]["analysis_dir"] = analysis_dir + "/"
        sample_config["analysis"]["analysis_type"] = analysis_type
        sample_config["samples"] = {}

    output_dir = os.path.join(os.path.abspath(analysis_dir), sample_id)

    if create_dir:
        os.makedirs(output_dir, exist_ok=True)

    ## Update fastq_path
    if fastq_path:
        if os.path.isdir(output_dir) and os.path.exists(output_dir):
            os.makedirs(os.path.join(output_dir, "fastq"), exist_ok=True)

        tumor_path = copy.deepcopy(os.path.abspath(fastq_path))
        if normal:
            normal_path = copy.deepcopy(os.path.abspath(fastq_path))

        fastq_path = os.path.join(output_dir, "fastq")

        link_fastq(
            os.path.abspath(tumor_path),
            os.path.abspath(fastq_path),
            tumor,
            read_prefix,
            check_fastq,
            fastq_prefix,
        )

        if normal:

            link_fastq(
                os.path.abspath(normal_path),
                os.path.abspath(fastq_path),
                normal,
                read_prefix,
                check_fastq,
                fastq_prefix,
            )

    else:
        fastq_path = os.path.join(output_dir, "fastq")

        if os.path.exists(output_dir) and not os.path.exists(fastq_path):
            os.makedirs(os.path.join(output_dir, "fastq"), exist_ok=True)

        tumor_path = os.path.dirname(os.path.abspath(tumor))
        tumor = os.path.basename(tumor)
        m = re.search(r"R_[12]" + fastq_prefix + ".fastq.gz$", tumor)
        if m is not None:
            tumor = tumor[0 : (m.span()[0] + 1)]

        link_fastq(
            os.path.abspath(tumor_path),
            os.path.abspath(fastq_path),
            tumor,
            read_prefix,
            check_fastq,
            fastq_prefix,
        )

        if normal:
            normal_path = os.path.dirname(os.path.abspath(normal))
            normal = os.path.basename(normal)
            m = re.search(r"R_[12]" + fastq_prefix + ".fastq.gz$", normal)
            if m is not None:
                normal = normal[0 : (m.span()[0] + 1)]

            link_fastq(
                os.path.abspath(normal_path),
                os.path.abspath(fastq_path),
                normal,
                read_prefix,
                check_fastq,
                fastq_prefix,
            )

    sample_config["samples"][tumor] = {
        "file_prefix": tumor,
        "type": "tumor",
        "readpair_suffix": read_prefix,
    }

    if normal:
        sample_config["samples"][normal] = {
            "file_prefix": normal,
            "type": "normal",
            "readpair_suffix": read_prefix,
        }

    sample_config["analysis"]["fastq_path"] = os.path.abspath(fastq_path) + "/"
    sample_config["analysis"]["BALSAMIC_version"] = bv

    conda_env = glob.glob(
        os.path.join(
            os.path.dirname(os.path.abspath(__file__)), "../..", "conda_yaml/*.yaml"
        )
    )

    bioinfo_config = dict()
    bioinfo_config["bioinfo_tools"] = get_package_split(conda_env)

    output_config = os.path.join(output_dir, output_config)
    click.echo("Writing output config file %s" % os.path.abspath(output_config))

    json_out = merge_json(
        analysis_config, sample_config, reference_config, install_config, bioinfo_config
    )

    dag_image = os.path.join(
        output_dir,
        output_config + '_BALSAMIC_' + bv + '_graph.pdf')

    json_out["analysis"]["dag"] = dag_image

    if panel_bed:
        json_out = set_panel_bed(json_out, panel_bed)

    if overwrite_config:
        write_json(json_out, output_config)

    FormatFile(output_config, in_place=True)

    shellcmd = ([
        'balsamic', 'run', '-s', output_config, '--snakemake-opt',
        '"--rulegraph"', "|", "sed", '"s/digraph', 'snakemake_dag',
        '{/digraph', 'BALSAMIC', '{', 'labelloc=\\"t\\"\;', 'label=\\"Title:',
        'BALSAMIC', bv, 'workflow', 'for', 'sample:',
        json_out["analysis"]["sample_id"], '\\"\;/g"', '|', 'dot', '-Tpdf',
        '1>', dag_image
    ])

    click.echo("Creating workflow dag image file: %s" % dag_image)
    subprocess.run(" ".join(shellcmd), shell=True)
'''format a file'''
from yapf.yapflib.yapf_api import FormatFile  # reformat a file
from glob import glob

for file in glob('*.py'):
    FormatFile(file, in_place=True)
#FormatFile('bval_muni_ndte_loader1.py', in_place=True)
#FormatFile('bval_muni_ndte_calculate_volatility3.py', in_place=True)
#FormatFile('bval_muni_ndte_calculate_error4.py', in_place=True)
#FormatFile('bval_muni_ndte_plot_graphs_5.py', in_place=True)
#FormatFile('bep_download_1a.py', in_place=True)