Skip to content

Commit

Permalink
Fix figure paths
Browse files Browse the repository at this point in the history
  • Loading branch information
i80and committed Mar 21, 2019
1 parent 3f9ba57 commit 9eff24a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion snooty/gizaparser/test_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def test_step() -> None:
project_path, project_config, project_diagnostics = ProjectConfig.open(Path('test_data'))
project_config, project_diagnostics = ProjectConfig.open(Path('test_data'))
assert project_diagnostics == []

category = GizaStepsCategory(project_config)
Expand Down
15 changes: 7 additions & 8 deletions snooty/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
class JSONVisitor:
"""Node visitor that creates a JSON-serializable structure."""
def __init__(self,
project_root: Path,
source_path: Path,
docpath: PurePath,
document: docutils.nodes.document) -> None:
self.project_root = project_root
self.source_path = source_path
self.docpath = docpath
self.document = document
self.state: List[Dict[str, Any]] = []
Expand Down Expand Up @@ -163,7 +163,6 @@ def handle_directive(self, node: docutils.nodes.Node, doc: Dict[str, Serializabl
static_asset = self.add_static_asset(Path(argument_text))
options['checksum'] = static_asset.checksum
except OSError as err:
print(util.get_line(node))
msg = '"figure" could not open "{}": {}'.format(
argument_text, os.strerror(err.errno))
self.diagnostics.append(Diagnostic.error(msg, util.get_line(node)))
Expand All @@ -172,7 +171,7 @@ def handle_directive(self, node: docutils.nodes.Node, doc: Dict[str, Serializabl
doc['options'] = options

def add_static_asset(self, path: Path) -> StaticAsset:
fileid, path = util.reroot_path(path, self.docpath, self.project_root)
fileid, path = util.reroot_path(path, self.docpath, self.source_path)
static_asset = StaticAsset.load(fileid.as_posix(), path)
self.static_assets.add(static_asset)
return static_asset
Expand All @@ -181,7 +180,7 @@ def add_diagnostics(self, diagnostics: Iterable[Diagnostic]) -> None:
self.diagnostics.extend(diagnostics)

def __make_child_visitor(self) -> 'JSONVisitor':
visitor = type(self)(self.project_root, self.docpath, self.document)
visitor = type(self)(self.source_path, self.docpath, self.document)
visitor.diagnostics = self.diagnostics
return visitor

Expand Down Expand Up @@ -253,13 +252,13 @@ def __init__(self,
root: Path,
backend: ProjectBackend) -> None:
root = root.resolve(strict=True)
root, self.config, config_diagnostics = ProjectConfig.open(root)
self.config, config_diagnostics = ProjectConfig.open(root)

if config_diagnostics:
backend.on_diagnostics(root, config_diagnostics)
backend.on_diagnostics(self.config.root, config_diagnostics)
raise ProjectConfigError()

self.root = root
self.root = self.config.source_path
self.parser = rstparser.Parser(self.config, JSONVisitor)
self.static_assets: Dict[PurePath, Set[StaticAsset]] = collections.defaultdict(set)
self.backend = backend
Expand Down
2 changes: 1 addition & 1 deletion snooty/rstparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def parse(self, path: Path, text: Optional[str]) -> Tuple[_V, str]:

parser.parse(text, document)

visitor = self.visitor_class(self.project_config.root, path, document)
visitor = self.visitor_class(self.project_config.source_path, path, document)
visitor.add_diagnostics(diagnostics)
document.walkabout(visitor)
return visitor, text
6 changes: 3 additions & 3 deletions snooty/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

def test_project() -> None:
path = Path('test_data/bad_project')
root_path, project_config, project_diagnostics = ProjectConfig.open(path)
project_config, project_diagnostics = ProjectConfig.open(path)
assert len(project_diagnostics) == 1
assert project_config.constants == {
'version': '3.4',
Expand All @@ -13,11 +13,11 @@ def test_project() -> None:
}

path = Path('test_data/empty_project')
root_path, project_config, project_diagnostics = ProjectConfig.open(path)
project_config, project_diagnostics = ProjectConfig.open(path)
assert project_config.constants == {}

# Test missing project behavior
root_path, project_config, project_diagnostics = ProjectConfig.open(Path('.').resolve())
project_config, project_diagnostics = ProjectConfig.open(Path('.').resolve())
assert project_config.name == 'untitled'
assert len(project_diagnostics) == 0

Expand Down
13 changes: 9 additions & 4 deletions snooty/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,28 @@ def get_id(self) -> PurePath:
class ProjectConfig:
root: Path
name: str
source: str = field(default='source')
constants: Dict[str, object] = field(default_factory=dict)

@property
def source_path(self) -> Path:
return self.root.joinpath(self.source)

@classmethod
def open(cls, root: Path) -> Tuple[Path, 'ProjectConfig', List[Diagnostic]]:
def open(cls, root: Path) -> Tuple['ProjectConfig', List[Diagnostic]]:
path = root
while path.parent != path:
try:
with path.joinpath('snooty.toml').open() as f:
data = toml.load(f)
data['root'] = root
data['root'] = path
result, diagnostics = check_type(ProjectConfig, data).render_constants()
return path, result, diagnostics
return result, diagnostics
except FileNotFoundError:
pass
path = path.parent

return root, cls(root, 'untitled'), []
return cls(root, 'untitled'), []

def render_constants(self) -> Tuple['ProjectConfig', List[Diagnostic]]:
if not self.constants:
Expand Down

0 comments on commit 9eff24a

Please sign in to comment.