# Path: wiremaps/web/json.py
# class JsonPage(rend.Page):
# 
#     flattenFactory = lambda self, *args: flat.flattenFactory(*args)
#     addSlash = True
# 
#     def renderHTTP(self, ctx):
#         request = inevow.IRequest(ctx)
#         if inevow.ICurrentSegments(ctx)[-1] != '':
#             request.redirect(request.URLPath().child(''))
#             return ''
#         request.setHeader("Content-Type",
#                           "application/json; charset=UTF-8")
#         d = defer.maybeDeferred(self.data_json, ctx, None)
#         d.addCallback(lambda x: self.render_json(ctx, x))
#         return d
# 
#     def render_json(self, ctx, data):
#         """Render the given data in a proper JSON string"""
# 
#         def sanitize(data, d=None):
#             """Nevow JSON serializer is not able to handle some types.
# 
#             We convert those types in proper types:
#              - string to unicode string
#              - PgSQL result set into list
#              - handling of deferreds
#             """
#             if type(data) in [list, tuple] or \
#                     (PgSQL and isinstance(data, PgSQL.PgResultSet)):
#                 return [sanitize(x, d) for x in data]
#             if PgSQL and isinstance(data, PgSQL.PgBooleanType):
#                 if data:
#                     return u"true"
#                 return u"false"
#             if type(data) == str:
#                 return unicode(data, errors='ignore')
#             if isinstance(data, rend.Fragment):
#                 io = StringIO()
#                 writer = io.write
#                 finisher = lambda result: io.getvalue()
#                 newctx = context.PageContext(parent=ctx, tag=data)
#                 data.rememberStuff(newctx)
#                 doc = data.docFactory.load()
#                 newctx = context.WovenContext(newctx, T.invisible[doc])
#                 fl = self.flattenFactory(doc, newctx, writer, finisher)
#                 fl.addCallback(sanitize, None)
#                 d.append(fl)
#                 return fl
#             if isinstance(data, defer.Deferred):
#                 if data.called:
#                     return sanitize(data.result)
#                 return data
#             if isinstance(data, failure.Failure):
#                 return unicode(
#                     "<span class='error'>An error occured (%s)</span>" % data.getErrorMessage(),
#                     errors='ignore')
#             return data
# 
#         def serialize(data):
#             return json.serialize(sanitize(data))
# 
#         d = []
#         data = sanitize(data, d)
#         d = defer.DeferredList(d)
#         d.addCallback(lambda x: serialize(data))
#         return d


import re
from nevow import rend, tags as T, loaders
from wiremaps.web.json import JsonPage

class CompleteResource(rend.Page):

    addSlash = True
    docFactory = loaders.stan(T.html [ T.body [ T.p [ "Nothing here" ] ] ])

    def __init__(self, dbpool):
        self.dbpool = dbpool
        rend.Page.__init__(self)

    MACSTART = re.compile("^(?:[0-9A-Fa-f]){1,2}:")
    IPSTART = re.compile("^(?:[0-9]){1,3}\.")

    def childFactory(self, ctx, name):
        """Dispatch to the correct completer.

        If the search term is less than 3 characters, then we return
        an empty set. Otherwise:
         - it can be a MAC (two digits, a double colon)
         - it can be an IP (digits and dots)
         - it can be an equipment name
        """
        if len(name) < 3:
            return CompleteEmptyResource()
        if self.MACSTART.match(name):
            return CompleteMacResource(self.dbpool, name)
        if self.IPSTART.match(name):
            return CompleteIpResource(self.dbpool, name)
        return CompleteEquipmentResource(self.dbpool, name)


# Path: wiremaps/web/json.py
# class JsonPage(rend.Page):
# 
#     flattenFactory = lambda self, *args: flat.flattenFactory(*args)
#     addSlash = True
# 
#     def renderHTTP(self, ctx):
#         request = inevow.IRequest(ctx)
#         if inevow.ICurrentSegments(ctx)[-1] != '':
#             request.redirect(request.URLPath().child(''))
#             return ''
#         request.setHeader("Content-Type",
#                           "application/json; charset=UTF-8")
#         d = defer.maybeDeferred(self.data_json, ctx, None)
#         d.addCallback(lambda x: self.render_json(ctx, x))
#         return d
# 
#     def render_json(self, ctx, data):
#         """Render the given data in a proper JSON string"""
# 
#         def sanitize(data, d=None):
#             """Nevow JSON serializer is not able to handle some types.
# 
#             We convert those types in proper types:
#              - string to unicode string
#              - PgSQL result set into list
#              - handling of deferreds
#             """
#             if type(data) in [list, tuple] or \
#                     (PgSQL and isinstance(data, PgSQL.PgResultSet)):
#                 return [sanitize(x, d) for x in data]
#             if PgSQL and isinstance(data, PgSQL.PgBooleanType):
#                 if data:
#                     return u"true"
#                 return u"false"
#             if type(data) == str:
#                 return unicode(data, errors='ignore')
#             if isinstance(data, rend.Fragment):
#                 io = StringIO()
#                 writer = io.write
#                 finisher = lambda result: io.getvalue()
#                 newctx = context.PageContext(parent=ctx, tag=data)
#                 data.rememberStuff(newctx)
#                 doc = data.docFactory.load()
#                 newctx = context.WovenContext(newctx, T.invisible[doc])
#                 fl = self.flattenFactory(doc, newctx, writer, finisher)
#                 fl.addCallback(sanitize, None)
#                 d.append(fl)
#                 return fl
#             if isinstance(data, defer.Deferred):
#                 if data.called:
#                     return sanitize(data.result)
#                 return data
#             if isinstance(data, failure.Failure):
#                 return unicode(
#                     "<span class='error'>An error occured (%s)</span>" % data.getErrorMessage(),
#                     errors='ignore')
#             return data
# 
#         def serialize(data):
#             return json.serialize(sanitize(data))
# 
#         d = []
#         data = sanitize(data, d)
#         d = defer.DeferredList(d)
#         d.addCallback(lambda x: serialize(data))
#         return d


# Path: wiremaps/web/complete.py
import re
from nevow import rend, tags as T, loaders
from wiremaps.web.json import JsonPage


class CompleteResource(rend.Page):

    addSlash = True
    docFactory = loaders.stan(T.html [ T.body [ T.p [ "Nothing here" ] ] ])

    def __init__(self, dbpool):
        self.dbpool = dbpool
        rend.Page.__init__(self)

    MACSTART = re.compile("^(?:[0-9A-Fa-f]){1,2}:")
    IPSTART = re.compile("^(?:[0-9]){1,3}\.")

    def childFactory(self, ctx, name):
        """Dispatch to the correct completer.

        If the search term is less than 3 characters, then we return
        an empty set. Otherwise:
         - it can be a MAC (two digits, a double colon)
         - it can be an IP (digits and dots)
         - it can be an equipment name
        """
        if len(name) < 3:
            return CompleteEmptyResource()
        if self.MACSTART.match(name):
            return CompleteMacResource(self.dbpool, name)
        if self.IPSTART.match(name):
            return CompleteIpResource(self.dbpool, name)
        return CompleteEquipmentResource(self.dbpool, name)


class CompleteEmptyResource(JsonPage):