コード例 #1
0
ファイル: usage.py プロジェクト: gigaquads/ravel
    def perform(self, context: Dict = None, **kwargs):
        if context is None:
            context = {}
        ctx = self.get_context()
        ctx.update({
            'app': 'cli',
            'entrypoint': StringUtils.dash(self.usage._package.__name__),
            'action': StringUtils.dash(self.usage._action.__name__),
        })
        ctx.update(context)

        # build a shell command
        # the first item is the command
        # the remaining items are arguments and kwarguments
        command = ctx['entrypoint']
        args = self.build_args([ctx['app'], ctx['action']] + ctx['args'])
        kwargs = self.build_kwargs(ctx['kwargs'])
        command_line = [command]
        command_line.extend(args)
        command_line.extend(kwargs)

        # optionally indent every line but the first
        if self._indent_after_command:
            command_line[1:] = [f'{self.INDENT}{k}' for k in command_line[1:]]
        # set the value of cli display for when rendering the template
        ctx['cli_display'] = self.SPLIT.join(command_line)
        # finally render the template
        return super().perform(context=ctx)
コード例 #2
0
ファイル: arg.py プロジェクト: gigaquads/appyratus
    def __init__(self, name=None, store=None, usage=None, **kwargs):
        """
        # Intialize the Flag Arg

        # Args
        - `name`, the name of the arg
        - `store`, the boolean value that this flag will take on when the arg
          has been specified. when True default is False, when False default is True
        """
        flags = (
            '-{}'.format(StringUtils.dash(name)),
            '-{}'.format(StringUtils.snake(name)),
        )
        if store is None:
            store = True
        if store is True:
            action = 'store_true'
        else:
            action = 'store_false'
        dest = StringUtils.snake(name)
        super().__init__(name=name,
                         dest=dest,
                         action=action,
                         flags=flags,
                         usage=usage)
コード例 #3
0
ファイル: arg.py プロジェクト: gigaquads/appyratus
 def __init__(self,
              name=None,
              flags=None,
              short_flag=None,
              long_flag=None,
              *args,
              **kwargs):
     short_flag = True if short_flag is None else short_flag
     long_flag = True if long_flag is None else long_flag
     if flags is None:
         flags = []
     if not isinstance(flags, list):
         flags = [flags]
     if not flags or short_flag or long_flag:
         if short_flag:
             short_name = name
             if short_flag is not True:
                 short_name = short_flag
             if short_name:
                 flags.append(f'-{short_name[0]}')
         if long_flag:
             long_name = name
             if long_flag is not True:
                 long_name = long_flag
             if long_name:
                 flags.append('--{}'.format(StringUtils.dash(long_name)))
                 flags.append('--{}'.format(StringUtils.snake(long_name)))
     super().__init__(name=name, flags=tuple(flags), *args, **kwargs)
コード例 #4
0
    def get_samples_path(cls, sample_type: Text = None):
        """
        # Get Samples Path
        Get the path containing all samples for a particular file type
        """

        return PathUtils.join(
            PathUtils.get_dir_path(__file__), 'sample',
            StringUtils.dash(cls.get_klass().__name__),
            sample_type if sample_type is not None else 'valid')
コード例 #5
0
ファイル: cli.py プロジェクト: gigaquads/ravel
 def _build_subparser_kwargs(self, func):
     parser_kwargs = self.decorator.kwargs.get('parser') or {}
     custom_args = self.decorator.kwargs.get('args') or []
     cli_args = self._build_cli_args(func, custom_args)
     name = StringUtils.dash(parser_kwargs.get('name') or self.program_name)
     return dict(parser_kwargs, **{
         'name': name,
         'args': cli_args,
         'perform': self,
     })
コード例 #6
0
 def get_renderer(self, name: Text = None):
     """
     # Get Renderer
     Get a renderer by the provided name.  Name is a mapping available in
     `_renderers`
     """
     renderer = None
     if name is None and self._renderer:
         renderer = self._renderer
     if not self._renderers:
         raise RenderersNotDefinedError()
     if isinstance(name, str):
         # renderer is a string.  let's find a suitable renderer
         renderer = self._renderers.get(StringUtils.dash(name))
         if not renderer:
             raise InvalidUsageRendererError(data={'renderer': name})
     if not renderer:
         raise UnknownUsageRendererError()
     return renderer
コード例 #7
0
ファイル: logging.py プロジェクト: gigaquads/ravel
    def process_message(self, level: Text, message: Text, data: Dict) -> Text:
        when = TimeUtils.utc_now().strftime('%m/%d/%Y %H:%M:%S')
        level = level.upper()[0]

        if data:
            data = self._json.decode(self._json.encode(data))
            if self._style == 'json':
                dumped_data = self._to_json(data)
            elif self._style == 'yaml':
                dumped_data = self._to_yaml(data)
            else:
                raise ValueError(f'unrcognized log style: {self.style}')
        else:
            dumped_data = None

        thread = StringUtils.dash(current_thread().name)

        display_string = (f'{when} ({level}) {self._name}' f' - {message}')
        if dumped_data:
            display_string += f'\n\n{dumped_data}\n'
        return display_string