Exemplo n.º 1
0
 async def parse_toc(self, version: Version, toc: List[Dict[str, Any]], path: str):
     await self.delete_all_from_version(version)
     position = 0
     for elem in toc:
         name = elem['name']
         with open(paths.join(path, f'{name}.md')) as f:
             content = f.read()
             article = await self.create({
                 'name': name,
                 'position': position,
                 'lang': blnt.init['default_lang'],
                 'markdown': content,
                 'html': self._read_markdown(content),
                 'version': version
             })
         await self.content_service.parse_article_toc(article)
         for lang in elem.get('languages', []):
             with open(paths.join(path, f'{name}.{lang}.md')) as f:
                 content = f.read()
                 article = await self.create({
                     'name': name,
                     'position': position,
                     'lang': lang,
                     'markdown': content,
                     'html': self._read_markdown(content),
                     'version': version
                 })
             await self.content_service.parse_article_toc(article)
         position += 1
Exemplo n.º 2
0
 async def _download_and_extract(self, path: str, archive_url: str):
     if not paths.exists(path):
         paths.mkdir(path)
     with open(paths.join(path, 'archive.tar.gz'), 'wb') as archive:
         response = requests.get(archive_url)
         archive.write(response.content)
     with tarfile.open(paths.join(path, 'archive.tar.gz'),
                       'r:gz') as archive:
         archive.extractall(path)
Exemplo n.º 3
0
 async def _process_docs(self, version: Version, path: str):
     for file in os.listdir(path):
         file_path = paths.join(path, file)
         app_path = paths.join(path, file, 'docs')
         if os.path.isdir(file_path) and paths.exists(app_path):
             with open(paths.join(app_path, 'docs.yaml'),
                       'r') as doc_config:
                 config = yaml.safe_load(doc_config)
             await self.article_service.parse_toc(version,
                                                  config.get('toc', []),
                                                  app_path)
Exemplo n.º 4
0
def _get_blnt_instance(console: Console, path: str):
    module_name = _find_module(path)
    if module_name is not None and paths.exists(paths.join(path, module_name)):
        if path not in sys.path:
            sys.path.append(path)
        module = importlib.import_module(module_name)
        main_func = next(
            iter(
                [
                    f
                    for f in module.__dict__.values()
                    if callable(f) and getattr(f, "__blnt__", None) == "__blnt_main__"
                ]
            ),
            None,
        )
        if main_func is None:
            console.error(
                f"No main func found in {module_name} module. "
                "Make sure to import a @main_func decorated function in the module's __init__.py file."
            )
            sys.exit(1)
        instance = main_func()
        if not isinstance(instance, Bolinette):
            console.error("The main_func did not return a Bolinette instance.")
            sys.exit(1)
        return instance
    return Bolinette(_anonymous=True)
Exemplo n.º 5
0
def read_manifest(path, *, params: dict[str, Any] = None):
    try:
        with open(paths.join(path, "manifest.blnt.yaml")) as f:
            raw = f.read()
            if params is not None:
                for param in params:
                    raw = raw.replace(f"__{param}__", params[param])
            return yaml.safe_load(raw)
    except FileNotFoundError:
        return None
Exemplo n.º 6
0
def _create_file(
    console: Console,
    *,
    source_dir: str,
    source_template: str,
    dest_dir: str,
    dest_file: str,
    params: dict[str, Any],
):
    if not paths.exists(dest_dir):
        console.error(
            f"Folder {dest_dir} not found.\n"
            "Make sure you have set the module in the manifest or you are "
            "using the CLI at project root directory.")
        sys.exit(1)
    dest = paths.join(dest_dir, dest_file)
    if paths.exists(dest):
        console.error(f"File {dest} already exist.")
        sys.exit(1)
    rendered = files.render_template(source_dir, source_template, params)
    files.write(dest, rendered)
Exemplo n.º 7
0
 def render_template(self,
                     name: str,
                     params: dict = None,
                     workdir: str = None,
                     *,
                     status=200):
     if workdir is None:
         workdir = self.context.templates_path()
     if params is None:
         params = {}
     for key, value in self.context.manifest.items():
         if key not in params:
             params[key] = value
     try:
         content = files.render_template(workdir, name, params)
     except TemplateNotFound:
         error_404_wd = self.context.internal_files_path("templates")
         error_404 = paths.join("errors", "404.html.jinja2")
         content = files.render_template(error_404_wd, error_404, params)
         status = 404
     return AioResponse(body=content,
                        status=status,
                        content_type="text/html")
Exemplo n.º 8
0
def read_requirements(path):
    try:
        with open(paths.join(path, "requirements.txt")) as f:
            return list(filter(lambda r: len(r), f.read().split("\n")))
    except FileNotFoundError:
        return []
Exemplo n.º 9
0
 async def write_file(self, content, key):
     path = self.context.instance_path("uploads")
     if not paths.exists(path):
         paths.mkdir(path)
     with open(paths.join(path, key), "wb") as f:
         f.write(content)
Exemplo n.º 10
0
 def root_path(self, *path):
     return paths.join(self._cwd, *path)
Exemplo n.º 11
0
 def internal_files_path(self, *path):
     return paths.join(self._origin, "_files", *path)
Exemplo n.º 12
0
 def internal_path(self, *path):
     return paths.join(self._origin, *path)
Exemplo n.º 13
0
def _update_init(path: str, module: str, folder: str, name: str,
                 class_name: str):
    file_path = paths.join(path, "__init__.py")
    files.write(file_path,
                f"from {module}.{folder}.{name} import {class_name}\n",
                mode="a+")
Exemplo n.º 14
0
 def _cwd_path(*path):
     return paths.join(paths.cwd(), *path)
Exemplo n.º 15
0
from bolinette.utils import files, paths
from setuptools import setup, find_packages

from bolinette import core

context = core.BolinetteContext(
    paths.join(paths.dirname(__file__), 'bolinette'))


def project_packages(module):
    return [m for m in find_packages() if m.startswith(module)]


setup(name='Bolinette',
      packages=project_packages('bolinette'),
      include_package_data=True,
      version=context.manifest['version'],
      license='MIT',
      description='An asynchronous Python web framework',
      long_description=files.read_file(context.root_path('README.md')),
      long_description_content_type='text/markdown',
      author='Pierre Chat',
      author_email='*****@*****.**',
      url='https://github.com/TheCaptainCat/bolinette',
      keywords=['Bolinette', 'Web', 'Framework', 'Async'],
      install_requires=files.read_requirements(context.root_path()),
      classifiers=[
          'Development Status :: 4 - Beta',
          'Intended Audience :: Developers',
          'Topic :: Software Development :: Build Tools',
          'License :: OSI Approved :: MIT License',
Exemplo n.º 16
0
 async def _parse_legends_plus(self,
                               path: str,
                               region: str,
                               parse_only: typing.List[str] = None):
     file_path = paths.join(path, f'{region}-legends_plus.xml')
     await self._parse_legends_file(file_path, parse_only)