periplus-map 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- periplus/__init__.py +70 -0
- periplus/checkmap.py +80 -0
- periplus/cli.py +658 -0
- periplus/contract/schema/map.schema.json +138 -0
- periplus/contract/schema/navigation.schema.json +200 -0
- periplus/contract/schema/pack-file.schema.json +1405 -0
- periplus/contract/schema/pack-manifest.schema.json +245 -0
- periplus/contract/schema/project-settings.schema.json +85 -0
- periplus/contract/spec/engine.md +151 -0
- periplus/contract/spec/rules.md +352 -0
- periplus/detect.py +228 -0
- periplus/engine/__init__.py +5 -0
- periplus/engine/check.py +481 -0
- periplus/engine/emit.py +368 -0
- periplus/engine/packload.py +2321 -0
- periplus/engine/pattern.py +27 -0
- periplus/engine/rules.py +1946 -0
- periplus/engine/select.py +491 -0
- periplus/engine/source.py +172 -0
- periplus/engine/tree.py +424 -0
- periplus/errors.py +332 -0
- periplus/init.py +358 -0
- periplus/layout.py +28 -0
- periplus/manifest.py +498 -0
- periplus/map.py +364 -0
- periplus/navigate/__init__.py +1 -0
- periplus/navigate/find.py +143 -0
- periplus/navigate/ids.py +30 -0
- periplus/navigate/listing.py +42 -0
- periplus/navigate/mapfile.py +189 -0
- periplus/navigate/path.py +175 -0
- periplus/navigate/show.py +175 -0
- periplus/navigate/types.py +83 -0
- periplus/navigate/walk.py +138 -0
- periplus/packs/advancedqueue_basic@0.0.1/pack.yaml +14 -0
- periplus/packs/advancedqueue_basic@0.0.1/queue_job_type/queue_job_type.yaml +9 -0
- periplus/packs/advancedqueue_basic@0.0.1/queue_job_type/queue_job_type_annotation.yaml +43 -0
- periplus/packs/advancedqueue_basic@0.0.1/queue_job_type/queue_job_type_attribute.yaml +43 -0
- periplus/packs/ai_basic@0.0.1/ai_agent/ai_agent.yaml +8 -0
- periplus/packs/ai_basic@0.0.1/ai_agent/ai_agent_attribute.yaml +43 -0
- periplus/packs/ai_basic@0.0.1/ai_function_call/ai_function_call.yaml +8 -0
- periplus/packs/ai_basic@0.0.1/ai_function_call/ai_function_call_attribute.yaml +43 -0
- periplus/packs/ai_basic@0.0.1/pack.yaml +15 -0
- periplus/packs/config_pages_basic@0.0.1/bundle/config_pages_bundle_config.yaml +54 -0
- periplus/packs/config_pages_basic@0.0.1/pack.yaml +11 -0
- periplus/packs/crop_basic@0.0.1/bundle/crop_bundle_config.yaml +54 -0
- periplus/packs/crop_basic@0.0.1/pack.yaml +11 -0
- periplus/packs/drupal@0.1.0/block/block_placement.yaml +25 -0
- periplus/packs/drupal@0.1.0/block/block_placement_code.yaml +58 -0
- periplus/packs/drupal@0.1.0/block/block_placement_config.yaml +53 -0
- periplus/packs/drupal@0.1.0/block/block_plugin.yaml +52 -0
- periplus/packs/drupal@0.1.0/block/block_plugin_altered.yaml +61 -0
- periplus/packs/drupal@0.1.0/block/block_plugin_annotation.yaml +69 -0
- periplus/packs/drupal@0.1.0/block/block_plugin_attribute.yaml +56 -0
- periplus/packs/drupal@0.1.0/block/block_plugin_content.yaml +46 -0
- periplus/packs/drupal@0.1.0/block/block_plugin_derived.yaml +110 -0
- periplus/packs/drupal@0.1.0/block/block_plugin_deriver.yaml +30 -0
- periplus/packs/drupal@0.1.0/block/block_type.yaml +14 -0
- periplus/packs/drupal@0.1.0/block/block_type_code.yaml +43 -0
- periplus/packs/drupal@0.1.0/block/block_type_config.yaml +45 -0
- periplus/packs/drupal@0.1.0/cache/cache.yaml +18 -0
- periplus/packs/drupal@0.1.0/cache/cache_backend.yaml +31 -0
- periplus/packs/drupal@0.1.0/component/sdc.yaml +19 -0
- periplus/packs/drupal@0.1.0/component/sdc_definition.yaml +25 -0
- periplus/packs/drupal@0.1.0/component/sdc_use.yaml +25 -0
- periplus/packs/drupal@0.1.0/config/config_object.yaml +65 -0
- periplus/packs/drupal@0.1.0/display/display.yaml +32 -0
- periplus/packs/drupal@0.1.0/display/display_code.yaml +53 -0
- periplus/packs/drupal@0.1.0/display/display_config.yaml +95 -0
- periplus/packs/drupal@0.1.0/entity/entity.yaml +33 -0
- periplus/packs/drupal@0.1.0/entity/entity_class.yaml +60 -0
- periplus/packs/drupal@0.1.0/entity/entity_save.yaml +27 -0
- periplus/packs/drupal@0.1.0/entity/entity_type_annotation.yaml +85 -0
- periplus/packs/drupal@0.1.0/entity/entity_type_attribute.yaml +76 -0
- periplus/packs/drupal@0.1.0/field/field_instance_code.yaml +40 -0
- periplus/packs/drupal@0.1.0/field/field_instance_config.yaml +58 -0
- periplus/packs/drupal@0.1.0/field/field_storage.yaml +37 -0
- periplus/packs/drupal@0.1.0/field/field_storage_code.yaml +39 -0
- periplus/packs/drupal@0.1.0/field/field_storage_config.yaml +24 -0
- periplus/packs/drupal@0.1.0/form/form.yaml +48 -0
- periplus/packs/drupal@0.1.0/form/form_class.yaml +50 -0
- periplus/packs/drupal@0.1.0/form/form_id.yaml +48 -0
- periplus/packs/drupal@0.1.0/form/form_methods.yaml +39 -0
- periplus/packs/drupal@0.1.0/hook/hook.yaml +8 -0
- periplus/packs/drupal@0.1.0/hook/hook_attribute.yaml +28 -0
- periplus/packs/drupal@0.1.0/module/module.yaml +8 -0
- periplus/packs/drupal@0.1.0/module/module_info.yaml +50 -0
- periplus/packs/drupal@0.1.0/pack.yaml +184 -0
- periplus/packs/drupal@0.1.0/plugin/plugin.yaml +17 -0
- periplus/packs/drupal@0.1.0/plugin/plugin_annotation.yaml +120 -0
- periplus/packs/drupal@0.1.0/recommendations/recommendations.yaml +14 -0
- periplus/packs/drupal@0.1.0/service/service.yaml +19 -0
- periplus/packs/drupal@0.1.0/service/service_autowire.yaml +28 -0
- periplus/packs/drupal@0.1.0/service/service_definition.yaml +24 -0
- periplus/packs/drupal@0.1.0/service/service_use.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/action/action.yaml +9 -0
- periplus/packs/drupal_basic@0.3.0/action/action_annotation.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/action/action_attribute.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/block/block_placement.yaml +16 -0
- periplus/packs/drupal_basic@0.3.0/block/block_placement_config.yaml +51 -0
- periplus/packs/drupal_basic@0.3.0/block_plugin/block_plugin.yaml +14 -0
- periplus/packs/drupal_basic@0.3.0/block_plugin/block_plugin_annotation.yaml +47 -0
- periplus/packs/drupal_basic@0.3.0/block_plugin/block_plugin_attribute.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/block_type/block_type.yaml +8 -0
- periplus/packs/drupal_basic@0.3.0/block_type/block_type_config.yaml +54 -0
- periplus/packs/drupal_basic@0.3.0/boundary/boundary_types.yaml +19 -0
- periplus/packs/drupal_basic@0.3.0/bundle/bundle.yaml +20 -0
- periplus/packs/drupal_basic@0.3.0/bundle/bundle_config.yaml +393 -0
- periplus/packs/drupal_basic@0.3.0/call/call.yaml +349 -0
- periplus/packs/drupal_basic@0.3.0/component/component.yaml +30 -0
- periplus/packs/drupal_basic@0.3.0/config/config_object.yaml +119 -0
- periplus/packs/drupal_basic@0.3.0/constraint/constraint.yaml +8 -0
- periplus/packs/drupal_basic@0.3.0/constraint/constraint_annotation.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/constraint/constraint_attribute.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/display/display.yaml +30 -0
- periplus/packs/drupal_basic@0.3.0/display/display_config.yaml +203 -0
- periplus/packs/drupal_basic@0.3.0/display/display_mode_config.yaml +60 -0
- periplus/packs/drupal_basic@0.3.0/entity_type/config_entity_type_annotation.yaml +46 -0
- periplus/packs/drupal_basic@0.3.0/entity_type/config_entity_type_attribute.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/entity_type/content_entity_type_annotation.yaml +46 -0
- periplus/packs/drupal_basic@0.3.0/entity_type/content_entity_type_attribute.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/entity_type/entity_type.yaml +16 -0
- periplus/packs/drupal_basic@0.3.0/field/field_instance_config.yaml +116 -0
- periplus/packs/drupal_basic@0.3.0/field/field_storage.yaml +21 -0
- periplus/packs/drupal_basic@0.3.0/field/field_storage_config.yaml +45 -0
- periplus/packs/drupal_basic@0.3.0/field_formatter/field_formatter.yaml +8 -0
- periplus/packs/drupal_basic@0.3.0/field_formatter/field_formatter_annotation.yaml +46 -0
- periplus/packs/drupal_basic@0.3.0/field_formatter/field_formatter_attribute.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/field_type/field_type_annotation.yaml +46 -0
- periplus/packs/drupal_basic@0.3.0/field_type/field_type_attribute.yaml +50 -0
- periplus/packs/drupal_basic@0.3.0/field_widget/field_widget_annotation.yaml +46 -0
- periplus/packs/drupal_basic@0.3.0/field_widget/field_widget_attribute.yaml +50 -0
- periplus/packs/drupal_basic@0.3.0/form/form_id.yaml +80 -0
- periplus/packs/drupal_basic@0.3.0/hook/hook.yaml +100 -0
- periplus/packs/drupal_basic@0.3.0/hook/hook_attribute.yaml +64 -0
- periplus/packs/drupal_basic@0.3.0/hook/hook_theme_include.yaml +34 -0
- periplus/packs/drupal_basic@0.3.0/library/library.yaml +12 -0
- periplus/packs/drupal_basic@0.3.0/library/library_definition.yaml +58 -0
- periplus/packs/drupal_basic@0.3.0/local_action/local_action.yaml +12 -0
- periplus/packs/drupal_basic@0.3.0/local_action/local_action_definition.yaml +49 -0
- periplus/packs/drupal_basic@0.3.0/mail/mail.yaml +8 -0
- periplus/packs/drupal_basic@0.3.0/mail/mail_attribute.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/media_source/media_source.yaml +8 -0
- periplus/packs/drupal_basic@0.3.0/media_source/media_source_annotation.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/media_source/media_source_attribute.yaml +79 -0
- periplus/packs/drupal_basic@0.3.0/menu_link/menu_link.yaml +18 -0
- periplus/packs/drupal_basic@0.3.0/menu_link/menu_link_definition.yaml +48 -0
- periplus/packs/drupal_basic@0.3.0/module/module.yaml +7 -0
- periplus/packs/drupal_basic@0.3.0/module/module_info.yaml +58 -0
- periplus/packs/drupal_basic@0.3.0/pack.yaml +528 -0
- periplus/packs/drupal_basic@0.3.0/permission/permission.yaml +7 -0
- periplus/packs/drupal_basic@0.3.0/permission/permission_definition.yaml +27 -0
- periplus/packs/drupal_basic@0.3.0/queue_worker/queue_worker.yaml +9 -0
- periplus/packs/drupal_basic@0.3.0/queue_worker/queue_worker_annotation.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/queue_worker/queue_worker_attribute.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/rest_resource/rest_resource.yaml +8 -0
- periplus/packs/drupal_basic@0.3.0/rest_resource/rest_resource_annotation.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/rest_resource/rest_resource_attribute.yaml +43 -0
- periplus/packs/drupal_basic@0.3.0/role/role.yaml +14 -0
- periplus/packs/drupal_basic@0.3.0/role/role_config.yaml +44 -0
- periplus/packs/drupal_basic@0.3.0/route/route.yaml +42 -0
- periplus/packs/drupal_basic@0.3.0/route/route_definition.yaml +113 -0
- periplus/packs/drupal_basic@0.3.0/route/route_options.yaml +55 -0
- periplus/packs/drupal_basic@0.3.0/route/route_requirements.yaml +108 -0
- periplus/packs/drupal_basic@0.3.0/service/service.yaml +56 -0
- periplus/packs/drupal_basic@0.3.0/service/service_definition.yaml +181 -0
- periplus/packs/drupal_basic@0.3.0/template/template.yaml +86 -0
- periplus/packs/drupal_basic@0.3.0/template/template_route_link.yaml +30 -0
- periplus/packs/drupal_basic@0.3.0/theme/theme.yaml +15 -0
- periplus/packs/drupal_basic@0.3.0/theme/theme_info.yaml +61 -0
- periplus/packs/drupal_basic@0.3.0/theme_hook/template_suggestion.yaml +29 -0
- periplus/packs/drupal_basic@0.3.0/theme_hook/theme_hook.yaml +87 -0
- periplus/packs/drupal_basic@0.3.0/theme_hook/theme_hook_render.yaml +20 -0
- periplus/packs/drupal_basic@0.3.0/url/url.yaml +88 -0
- periplus/packs/drupal_basic@0.3.0/views_plugin/views_field_attribute.yaml +80 -0
- periplus/packs/drupal_basic@0.3.0/views_plugin/views_filter_attribute.yaml +80 -0
- periplus/packs/drupal_basic@0.3.0/views_plugin/views_plugin.yaml +15 -0
- periplus/packs/drupal_basic@0.3.0/views_plugin/views_style_attribute.yaml +80 -0
- periplus/packs/drupal_js_basic@0.0.1/attachment/attachment.yaml +12 -0
- periplus/packs/drupal_js_basic@0.0.1/attachment/attachment_in_php.yaml +15 -0
- periplus/packs/drupal_js_basic@0.0.1/attachment/attachment_in_php_array.yaml +52 -0
- periplus/packs/drupal_js_basic@0.0.1/attachment/attachment_in_twig.yaml +26 -0
- periplus/packs/drupal_js_basic@0.0.1/attachment/php_function_holder.yaml +13 -0
- periplus/packs/drupal_js_basic@0.0.1/behavior/behavior.yaml +198 -0
- periplus/packs/drupal_js_basic@0.0.1/pack.yaml +153 -0
- periplus/packs/drupal_js_basic@0.0.1/setting/setting.yaml +13 -0
- periplus/packs/drupal_js_basic@0.0.1/setting/setting_in_js.yaml +15 -0
- periplus/packs/drupal_js_basic@0.0.1/setting/setting_in_php.yaml +92 -0
- periplus/packs/drush_basic@0.0.1/command/command.yaml +15 -0
- periplus/packs/drush_basic@0.0.1/command/command_annotation.yaml +60 -0
- periplus/packs/drush_basic@0.0.1/command/command_attribute.yaml +88 -0
- periplus/packs/drush_basic@0.0.1/pack.yaml +29 -0
- periplus/packs/eck_basic@0.0.1/bundle/eck_bundle_config.yaml +56 -0
- periplus/packs/eck_basic@0.0.1/entity_type/eck_entity_type_config.yaml +26 -0
- periplus/packs/eck_basic@0.0.1/pack.yaml +28 -0
- periplus/packs/go@0.0.2/interfaces/satisfies.yaml +47 -0
- periplus/packs/go@0.0.2/pack.yaml +51 -0
- periplus/packs/go@0.0.2/types/find_declarations.yaml +137 -0
- periplus/packs/go@0.0.2/types/node_types.yaml +36 -0
- periplus/packs/go_basic@0.0.3/pack.yaml +104 -0
- periplus/packs/go_basic@0.0.3/rules/calls.yaml +25 -0
- periplus/packs/go_basic@0.0.3/rules/declarations.yaml +40 -0
- periplus/packs/go_basic@0.0.3/rules/imports.yaml +16 -0
- periplus/packs/go_basic@0.0.3/types/node_types.yaml +15 -0
- periplus/packs/js_basic@0.0.1/function/function.yaml +68 -0
- periplus/packs/js_basic@0.0.1/pack.yaml +50 -0
- periplus/packs/laravel_basic@0.0.5/pack.yaml +313 -0
- periplus/packs/laravel_basic@0.0.5/rules/classes.yaml +42 -0
- periplus/packs/laravel_basic@0.0.5/rules/config.yaml +229 -0
- periplus/packs/laravel_basic@0.0.5/rules/routes.yaml +210 -0
- periplus/packs/laravel_basic@0.0.5/rules/types.yaml +50 -0
- periplus/packs/laravel_basic@0.0.5/rules/views.yaml +100 -0
- periplus/packs/paragraphs_basic@0.0.1/bundle/paragraph_bundle_config.yaml +54 -0
- periplus/packs/paragraphs_basic@0.0.1/pack.yaml +12 -0
- periplus/packs/php@0.1.0/edges/edge_kinds.yaml +64 -0
- periplus/packs/php@0.1.0/edges/find_calls.yaml +34 -0
- periplus/packs/php@0.1.0/edges/find_imports.yaml +18 -0
- periplus/packs/php@0.1.0/edges/find_member_access.yaml +22 -0
- periplus/packs/php@0.1.0/edges/find_structure.yaml +38 -0
- periplus/packs/php@0.1.0/embedded/embedded_regions.yaml +19 -0
- periplus/packs/php@0.1.0/imports/imports.yaml +20 -0
- periplus/packs/php@0.1.0/pack.yaml +117 -0
- periplus/packs/php@0.1.0/resolution/excluded_tokens.yaml +23 -0
- periplus/packs/php@0.1.0/resolution/resolution.yaml +35 -0
- periplus/packs/php@0.1.0/types/find_declarations.yaml +102 -0
- periplus/packs/php@0.1.0/types/node_types.yaml +55 -0
- periplus/packs/php_basic@0.2.0/edges/edge_kinds.yaml +48 -0
- periplus/packs/php_basic@0.2.0/edges/find_structure.yaml +750 -0
- periplus/packs/php_basic@0.2.0/imports/imports.yaml +19 -0
- periplus/packs/php_basic@0.2.0/pack.yaml +94 -0
- periplus/packs/php_basic@0.2.0/types/find_declarations.yaml +267 -0
- periplus/packs/php_basic@0.2.0/types/node_types.yaml +55 -0
- periplus/packs/profile_basic@0.0.1/bundle/profile_bundle_config.yaml +54 -0
- periplus/packs/profile_basic@0.0.1/pack.yaml +12 -0
- periplus/packs/salesforce_basic@0.0.1/event/salesforce_event.yaml +36 -0
- periplus/packs/salesforce_basic@0.0.1/pack.yaml +31 -0
- periplus/packs/salesforce_basic@0.0.1/salesforce_mapping/salesforce_mapping.yaml +19 -0
- periplus/packs/salesforce_basic@0.0.1/salesforce_mapping/salesforce_mapping_config.yaml +83 -0
- periplus/packs/salesforce_basic@0.0.1/salesforce_mapping_field/salesforce_mapping_field.yaml +8 -0
- periplus/packs/salesforce_basic@0.0.1/salesforce_mapping_field/salesforce_mapping_field_annotation.yaml +43 -0
- periplus/packs/twig@0.0.1/pack.yaml +26 -0
- periplus/packs/twig@0.0.1/types/template.yaml +57 -0
- periplus/packs/twig_basic@0.1.0/pack.yaml +11 -0
- periplus/packs/twig_tweak_basic@0.0.1/pack.yaml +27 -0
- periplus/packs/twig_tweak_basic@0.0.1/rules/block.yaml +20 -0
- periplus/packs/twig_tweak_basic@0.0.1/rules/entity.yaml +35 -0
- periplus/packs/twig_tweak_basic@0.0.1/rules/field.yaml +21 -0
- periplus/packs/twig_tweak_basic@0.0.1/rules/menu.yaml +21 -0
- periplus/packs/webform_basic@0.0.1/bundle/webform_submission_bundle_config.yaml +54 -0
- periplus/packs/webform_basic@0.0.1/pack.yaml +20 -0
- periplus/packs/webform_basic@0.0.1/webform_handler/webform_handler.yaml +8 -0
- periplus/packs/webform_basic@0.0.1/webform_handler/webform_handler_annotation.yaml +43 -0
- periplus/packs/yaml@0.0.1/pack.yaml +72 -0
- periplus/packs/yaml@0.0.1/types/find_mapping_entry.yaml +21 -0
- periplus/packs/yaml@0.0.1/types/yaml_types.yaml +44 -0
- periplus/packs/yaml_basic@0.1.0/pack.yaml +10 -0
- periplus/packs.py +447 -0
- periplus/preflight.py +102 -0
- periplus/py.typed +0 -0
- periplus/report.py +596 -0
- periplus/resolution.py +355 -0
- periplus/settings.py +617 -0
- periplus/settings.stub.yml +99 -0
- periplus/spec.py +774 -0
- periplus/update.py +197 -0
- periplus/validate.py +655 -0
- periplus_map-0.2.1.dist-info/METADATA +566 -0
- periplus_map-0.2.1.dist-info/RECORD +272 -0
- periplus_map-0.2.1.dist-info/WHEEL +4 -0
- periplus_map-0.2.1.dist-info/entry_points.txt +2 -0
- periplus_map-0.2.1.dist-info/licenses/LICENSE +202 -0
- periplus_map-0.2.1.dist-info/licenses/NOTICE +2 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"""Load a written map and index it in memory.
|
|
2
|
+
|
|
3
|
+
The map is read whole on every call: a 15 MB map loads in about 0.2 s, so there is no index file
|
|
4
|
+
and no cache to go stale.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import difflib
|
|
10
|
+
import json
|
|
11
|
+
from collections import defaultdict
|
|
12
|
+
from collections.abc import Iterator, Mapping
|
|
13
|
+
from contextlib import contextmanager
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from periplus.errors import ExitCode, HarnessError
|
|
18
|
+
from periplus.layout import PROJECT_MARKER
|
|
19
|
+
from periplus.settings import find_project_root
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"MapIndex",
|
|
23
|
+
"MapNotFound",
|
|
24
|
+
"NodeNotFound",
|
|
25
|
+
"NotAMap",
|
|
26
|
+
"as_text",
|
|
27
|
+
"damaged",
|
|
28
|
+
"load_map",
|
|
29
|
+
"reading_fields",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
#: The shipped schema ``periplus check-map`` checks a map against; ``periplus map`` checks its
|
|
33
|
+
#: output with the same one.
|
|
34
|
+
MAP_SCHEMA = "schema/map.schema.json"
|
|
35
|
+
|
|
36
|
+
_DEFAULT_MAP = "map.json"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class MapNotFound(HarnessError):
|
|
40
|
+
"""No map file is there to read."""
|
|
41
|
+
|
|
42
|
+
code = ExitCode.MAP_NOT_FOUND
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class NotAMap(HarnessError):
|
|
46
|
+
"""The file is not JSON, or is JSON without a map's outline."""
|
|
47
|
+
|
|
48
|
+
code = ExitCode.NOT_A_MAP
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class NodeNotFound(HarnessError):
|
|
52
|
+
"""An id names no node in the map."""
|
|
53
|
+
|
|
54
|
+
code = ExitCode.NODE_NOT_FOUND
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class MapIndex:
|
|
58
|
+
"""A loaded map with its edges indexed by end and its type hierarchy by parent."""
|
|
59
|
+
|
|
60
|
+
def __init__(self, document: Mapping[str, Any]) -> None:
|
|
61
|
+
"""Index ``document``, which has the map outline ``load_map`` checks."""
|
|
62
|
+
self.nodes: dict[str, dict[str, Any]] = {n["id"]: n for n in document["nodes"]}
|
|
63
|
+
self._outgoing: dict[str, list[dict[str, Any]]] = defaultdict(list)
|
|
64
|
+
self._incoming: dict[str, list[dict[str, Any]]] = defaultdict(list)
|
|
65
|
+
for edge in document["edges"]:
|
|
66
|
+
self._outgoing[edge["from"]].append(edge)
|
|
67
|
+
self._incoming[edge["to"]].append(edge)
|
|
68
|
+
self._parents: dict[str, str | None] = {}
|
|
69
|
+
self._subtypes: dict[str, list[str]] = defaultdict(list)
|
|
70
|
+
for row in document.get("type_hierarchy", []):
|
|
71
|
+
self._parents[row["type"]] = row["parent"]
|
|
72
|
+
if row["parent"] is not None:
|
|
73
|
+
self._subtypes[row["parent"]].append(row["type"])
|
|
74
|
+
self.edges: list[dict[str, Any]] = list(document["edges"])
|
|
75
|
+
|
|
76
|
+
def outgoing(self, id: str) -> list[dict[str, Any]]:
|
|
77
|
+
"""The edges leaving ``id``, in map order."""
|
|
78
|
+
return list(self._outgoing.get(id, ()))
|
|
79
|
+
|
|
80
|
+
def incoming(self, id: str) -> list[dict[str, Any]]:
|
|
81
|
+
"""The edges arriving at ``id``, in map order."""
|
|
82
|
+
return list(self._incoming.get(id, ()))
|
|
83
|
+
|
|
84
|
+
def parent(self, type: str) -> str | None:
|
|
85
|
+
"""The type ``type`` descends from, or ``None`` for a root or an unlisted type."""
|
|
86
|
+
return self._parents.get(type)
|
|
87
|
+
|
|
88
|
+
def subtypes(self, type: str) -> list[str]:
|
|
89
|
+
"""The types whose parent is ``type``, sorted."""
|
|
90
|
+
return sorted(self._subtypes.get(type, ()))
|
|
91
|
+
|
|
92
|
+
def node(self, id: str) -> dict[str, Any]:
|
|
93
|
+
"""The node with this id, or ``NodeNotFound`` naming the id and the closest ids."""
|
|
94
|
+
found = self.nodes.get(id)
|
|
95
|
+
if found is not None:
|
|
96
|
+
return found
|
|
97
|
+
close = difflib.get_close_matches(id, sorted(self.nodes), n=5)
|
|
98
|
+
hint = f"; close ids: {', '.join(close)}" if close else ""
|
|
99
|
+
raise NodeNotFound(
|
|
100
|
+
f"no node has the id {id!r}{hint}; periplus find searches the ids",
|
|
101
|
+
{"id": id},
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def load_map(start: Path, env: Mapping[str, str], map_path: Path | None) -> MapIndex:
|
|
106
|
+
"""Read the map at ``map_path``, or at ``<root>/.periplus/map.json`` when none is given.
|
|
107
|
+
|
|
108
|
+
Only the outline is checked: the full schema check took 3.1 s of a 3.8 s run on a real site's
|
|
109
|
+
map, and ``periplus check-map`` runs it. ``env`` is accepted so every navigation entry point
|
|
110
|
+
has one signature; the map's place does not depend on it.
|
|
111
|
+
"""
|
|
112
|
+
path, document = _read(start, map_path)
|
|
113
|
+
version = document.get("periplus_map_version") if isinstance(document, dict) else None
|
|
114
|
+
if not (
|
|
115
|
+
isinstance(version, int)
|
|
116
|
+
and not isinstance(version, bool)
|
|
117
|
+
and version >= 0
|
|
118
|
+
and isinstance(document.get("nodes"), list)
|
|
119
|
+
and isinstance(document.get("edges"), list)
|
|
120
|
+
):
|
|
121
|
+
raise NotAMap(
|
|
122
|
+
f"{path} is not a periplus map: it needs periplus_map_version, nodes and edges",
|
|
123
|
+
{"path": str(path)},
|
|
124
|
+
)
|
|
125
|
+
try:
|
|
126
|
+
return MapIndex(document)
|
|
127
|
+
except (KeyError, TypeError) as error:
|
|
128
|
+
raise NotAMap(
|
|
129
|
+
f"{path} is not a periplus map: a node, edge or type_hierarchy row is malformed "
|
|
130
|
+
f"({type(error).__name__}: {error})",
|
|
131
|
+
{"path": str(path)},
|
|
132
|
+
) from error
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def damaged(start: Path, map_path: Path | None, error: Exception) -> NotAMap:
|
|
136
|
+
"""The ``NotAMap`` for a field a navigation command read from a map ``load_map`` accepted."""
|
|
137
|
+
path = _locate(start, map_path)
|
|
138
|
+
return NotAMap(
|
|
139
|
+
f"{path} is damaged: a node or edge field is missing or malformed "
|
|
140
|
+
f"({type(error).__name__}: {error}); run periplus check-map for the exact error",
|
|
141
|
+
{"path": str(path)},
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@contextmanager
|
|
146
|
+
def reading_fields(start: Path, map_path: Path | None) -> Iterator[None]:
|
|
147
|
+
"""Raise ``damaged``'s ``NotAMap`` for what a missing or malformed map field raises inside.
|
|
148
|
+
|
|
149
|
+
``load_map`` checks only the outline, so a command building its report from node and edge
|
|
150
|
+
fields wraps that building in this, and nothing else.
|
|
151
|
+
"""
|
|
152
|
+
try:
|
|
153
|
+
yield
|
|
154
|
+
except (KeyError, TypeError, AttributeError) as error:
|
|
155
|
+
raise damaged(start, map_path, error) from error
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def as_text(value: Any) -> str:
|
|
159
|
+
"""``value``, which a report prints as text; a ``TypeError`` when the map holds a non-string."""
|
|
160
|
+
if not isinstance(value, str):
|
|
161
|
+
raise TypeError(f"expected a string, got {type(value).__name__} {value!r}")
|
|
162
|
+
return value
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _locate(start: Path, map_path: Path | None) -> Path:
|
|
166
|
+
"""``map_path``, or the default map under the project root above ``start``."""
|
|
167
|
+
if map_path is not None:
|
|
168
|
+
return map_path
|
|
169
|
+
root = find_project_root(start)
|
|
170
|
+
if root is None:
|
|
171
|
+
raise MapNotFound(
|
|
172
|
+
f"no {PROJECT_MARKER}/ directory at or above {start}; "
|
|
173
|
+
"run periplus map first or pass --map",
|
|
174
|
+
{"start": str(start)},
|
|
175
|
+
)
|
|
176
|
+
return root / PROJECT_MARKER / _DEFAULT_MAP
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _read(start: Path, map_path: Path | None) -> tuple[Path, Any]:
|
|
180
|
+
"""Find the map as ``load_map`` does and parse it as JSON, unchecked."""
|
|
181
|
+
map_path = _locate(start, map_path)
|
|
182
|
+
if not map_path.is_file():
|
|
183
|
+
raise MapNotFound(f"no map at {map_path}; run periplus map", {"path": str(map_path)})
|
|
184
|
+
try:
|
|
185
|
+
return map_path, json.loads(map_path.read_text(encoding="utf-8"))
|
|
186
|
+
except OSError as error:
|
|
187
|
+
raise MapNotFound(f"cannot read {map_path}: {error}", {"path": str(map_path)}) from error
|
|
188
|
+
except (UnicodeDecodeError, json.JSONDecodeError) as error:
|
|
189
|
+
raise NotAMap(f"{map_path} is not JSON: {error}", {"path": str(map_path)}) from error
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"""``periplus path``: every shortest directed chain of edges between two items."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from collections.abc import Collection, Mapping
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from periplus.errors import ExitCode, HarnessError, Problem
|
|
12
|
+
from periplus.navigate.ids import resolve
|
|
13
|
+
from periplus.navigate.listing import Listing, footer, payload
|
|
14
|
+
from periplus.navigate.mapfile import MapIndex, as_text, load_map, reading_fields
|
|
15
|
+
from periplus.report import _escape
|
|
16
|
+
|
|
17
|
+
__all__ = ["RENDERERS", "PathReport", "render_json", "render_text", "run"]
|
|
18
|
+
|
|
19
|
+
_NONE = Listing((), 0, 0, False)
|
|
20
|
+
|
|
21
|
+
Step = tuple[str, str, str]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True, slots=True)
|
|
25
|
+
class PathReport:
|
|
26
|
+
"""The shortest chains from ``source`` to ``target`` as a ``Listing`` of step lists."""
|
|
27
|
+
|
|
28
|
+
source: str = ""
|
|
29
|
+
target: str = ""
|
|
30
|
+
chains: Listing = _NONE
|
|
31
|
+
warnings: tuple[str, ...] = ()
|
|
32
|
+
problems: tuple[Problem, ...] = ()
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def exit_code(self) -> ExitCode:
|
|
36
|
+
"""The lowest code among the problems, or ``OK`` when there were none."""
|
|
37
|
+
return min((problem.code for problem in self.problems), default=ExitCode.OK)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _steps(index: MapIndex, node: str, kinds: Collection[str]) -> list[Step]:
|
|
41
|
+
"""The steps leaving ``node`` as (from, kind, to), distinct and sorted."""
|
|
42
|
+
return sorted(
|
|
43
|
+
{
|
|
44
|
+
(node, as_text(e["kind"]), as_text(e["to"]))
|
|
45
|
+
for e in index.outgoing(node)
|
|
46
|
+
if not kinds or e["kind"] in kinds
|
|
47
|
+
},
|
|
48
|
+
key=lambda s: (s[1], s[2]),
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def shortest_chains(
|
|
53
|
+
index: MapIndex, source: str, target: str, kinds: Collection[str], limit: int
|
|
54
|
+
) -> tuple[list[list[Step]], int]:
|
|
55
|
+
"""The first ``limit`` shortest chains (0 = all) in step order, and the exact count of them."""
|
|
56
|
+
if source == target:
|
|
57
|
+
return [[]], 1
|
|
58
|
+
distance = {source: 0}
|
|
59
|
+
layer = [source]
|
|
60
|
+
layers = [layer]
|
|
61
|
+
while layer and target not in distance:
|
|
62
|
+
reached: list[str] = []
|
|
63
|
+
for item in layer:
|
|
64
|
+
for _, _, other in _steps(index, item, kinds):
|
|
65
|
+
if other not in distance:
|
|
66
|
+
distance[other] = distance[item] + 1
|
|
67
|
+
reached.append(other)
|
|
68
|
+
layer = reached
|
|
69
|
+
layers.append(layer)
|
|
70
|
+
if target not in distance:
|
|
71
|
+
return [], 0
|
|
72
|
+
# ways[x]: the shortest chains from x on to the target, counted from the far layer back.
|
|
73
|
+
ways = {target: 1}
|
|
74
|
+
for layer in reversed(layers[: distance[target]]):
|
|
75
|
+
for item in layer:
|
|
76
|
+
ways[item] = sum(
|
|
77
|
+
ways.get(other, 0)
|
|
78
|
+
for _, _, other in _steps(index, item, kinds)
|
|
79
|
+
if distance.get(other) == distance[item] + 1
|
|
80
|
+
)
|
|
81
|
+
chains: list[list[Step]] = []
|
|
82
|
+
|
|
83
|
+
def extend(node: str, head: list[Step]) -> None:
|
|
84
|
+
if limit and len(chains) >= limit:
|
|
85
|
+
return
|
|
86
|
+
if node == target:
|
|
87
|
+
chains.append(list(head))
|
|
88
|
+
return
|
|
89
|
+
for step in _steps(index, node, kinds):
|
|
90
|
+
if distance.get(step[2]) == distance[node] + 1 and ways.get(step[2]):
|
|
91
|
+
head.append(step)
|
|
92
|
+
extend(step[2], head)
|
|
93
|
+
head.pop()
|
|
94
|
+
|
|
95
|
+
extend(source, [])
|
|
96
|
+
return chains, ways[source]
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def run(
|
|
100
|
+
start: Path,
|
|
101
|
+
env: Mapping[str, str],
|
|
102
|
+
from_id: str,
|
|
103
|
+
to_id: str,
|
|
104
|
+
kinds: Collection[str],
|
|
105
|
+
limit: int,
|
|
106
|
+
map_path: Path | None,
|
|
107
|
+
) -> PathReport:
|
|
108
|
+
"""Find the shortest chains from ``from_id`` to ``to_id``; an unknown id is a problem."""
|
|
109
|
+
try:
|
|
110
|
+
index = load_map(start, env, map_path)
|
|
111
|
+
from_id = resolve(index, from_id)
|
|
112
|
+
to_id = resolve(index, to_id)
|
|
113
|
+
with reading_fields(start, map_path):
|
|
114
|
+
index.node(from_id)
|
|
115
|
+
index.node(to_id)
|
|
116
|
+
held = {edge["kind"] for edge in index.edges}
|
|
117
|
+
chains, total = shortest_chains(index, from_id, to_id, set(kinds), limit)
|
|
118
|
+
except HarnessError as error:
|
|
119
|
+
return PathReport(problems=(error.problem(),))
|
|
120
|
+
warnings = tuple(
|
|
121
|
+
f"the map holds no edge of kind {kind!r}" for kind in sorted(set(kinds) - held)
|
|
122
|
+
)
|
|
123
|
+
listing = Listing(tuple(chains), total, len(chains), len(chains) < total)
|
|
124
|
+
return PathReport(from_id, to_id, listing, warnings)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _chain_lines(chain: list[Step]) -> list[str]:
|
|
128
|
+
if not chain:
|
|
129
|
+
return [" (same item)"]
|
|
130
|
+
return [f" {_escape(a)} --{_escape(kind)}--> {_escape(b)}" for a, kind, b in chain]
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def render_text(report: PathReport) -> str:
|
|
134
|
+
"""The report as lines a person reads. Every outside string goes through ``_escape``."""
|
|
135
|
+
lines = ["periplus path"]
|
|
136
|
+
if report.source:
|
|
137
|
+
lines[0] += f" {_escape(report.source)} {_escape(report.target)}"
|
|
138
|
+
if report.chains.total == 0:
|
|
139
|
+
lines.append("no path")
|
|
140
|
+
else:
|
|
141
|
+
lines.append(f"{report.chains.total} shortest chains")
|
|
142
|
+
for number, chain in enumerate(report.chains.items, 1):
|
|
143
|
+
lines += [f" chain {number}:", *_chain_lines(chain)]
|
|
144
|
+
note = footer(report.chains)
|
|
145
|
+
if note:
|
|
146
|
+
lines.append(note)
|
|
147
|
+
lines += [f"warning: {_escape(w)}" for w in report.warnings]
|
|
148
|
+
lines += [f"{int(p.code)} {_escape(p.message)}" for p in report.problems]
|
|
149
|
+
return "\n".join(lines) + "\n"
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def render_json(report: PathReport) -> str:
|
|
153
|
+
"""The report as one JSON document, keys sorted, ``ensure_ascii=True``."""
|
|
154
|
+
chains = Listing(
|
|
155
|
+
tuple(
|
|
156
|
+
[{"from": a, "kind": k, "to": b} for a, k, b in chain] for chain in report.chains.items
|
|
157
|
+
),
|
|
158
|
+
report.chains.total,
|
|
159
|
+
report.chains.shown,
|
|
160
|
+
report.chains.truncated,
|
|
161
|
+
)
|
|
162
|
+
document: dict[str, Any] = {
|
|
163
|
+
"from": report.source,
|
|
164
|
+
"to": report.target,
|
|
165
|
+
"chains": payload(chains),
|
|
166
|
+
"warnings": list(report.warnings),
|
|
167
|
+
"problems": [
|
|
168
|
+
{"code": int(p.code), "message": p.message, "detail": dict(p.detail)}
|
|
169
|
+
for p in report.problems
|
|
170
|
+
],
|
|
171
|
+
}
|
|
172
|
+
return json.dumps(document, sort_keys=True, indent=2, ensure_ascii=True) + "\n"
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
RENDERERS = {"text": render_text, "json": render_json}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"""``periplus show``: one node and every edge arriving at it or leaving it, grouped by kind."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from collections.abc import Mapping
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from periplus.errors import ExitCode, HarnessError, Problem
|
|
12
|
+
from periplus.navigate.ids import resolve
|
|
13
|
+
from periplus.navigate.listing import Listing, cut, footer, payload
|
|
14
|
+
from periplus.navigate.mapfile import MapIndex, as_text, load_map, reading_fields
|
|
15
|
+
from periplus.report import _escape
|
|
16
|
+
|
|
17
|
+
__all__ = ["RENDERERS", "ShowReport", "render_json", "render_text", "run"]
|
|
18
|
+
|
|
19
|
+
_NONE = Listing((), 0, 0, False)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True, slots=True)
|
|
23
|
+
class ShowReport:
|
|
24
|
+
"""One node, and its ``outgoing`` and ``incoming`` edge rows as a ``Listing`` each."""
|
|
25
|
+
|
|
26
|
+
id: str = ""
|
|
27
|
+
type: str = ""
|
|
28
|
+
state: str = ""
|
|
29
|
+
roles: tuple[str, ...] = ()
|
|
30
|
+
attributes: Mapping[str, Any] | None = None
|
|
31
|
+
locations: tuple[Mapping[str, Any], ...] = ()
|
|
32
|
+
provenance: tuple[Mapping[str, Any], ...] = ()
|
|
33
|
+
outgoing: Listing = _NONE
|
|
34
|
+
incoming: Listing = _NONE
|
|
35
|
+
problems: tuple[Problem, ...] = ()
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def exit_code(self) -> ExitCode:
|
|
39
|
+
"""The lowest code among the problems, or ``OK`` when there were none."""
|
|
40
|
+
return min((problem.code for problem in self.problems), default=ExitCode.OK)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _first(locations: list[dict[str, Any]]) -> tuple[str, int]:
|
|
44
|
+
return (locations[0]["file"], locations[0]["line"]) if locations else ("", 0)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _rows(index: MapIndex, edges: list[dict[str, Any]], end: str) -> list[dict[str, Any]]:
|
|
48
|
+
"""Edge rows sorted by kind, then other-end id, then first location."""
|
|
49
|
+
rows = []
|
|
50
|
+
for edge in edges:
|
|
51
|
+
other = index.nodes[edge[end]]
|
|
52
|
+
confidences = {as_text(p["confidence"]) for p in edge["provenance"] if "confidence" in p}
|
|
53
|
+
rows.append(
|
|
54
|
+
{
|
|
55
|
+
"kind": as_text(edge["kind"]),
|
|
56
|
+
"other": as_text(other["id"]),
|
|
57
|
+
"other_type": as_text(other["type"]),
|
|
58
|
+
"other_state": as_text(other["state"]),
|
|
59
|
+
"locations": _placed(edge["locations"]),
|
|
60
|
+
"occurrences": edge.get("occurrences"),
|
|
61
|
+
"confidences": sorted(confidences),
|
|
62
|
+
"rules": sorted({f"{p['pack']}:{p['rule']}" for p in edge["provenance"]}),
|
|
63
|
+
}
|
|
64
|
+
)
|
|
65
|
+
return sorted(rows, key=lambda r: (r["kind"], r["other"], _first(r["locations"])))
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def run(
|
|
69
|
+
start: Path,
|
|
70
|
+
env: Mapping[str, str],
|
|
71
|
+
node_id: str,
|
|
72
|
+
limit: int,
|
|
73
|
+
map_path: Path | None,
|
|
74
|
+
) -> ShowReport:
|
|
75
|
+
"""Show the node ``node_id``; a missing map or node is a problem, not a raise."""
|
|
76
|
+
try:
|
|
77
|
+
index = load_map(start, env, map_path)
|
|
78
|
+
node_id = resolve(index, node_id)
|
|
79
|
+
with reading_fields(start, map_path):
|
|
80
|
+
node = index.node(node_id)
|
|
81
|
+
attributes = node.get("attributes", {})
|
|
82
|
+
if not isinstance(attributes, dict):
|
|
83
|
+
raise TypeError(f"attributes is a {type(attributes).__name__}, not an object")
|
|
84
|
+
for p in node["provenance"]:
|
|
85
|
+
as_text(p["pack"])
|
|
86
|
+
as_text(p["rule"])
|
|
87
|
+
as_text(p.get("confidence", ""))
|
|
88
|
+
return ShowReport(
|
|
89
|
+
id=node["id"],
|
|
90
|
+
type=as_text(node["type"]),
|
|
91
|
+
state=as_text(node["state"]),
|
|
92
|
+
roles=tuple(as_text(role) for role in node.get("roles", ())),
|
|
93
|
+
attributes=attributes,
|
|
94
|
+
locations=tuple(_placed(node["locations"])),
|
|
95
|
+
provenance=tuple(node["provenance"]),
|
|
96
|
+
outgoing=cut(_rows(index, index.outgoing(node_id), "to"), limit),
|
|
97
|
+
incoming=cut(_rows(index, index.incoming(node_id), "from"), limit),
|
|
98
|
+
)
|
|
99
|
+
except HarnessError as error:
|
|
100
|
+
return ShowReport(problems=(error.problem(),))
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _place(location: Mapping[str, Any]) -> str:
|
|
104
|
+
return f"{location['file']}:{location['line']}"
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _placed(locations: Any) -> Any:
|
|
108
|
+
"""``locations`` unchanged, once each has the file and line the text report prints."""
|
|
109
|
+
for location in locations:
|
|
110
|
+
_place(location)
|
|
111
|
+
return locations
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _edge_lines(listing: Listing) -> list[str]:
|
|
115
|
+
lines = []
|
|
116
|
+
kind = None
|
|
117
|
+
for row in listing.items:
|
|
118
|
+
if row["kind"] != kind:
|
|
119
|
+
kind = row["kind"]
|
|
120
|
+
lines.append(f" {_escape(kind)}:")
|
|
121
|
+
where = ", ".join(_place(loc) for loc in row["locations"]) or "no location"
|
|
122
|
+
count = f" x{row['occurrences']}" if row["occurrences"] else ""
|
|
123
|
+
confidences = ",".join(_escape(c) for c in row["confidences"]) or "no confidence"
|
|
124
|
+
lines.append(
|
|
125
|
+
f" {_escape(row['other'])} {_escape(row['other_type'])}"
|
|
126
|
+
f" {_escape(row['other_state'])}"
|
|
127
|
+
f" {_escape(where)}{count} {confidences}"
|
|
128
|
+
f" {_escape(', '.join(row['rules']))}"
|
|
129
|
+
)
|
|
130
|
+
note = footer(listing)
|
|
131
|
+
return lines + ([f" {note}"] if note else []) or [" none"]
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def render_text(report: ShowReport) -> str:
|
|
135
|
+
"""The report as lines a person reads. Every outside string goes through ``_escape``."""
|
|
136
|
+
lines = [f"periplus show {_escape(report.id)}" if report.id else "periplus show"]
|
|
137
|
+
if report.id:
|
|
138
|
+
lines += [f"type: {_escape(report.type)}", f"state: {_escape(report.state)}"]
|
|
139
|
+
if report.roles:
|
|
140
|
+
lines.append(f"roles: {_escape(', '.join(report.roles))}")
|
|
141
|
+
for key, value in sorted((report.attributes or {}).items()):
|
|
142
|
+
lines.append(f"attribute {_escape(key)}: {_escape(json.dumps(value, sort_keys=True))}")
|
|
143
|
+
lines += [f"location: {_escape(_place(loc))}" for loc in report.locations]
|
|
144
|
+
for p in report.provenance:
|
|
145
|
+
lines.append(
|
|
146
|
+
f"provenance: {_escape(p['pack'])}:{_escape(p['rule'])}"
|
|
147
|
+
f" {_escape(p.get('confidence', 'no confidence'))}"
|
|
148
|
+
)
|
|
149
|
+
lines += ["", f"Outgoing ({report.outgoing.total}):", *_edge_lines(report.outgoing)]
|
|
150
|
+
lines += ["", f"Incoming ({report.incoming.total}):", *_edge_lines(report.incoming)]
|
|
151
|
+
lines += [f"{int(p.code)} {_escape(p.message)}" for p in report.problems]
|
|
152
|
+
return "\n".join(lines) + "\n"
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def render_json(report: ShowReport) -> str:
|
|
156
|
+
"""The report as one JSON document, keys sorted, ``ensure_ascii=True``."""
|
|
157
|
+
document = {
|
|
158
|
+
"id": report.id,
|
|
159
|
+
"type": report.type,
|
|
160
|
+
"state": report.state,
|
|
161
|
+
"roles": list(report.roles),
|
|
162
|
+
"attributes": dict(report.attributes or {}),
|
|
163
|
+
"locations": list(report.locations),
|
|
164
|
+
"provenance": list(report.provenance),
|
|
165
|
+
"outgoing": payload(report.outgoing),
|
|
166
|
+
"incoming": payload(report.incoming),
|
|
167
|
+
"problems": [
|
|
168
|
+
{"code": int(p.code), "message": p.message, "detail": dict(p.detail)}
|
|
169
|
+
for p in report.problems
|
|
170
|
+
],
|
|
171
|
+
}
|
|
172
|
+
return json.dumps(document, sort_keys=True, indent=2, ensure_ascii=True) + "\n"
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
RENDERERS = {"text": render_text, "json": render_json}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""``periplus types``: the node types and edge kinds a map holds, with counts and parents."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from collections import Counter
|
|
7
|
+
from collections.abc import Mapping
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from periplus.errors import ExitCode, HarnessError, Problem
|
|
12
|
+
from periplus.navigate.mapfile import as_text, load_map, reading_fields
|
|
13
|
+
from periplus.report import _escape
|
|
14
|
+
|
|
15
|
+
__all__ = ["RENDERERS", "TypesReport", "render_json", "render_text", "run"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True, slots=True)
|
|
19
|
+
class TypesReport:
|
|
20
|
+
"""Node types as (type, count, parent) and edge kinds as (kind, count), each sorted."""
|
|
21
|
+
|
|
22
|
+
node_types: tuple[tuple[str, int, str | None], ...] = ()
|
|
23
|
+
edge_kinds: tuple[tuple[str, int], ...] = ()
|
|
24
|
+
problems: tuple[Problem, ...] = ()
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def exit_code(self) -> ExitCode:
|
|
28
|
+
"""The lowest code among the problems, or ``OK`` when there were none."""
|
|
29
|
+
return min((problem.code for problem in self.problems), default=ExitCode.OK)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def run(start: Path, env: Mapping[str, str], map_path: Path | None) -> TypesReport:
|
|
33
|
+
"""Count the node types and edge kinds present in the map; a load failure is a problem."""
|
|
34
|
+
try:
|
|
35
|
+
index = load_map(start, env, map_path)
|
|
36
|
+
with reading_fields(start, map_path):
|
|
37
|
+
nodes = Counter(as_text(node["type"]) for node in index.nodes.values())
|
|
38
|
+
edges = Counter(as_text(edge["kind"]) for edge in index.edges)
|
|
39
|
+
return TypesReport(
|
|
40
|
+
tuple(
|
|
41
|
+
(t, n, None if (p := index.parent(t)) is None else as_text(p))
|
|
42
|
+
for t, n in sorted(nodes.items())
|
|
43
|
+
),
|
|
44
|
+
tuple(sorted(edges.items())),
|
|
45
|
+
)
|
|
46
|
+
except HarnessError as error:
|
|
47
|
+
return TypesReport((), (), (error.problem(),))
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _section(heading: str, rows: list[str]) -> list[str]:
|
|
51
|
+
"""A blank line, a heading, and the rows indented under it; ``none`` when there are none."""
|
|
52
|
+
return ["", f"{heading}:", *[f" {row}".rstrip() for row in rows or ["none"]]]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def render_text(report: TypesReport) -> str:
|
|
56
|
+
"""The report as lines a person reads. Every outside string goes through ``_escape``."""
|
|
57
|
+
lines = ["periplus types"]
|
|
58
|
+
lines += _section(
|
|
59
|
+
"Node types",
|
|
60
|
+
[
|
|
61
|
+
f"{_escape(t)} {n} parent: {_escape(p) if p is not None else 'none'}"
|
|
62
|
+
for t, n, p in report.node_types
|
|
63
|
+
],
|
|
64
|
+
)
|
|
65
|
+
lines += _section("Edge kinds", [f"{_escape(k)} {n}" for k, n in report.edge_kinds])
|
|
66
|
+
lines += _section("Problems", [f"{int(p.code)} {_escape(p.message)}" for p in report.problems])
|
|
67
|
+
return "\n".join(lines) + "\n"
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def render_json(report: TypesReport) -> str:
|
|
71
|
+
"""The report as one JSON document, keys sorted, ``ensure_ascii=True``."""
|
|
72
|
+
payload = {
|
|
73
|
+
"node_types": [{"type": t, "count": n, "parent": p} for t, n, p in report.node_types],
|
|
74
|
+
"edge_kinds": [{"kind": k, "count": n} for k, n in report.edge_kinds],
|
|
75
|
+
"problems": [
|
|
76
|
+
{"code": int(p.code), "message": p.message, "detail": dict(p.detail)}
|
|
77
|
+
for p in report.problems
|
|
78
|
+
],
|
|
79
|
+
}
|
|
80
|
+
return json.dumps(payload, sort_keys=True, indent=2, ensure_ascii=True) + "\n"
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
RENDERERS = {"text": render_text, "json": render_json}
|