Commits

Kirill Simonov committed 7b75bb0

Allow act() to accept a string or a syntax node in place of a command.
Allow emit() to accept a content type string in place of a format.

Comments (0)

Files changed (11)

src/htsql/core/cmd/act.py

 from .command import Command, UniversalCmd, DefaultCmd, FormatCmd, FetchCmd
 from .summon import recognize
 from ..syn.parse import parse
-from ..fmt.format import emit, emit_headers
+from ..syn.syntax import Syntax
+from ..fmt.emit import emit, emit_headers
 from ..fmt.accept import accept
 
 
         self.parameters = parameters
 
 
-
-
 class RenderAction(Action):
 
     def __init__(self, environ):
     adapt(UniversalCmd, Action)
 
     def __call__(self):
-        syntax = parse(self.command.query)
-        command = recognize(syntax)
-        return act(command, self.action)
+        return act(self.command.query, self.action)
 
 
 class ActDefault(Act):
         return (status, headers, body)
 
 
-act = Act.__invoke__
+def act(command, action):
+    assert isinstance(command, (Command, Syntax, unicode, str))
+    assert isinstance(action, Action)
+    if not isinstance(command, Command):
+        command = recognize(command)
+    return Act.__invoke__(command, action)
 
 
 def produce(command, parameters=None):

src/htsql/core/cmd/summon.py

 from ..util import to_name
 from ..syn.syntax import (Syntax, SkipSyntax, FunctionSyntax, PipeSyntax,
         ApplySyntax, CollectSyntax)
+from ..syn.parse import parse
 from ..fmt.format import (TextFormat, HTMLFormat, RawFormat, JSONFormat,
         CSVFormat, TSVFormat, XMLFormat)
 from .command import SkipCmd, FetchCmd, FormatCmd, SQLCmd, DefaultCmd
 
 
 def recognize(syntax):
+    assert isinstance(syntax, (Syntax, unicode, str))
+    if not isinstance(syntax, Syntax):
+        syntax = parse(syntax)
     command = Recognize.__invoke__(syntax)
     if command is None:
         command = DefaultCmd(syntax)

src/htsql/core/fmt/__init__.py

 """
 
 
-from . import accept, format, html, json, tabular, text, xml
+from . import accept, emit, format, html, json, tabular, text, xml
 
 

src/htsql/core/fmt/accept.py

 #
 
 
-from ..adapter import Protocol, call, adapt
-from .format import (HTMLFormat, RawFormat, JSONFormat, CSVFormat, TSVFormat,
-                     XMLFormat, ProxyFormat, TextFormat, Emit, EmitHeaders,
-                     emit, emit_headers)
+from ..adapter import Protocol, call
+from .format import (DefaultFormat, HTMLFormat, RawFormat, JSONFormat,
+        CSVFormat, TSVFormat, XMLFormat, ProxyFormat, TextFormat)
 
 
 class Accept(Protocol):
 
-    format = TextFormat
+    format = DefaultFormat
 
     def __init__(self, content_type):
         self.content_type = content_type
     format = TextFormat
 
 
-class EmitProxyHeaders(EmitHeaders):
-
-    adapt(ProxyFormat)
-
-    def __call__(self):
-        for header in emit_headers(self.format.format, self.product):
-            yield header
-        yield ('Vary', 'Accept')
-
-
-class EmitProxy(Emit):
-
-    adapt(ProxyFormat)
-
-    def __call__(self):
-        return emit(self.format.format, self.product)
-
-
 def accept(environ):
     content_type = ""
     if 'HTTP_ACCEPT' in environ:

src/htsql/core/fmt/emit.py

+#
+# Copyright (c) 2006-2012, Prometheus Research, LLC
+#
+
+
+from ..adapter import Adapter, adapt
+from .format import Format, DefaultFormat, TextFormat, ProxyFormat
+from .accept import Accept
+
+
+class EmitHeaders(Adapter):
+
+    adapt(Format)
+
+    def __init__(self, format, product):
+        self.format = format
+        self.product = product
+        self.meta = product.meta
+        self.data = product.data
+
+    def __call__(self):
+        raise NotImplementedError()
+
+
+class Emit(Adapter):
+
+    adapt(Format)
+
+    def __init__(self, format, product):
+        self.format = format
+        self.product = product
+        self.meta = product.meta
+        self.data = product.data
+
+    def __call__(self):
+        raise NotImplementedError()
+
+
+class EmitDefaultHeaders(EmitHeaders):
+
+    adapt(DefaultFormat)
+
+    def __call__(self):
+        format = TextFormat()
+        return emit_headers(format, self.product)
+
+
+class EmitDefault(Emit):
+
+    adapt(DefaultFormat)
+
+    def __call__(self):
+        format = TextFormat()
+        return emit(format, self.product)
+
+
+class EmitProxyHeaders(EmitHeaders):
+
+    adapt(ProxyFormat)
+
+    def __call__(self):
+        for header in emit_headers(self.format.format, self.product):
+            yield header
+        yield ('Vary', 'Accept')
+
+
+class EmitProxy(Emit):
+
+    adapt(ProxyFormat)
+
+    def __call__(self):
+        return emit(self.format.format, self.product)
+
+
+def emit_headers(format, product):
+    if isinstance(format, (str, unicode)):
+        format = Accept.__invoke__(format)
+        assert not isinstance(format, DefaultFormat), "unknown format"
+    return list(EmitHeaders.__invoke__(format, product))
+
+
+def emit(format, headers):
+    if isinstance(format, (str, unicode)):
+        format = Accept.__invoke__(format)
+        assert not isinstance(format, DefaultFormat), "unknown format"
+    for line in Emit.__invoke__(format, headers):
+        if isinstance(line, unicode):
+            line = line.encode('utf-8')
+        yield line
+
+

src/htsql/core/fmt/format.py

 #
 
 
-"""
-:mod:`htsql.core.fmt.format`
-============================
-
-This module implements the format adapter.
-"""
-
-
 from ..util import setof
 from ..adapter import Adapter, Utility, adapt
 from ..domain import Domain, ListDomain, RecordDomain, Profile
     pass
 
 
+class DefaultFormat(Format):
+    pass
+
+
 class RawFormat(Format):
     pass
 
         self.format = format
 
 
-class EmitHeaders(Adapter):
-
-    adapt(Format)
-
-    def __init__(self, format, product):
-        self.format = format
-        self.product = product
-        self.meta = product.meta
-        self.data = product.data
-
-    def __call__(self):
-        raise NotImplementedError()
-
-
-class Emit(Adapter):
-
-    adapt(Format)
-
-    def __init__(self, format, product):
-        self.format = format
-        self.product = product
-        self.meta = product.meta
-        self.data = product.data
-
-    def __call__(self):
-        raise NotImplementedError()
-
-
-def emit_headers(format, product):
-    return list(EmitHeaders.__invoke__(format, product))
-
-
-def emit(format, headers):
-    for line in Emit.__invoke__(format, headers):
-        if isinstance(line, unicode):
-            line = line.encode('utf-8')
-        yield line
-
-

src/htsql/core/fmt/html.py

 
 from ..util import listof, tupleof, maybe, Printable
 from ..adapter import Adapter, adapt, adapt_many
-from .format import HTMLFormat, EmitHeaders, Emit
+from .format import HTMLFormat
+from .emit import EmitHeaders, Emit
 from ..error import InternalServerError, Mark
 from ..domain import (Domain, BooleanDomain, NumberDomain, DecimalDomain,
         StringDomain, EnumDomain, DateDomain, TimeDomain, DateTimeDomain,

src/htsql/core/fmt/json.py

         StringDomain, EnumDomain, DateDomain, TimeDomain, DateTimeDomain,
         ListDomain, RecordDomain, IdentityDomain, UntypedDomain, VoidDomain,
         OpaqueDomain, Profile)
-from .format import RawFormat, JSONFormat, EmitHeaders, Emit
+from .format import RawFormat, JSONFormat
+from .emit import EmitHeaders, Emit
 import re
 import math
 import decimal

src/htsql/core/fmt/tabular.py

 
 from ..util import listof
 from ..adapter import Adapter, adapt, adapt_many
-from .format import Emit, EmitHeaders, CSVFormat
+from .format import CSVFormat
+from .emit import EmitHeaders, Emit
 from ..domain import (Domain, BooleanDomain, NumberDomain, FloatDomain,
         DecimalDomain, StringDomain, EnumDomain, DateDomain, TimeDomain,
         DateTimeDomain, ListDomain, RecordDomain, UntypedDomain, VoidDomain,

src/htsql/core/fmt/text.py

 from ..adapter import Adapter, adapt, adapt_many
 from ..util import maybe, oneof
 from ..context import context
-from .format import TextFormat, EmitHeaders, Emit
+from .format import TextFormat
+from .emit import EmitHeaders, Emit
 from ..domain import (Domain, BooleanDomain, NumberDomain, IntegerDomain,
         DecimalDomain, FloatDomain, StringDomain, EnumDomain, DateDomain,
         TimeDomain, DateTimeDomain, ListDomain, RecordDomain, UntypedDomain,

src/htsql/core/fmt/xml.py

         StringDomain, EnumDomain, DateDomain, TimeDomain, DateTimeDomain,
         ListDomain, RecordDomain, UntypedDomain, VoidDomain, OpaqueDomain,
         Profile)
-from .format import XMLFormat, EmitHeaders, Emit
+from .format import XMLFormat
+from .emit import EmitHeaders, Emit
 import re
 import decimal
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.