Exemplo n.º 1
0
class InitialUserRole(Struct):
    username = Field(str)
    password = Field(str, default=None)
    password_hash = Field(str, JsonFieldName('password-hash'), default=None)

    def execute(self):
        logger = logging.getLogger(__name__)
        if not self.password and not self.password_hash:
            logger.warning('InitialUserRole missing password/password-hash')
            return

        user = User.get(username=self.username)
        if user and not user.initial:
            logger.error(
                'InitialUserRole targets non-initial user {!r}'.format(
                    user.username))
            return
        if user:
            if self.password:
                user.set_password(self.password)
            else:
                user.password_hash = self.password_hash
            logger.info('Updated initial user {!r}'.format(self.username))
        else:
            user = User(username=self.username,
                        password=self.password,
                        password_hash=self.password_hash,
                        initial=True)
            logger.info('Created initial user {!r}'.format(self.username))
Exemplo n.º 2
0
class DemistoMarkdownRenderer(MarkdownRenderer):

    func_prefix = Field(str, default=None)

    module_overview = Field(str, default=None)

    # Overriding header levels from MarkdownRenderer to Function header to level 2
    header_level_by_type = Field({int},
                                 default={
                                     'Module': 1,
                                     'Class': 2,
                                     'Method': 4,
                                     'Function': 2,
                                     'Data': 4,
                                 })

    def _format_function_signature(self,
                                   func: docspec.Function,
                                   override_name: str = None,
                                   add_method_bar: bool = True) -> str:
        function_signature = super()._format_function_signature(
            func, override_name, add_method_bar)
        if self.func_prefix:
            function_signature = f'{self.func_prefix}{function_signature}'
        return function_signature

    def _render_header(self, fp, level, obj):
        if isinstance(obj, docspec.Module) and self.module_overview:
            fp.write(self.module_overview)
            fp.write('\n\n')
        else:
            super()._render_header(fp, level, obj)
Exemplo n.º 3
0
class HugoConfig(Struct):
    """
  Represents the Hugo configuration file that is rendered into the build directory.

  ### Options
  """

    #: Base URL.
    baseURL = Field(str, default=None)

    #: Language code. Default: `en-us`
    languageCode = Field(str, default='en-us')

    #: Title of the site. This is a mandatory field.
    title = Field(str)

    #: The theme of the site. This is a mandatory field. It must be a string, a #HugoThemePath
    #: or a #HugoThemeGitUrl object. Examples:
    #:
    #: ```yml
    #: theme: antarctica
    #: theme: {clone_url: "https://github.com/alex-shpak/hugo-book.git"}
    #: theme: docs/hugo-theme/
    #: ```
    theme = Field((str, HugoThemePath, HugoThemeGitUrl))

    #: This field collects all remaining options that do not match any of the above
    #: and will be forwarded directly into the Hugo `config.yaml` when it is rendered
    #: into the build directory.
    additional_options = Field(dict, Remainder())

    def to_toml(self, fp: TextIO) -> None:
        data = self.additional_options.copy()
        for field in self.__fields__:
            if field in ('additional_options', 'theme'):
                continue
            value = getattr(self, field)
            if value:
                data[field] = value
        if isinstance(self.theme, str):
            data['theme'] = self.theme
        elif isinstance(self.theme, (HugoThemePath, HugoThemeGitUrl)):
            data['theme'] = self.theme.name
        else:
            assert False
        fp.write(toml.dumps(data))
Exemplo n.º 4
0
class InitialResourceRole(Struct):
    id = Field(str)

    @classmethod
    def execute_once(self):
        logger = logging.getLogger(__name__)
        logger.info('Bulk-deleting initial resources')
        Resource.select(lambda r: r.initial).delete(bulk=True)

    def execute(self):
        logger = logging.getLogger(__name__)
        resource = Resource.get(id=self.id)
        if resource is not None:
            if resource.initial:
                logger.warn('InitialResourceRole {!r} already exists'.format(
                    self.id))
            else:
                logger.error(
                    'InitialResourceRole {!r} is a non-initial resource'.
                    format(self.id))
            return
        resource = Resource(id=self.id, initial=True)
        logger.info('InitialResourceRole {!r} created'.format(self.id))