示例#1
0
    def setUp(self):
        self.manifest = Manifest({
            '': Manifest({
                '': Manifest({
                    '': Program(name='root'),
                    'deep': Program(name="deep")
                }),
            }),
            'sub': {
                'prog': Program(name="prog"),
                'another': {
                    '': blank,
                    'prog2': Program(name='prog2'),
                    'prog3': Program(name='prog3'),
                    'http_only': Program(name='http_only', controllers=['http-get']),
                    'irc_only': Program(name='irc_only', controllers=['irc']),
                    'both': both
                },
                'double': [double_get, double_post]
            },
            'string_redirect': '/redirect',
            'redirect': Program(name='redirect'),
        })

        self.all_urls = set([
            '/', '/deep',
            '/sub/another/irc_only', '/sub/another/http_only', '/sub/another/both',
            '/sub/prog', '/sub/another', '/sub/another/prog2', '/sub/another/prog3',
            '/redirect', '/string_redirect', '/sub/double'
        ])

        self.irc_only_urls = set(['/sub/another/irc_only'])
        self.http_only_urls = set(['/sub/another/http_only', '/sub/double'])
示例#2
0
    def setUp(self):
        self.manifest = Manifest({
            '':
            Manifest({
                '':
                Manifest({
                    '': Program(name='root'),
                    'deep': Program(name="deep")
                }),
            }),
            'sub': {
                'prog': Program(name="prog"),
                'another': {
                    '':
                    blank,
                    'prog2':
                    Program(name='prog2'),
                    'prog3':
                    Program(name='prog3'),
                    'http_only':
                    Program(name='http_only', controllers=['http-get']),
                    'irc_only':
                    Program(name='irc_only', controllers=['irc']),
                    'both':
                    both
                },
                'double': [double_get, double_post]
            },
            'string_redirect':
            '/redirect',
            'redirect':
            Program(name='redirect'),
        })

        self.all_urls = set([
            '/', '/deep', '/sub/another/irc_only', '/sub/another/http_only',
            '/sub/another/both', '/sub/prog', '/sub/another',
            '/sub/another/prog2', '/sub/another/prog3', '/redirect',
            '/string_redirect', '/sub/double'
        ])

        self.irc_only_urls = set(['/sub/another/irc_only'])
        self.http_only_urls = set(['/sub/another/http_only', '/sub/double'])
示例#3
0
 def setUp(self):
     initialize()
     self.manifest = Manifest({
         '':
         Program(model=[none], view=BasicView()),
         'named':
         Program(model=[defaults], view=BasicView()),
         "another": {
             '': Program(name='another root'),
             'name': Program(name='another name', view=BasicView())
         }
     })
示例#4
0
 def setUp(self):
     initialize()
     self.manifest = Manifest({
         'no_defaults':
         Program(model=[no_defaults], view=BasicView()),
         'defaults':
         Program(model=[defaults], view=BasicView()),
         'primitives':
         Program(model=[primitive], view=BasicView()),
         'raw':
         Program(model=[raw], view=BasicView()),
         'none':
         Program(model=[none], view=BasicView),
         'order':
         Program(model=[order], view=BasicView)
     })
示例#5
0
class StackedRootTest(unittest.TestCase):
    def setUp(self):
        self.manifest = Manifest({
            '': Manifest({
                '': Manifest({
                    '': Program(name='root'),
                    'deep': Program(name="deep")
                }),
            }),
            'sub': {
                'prog': Program(name="prog"),
                'another': {
                    '': blank,
                    'prog2': Program(name='prog2'),
                    'prog3': Program(name='prog3'),
                    'http_only': Program(name='http_only', controllers=['http-get']),
                    'irc_only': Program(name='irc_only', controllers=['irc']),
                    'both': both
                },
                'double': [double_get, double_post]
            },
            'string_redirect': '/redirect',
            'redirect': Program(name='redirect'),
        })

        self.all_urls = set([
            '/', '/deep',
            '/sub/another/irc_only', '/sub/another/http_only', '/sub/another/both',
            '/sub/prog', '/sub/another', '/sub/another/prog2', '/sub/another/prog3',
            '/redirect', '/string_redirect', '/sub/double'
        ])

        self.irc_only_urls = set(['/sub/another/irc_only'])
        self.http_only_urls = set(['/sub/another/http_only', '/sub/double'])

    def test_get_all_urls(self):
        self.assertEquals(self.manifest.get_urls(), self.all_urls)

    def test_controllertag_filtering(self):
        """
        When a controller tag is added to the call to get_urls, the returned set
        of urls should be filtered by the controler tag defined in the manifest.
        """
        irc_urls = sorted(list(self.all_urls - self.http_only_urls))
        generated = sorted(list(self.manifest.get_urls(controllers=['irc'])))
        self.assertEquals(generated, irc_urls)

        http_urls = self.all_urls - self.irc_only_urls
        self.assertEquals(self.manifest.get_urls(controllers=['http-get']), http_urls)

    def test_get_program(self):
        name = self.manifest.get_program('/').name
        self.assertEquals(name, 'root')

    def test_parse_invocation_simple(self):
        parsed = self.manifest.parse_invocation('/sub/another/both', 'http-get')
        correct = {
            'args': [],
            'program': both,
            'superformat': None,
            'superformat_mime': None,
            'raw_args': '',
            'invocation': '/sub/another/both',
            'path': '/sub/another/',
            'program_name': 'both'
        }
        self.assertEquals(parsed, correct)

    def test_parse_invocation_root_arg(self):
        parsed = self.manifest.parse_invocation('/sub/another/aaaa', 'http-get')
        correct = {
            'args': ['aaaa'],
            'raw_args': 'aaaa',
            'program': blank,
            'superformat': None,
            'superformat_mime': None,
            'invocation': '/sub/another/aaaa',
            'path': '/sub/',
            'program_name': 'another'
        }
        self.assertEquals(parsed, correct)

    def test_parse_invocation_superformat(self):
        parsed = self.manifest.parse_invocation('/sub/another/both.html', 'http-get')
        correct = {
            'args': [],
            'raw_args': '',
            'program': both,
            'superformat': 'html',
            'superformat_mime': 'text/html',
            'invocation': '/sub/another/both.html',
            'path': '/sub/another/',
            'program_name': 'both'
        }
        self.assertEquals(parsed, correct)


    def test_parse_invocation_superformat_with_args(self):
        parsed = self.manifest.parse_invocation('/sub/another/both.html/aaaa/bbbbbb', 'http-get')
        correct = {
            'args': ['aaaa', 'bbbbbb'],
            'raw_args': 'aaaa/bbbbbb',
            'program': both,
            'superformat': 'html',
            'superformat_mime': 'text/html',
            'invocation': '/sub/another/both.html/aaaa/bbbbbb',
            'path': '/sub/another/',
            'program_name': 'both'
        }
        self.assertEquals(parsed, correct)

    def test_parse_invocation_double_controller(self):
        for controller_tag, program in [['http-get', double_get], ['http-post', double_post]]:
            parsed = self.manifest.parse_invocation('/sub/double', controller_tag)
            correct = {
                'args': [],
                'raw_args': '',
                'program': program,
                'superformat': None,
                'superformat_mime': None,
                'invocation': '/sub/double',
                'path': '/sub/',
                'program_name': 'double'
            }
            self.assertEquals(parsed, correct)

    def xtest_parse_invocation_invalid(self):
        #print self.manifest.parse_invocation('/sub/double', 'irc')
        #print self.manifest.get_urls('irc')
        self.assertRaises(
            ProgramNotFound, 
            lambda: self.manifest.parse_invocation('/sub/double', 'irc')
        )
示例#6
0
from giotto.programs import Program, Manifest
from giotto.programs.management import management_manifest
from giotto.views import BasicView

manifest = Manifest({
    '': Program(
        model=[lambda: "Welcome to Giotto!"],
        view=BasicView
    ),
    'mgt': management_manifest,
})
示例#7
0
def create_auth_manifest(**kwargs):
    """
    Creates a basic authentication manifest for logging in, logging out and
    registering new accounts.
    """
    class AuthProgram(Program):
        pre_input_middleware = [AuthenticationMiddleware]

    def register(username, password, password2):
        """
        Decorated version of basic_register with a callback added.
        """
        result = basic_register(username, password, password2)
        callback = kwargs.get('post_register_callback', None)
        if callback:
            user = User.objects.get(username=username)
            callback(user)
        return result

    return Manifest({
        'login': [
            AuthProgram(
                """
                Prints out the HTML form for logging in.
                """,
                name="Login (form)",
                input_middleware=[NotAuthenticatedOrRedirect('/')],
                view=BasicView(
                    html=jinja_template('login.html'),
                ),
            ),
            AuthProgram(
                """
                Matches up the username/password against the database, and adds the auth cookies.
                """,
                name="Login (post)",
                input_middleware=[NotAuthenticatedOrDie],
                controllers=['http-post', 'cmd'],
                model=[create_session, {'username': '******', 'session_key': 'XXXXXXXXXXXXXXX'}],
                view=BasicView(
                    persist=lambda m: {'giotto_session': m['session_key']},
                    html=lambda m: Redirection('/'),
                ),
            ),
        ],
        'logout': AuthProgram(
            """
            Send the user here to log them out. Removes their cookies and deletes the auth session.
            """,
            name="Logout",
            view=BasicView(
                html=Redirection('/'),
            ),
            output_middleware=[LogoutMiddleware],
        ),
        'register': [
            AuthProgram(
                """
                This program returns the HTML page with the form for registering a new account.
                HTTP-get only.
                """,
                name="Register (form)",
                input_middleware=[NotAuthenticatedOrRedirect('/')],
                view=BasicView(
                    html=jinja_template('register.html'),
                ),
            ),
            AuthProgram(
                """
                When you POST the register form, this program handles creating the new user, then redirecting you to '/'
                """,
                name="Register (post)",
                controllers=['http-post'],
                model=[register],
                view=BasicView(
                    persist=lambda m: {'giotto_session': m['session_key']},
                    html=lambda m: Redirection('/'),
                ),
            ),
        ],
    })
示例#8
0
from giotto.contrib.static.programs import StaticServe
from giotto.programs import Program, Manifest
from giotto.programs.management import management_manifest
from giotto.views import BasicView, jinja_template
from models import show_listings, CrawlResult, stats

manifest = Manifest({
    '':
    '/stats',
    'stats':
    Program(model=[stats],
            view=BasicView(html=jinja_template('landing.html'))),
    'api_docs':
    Program(view=BasicView(html=jinja_template('api.html'))),
    'listings':
    Program(model=[show_listings],
            view=BasicView(html=jinja_template("show_listings.html"))),
    'crawl':
    Program(
        controllers=['cmd'],
        model=[CrawlResult.do_crawl],
    ),
    'static':
    StaticServe('/static'),
    'mgt':
    management_manifest,
})
示例#9
0
class StackedRootTest(unittest.TestCase):
    def setUp(self):
        self.manifest = Manifest({
            '':
            Manifest({
                '':
                Manifest({
                    '': Program(name='root'),
                    'deep': Program(name="deep")
                }),
            }),
            'sub': {
                'prog': Program(name="prog"),
                'another': {
                    '':
                    blank,
                    'prog2':
                    Program(name='prog2'),
                    'prog3':
                    Program(name='prog3'),
                    'http_only':
                    Program(name='http_only', controllers=['http-get']),
                    'irc_only':
                    Program(name='irc_only', controllers=['irc']),
                    'both':
                    both
                },
                'double': [double_get, double_post]
            },
            'string_redirect':
            '/redirect',
            'redirect':
            Program(name='redirect'),
        })

        self.all_urls = set([
            '/', '/deep', '/sub/another/irc_only', '/sub/another/http_only',
            '/sub/another/both', '/sub/prog', '/sub/another',
            '/sub/another/prog2', '/sub/another/prog3', '/redirect',
            '/string_redirect', '/sub/double'
        ])

        self.irc_only_urls = set(['/sub/another/irc_only'])
        self.http_only_urls = set(['/sub/another/http_only', '/sub/double'])

    def test_get_all_urls(self):
        self.assertEquals(self.manifest.get_urls(), self.all_urls)

    def test_controllertag_filtering(self):
        """
        When a controller tag is added to the call to get_urls, the returned set
        of urls should be filtered by the controler tag defined in the manifest.
        """
        irc_urls = sorted(list(self.all_urls - self.http_only_urls))
        generated = sorted(list(self.manifest.get_urls(controllers=['irc'])))
        self.assertEquals(generated, irc_urls)

        http_urls = self.all_urls - self.irc_only_urls
        self.assertEquals(self.manifest.get_urls(controllers=['http-get']),
                          http_urls)

    def test_get_program(self):
        name = self.manifest.get_program('/').name
        self.assertEquals(name, 'root')

    def test_parse_invocation_simple(self):
        parsed = self.manifest.parse_invocation('/sub/another/both',
                                                'http-get')
        correct = {
            'args': [],
            'program': both,
            'superformat': None,
            'superformat_mime': None,
            'raw_args': '',
            'invocation': '/sub/another/both',
            'path': '/sub/another/',
            'program_name': 'both'
        }
        self.assertEquals(parsed, correct)

    def test_parse_invocation_root_arg(self):
        parsed = self.manifest.parse_invocation('/sub/another/aaaa',
                                                'http-get')
        correct = {
            'args': ['aaaa'],
            'raw_args': 'aaaa',
            'program': blank,
            'superformat': None,
            'superformat_mime': None,
            'invocation': '/sub/another/aaaa',
            'path': '/sub/',
            'program_name': 'another'
        }
        self.assertEquals(parsed, correct)

    def test_parse_invocation_superformat(self):
        parsed = self.manifest.parse_invocation('/sub/another/both.html',
                                                'http-get')
        correct = {
            'args': [],
            'raw_args': '',
            'program': both,
            'superformat': 'html',
            'superformat_mime': 'text/html',
            'invocation': '/sub/another/both.html',
            'path': '/sub/another/',
            'program_name': 'both'
        }
        self.assertEquals(parsed, correct)

    def test_parse_invocation_superformat_with_args(self):
        parsed = self.manifest.parse_invocation(
            '/sub/another/both.html/aaaa/bbbbbb', 'http-get')
        correct = {
            'args': ['aaaa', 'bbbbbb'],
            'raw_args': 'aaaa/bbbbbb',
            'program': both,
            'superformat': 'html',
            'superformat_mime': 'text/html',
            'invocation': '/sub/another/both.html/aaaa/bbbbbb',
            'path': '/sub/another/',
            'program_name': 'both'
        }
        self.assertEquals(parsed, correct)

    def test_parse_invocation_double_controller(self):
        for controller_tag, program in [['http-get', double_get],
                                        ['http-post', double_post]]:
            parsed = self.manifest.parse_invocation('/sub/double',
                                                    controller_tag)
            correct = {
                'args': [],
                'raw_args': '',
                'program': program,
                'superformat': None,
                'superformat_mime': None,
                'invocation': '/sub/double',
                'path': '/sub/',
                'program_name': 'double'
            }
            self.assertEquals(parsed, correct)

    def xtest_parse_invocation_invalid(self):
        #print self.manifest.parse_invocation('/sub/double', 'irc')
        #print self.manifest.get_urls('irc')
        self.assertRaises(
            ProgramNotFound,
            lambda: self.manifest.parse_invocation('/sub/double', 'irc'))
示例#10
0
from giotto.programs import Program, Manifest
from giotto.programs.shell import shell
from giotto.programs.tables import syncdb, flush
from giotto.views import BasicView

management_manifest = Manifest({
    'syncdb':
    Program(name="Make Tables",
            controllers=['cmd'],
            model=[syncdb],
            view=BasicView()),
    'flush':
    Program(
        name="Blast Tables",
        controllers=['cmd'],
        model=[flush],
        view=BasicView(),
    ),
    'shell':
    Program(
        name="Giotto Shell",
        controllers=['cmd'],
        model=[shell],
        view=BasicView(),
    ),
})