88
99##############################################################################
1010# Local imports.
11+ from .exceptions import EmptyMap
1112from .item import GopherItem
1213
1314##############################################################################
1920class GopherMap :
2021 """A class for parsing and holding a Gopher map."""
2122
22- def __init__ (self , map_text : str ) -> None :
23+ def __init__ (self , map_text : str , strict : bool = False ) -> None :
2324 """Initialise the Gopher map.
2425
2526 Args:
2627 map_text: The text of the Gopher map.
28+ strict: Whether to be strict about parsing the Gopher map.
2729 """
2830 self ._raw = map_text
2931 """The raw text of the Gopher map."""
32+ self ._strict = strict
33+ """Whether to be strict about parsing the Gopher map."""
3034
31- @staticmethod
32- def _parse_map (map_text : str ) -> Iterator [GopherItem ]:
35+ def _parse_map (self , map_text : str ) -> Iterator [GopherItem ]:
3336 """Parse the Gopher map text into a list of Gopher items.
3437
3538 Args:
@@ -38,10 +41,12 @@ def _parse_map(map_text: str) -> Iterator[GopherItem]:
3841 Yields:
3942 Gopher items.
4043 """
44+ if self ._strict and not map_text :
45+ raise EmptyMap ("Gopher map is empty" )
4146 for line in map_text .splitlines ():
4247 if line == EOF :
4348 break
44- yield GopherItem (line )
49+ yield GopherItem (line , self . _strict )
4550
4651 @property
4752 def raw (self ) -> str :
@@ -50,7 +55,13 @@ def raw(self) -> str:
5055
5156 @cached_property
5257 def items (self ) -> tuple [GopherItem , ...]:
53- """The list of Gopher items in the map."""
58+ """The list of Gopher items in the map.
59+
60+ Raises:
61+ EmptyMap: If the map is empty and strict mode is enabled.
62+ NoFields: If the line is missing a tab character and strict mode is enabled.
63+ UnknownItemType: If the item type is unknown and strict mode is enabled.
64+ """
5465 return tuple (self ._parse_map (self ._raw ))
5566
5667
0 commit comments