def coerce_type(cls, value, dest_type): if value is not None: if cls.is_array_type(dest_type): base_type = cls.get_base_type(dest_type) return [ cls.coerce_type(v, base_type) for v in value ] elif cls.is_dict_type(dest_type): base_type = cls.get_base_type(dest_type) return { k : cls.coerce_type(v, base_type) for k,v in value.items() } if dest_type == cls.INT: return int(value) elif dest_type == cls.FLOAT: return float(value) elif dest_type == cls.STRING: if isinstance(value, bytes): value = value.decode("utf-8") return str(value) elif dest_type == cls.ASCII: if isinstance(value, str): value = value.encode("ascii","ignore") return value.decode("ascii","ignore") elif dest_type == cls.BOOL: return value in cls.TRUTHY_VALUES elif dest_type == cls.UNIXTIME: if isinstance(value, datetime.datetime): return value #cast to float from string then to int to remove decimal try: ts = int(float(value)) if ts > 0: return DateUtils.unix_to_datetime(ts) except (ValueError, TypeError): pass try: return DateUtils.mysql_to_datetime(value) except (ValueError, TypeError): pass return None elif dest_type == cls.JSON: return json.loads(value) #unknown type return value
def get_token(self) -> str: """ return String, Int """ time = DateUtils.datetime_to_unix(datetime.datetime.now()) hash = hashlib.sha256( str(self.secret).encode("utf-8") + str(time).encode("utf-8") ).hexdigest() return "_".join([str(self.user_id), hash, str(time)])
def coerce_type(cls, value, dest_type): if value is not None: if cls.is_array_type(dest_type): base_type = cls.get_base_type(dest_type) return [ cls.coerce_type(v, base_type) for v in value ] elif cls.is_dict_type(dest_type): base_type = cls.get_base_type(dest_type) return { k : cls.coerce_type(v, base_type) for k,v in value.items() } if dest_type == cls.INT: return int(value) elif dest_type == cls.FLOAT: return float(value) elif dest_type == cls.STRING: if isinstance(value, bytes): value = value.decode("utf-8") return str(value) elif dest_type == cls.BOOL: return value in cls.TRUTHY_VALUES elif dest_type == cls.UNIXTIME: if isinstance(value, datetime.datetime): return value #cast to float from string then to int to remove decimal try: ts = int(float(value)) if ts > 0: return DateUtils.unix_to_datetime(ts) except (ValueError, TypeError): pass try: return DateUtils.mysql_to_datetime(value) except (ValueError, TypeError): pass return None elif dest_type == cls.JSON: return json.loads(value) #unknown type return value
def get_token(self) -> str: """ return String, Int """ time = DateUtils.datetime_to_unix(datetime.datetime.now()) hash = hashlib.sha256( str(self.secret).encode("utf-8") + str(time).encode("utf-8")).hexdigest() return "_".join([str(self.user_id), hash, str(time)])
def _get_renderer_url(self, stat_name, start_dt, end_dt=None, interval=INTERVAL_1_MINUTE, func=FUNCTION_SUM): url = self._get_graphite_endpoint() stat_name = stat_name + ".count" url += "/render?" url += "target=summarize(" + stat_name + ',"' + str( interval) + 'second","' + func + '",true)' url += "&from=" + str(DateUtils.datetime_to_unix(start_dt)) if end_dt: url += "&until=" + str(DateUtils.datetime_to_unix(end_dt)) url += "&format=json" return url
def parse(cls, token, max_time=3600): try: provider_id, hash, ts = token.split("_") now = DateUtils.datetime_to_unix(datetime.datetime.now()) if int(ts) + max_time < now: raise ExpiredTokenException() except ValueError: raise InvalidTokenException() return provider_id, hash, int(ts)
def parse(cls, token, max_time=3600): try: provider_id, hash, ts = token.split("_") now = DateUtils.datetime_to_unix(datetime.datetime.now()) if int(ts) + max_time < now: raise ExpiredTokenException() except ValueError: raise InvalidTokenException() return provider_id, hash, int(ts)
def json_helper(value): #stringifies otherwise non-json nodes try: value.set_context(context) return value.to_dict(True, optional_keys, for_api=True) except AttributeError: pass try: if isinstance(value, datetime.datetime): return DateUtils.datetime_to_unix(value) if stringify_remainder: return str(value) except AttributeError: pass raise TypeError()
def json_helper(value): #stringifies otherwise non-json nodes try: value.set_context(context) return value.to_dict(True, optional_keys, for_api=True) except AttributeError: pass try: if isinstance(value, datetime.datetime): return DateUtils.datetime_to_unix(value) if stringify_remainder: return str(value) except AttributeError: pass raise TypeError()
def _stringify_id(self, value): # try int try: if value > Id.MAX_32_BIT_INT: return str(value) except (TypeError, AttributeError) as e: pass if isinstance(value, datetime.datetime): return DateUtils.datetime_to_unix(value) # try bytes try: return base64.standard_b64encode(value).decode("utf-8") except (TypeError, AttributeError) as e: pass return value
def _coerce_type(self, value, type): truthy_values = ["true", "True", "1", 1, True] if value is not None: if type == "int": return int(value) elif type == "float": return float(value) elif type == "string": return str(value) elif type == "bool": return value in truthy_values elif type == "unixtime": #cast to float from string then to int to remove decimal return DateUtils.unix_to_datetime(int(float(value))) elif type == "json" or type == "dict": if isinstance(value, list) or isinstance(value, dict): return value #return already parsed JSON #else parse it return json.loads(value) #unknown type return value
def _coerce_type(self, value, type): truthy_values = ["true", "True", "1", 1, True] if value is not None: if type == "int": return int(value) elif type == "float": return float(value) elif type == "string": return str(value) elif type == "bool": return value in truthy_values elif type == "unixtime": #cast to float from string then to int to remove decimal return DateUtils.unix_to_datetime(int(float(value))) elif type == "json": if isinstance(value,list) or isinstance(value, dict): return value #return already parsed JSON #else parse it return json.loads(value) #unknown type return value
def _add_in_extra_keys(self): self._data_dict["success"] = self._success self._data_dict["now_ts"] = DateUtils.datetime_to_unix() self._data_dict["v"] = SERVER_VERSION self._data_dict["min_v"] = MIN_SUPPORTED_CLIENTS
def set_date(self, dt=None): if dt is None: dt = datetime.datetime.now() self._ts = DateUtils.datetime_to_unix(dt) return self
def get_date(self): return DateUtils.unix_to_datetime(self._ts)
def set_date(self, dt=None): if dt is None: dt = datetime.datetime.now() self._ts = DateUtils.datetime_to_unix(dt) return self
def get_date(self): return DateUtils.unix_to_datetime(self._ts)
def _add_in_extra_keys(self): self._data_dict["success"] = self._success self._data_dict["now_ts"] = DateUtils.datetime_to_unix() self._data_dict["v"] = SERVER_VERSION self._data_dict["min_v"] = MIN_SUPPORTED_CLIENTS