# Path: loompy/normalize.py
# def materialize_attr_values(a: np.ndarray) -> np.ndarray:
# 	scalar = False
# 	if np.isscalar(a):
# 		scalar = True
# 		a = np.array([a])
# 	result: np.ndarray = None   # This second clause takes care of attributes stored as variable-length ascii, which can be generated by loomR or Seurat
# 	if np.issubdtype(a.dtype, np.string_) or np.issubdtype(a.dtype, np.object_):
# 		# First ensure that what we load is valid ascii (i.e. ignore anything outside 7-bit range)
# 		if hasattr(a, "decode"):  # This takes care of Loom files that store strings as UTF8, which comes in as str and doesn't have a decode method
# 			temp = np.array([x.decode('ascii', 'ignore') for x in a])
# 		else:
# 			temp = a
# 		# Then unescape XML entities and convert to unicode
# 		try:
# 			result = np.array([html.unescape(x) for x in temp.astype(str)], dtype=object)
# 		except:  # Dirty hack to handle UTF-8 non-break-space in scalar strings. TODO: Rewrite this whole method completely!
# 			if type(a[0]) == np.bytes_:
# 				result = [ a[0].replace(b'\xc2\xa0', b'') ]
# 	elif np.issubdtype(a.dtype, np.str_) or np.issubdtype(a.dtype, np.unicode_):
# 		result = np.array(a.astype(str), dtype=object)
# 	else:
# 		result = a
# 	if scalar:
# 		return result[0]
# 	else:
# 		return result


import logging
import datetime
from inspect import currentframe, getouterframes
from h5py import File as HDF5File
from .normalize import materialize_attr_values


def deprecated(message: str) -> None:
	frameinfo = getouterframes(currentframe())
	logging.warn(f"╭── " + message)
	logging.warn(f"╰──> at {frameinfo[2].filename}, line {frameinfo[2].lineno}")


def timestamp() -> str:
	return datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%S.%fZ")


def get_loom_spec_version(f: HDF5File) -> str:
	if "attrs" in f and "LOOM_SPEC_VERSION" in f["/attrs"]:
		return materialize_attr_values(f["/attrs"]["LOOM_SPEC_VERSION"][()])
	if "LOOM_SPEC_VERSION" in f.attrs:

# Path: loompy/normalize.py
# def materialize_attr_values(a: np.ndarray) -> np.ndarray:
# 	scalar = False
# 	if np.isscalar(a):
# 		scalar = True
# 		a = np.array([a])
# 	result: np.ndarray = None   # This second clause takes care of attributes stored as variable-length ascii, which can be generated by loomR or Seurat
# 	if np.issubdtype(a.dtype, np.string_) or np.issubdtype(a.dtype, np.object_):
# 		# First ensure that what we load is valid ascii (i.e. ignore anything outside 7-bit range)
# 		if hasattr(a, "decode"):  # This takes care of Loom files that store strings as UTF8, which comes in as str and doesn't have a decode method
# 			temp = np.array([x.decode('ascii', 'ignore') for x in a])
# 		else:
# 			temp = a
# 		# Then unescape XML entities and convert to unicode
# 		try:
# 			result = np.array([html.unescape(x) for x in temp.astype(str)], dtype=object)
# 		except:  # Dirty hack to handle UTF-8 non-break-space in scalar strings. TODO: Rewrite this whole method completely!
# 			if type(a[0]) == np.bytes_:
# 				result = [ a[0].replace(b'\xc2\xa0', b'') ]
# 	elif np.issubdtype(a.dtype, np.str_) or np.issubdtype(a.dtype, np.unicode_):
# 		result = np.array(a.astype(str), dtype=object)
# 	else:
# 		result = a
# 	if scalar:
# 		return result[0]
# 	else:
# 		return result


# Path: loompy/utils.py
import logging
import datetime
from inspect import currentframe, getouterframes
from h5py import File as HDF5File
from .normalize import materialize_attr_values



def deprecated(message: str) -> None:
	frameinfo = getouterframes(currentframe())
	logging.warn(f"╭── " + message)
	logging.warn(f"╰──> at {frameinfo[2].filename}, line {frameinfo[2].lineno}")


def timestamp() -> str:
	return datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%S.%fZ")


def get_loom_spec_version(f: HDF5File) -> str:
	if "attrs" in f and "LOOM_SPEC_VERSION" in f["/attrs"]:
		return materialize_attr_values(f["/attrs"]["LOOM_SPEC_VERSION"][()])
	if "LOOM_SPEC_VERSION" in f.attrs:

		return materialize_attr_values(f.attrs["LOOM_SPEC_VERSION"])