def prepare_params(self): """params for elasticsearch """ if not isinstance(self.url, (list, tuple)): urls = [self.url] else: urls = self.url params = {"hosts": list()} params.update(self.extra) def _make_bool(string): return string.lower() in ( "true", "t", "yes", "y", "1", ) for url in urls: item = {"host": url.host, "port": url.port or 9200} query = url.query.copy() if url.username: item["http_auth"] = "{0}:{1}".format(url.username, url.password or "") if "use_ssl" in query: item["use_ssl"] = _make_bool(query.pop("use_ssl")) if "url_prefix" in query: item["url_prefix"] = query.pop("url_prefix") params["hosts"].append(item) if "sniff_on_start" in query: params["sniff_on_start"] = _make_bool(query.pop("sniff_on_start")) if "sniffer_timeout" in query: params["sniffer_timeout"] = int(query.pop("sniffer_timeout")) if "sniff_timeout" in query: params["sniff_timeout"] = float(query.pop("sniff_timeout")) if "sniff_on_connection_fail" in query: params["sniff_on_connection_fail"] = _make_bool( query.pop("sniff_on_connection_fail") ) if "max_retries" in query: params["max_retries"] = int(query.pop("max_retries")) if "retry_on_status" in query: params["retry_on_status"] = tuple( map( lambda x: int(x.strip()), query.pop("retry_on_status").split(","), ) ) if "retry_on_timeout" in query: params["retry_on_timeout"] = _make_bool(query.pop("retry_on_timeout")) if "serializer" in query: params["serializer"] = import_string(query.pop("serializer"))() if "host_info_callback" in query: params["host_info_callback"] = import_string( query.pop("host_info_callback") ) return params
def __init__(self, url, klass, **extra): """ :param url: URL instance. :param klass: Connection Class or full path of string class. """ if isinstance(url, (list, tuple)): self.url = [IURL(u) for u in url] else: self.url = IURL(url) if isinstance(klass, (str, bytes)): klass = import_string(klass) self.klass = klass self.wrapper_class = extra.pop("wrapper_class", None) self.extra = extra
def wrap(self, raw_conn): """ """ if isinstance(self.url, (list, tuple)): url_ = self.url[0] else: url_ = self.url wrapper_class = url_.query.get("wrapper_class", self.wrapper_class) if wrapper_class is None: if raw_conn.__class__.__name__ == "AsyncElasticsearch": wrapper_class = AsyncElasticsearchConnection else: wrapper_class = ElasticsearchConnection if isinstance(wrapper_class, (str, bytes)): wrapper_class = import_string(wrapper_class) return wrapper_class.from_prepared(raw_conn)
def create_connection(conn_string, klass=None, **extra): """ """ mod_pattern = "fhirpath.connectors.factory.{driver_mod}.create" if isinstance(conn_string, (tuple, list)): url = [make_url(conn) for conn in conn_string] url_ = url[0] else: url_ = url = make_url(conn_string) driver_mod = url_.drivername.split("+")[0] try: factory = import_string(mod_pattern.format(driver_mod=driver_mod)) return factory(url, klass, **extra) except ImportError: reraise( Invalid, "Invalid ({0}) drivername or not supported yet!.".format( url_.drivername), )
def _init( self, resource_class, resource_interface, resource_type, fhir_release, **kw ): """ """ if "default" in kw: if ( isinstance(kw["default"], (str, dict)) or kw["default"] is None ) is False: msg = ( "Only dict or string or None is accepted as " "default value but got {0}".format(type(kw["default"])) ) raise Invalid(msg) field_attributes = get_fields(IFhirField) attribute = field_attributes["resource_class"].bind(self) if resource_class is None: attribute.validate(resource_class) attribute_val = None else: attribute_val = attribute.from_unicode(resource_class) attribute.set(self, attribute_val) attribute = field_attributes["resource_interface"].bind(self) if resource_interface is None: attribute.validate(resource_interface) attribute_val = None else: attribute_val = attribute.from_unicode(resource_interface) attribute.set(self, attribute_val) attribute = field_attributes["resource_type"].bind(self) if resource_type is None: attribute.validate(resource_type) attribute_val = None else: attribute_val = attribute.from_unicode(resource_type) attribute.set(self, attribute_val) attribute = field_attributes["fhir_release"].bind(self) if fhir_release is None: attribute.validate(fhir_release) attribute_val = None else: attribute_val = attribute.from_unicode(fhir_release) # just for ensure correct value FHIR_VERSION[attribute_val] attribute.set(self, attribute_val) if self.resource_type and self.resource_class is not None: raise Invalid( "Either `resource_class` or `resource_type` value is acceptable! " "you cannot provide both!" ) if self.resource_class: try: klass = import_string(self.resource_class) self.ensure_fhir_abstract(klass) except ImportError: msg = ( "Invalid FHIR Resource class `{0}`! " "Please check the module or class name." ).format(self.resource_class) return reraise(Invalid, msg) if not IFhirResource.implementedBy(klass): raise Invalid( "{0!r} must be valid resource class from fhir.resources".format( klass ) ) self._resource_class = klass if self.resource_type: try: self._resource_class = implementer(IFhirResource)( lookup_fhir_class(self.resource_type) ) except ImportError: msg = "{0} is not valid fhir resource type!".format(self.resource_type) return reraise(Invalid, msg) if self.resource_interface: try: klass = implementer(IFhirResource)( import_string(self.resource_interface) ) except ImportError: msg = ( "Invalid FHIR Resource Interface`{0}`! " "Please check the module or class name." ).format(self.resource_interface) return reraise(Invalid, msg) if not IInterface.providedBy(klass): raise WrongType("An interface is required", klass, self.__name__) if klass is not IFhirResource and not issubclass(klass, IFhirResource): msg = "`{0!r}` must be derived from {1}".format( klass, IFhirResource.__module__ + "." + IFhirResource.__class__.__name__, ) raise Invalid(msg) self._resource_interface_class = klass