def load_gpkg(ancestry: Ancestry, gpkg_path: PathLike) -> None: gpkg_path = Path(gpkg_path) try: tar_file = gzip.open(gpkg_path) try: with TemporaryDirectory() as cache_directory_path: tarfile.open(fileobj=tar_file).extractall(cache_directory_path) load_gramps(ancestry, Path(cache_directory_path) / 'data.gramps') except tarfile.ReadError: raise GrampsLoadFileError( 'Could not read "%s" as a *.tar file after un-gzipping it.' % gpkg_path) except OSError: raise GrampsLoadFileError('Could not un-gzip "%s".' % gpkg_path)
def load_gramps(ancestry: Ancestry, gramps_path: PathLike) -> None: gramps_path = Path(gramps_path) try: with gzip.open(gramps_path) as f: xml = f.read() load_xml(ancestry, xml, rootname(gramps_path)) except OSError: raise GrampsLoadFileError()
def _family_tree_configurations_schema(family_trees_configuration_dict: Any) -> List[FamilyTreeConfiguration]: schema = Schema({ 'file': All(str, IsFile(), Path()), }) family_trees_configuration = [] for family_tree_configuration_dict in family_trees_configuration_dict: schema(family_tree_configuration_dict) family_trees_configuration.append(FamilyTreeConfiguration(family_tree_configuration_dict['file'])) return family_trees_configuration
def load_xml(ancestry: Ancestry, xml: Union[str, PathLike], gramps_tree_directory_path: PathLike) -> None: gramps_tree_directory_path = Path(gramps_tree_directory_path) with suppress(FileNotFoundError, OSError): with open(xml) as f: xml = f.read() try: tree = ElementTree.ElementTree(ElementTree.fromstring(xml)) except ElementTree.ParseError as e: raise GrampsLoadFileError(e) _Loader(ancestry, tree, gramps_tree_directory_path).load()
def _update_configuration_file_path(file_path: str) -> None: if not file_path: self._widget._save_and_close.setDisabled(True) return try: if self._family_tree is None: self._family_tree = FamilyTreeConfiguration(file_path) else: self._family_tree.file_path = Path(file_path) mark_valid(self._widget._file_path) self._widget._save_and_close.setDisabled(False) except ConfigurationError as e: mark_invalid(self._widget._file_path, str(e)) self._widget._save_and_close.setDisabled(True)
class Gramps(Plugin, Parser): configuration_schema: Schema = Schema({ 'file': All(str, IsFile(), Path()), }) def __init__(self, site: Site, gramps_file_path: str): self._site = site self._gramps_file_path = gramps_file_path @classmethod def for_site(cls, site: Site, configuration: Any = NO_CONFIGURATION): return cls(site, configuration['file']) async def parse(self) -> None: parse_xml(self._site, self._gramps_file_path)
class Gramps(Plugin): configuration_schema: Schema = Schema({ 'file': All(str, IsFile(), Path()), }) def __init__(self, site: Site, gramps_file_path: str): self._site = site self._gramps_file_path = gramps_file_path @classmethod def for_site(cls, site: Site, configuration: Dict): return cls(site, configuration['file']) def subscribes_to(self) -> List[Tuple[Type[DispatchedEvent], Callable]]: return [ (ParseEvent, self._parse), ] async def _parse(self, event: ParseEvent) -> None: parse_xml(self._site, self._gramps_file_path)
async def load_file(ancestry: Ancestry, file_path: PathLike) -> None: file_path = Path(file_path) logger = getLogger() logger.info('Loading %s...' % str(file_path)) with suppress(GrampsLoadFileError): load_gpkg(ancestry, file_path) return with suppress(GrampsLoadFileError): load_gramps(ancestry, file_path) return with suppress(GrampsLoadFileError): async with aiofiles.open(file_path) as f: xml = await f.read() load_xml(ancestry, xml, file_path.anchor) return raise GrampsLoadFileError( 'Could not load "%s" as a *.gpkg, a *.gramps, or an *.xml family tree.' % file_path)
def file_path(self, file_path: PathLike) -> None: self._file_path = Path(file_path)