Exemple #1
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(
         func,
         Scheduled(cron=cron,
                   interval=interval,
                   initial_delay=initial_delay))
     return func
    def load_bean_definitions(self, bean_name: str,
                              bean_definition: BeanDefinition):
        source = bean_definition.source
        meta_data = AnnotationUtils.get_annotation_metadata(source)
        if meta_data.is_annotated(Include):
            self._load_bean_definition_for_included_config(meta_data)

        for m in dir(source):
            func = getattr(source, m)
            if inspect.isfunction(func):
                method_metadata = AnnotationUtils.get_annotation_metadata(func)
                if method_metadata is not None and method_metadata.is_annotated(
                        Bean):
                    self._load_bean_definition_for_bean_method(
                        bean_name, method_metadata)
Exemple #3
0
 def post_process_after_initialization(self, bean, bean_name: str):
     bean_type = bean.__class__
     annotation = AnnotationUtils.get_annotation(bean_type, Blueprint)
     if annotation is not None:
         options = annotation['options'] or {}
         b = FlaskBlueprint(bean_name,
                            bean.__module__,
                            url_prefix=annotation['url_prefix'],
                            **options)
         annotation['blueprint'] = b
         for m in dir(bean_type):
             func = getattr(bean_type, m)
             if inspect.isfunction(func):
                 method = getattr(bean, m)
                 try:
                     self._resolve_route(b, method)
                     self._resolve_method_def_filter(b, method)
                 except Exception:
                     log.error(
                         "Failed to resolve the route function '%s' in blueprint '%s'",
                         m,
                         bean_type.__name__,
                     )
                     raise
         self.blueprints.append(b)
     return bean
 def post_process_before_instantiation(self, bean_type: type,
                                       bean_name: str):
     for m in dir(bean_type):
         method = getattr(bean_type, m)
         if inspect.isfunction(method):
             a = AnnotationUtils.get_annotation(method, Autowired)
             if a is not None:
                 self._autowired_methods[bean_name].append(m)
 def register_bean(self, annotated_element, name=None):
     if self._condition_evaluator.should_skip(
             AnnotationUtils.get_annotation_metadata(annotated_element)):
         return
     bean_definition = BeanDefinition(annotated_element)
     bean_name = name or self._bean_name_generator.generate_bean_name(
         bean_definition, self._registry)
     self._registry.register_bean_definition(bean_name, bean_definition)
Exemple #6
0
 def _resolve_route(self, blueprint: FlaskBlueprint, method):
     a = AnnotationUtils.get_annotation(method, Route)
     if a is not None:
         rule_options = a['options'] or {}
         blueprint.add_url_rule(a['rule'],
                                endpoint=method.__name__,
                                view_func=self._wrap_view_func(
                                    a['rule'], method),
                                **rule_options)
 def post_process_after_initialization(self, bean, bean_name: str):
     bean_type = bean.__class__
     for m in dir(bean_type):
         func = getattr(bean_type, m)
         if inspect.isfunction(func):
             a = AnnotationUtils.get_annotation(func, AsyncRun)
             if a is not None:
                 method = getattr(bean, m)
                 self._async_methods.append((a, bean, method))
     return bean
Exemple #8
0
 def _resolve_method_def_filter(self, blueprint, method):
     a = AnnotationUtils.get_annotation(method, MethodDefFilter)
     if a is None:
         return
     for v in a['values']:
         name = v['name']
         args = v['args']
         if args is None:
             getattr(blueprint, name)(method)
         else:
             getattr(blueprint, name)(*args)(method)
 def _is_configuration_class_candidate(self,
                                       bean_definition: BeanDefinition):
     annotation_metadata = AnnotationUtils.get_annotation_metadata(
         bean_definition.source)
     if annotation_metadata is None:
         return False
     if annotation_metadata.is_annotated(Configuration):
         return True
     if annotation_metadata.is_annotated(Component):
         return True
     if annotation_metadata.is_annotated(Include):
         return True
     return False
 def _find_candidate_components(self, module):
     candidates = []
     selected_id = set()
     for obj in vars(module).values():
         if inspect.isclass(obj) or inspect.isfunction(obj):
             if obj.__module__ == module.__name__:
                 annotation_metadata = AnnotationUtils.get_annotation_metadata(
                     obj)
                 if annotation_metadata is not None and self._is_candidate_component(
                         annotation_metadata):
                     obj_id = id(obj)
                     if obj_id not in selected_id:
                         selected_id.add(obj_id)
                         bean_definition = BeanDefinition(obj)
                         candidates.append(bean_definition)
     return candidates
Exemple #11
0
 def generate_bean_name(self, bean_definition: BeanDefinition,
                        registry: BeanDefinitionRegistry) -> str:
     bean_name = None
     annotations = AnnotationUtils.get_annotations(bean_definition.source)
     for annotation in annotations:
         if isinstance(annotation, Component):
             name = annotation['name']
             if name is not None:
                 if bean_name is not None and name != bean_name:
                     raise ValueError(
                         'Annotations suggest inconsistent component names: '
                         f'"{bean_name}" versus {name}')
                 bean_name = name
     if bean_name is not None:
         return bean_name
     return self._default_generator.generate_bean_name(
         bean_definition, registry)
 def _load_bean_definition_for_included_config(
         self, metadata: AnnotationMetadata):
     annotation = metadata.get_annotation(Include)
     config_set = annotation['values']
     if config_set:
         for config_cls in config_set:
             if isinstance(config_cls, str):
                 _m, _c = config_cls.rsplit('.', maxsplit=1)
                 config_cls = getattr(import_module(_m), _c)
             config_cls_metadata = AnnotationUtils.get_annotation_metadata(
                 config_cls)
             if self._condition_evaluator.should_skip(config_cls_metadata):
                 continue
             bean_definition = BeanDefinition(config_cls)
             bean_name = self._include_bean_name_generator.generate_bean_name(
                 bean_definition, self._registry)
             self._registry.register_bean_definition(
                 bean_name, bean_definition)
Exemple #13
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(
         func, Blueprint(url_prefix=url_prefix, **options))
     return func
Exemple #14
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(func, Bean(name=name))
     return func
Exemple #15
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(func, AsyncRun(executor=executor))
     return func
Exemple #16
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(func, Repository(name=name))
     return func
Exemple #17
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(func, Controller(name=name))
     return func
Exemple #18
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(func, Route(rule=rule, **options))
     return func
Exemple #19
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(func, Include(values))
     return func
Exemple #20
0
def autowired(func):
    AnnotationUtils.add_annotation(func, Autowired())
    return func
Exemple #21
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(func, Conditional(condition))
     return func
Exemple #22
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(func, Configuration(name=name))
     return func
Exemple #23
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(func, Component(name=name))
     return func
Exemple #24
0
def _add_method_def_filter_annotation(func, name, args=None):
    a = AnnotationUtils.get_annotation(func, MethodDefFilter)
    if a is None:
        a = MethodDefFilter()
        AnnotationUtils.add_annotation(func, a)
    a.add_filter(name, args=args)
Exemple #25
0
 def wrap_func(func):
     AnnotationUtils.add_annotation(func, Service(name=name))
     return func