Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
Slightly refactor httpolice.known
Browse files Browse the repository at this point in the history
  • Loading branch information
vfaronov committed Jul 30, 2016
1 parent 5deffee commit 4cee846
Show file tree
Hide file tree
Showing 19 changed files with 32 additions and 48 deletions.
28 changes: 10 additions & 18 deletions httpolice/known/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from httpolice import structure
from httpolice.known.alt_svc_param import known as altsvc
from httpolice.known.auth_scheme import known as auth
from httpolice.known.base import KnownDict
from httpolice.known.cache_directive import known as cache
from httpolice.known.content_coding import known as cc
from httpolice.known.header import known as h
Expand All @@ -17,27 +18,18 @@
from httpolice.known.upgrade_token import known as upgrade
from httpolice.known.warn_code import known as warn

classes = {
structure.AltSvcParam: altsvc,
structure.AuthScheme: auth,
structure.CacheDirective: cache,
structure.ContentCoding: cc,
structure.FieldName: h,
structure.HSTSDirective: hsts,
structure.MediaType: media,
structure.Method: m,
structure.ProductName: prod,
structure.RangeUnit: unit,
structure.RelationType: rel,
structure.StatusCode: st,
structure.TransferCoding: tc,
structure.UpgradeToken: upgrade,
structure.WarnCode: warn,
}

def _collect():
gs = globals()
return {gs[name].cls: (gs[name], name)
for name in gs
if isinstance(gs[name], KnownDict)}

classes = _collect() # This dict contains items like: ``Method: (m, 'm')``


def get_info(obj):
for cls, known in classes.items():
for cls, (known, _) in classes.items():
if isinstance(obj, cls):
return known.get_info(obj)

Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/alt_svc_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def parser_for(name):
return known.get_info(name).get('parser')


known = KnownDict([
known = KnownDict(AltSvcParam, [
{'_': AltSvcParam(u'ma'),
'_citations': [RFC(7838, section=(3, 1))],
'parser': rfc7838.ma},
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/auth_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from httpolice.structure import AuthScheme


known = KnownDict([
known = KnownDict(AuthScheme, [
{'_': AuthScheme(u'Basic'), '_citations': [RFC(7617)]},
{'_': AuthScheme(u'Bearer'), '_citations': [RFC(6750)]},
{'_': AuthScheme(u'Digest'), '_citations': [RFC(7616)]},
Expand Down
3 changes: 2 additions & 1 deletion httpolice/known/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

class KnownDict(object):

def __init__(self, items, extra_info=None, name_from_title=False):
def __init__(self, cls, items, extra_info=None, name_from_title=False):
self.cls = cls
allowed_info = set(['_', '_citations', '_no_sync', '_title'] +
(extra_info or []))
self._name_from_title = name_from_title
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/cache_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def parser_for(name):
# refers to a request directive or a response directive,
# so we have to use one, more general citation.

known = KnownDict([
known = KnownDict(CacheDirective, [
{'_': CacheDirective(u'max-age'),
'_citations': [RFC(7234, section=(5, 2))],
'_no_sync': ['_citations'],
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/content_coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from httpolice.structure import ContentCoding


known = KnownDict([
known = KnownDict(ContentCoding, [
{'_': ContentCoding(u'compress'),
'_citations': [RFC(7230, section=(4, 2, 1))]},
{'_': ContentCoding(u'deflate'),
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def parser_for(name):
return known.get_info(name).get('parser')


known = KnownDict([
known = KnownDict(FieldName, [
{'_': FieldName(u'A-IM'), '_citations': [RFC(4229)]},
{'_': FieldName(u'Accept'),
'_citations': [RFC(7231, section=(5, 3, 2))],
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/hsts_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def parser_for(name):
return known.get_info(name).get('parser')


known = KnownDict([
known = KnownDict(HSTSDirective, [
{'_': HSTSDirective(u'includeSubDomains'),
'_citations': [RFC(6797, section=(6, 1, 2))],
'argument': NO},
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/media_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def is_patch(name):
return known.get_info(name).get('patch')


known = KnownDict([
known = KnownDict(MediaType, [
{'_': MediaType(u'application/1d-interleaved-parityfec'),
'_citations': [RFC(6015)]},
{'_': MediaType(u'application/alto-costmap+json'), '_citations': [RFC(7285)]},
Expand Down
3 changes: 3 additions & 0 deletions httpolice/known/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def is_safe(name):

class KnownMethods(KnownDict):

def __init__(self, *args, **kwargs):
super(KnownMethods, self).__init__(Method, *args, **kwargs)

@classmethod
def _name_for(cls, item):
return item['_'].replace(u'-', u'_')
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def is_library(name):
return known.get_info(name).get('library')


known = KnownDict([
known = KnownDict(ProductName, [
{'_': ProductName(u'android-async-http'),
'library': True},
{'_': ProductName(u'Apache-HttpAsyncClient'),
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/range_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from httpolice.structure import RangeUnit


known = KnownDict([
known = KnownDict(RangeUnit, [
{'_': RangeUnit(u'bytes'), '_citations': [RFC(7233, section=(2, 1))]},
{'_': RangeUnit(u'none'), '_no_sync': True},
])
2 changes: 1 addition & 1 deletion httpolice/known/relation_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from httpolice.structure import RelationType


known = KnownDict([
known = KnownDict(RelationType, [
{'_': RelationType(u'about'), '_citations': [RFC(6903, section=(2,))]},
{'_': RelationType(u'alternate'),
'_citations': [Citation(None,
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/status_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def is_cacheable(code):
return known.get_info(code).get('cacheable')


known = KnownDict([
known = KnownDict(StatusCode, [
{'_': StatusCode(100),
'_citations': [RFC(7231, section=(6, 2, 1))],
'_title': u'Continue',
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/transfer_coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from httpolice.structure import TransferCoding


known = KnownDict([
known = KnownDict(TransferCoding, [
{'_': TransferCoding(u'chunked'),
'_citations': [RFC(7230, section=(4, 1))]},
{'_': TransferCoding(u'compress'),
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/upgrade_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from httpolice.structure import UpgradeToken


known = KnownDict([
known = KnownDict(UpgradeToken, [
{'_': UpgradeToken(u'HTTP'),
'_citations': [RFC(7230, section=(2, 6))],
'_title': u'Hypertext Transfer Protocol'},
Expand Down
2 changes: 1 addition & 1 deletion httpolice/known/warn_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from httpolice.structure import WarnCode


known = KnownDict([
known = KnownDict(WarnCode, [
{'_': WarnCode(110),
'_citations': [RFC(7234, section=(5, 5, 1))],
'_title': u'Response is Stale'},
Expand Down
16 changes: 2 additions & 14 deletions httpolice/notice.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import lxml.etree
import six

from httpolice import citation, structure
from httpolice import citation, known
from httpolice.util.ordered_enum import OrderedEnum


Expand All @@ -42,19 +42,7 @@ class Severity(OrderedEnum):
debug = 0


known_map = {
'auth': structure.AuthScheme,
'cache': structure.CacheDirective,
'cc': structure.ContentCoding,
'h': structure.FieldName,
'hsts': structure.HSTSDirective,
'm': structure.Method,
'media': structure.MediaType,
'rel': structure.RelationType,
'st': structure.StatusCode,
'tc': structure.TransferCoding,
'warn': structure.WarnCode,
}
known_map = {name: cls for (cls, (_, name)) in known.classes.items()}


# pylint: disable=property-on-old-class
Expand Down
2 changes: 1 addition & 1 deletion tools/iana.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def main():
exit_status = 0
for reg in Registry.__subclasses__():
for cls, entries in reg().get_all():
here = httpolice.known.classes[cls]
(here, _) = httpolice.known.classes[cls]
there = {entry['_']: entry for entry in entries}
missing, mismatch = make_diff(here, there)
for title, updates in [('missing', missing),
Expand Down

0 comments on commit 4cee846

Please sign in to comment.