API ¶
Basic Usage ¶
from projectcard.io import read_cards
# Read in cards from a directory with the tag "Baseline 2030"
project_cards = read_cards(directory, filter_tags=["Baseline2030"])
# Iterate through a deck of cards for validity
for project_name,card in project_cards.items():
print(f"{project_name}: {card.valid}")
# Print out a summary of the card with the project name "4th Ave Busway"
print(project_cards["4th Ave Busway"])
Project Card class for project card data schema.
DictDotAccessMixin
¶
Provides dictionary-like access to class instance attributes and dot-like access to dict attributes.
Source code in projectcard/projectcard.py
__getattr__(key)
¶
ProjectCard
¶
Bases: DictDotAccessMixin
Representation of a Project Card.
Attributes:
Name | Type | Description |
---|---|---|
__dict__ |
Dictionary of project card attributes |
|
project |
Name of project |
|
dependencies |
dict
|
Dependencies of project |
tags |
list[str]
|
Tags of project |
notes |
str
|
Notes about project |
valid |
bool
|
Boolean indicating if data conforms to project card data schema |
change_types |
list[str]
|
List of all project types in project card |
change_type |
str
|
either singular project type in project card or the string “multiple” |
sub_projects |
str
|
list of sub_project objects |
Source code in projectcard/projectcard.py
change_type: str
property
¶
Return single change type if single project or “multiple” if >1 subproject.
change_types: list[str]
property
¶
Returns list of all change types from project/subproject.
to_dict: dict
property
¶
Return dictionary of public project card attributes.
valid: bool
property
¶
Return True if project card is valid, False otherwise.
__init__(attribute_dictonary, use_defaults=True)
¶
Constructor for ProjectCard object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attribute_dictonary
|
dict
|
a nested dictionary of attributes |
required |
use_defaults
|
bool
|
if True, will use default values for missing required attributes, if exist in schema. Defaults to True. |
True
|
Source code in projectcard/projectcard.py
__str__()
¶
SubProject
¶
Bases: DictDotAccessMixin
Representation of a SubProject within a ProjectCard.
Attributes:
Name | Type | Description |
---|---|---|
parent_project |
ProjectCard
|
reference to parent ProjectCard object |
type |
ProjectCard
|
project type |
tags |
list[str]
|
reference to parent project card tags |
dependencies |
dict
|
reference to parent project card’s dependencies |
project |
str
|
reference to the name of the parent project card’s name |
Source code in projectcard/projectcard.py
change_type: str
property
¶
Return change type from subproject.
dependencies: dict
property
¶
Return dependencies from parent project card.
parent_project: ProjectCard
property
¶
Return parent project from parent project card.
project: str
property
¶
Return project name from parent project card.
tags: list[str]
property
¶
Return tags from parent project card.
valid: bool
property
¶
Check if subproject is valid.
__init__(sp_dictionary, parent_project)
¶
Constructor for SubProject object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sp_dictionary
|
dict
|
dictionary of sub-project attributes contained within “changes” list of parent projet card |
required |
parent_project
|
ProjectCard
|
ProjectCard object for parent project card |
required |
Source code in projectcard/projectcard.py
Functions for reading and writing project cards.
dict_to_yaml_with_comments(d)
¶
Converts a dictionary to a YAML string with comments.
Source code in projectcard/io.py
read_card(filepath, validate=True)
¶
Read single project card from a path and return project card object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
ProjectCardFilepath
|
file where the project card is. |
required |
validate
|
bool
|
if True, will validate the project card schema. Defaults to True. |
True
|
Source code in projectcard/io.py
read_cards(filepath, filter_tags=None, recursive=False, base_path=DEFAULT_BASE_PATH, existing_projects=None)
¶
Reads collection of project card files by inferring the file type.
Lowercases all keys, but then replaces any that need to be uppercased using the REPLACE_KEYS mapping. Needed to keep “A” and “B” uppercased.
If a path is given as a relative path, it will be resolved to an absolute path using the base_path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath
|
ProjectCardFilepaths
|
where the project card is. A single path, list of paths, a directory, or a glob pattern. |
required |
filter_tags
|
Optional[list[str]]
|
list of tags to filter by. |
None
|
recursive
|
bool
|
if True, will search recursively in subdirs. |
False
|
base_path
|
Path
|
base path to resolve relative paths from. Defaults to current working directory. |
DEFAULT_BASE_PATH
|
existing_projects
|
Optional[list[str]]
|
list of existing project names to check for uniqueness. |
None
|
Source code in projectcard/io.py
write_card(project_card, filename=None)
¶
Writes project card dictionary to YAML file.
Source code in projectcard/io.py
Validates ProjectCard JSON data against a JSON schema.
CRITICAL_ERRORS = ['E9', 'F821', 'F823', 'F405']
module-attribute
¶
Errors in Ruff that will cause a code execution failure. E9: Syntax errors. F821: Undefined name. F823: Local variable referenced before assignment. F405: Name may be undefined, or defined from star imports.
package_schema(schema_path=PROJECTCARD_SCHEMA, outfile_path=None)
¶
Consolidates referenced schemas into a single schema and writes it out.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema_path
|
Union[Path, str]
|
Schema to read int and package. Defaults to PROJECTCARD_SCHEMA which is ROOTDIR / “schema” / “projectcard.json”. |
PROJECTCARD_SCHEMA
|
outfile_path
|
Optional[Union[Path, str]]
|
Where to write out packaged schema. Defaults to schema_path.basepath.packaged.json |
None
|
Source code in projectcard/validate.py
update_dict_with_schema_defaults(data, schema=PROJECTCARD_SCHEMA)
¶
Recursively update missing required properties with default values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict
|
The data dictionary to update. |
required |
schema
|
Union[Path, dict]
|
The schema dictionary or path to the schema file. |
PROJECTCARD_SCHEMA
|
Returns:
Type | Description |
---|---|
dict
|
The updated data dictionary. |
Source code in projectcard/validate.py
validate_card(jsondata, schema_path=PROJECTCARD_SCHEMA, parse_defaults=True)
¶
Validates json-like data to specified schema.
If pycode
key exists, will evaluate it for basic runtime errors using Flake8.
Note: will not flag any invalid use of RoadwayNetwork or TransitNetwork APIs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
jsondata
|
dict
|
json-like data to validate. |
required |
schema_path
|
Path
|
path to schema to validate to. Defaults to PROJECTCARD_SCHEMA which is ROOTDIR / “schema” / “projectcard.json” |
PROJECTCARD_SCHEMA
|
parse_defaults
|
bool
|
if True, will use default values for missing required attributes. |
True
|
Raises:
Type | Description |
---|---|
ValidationError
|
If jsondata doesn’t conform to specified schema. |
SchemaError
|
If schema itself is not valid. |
Source code in projectcard/validate.py
validate_schema_file(schema_path=PROJECTCARD_SCHEMA)
¶
Validates that a schema file is a valid JSON-schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema_path
|
Path
|
description. Defaults to PROJECTCARD_SCHEMA which is ROOTDIR / “schema” / “projectcard.json”. |
PROJECTCARD_SCHEMA
|