Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
f6637b7f
Commit
f6637b7f
authored
Oct 27, 2012
by
Victor Shnayder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a registry that keeps track of tag->renderer/input mappings
parent
2fab97c1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
0 deletions
+49
-0
common/lib/capa/capa/registry.py
+49
-0
No files found.
common/lib/capa/capa/registry.py
0 → 100644
View file @
f6637b7f
class
TagRegistry
(
object
):
"""
A registry mapping tags to handlers.
(A dictionary with some extra error checking.)
"""
def
__init__
(
self
):
self
.
_mapping
=
{}
def
register
(
self
,
cls
):
"""
Register cls as a supported tag type. It is expected to define cls.tags as a list of tags
that it implements.
If an already-registered type has registered one of those tags, will raise ValueError.
If there are no tags in cls.tags, will also raise ValueError.
"""
# Do all checks and complain before changing any state.
if
len
(
cls
.
tags
)
==
0
:
raise
ValueError
(
"No tags specified for class {0}"
.
format
(
cls
.
__name__
))
for
t
in
cls
.
tags
:
if
t
in
self
.
_mapping
:
other_cls
=
self
.
_mapping
[
t
]
if
cls
==
other_cls
:
# registering the same class multiple times seems silly, but ok
continue
raise
ValueError
(
"Tag {0} already registered by class {1}."
" Can't register for class {2}"
.
format
(
t
,
other_cls
.
__name__
,
cls
.
__name__
))
# Ok, should be good to change state now.
for
t
in
cls
.
tags
:
self
.
_mapping
[
t
]
=
cls
def
registered_tags
(
self
):
"""
Get a list of all the tags that have been registered.
"""
return
self
.
_mapping
.
keys
()
def
get_class_for_tag
(
self
,
tag
):
"""
For any tag in registered_tags(), returns the corresponding class. Otherwise, will raise
KeyError.
"""
return
self
.
_mapping
[
tag
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment