Skip to content

Commit

Permalink
Update project card schema to nest properties under a key
Browse files Browse the repository at this point in the history
  • Loading branch information
e-lo committed Mar 5, 2024
1 parent 3215db8 commit 5dd1cfa
Show file tree
Hide file tree
Showing 34 changed files with 420 additions and 340 deletions.
31 changes: 14 additions & 17 deletions bin/validate_card
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
#!/usr/bin/env python3
USAGE = "validate_card search_dir --filter_tags ['baseline2030']"
"""Validates Project Cards from command line.
import argparse
eg:
validate_card card_search_dir --filter_tags ['baseline2030']
"""

from projectcard import read_cards
from projectcard import CardLogger
import argparse
from projectcard import read_cards, CardLogger

parser = argparse.ArgumentParser(description='Project Card Validator')

parser.add_argument(
'card_search_dir',
type=str,
help='Directory or file location to find project cards.'
)

)
parser.add_argument(
'filter_tags',
type=list,
help='Tags to filter project cards by. If not provided, will look at all.'
)
nargs='*',
type=str,
help='Tags to filter project cards by. If not provided, will look at all.')

if __name__ == "__main__":

args = parser.parse_args()
dir = args.card_search_dir
filter_tags = []
if args.filter_tags:
filter_tags = args.filter_tags
filter_tags = args.filter_tags if args.filter_tags else []

card_dict = read_cards(dir, filter_tags=filter_tags)

Expand All @@ -36,6 +33,6 @@ if __name__ == "__main__":
try:
assert card.valid
CardLogger.info(f"{project} - Valid")
except as e:
CardLogger.error((f"{project} - Not Valid"))
CardLogger.error((f"{project} Error:\n{e}"))
except Exception as e:
CardLogger.error(f"{project} - Not Valid")
CardLogger.error(f"{project} Error:\n{e}")
8 changes: 4 additions & 4 deletions examples/roadway-add-managed-lane-restricted-access.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ roadway_managed_lanes:
ML_lanes:
set: 0
timeofday:
- time: ['6:00', '9:00']
- timespan: ['6:00', '9:00']
set: 1
- time: ['15:00', '18:00']
- timespan: ['15:00', '18:00']
set: 1
lanes:
change: 0
timeofday:
- time: ['6:00', '9:00']
- timespan: ['6:00', '9:00']
change: -1
- time: ['15:00', '18:00']
- timespan: ['15:00', '18:00']
change: -1
segment_id:
set: 5
Expand Down
24 changes: 12 additions & 12 deletions examples/roadway-add-managed-lane-simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ roadway_managed_lanes:
links:
name:
- 'I 35E'
from:
osm_node_id: '961117623'
to:
osm_node_id: '2564047368'
from:
osm_node_id: '961117623'
to:
osm_node_id: '2564047368'
property_changes:
segment_id:
set: 35EHOT-A
lanes:
lanes:
set: 3
timeofday:
- time: ['6:00', '9:00']
- timespan: ['6:00', '9:00']
set: 2
- time: ['16:00', '19:00']
- timespan: ['16:00', '19:00']
set: 2
ML_lanes:
set: 0
timeofday:
- time: ['6:00', '9:00']
- timespan: ['6:00', '9:00']
set: 1
- time: ['16:00', '19:00']
- timespan: ['16:00', '19:00']
set: 1
ML_price:
set: 0
group:
- category: ['sov']
timeofday:
- time: ['6:00', '9:00']
- timespan: ['6:00', '9:00']
set: 1.5
- time: ['16:00', '19:00']
- timespan: ['16:00', '19:00']
set: 2.5
- category: ['hov2']
timeofday:
- time: ['6:00', '9:00']
- timespan: ['6:00', '9:00']
set: 1.0
- time: ['16:00', '19:00']
set: 2.0
Expand Down
2 changes: 1 addition & 1 deletion examples/roadway-calculated.wr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
project: example calculate mega freeways
self: RoadwayNetwork
self_obj_type: RoadwayNetwork
---

self.links_df.loc[self.links_df['lanes'] == 4, 'lanes'] = 12
2 changes: 1 addition & 1 deletion examples/roadway-property-change-all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tags:
- 'example'
roadway_property_change:
facility:
links:
links:
modes: ['drive']
all: 'True'
lanes: 2
Expand Down
8 changes: 4 additions & 4 deletions examples/roadway-property-change.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ roadway_property_change:
osm_link_id:
- '1234'
- '2345'
from:
osm_node_id: '4321'
to:
osm_node_id: '4322'
from:
osm_node_id: '4321'
to:
osm_node_id: '4322'
property_changes:
lanes:
set: 2
Expand Down
8 changes: 4 additions & 4 deletions examples/transit-property-change.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ tags:
- example
transit_property_change:
service:
trip_id:
- 14940701-JUN19-MVS-BUS-Weekday-01
time:
- ['06:00:00', '09:00:00']
trip_properties:
trip_id:
- 14940701-JUN19-MVS-BUS-Weekday-01
timespan: ['06:00:00', '09:00:00']
property_changes:
headway_secs:
set: 900
7 changes: 4 additions & 3 deletions examples/transit-shape-change.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ tags:
- existing_plus_committed
transit_routing_change:
service:
route_id:
- 294-111
direction_id: 1
trip_properties:
route_id:
- 294-111
direction_id: 1
routing:
existing:
- -37582
Expand Down
8 changes: 2 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ def document_schema(schema_filename: str) -> str:
return content

@env.macro
def include_file(
filename: str, downshift_h1=True, start_line: int = 0, end_line: int = None
):
def include_file(filename: str, downshift_h1=True, start_line: int = 0, end_line: int = None):
"""
Include a file, optionally indicating start_line and end_line.
Expand Down Expand Up @@ -149,9 +147,7 @@ def list_examples(data_dir: str) -> str:
)

def _card_to_mdrow(card, fields):
_md_row = (
f"| [{card.project}](#{_make_slug(card.project).replace('_','-')}) | "
)
_md_row = f"| [{card.project}](#{_make_slug(card.project).replace('_','-')}) | "
_md_row += f"{_categories_as_str(card)}" " |\n"
return _md_row

Expand Down
4 changes: 2 additions & 2 deletions projectcard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .io import read_cards, read_card,write_card
from .io import read_card, read_cards, write_card
from .logger import CardLogger, setup_logging
from .projectcard import ProjectCard, SubProject
from .validate import validate_card, validate_schema_file, PycodeError, ValidationError
from .validate import PycodeError, ValidationError, validate_card, validate_schema_file
24 changes: 13 additions & 11 deletions projectcard/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
from .logger import CardLogger
from .projectcard import REPLACE_KEYS, VALID_EXT, ProjectCard


class ProjectCardReadError(Exception):
pass


def _get_cardpath_list(filepath, valid_ext: Collection[str] = VALID_EXT):
"""Returns a list of valid paths to project cards given a search string.
Expand Down Expand Up @@ -153,23 +155,25 @@ def _change_keys(obj: dict, convert: Callable = _replace_selected) -> dict:
".wrangler": _read_wrangler,
}

def read_card(filepath: str, validate:bool = False):

def read_card(filepath: str, validate: bool = False):
"""Read single project card from a path and return project card object.
args:
filepath: file where the project card is.
validate: if True, will validate the project card schemea
"""
if not Path(filepath).is_file():
raise FileNotFoundError(f"Cannot find project card file: {filepath}")
card_dict = read_cards(filepath,_cards={})
card_dict = read_cards(filepath, _cards={})
card = list(card_dict.values())[0]
if validate:
assert card.valid
return card


def read_cards(
filepath: Union[Collection[str],str],
filepath: Union[Collection[str], str],
filter_tags: Collection[str] = [],
_cards: Mapping[str, ProjectCard] = {},
) -> Mapping[str, ProjectCard]:
Expand All @@ -184,15 +188,15 @@ def read_cards(
Returns: dictionary of project cards by project name
"""
CardLogger.debug(f"Reading cards from {filepath}." )
CardLogger.debug(f"Reading cards from {filepath}.")

filter_tags = list(map(str.lower, filter_tags))
if isinstance(filepath,list) or not os.path.isfile(filepath):
if isinstance(filepath, list) or not os.path.isfile(filepath):
_card_paths = _get_cardpath_list(filepath, valid_ext=_read_method_map.keys())
for p in _card_paths:
_cards.update(read_cards(p, filter_tags=filter_tags, _cards=_cards))
return _cards

_ext = os.path.splitext(filepath)[1]
if _ext not in _read_method_map.keys():
CardLogger.debug(f"Unsupported file type for file {filepath}")
Expand All @@ -208,10 +212,8 @@ def read_cards(
if filter_tags and set(list(map(str.lower, _card_dict.get("tags", [])))).isdisjoint(
set(filter_tags)
):
CardLogger.debug(
f"Skipping {_project_name} - no overlapping tags with {filter_tags}."
)
CardLogger.debug(f"Skipping {_project_name} - no overlapping tags with {filter_tags}.")
return _cards
_cards[_project_name] = ProjectCard(_card_dict)

return _cards
Loading

0 comments on commit 5dd1cfa

Please sign in to comment.