Load rst file and sanitize it for PyPI.
    Remove unsupported github tags:
     - code-block directive
517552
def rst(filename):
    '''
    
    '''
    content = open(filename).read()
    return re.sub(r'\.\.\s? code-block::\s*(\w|\+)+', '::', content)
1347814
def rewrite_tg(env, tg_name, code):
    

    visitor = ReplaceTG(env, tg_name)
    assert visitor.tg_name

    tree = visitor.visit(ast.parse(code))

    if visitor.loc:
        loc = ' #' + visitor.loc
    else:
        loc = file_loc()  # The AST visitor didn't match a call node

    if visitor.trans_gen:
        tg = meta.dump_python_source(visitor.trans_gen).strip()
    else:
        tg = None

    return meta.dump_python_source(tree).strip(), tg, loc
900385
def parse_readme(cls, readme_path: str = 'README.rst', encoding: str = 'utf-8') -> str:
        
        with HERE.joinpath(readme_path).open(encoding=encoding) as readme_file:
            long_description = readme_file.read()  # type: str

        if readme_path.endswith('.rst') and cls.download_url.startswith('https://github.com/'):
            base_url = '{}/blob/v{}/'.format(cls.download_url, cls.version)
            long_description = resolve_relative_rst_links(long_description, base_url)

        return long_description
267063
def fix_code(code, directory):
    
    if not yapf_api:
        raise Fault('yapf not installed', code=400)
    style_config = file_resources.GetDefaultStyleForDir(directory or os.getcwd())
    try:
        reformatted_source, _ = yapf_api.FormatCode(code,
                                                    filename='<stdin>',
                                                    style_config=style_config,
                                                    verify=False)
        return reformatted_source
    except Exception as e:
            raise Fault("Error during formatting: {}".format(e),
                        code=400)
937636
def sanitize_qualifiers(repos=None, followers=None, language=None):
    '''
    
    '''
    qualifiers = ''

    if repos:
        qualifiers += 'repos:{0} '.format(repos)
        qualifiers = re.sub(r"([+])([=a-zA-Z0-9]+)", r">\2", qualifiers)
        qualifiers = re.sub(r"([-])([=a-zA-Z0-9]+)", r"<\2", qualifiers)

    if followers:
        qualifiers += 'followers:{0} '.format(followers)
        qualifiers = re.sub(r"([+])([=a-zA-Z0-9]+)", r">\2", qualifiers)
        qualifiers = re.sub(r"([-])([=a-zA-Z0-9]+)", r"<\2", qualifiers)

    try:
        if language in ALLOWED_LANGUAGES and not language == '':
            qualifiers += 'language:{0} '.format(language)
        elif language == '':
            qualifiers += ''
        else:
            raise AllowedLanguagesException
    except AllowedLanguagesException as e:
        print(e)

    return qualifiers
924095
function serveReadme (req, res, next) {
  res.set('content-type', 'text/html')
  Promise.all([
    require.resolve('github-markdown-css'),
    path.join(__dirname, '..', '..', 'README.md')
  ].map(function (item) {
    return fs.readFileAsync(item, 'utf8')
  }))
    .then(function (contents) {
      res.send(
        '<!doctype html>' +
        '<html>' +
        '<head>' +
        '<meta charset="utf-8">' +
        '<title>Shutter - README.md</title>' +
        '<style type="text/css">' +
        contents[0] +
        '</style>' +
        '</head>' +
        '<body>' +
        '<div class="markdown-body">' +
        marked(contents[1]) +
        '</div>' +
        '</body>' +
        '</html>'
      )
      res.end()
    })
    .catch((reason) => {
      log.error("Can't serve README.md", reason)
      res.status(200).send()
    })
}
290790
def _load_github_hooks(github_url='https://api.github.com'):
    
    try:
        resp = requests.get(github_url + '/meta')
        if resp.status_code == 200:
            return resp.json()['hooks']
        else:
            if resp.headers.get('X-RateLimit-Remaining') == '0':
                reset_ts = int(resp.headers['X-RateLimit-Reset'])
                reset_string = time.strftime('%a, %d %b %Y %H:%M:%S GMT',
                                             time.gmtime(reset_ts))
                raise ServiceUnavailable('Rate limited from GitHub until ' +
                                         reset_string)
            else:
                raise ServiceUnavailable('Error reaching GitHub')
    except (KeyError, ValueError, requests.exceptions.ConnectionError):
        raise ServiceUnavailable('Error reaching GitHub')
810448
def beginning_of_code_block(node, line_number, full_contents, is_code_node):
    """"""
    if SPHINX_INSTALLED and not is_code_node:
        delta = len(node.non_default_attributes())
        current_line_contents = full_contents.splitlines()[line_number:]
        blank_lines = next(
            (i for (i, x) in enumerate(current_line_contents) if x),
            0)
        return (
            line_number +
            delta - 1 +
            blank_lines - 1 +
            SPHINX_CODE_BLOCK_DELTA)
    else:
        lines = full_contents.splitlines()
        code_block_length = len(node.rawsource.splitlines())

        try:
            # Case where there are no extra spaces.
            if lines[line_number - 1].strip():
                return line_number - code_block_length + 1
        except IndexError:
            pass

        # The offsets are wrong if the RST text has multiple blank lines after
        # the code block. This is a workaround.
        for line_number in range(line_number, 1, -1):
            if lines[line_number - 2].strip():
                break

        return line_number - code_block_length
1039385
def clean_python(ctx, dry_run=False):
    """"""
    # MAYBE NOT: "**/__pycache__"
    cleanup_dirs(["build", "dist", "*.egg-info", "**/__pycache__"],
                 dry_run=dry_run)
    if not dry_run:
        ctx.run("py.cleanup")
    cleanup_files(["**/*.pyc", "**/*.pyo", "**/*$py.class"], dry_run=dry_run)
237538
def main():
    
    parser = argparse.ArgumentParser()
    parser.add_argument("--log-level", default="INFO",
                        choices=logging._nameToLevel,
                        help="Logging verbosity.")
    subparsers = parser.add_subparsers(help="Commands", dest="command")

    def add_backend_args(p):
        p.add_argument("--backend", default=None, help="Backend to use.")
        p.add_argument("--args", default=None, help="Backend's arguments.")

    def add_index_args(p):
        p.add_argument("--username", default="",
                       help="Username for the Git repository with the index.")
        p.add_argument("--password", default="",
                       help="Password for the Git repository with the index.")
        p.add_argument("--index-repo", default=None,
                       help="Url of the remote Git repository.")
        p.add_argument("--cache", default=None,
                       help="Path to the folder where the Git repository will be cached.")
        p.add_argument("-s", "--signoff", action="store_true",
                       help="Add Signed-off-by line by the committer at the end of the commit log "
                            "message. The meaning of a signoff depends on the project, but it "
                            "typically certifies that committer has the rights to submit this work"
                            " under the same license and agrees to a Developer Certificate of "
                            "Origin (see http://developercertificate.org/ for more information).")

    def add_templates_args(p):
        p.add_argument(
            "--template-model",
            default=os.path.join(os.path.dirname(__file__), "templates/template_model.md.jinja2"),
            help="Path to the jinja2 template used in the index for the model.")
        p.add_argument(
            "--template-readme",
            default=os.path.join(os.path.dirname(__file__), "templates/template_readme.md.jinja2"),
            help="Path to the jinja2 template used in the index for the readme.")

    # ------------------------------------------------------------------------
    init_parser = subparsers.add_parser("init", help="Initialize the registry.")
    init_parser.set_defaults(handler=initialize_registry)
    init_parser.add_argument("-f", "--force", action="store_true",
                             help="Destructively initialize the registry.")
    add_index_args(init_parser)
    add_backend_args(init_parser)
    # ------------------------------------------------------------------------
    dump_parser = subparsers.add_parser(
        "dump", help="Print a brief information about the model to stdout.")
    dump_parser.set_defaults(handler=dump_model)
    dump_parser.add_argument(
        "input", help="Path to the model file, URL or UUID.")
    add_index_args(dump_parser)
    add_backend_args(dump_parser)
    # ------------------------------------------------------------------------
    install_parser = subparsers.add_parser(
        "install", help="Install the environment to run the model.")
    install_parser.set_defaults(handler=install_environment)
    install_parser.add_argument(
        "input", help="Path to the model file, URL or UUID.")
    install_parser.add_argument(
        "--reproduce", action="store_true",
        help="Ensure that training the model is possible.")
    install_parser.add_argument(
        "--pip", nargs="*", help="Additional arguments to pass to pip.")
    add_index_args(install_parser)
    add_backend_args(install_parser)
    # ------------------------------------------------------------------------
    publish_parser = subparsers.add_parser(
        "publish", help="Upload the model and update the registry.")
    publish_parser.set_defaults(handler=publish_model)
    publish_parser.add_argument(
        "model", help="The path to the model to publish.")
    publish_parser.add_argument(
        "--meta", default=os.path.join(os.path.dirname(__file__), "templates/template_meta.json"),
        help="Path to the JSON file which contains the additional metadata of the model.")
    publish_parser.add_argument("-d", "--update-default", action="store_true",
                                help="Set this model as the default one.")
    publish_parser.add_argument("-f", "--force", action="store_true",
                                help="Overwrite existing models.")
    add_index_args(publish_parser)
    add_backend_args(publish_parser)
    add_templates_args(publish_parser)
    # ------------------------------------------------------------------------
    list_parser = subparsers.add_parser(
        "list", help="Lists all the models in the registry.")
    list_parser.set_defaults(handler=list_models)
    add_index_args(list_parser)
    # ------------------------------------------------------------------------
    delete_parser = subparsers.add_parser("delete", help="Delete a model.")
    delete_parser.set_defaults(handler=delete_model)
    delete_parser.add_argument(
        "input", help="UUID of the model to be deleted.")
    add_index_args(delete_parser)
    add_backend_args(delete_parser)
    add_templates_args(delete_parser)
    # ------------------------------------------------------------------------
    args = parser.parse_args()
    args.log_level = logging._nameToLevel[args.log_level]
    slogging.setup(args.log_level, False)
    try:
        handler = args.handler
    except AttributeError:
        def print_usage(_):
            parser.print_usage()

        handler = print_usage
    return handler(args)
851432
def to_code_array(self):
        """"""

        state = None

        # Check if version section starts with first line
        first_line = True

        # Reset pys_file to start to enable multiple calls of this method
        self.pys_file.seek(0)

        for line in self.pys_file:
            if first_line:
                # If Version section does not start with first line then
                # the file is invalid.
                if line == "[Pyspread save file version]\n":
                    first_line = False
                else:
                    raise ValueError(_("File format unsupported."))

            if line in self._section2reader:
                state = line

            elif state is not None:
                self._section2reader[state](line)
556104
def __get_git_tag():
    
    with open(os.devnull, 'wb') as devnull:
        version = subprocess.check_output(['git', 'describe', '--tags'], stderr=devnull)
        version = version.rstrip()
    if hasattr(version, 'decode'):
        version = version.decode('utf-8')
    return version
1341656
def clean_traceback(tb):
    ''''''
    prefix = __file__[:__file__.find("feat/common/error.py")]
    regex = re.compile("(\s*File\s*\")(%s)([a-zA-Z-_\. \\/]*)(\".*)"
                       % prefix.replace("\\", "\\\\"))

    def cleanup(line):
        m = regex.match(line)
        if m:
            return m.group(1) + ".../" + m.group(3) + m.group(4)
        else:
            return line

    return '\n'.join(map(cleanup, tb.split('\n')))
142336
def to_rst(cls) -> str:
        """"""
        sep_line = '+' + 6 * '-' + '+' + '-' * 71 + '+\n'
        blank_line = '|' + 78 * ' ' + '|\n'
        table = ''
        for group in cls.groups:
            table += sep_line
            table += blank_line
            table += '|' + '**{}**'.format(group.name).center(78) + '|\n'
            table += blank_line
            for error in group.errors:
                table += sep_line
                table += ('|' + error.code.center(6) + '| ' +
                          error.short_desc.ljust(70) + '|\n')
        table += sep_line
        return table
255207
def render(txt):
    

    # Removing links to other channels
    txt = re.sub(r'<#[^\|]*\|(.*)>', r'#\g<1>', txt)

    # Removing links to other users
    txt = re.sub(r'<(@.*)>', r'\g<1>', txt)

    # handle named hyperlinks
    txt = re.sub(r'<([^\|]*)\|([^\|]*)>', r'<a href="\g<1>" target="blank">\g<2></a>', txt)

    # handle unnamed hyperlinks
    txt = re.sub(r'<([^a|/a].*)>', r'<a href="\g<1>" target="blank">\g<1></a>', txt)

    # handle ordered and unordered lists
    for delimeter in LIST_DELIMITERS:
        slack_tag = delimeter
        class_name = LIST_DELIMITERS[delimeter]

        # Wrap any lines that start with the slack_tag in <li></li>
        list_regex = u'(?:^|\n){}\s?(.*)'.format(slack_tag)
        list_repl = r'<li class="list-item-{}">\g<1></li>'.format(class_name)
        txt = re.sub(list_regex, list_repl, txt)

    # hanlde blockquotes
    txt = re.sub(u'(^|\n)(?:&gt;){3}\s?(.*)$', r'\g<1><blockquote>\g<2></blockquote>', txt, flags=re.DOTALL)
    txt = re.sub(u'(?:^|\n)&gt;\s?(.*)\n?', r'<blockquote>\g<1></blockquote>', txt)

    # handle code blocks
    txt = re.sub(r'```\n?(.*)```', r'<pre>\g<1></pre>', txt, flags=re.DOTALL)
    txt = re.sub(r'\n(</pre>)', r'\g<1>', txt)

    # handle bolding, italics, and strikethrough
    for wrapper in FORMATTERS:
        slack_tag = wrapper
        html_tag = FORMATTERS[wrapper]

        # Grab all text in formatted characters on the same line unless escaped
        regex = r'(?<!\\)\{t}([^\{t}|\n]*)\{t}'.format(t=slack_tag)
        repl = r'<{t}>\g<1></{t}>'.format(t=html_tag)
        txt = re.sub(regex, repl, txt)

    # convert line breaks
    txt = txt.replace('\n', '<br />')

    # clean up bad HTML
    parser = CustomSlackdownHTMLParser(txt)
    txt = parser.clean()

    # convert multiple spaces
    txt = txt.replace(r'  ', ' &nbsp')

    return txt
965017
def load_bot_code
      load_special_tasks
      files = Dir["#{File.expand_path('./lib', @options[:current_dir])}/**/*"]
      files.each do |file|
        require_relative file.sub(/\.rb$/, '') if file.end_with? '.rb'
      end
    end
521758
def _validate_markdown(self, expfile):
        ''''''

        try:
            import yaml
        except:
            bot.error('Python yaml is required for testing yml/markdown files.')
            sys.exit(1)

        self.metadata = {}
        uid = os.path.basename(expfile).strip('.md')
     
        if os.path.exists(expfile):
            with open(expfile, "r") as stream:
                docs = yaml.load_all(stream)
                for doc in docs:
                    if isinstance(doc,dict):
                        for k,v in doc.items():
                            print('%s: %s' %(k,v))
                            self.metadata[k] = v
            self.metadata['uid'] = uid
       
            fields = ['github', 'preview', 'name', 'layout',
                      'tags', 'uid', 'maintainer']

            # Tests for all fields
            for field in fields:
                if field not in self.metadata:
                    return False
                if self.metadata[field] in ['',None]:
                    return False

            if 'github' not in self.metadata['github']:
                return notvalid('%s: not a valid github repository' % name)
            if not isinstance(self.metadata['tags'],list):
                return notvalid('%s: tags must be a list' % name)
            if not re.search("(\w+://)(.+@)*([\w\d\.]+)(:[\d]+){0,1}/*(.*)", self.metadata['github']):
                return notvalid('%s is not a valid URL.' %(self.metadata['github']))

        return True