Skip to content

Commit bbc95ae

Browse files
sophstadi80and
authored andcommitted
Remove duplicate code for published-branches.yaml parsing
1 parent 518583c commit bbc95ae

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

snooty/gizaparser/published_branches.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
"""
77

88
from dataclasses import dataclass
9-
from typing import Dict, List, Optional
9+
from pathlib import Path
10+
from typing import Dict, List, Optional, Tuple
11+
from ..diagnostics import Diagnostic
1012
from ..flutter import checked
11-
from ..types import SerializableType
13+
from ..types import ProjectConfig, SerializableType
14+
from .parse import parse
1215

1316

1417
@checked
@@ -71,3 +74,15 @@ def serialize(self) -> SerializableType:
7174
published_branches_node["git"] = self.git.serialize()
7275

7376
return published_branches_node
77+
78+
79+
def parse_published_branches(
80+
path: Path, project_config: ProjectConfig, text: Optional[str] = None
81+
) -> Tuple[Optional[PublishedBranches], List[Diagnostic]]:
82+
try:
83+
published_branches, _, diagnostics = parse(
84+
PublishedBranches, path, project_config, text
85+
)
86+
return published_branches[0], diagnostics
87+
except IndexError:
88+
return None, diagnostics

snooty/parser.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import pwd
99
import subprocess
1010
import threading
11-
import yaml
1211
from copy import deepcopy
1312
from dataclasses import dataclass
1413
from functools import partial
@@ -19,10 +18,9 @@
1918
import watchdog.events
2019
import networkx
2120

22-
from .flutter import check_type, LoadError
2321
from . import n, gizaparser, rstparser, util
2422
from .gizaparser.nodes import GizaCategory
25-
from .gizaparser.published_branches import PublishedBranches
23+
from .gizaparser.published_branches import PublishedBranches, parse_published_branches
2624
from .postprocess import DevhubPostprocessor, Postprocessor
2725
from .util import RST_EXTENSIONS
2826
from .types import (
@@ -50,8 +48,6 @@
5048
InvalidURL,
5149
InvalidLiteralInclude,
5250
InvalidTableStructure,
53-
UnmarshallingError,
54-
ErrorParsingYAMLFile,
5551
)
5652

5753
# XXX: Work around to get snooty working with Python 3.8 until we can fix
@@ -834,28 +830,13 @@ def __init__(
834830
def get_parsed_branches(
835831
self
836832
) -> Tuple[Optional[PublishedBranches], List[Diagnostic]]:
837-
path = self.config.root
838833
try:
839-
with path.joinpath("published-branches.yaml").open(encoding="utf-8") as f:
840-
data = yaml.safe_load(f)
841-
try:
842-
result = check_type(PublishedBranches, data)
843-
return result, []
844-
except LoadError as err:
845-
line: int = getattr(err.bad_data, "_start_line", 0) + 1
846-
error_node: Diagnostic = UnmarshallingError(str(err), line)
847-
return None, [error_node]
834+
path = self.config.root
835+
return parse_published_branches(
836+
path.joinpath("published-branches.yaml"), self.config
837+
)
848838
except FileNotFoundError:
849839
pass
850-
except LoadError as err:
851-
load_error_line: int = getattr(err.bad_data, "_start_line", 0) + 1
852-
load_error_node: Diagnostic = UnmarshallingError(str(err), load_error_line)
853-
return None, [load_error_node]
854-
except yaml.error.MarkedYAMLError as err:
855-
yaml_error_node: Diagnostic = ErrorParsingYAMLFile(
856-
path, str(err), err.problem_mark.line
857-
)
858-
return None, [yaml_error_node]
859840
return None, []
860841

861842
def get_fileid(self, path: PurePath) -> FileId:

0 commit comments

Comments
 (0)