<commit_before># test_sagews.py
# basic tests of sage worksheet using TCP protocol with sage_server
import socket
import conftest
import os
import re

from textwrap import dedent

class TestErr:
    def test_non_ascii(self, test_id, sagews):
        # assign x to hbar to trigger non-ascii warning
        code = ("x = " + unichr(295) + "\nx").encode('utf-8')
        m = conftest.message.execute_code(code = code, id = test_id)
        sagews.send_json(m)
        # expect 3 messages from worksheet client, including final done:true
        # 1 stderr Error in lines 1-1
        typ, mesg = sagews.recv()
        assert typ == 'json'
        assert mesg['id'] == test_id
        assert 'stderr' in mesg
        assert 'Error in lines 1-1' in mesg['stderr']
        # 2 stderr WARNING: Code contains non-ascii characters
        typ, mesg = sagews.recv()
        assert typ == 'json'
        assert mesg['id'] == test_id
        assert 'stderr' in mesg
        assert 'WARNING: Code contains non-ascii characters' in mesg['stderr']
        # 3 done
        conftest.recv_til_done(sagews, test_id)

class TestLinearAlgebra:
    def test_solve_right(self, exec2):
        code = dedent(r"""
        A=matrix([[1,2,6],[1,2,0],[1,-2,3]])
        b=vector([1,-1,1])
        A.solve_right(b)""")
        exec2(code,"(-1/2, -1/4, 1/3)\n")

    def test_kernel(self, exec2):
        code = dedent(r"""
        A=matrix([[1,2,3],[1,2,3],[1,2,3]])
        kernel(A)""")
        pat = "\[ 1  0 -1\]\n\[ 0  1 -1\]"
        exec2(code, pattern = pat)

    def test_charpoly(self, exec2):
        code = dedent(r"""
        A=matrix([[1,2,3],[1,2,3],[1,2,3]])
        A.charpoly()""")
        exec2(code, "x^3 - 6*x^2\n")

    def test_eigenvalues(self, exec2):
        code = dedent(r"""
        A=matrix([[1,2,3],[1,2,3],[1,2,3]])
        A=matrix([[1,2,3],[1,2,3],[1,2,3]])
        A.eigenvalues()""")
        exec2(code, "[6, 0, 0]\n")

class TestBasic:
    def test_connection_type(self, sagews):
        print("type %s"%type(sagews))
        assert isinstance(sagews, conftest.ConnectionJSON)
        return

    def test_set_file_env(self, exec2):
        code = "os.chdir(salvus.data[\'path\']);__file__=salvus.data[\'file\']"
        exec2(code)

    def test_sage_assignment(self, exec2):
        code = "x = 42\nx\n"
        output = "42\n"
        exec2(code, output)

    def test_issue70(self, exec2):
        code = dedent(r"""
        for i in range(1):
            pass
        'x'
        """)
        output = dedent(r"""
        'x'
        """).lstrip()
        exec2(code, output)

    def test_issue819(self, exec2):
        code = dedent(r"""
        def never_called(a):
            print 'should not execute 1', a
            # comment
        # comment at indent 0
            print 'should not execute 2', a
        22
        """)
        output = "22\n"
        exec2(code, output)

    def test_search_doc(self, exec2):
        code = "search_doc('laurent')"
        html = "https://www.google.com/search\?q=site%3Adoc.sagemath.org\+laurent\&oq=site%3Adoc.sagemath.org"
        exec2(code, html_pattern = html)

    def test_show_doc(self, test_id, sagews):
        # issue 476
        code = "show?"
        patn = "import smc_sagews.graphics\nsmc_sagews.graphics.graph_to_d3_jsonable?"
        m = conftest.message.execute_code(code = code, id = test_id)
        sagews.send_json(m)
        typ, mesg = sagews.recv()
        assert typ == 'json'
        assert mesg['id'] == test_id
        assert 'code' in mesg
        assert 'source' in mesg['code']
        assert re.sub('\s+','',patn) in re.sub('\s+','',mesg['code']['source'])
        conftest.recv_til_done(sagews, test_id)

    # https://github.com/sagemathinc/smc/issues/1107
    def test_sage_underscore_1(self, exec2):
        exec2("2/5","2/5\n")
    def test_sage_underscore_2(self, exec2):
        exec2("_","2/5\n")

    # https://github.com/sagemathinc/smc/issues/978
    def test_mode_comments_1(self, exec2):
        exec2(dedent("""
        def f(s):
            print "s='%s'"%s"""))
    def test_mode_comments_2(self, exec2):
        exec2(dedent("""
        %f
        123
        # foo
        456"""), dedent("""
        s='123
        # foo
        456'
        """).lstrip())

    def test_block_parser(self, exec2):
        """
        .. NOTE::

            This function supplies a list of expected outputs to `exec2`.
        """
        exec2(dedent("""
        pi.n().round()
        [x for x in [1,2,3] if x<3]
        for z in ['a','b']:
            z
        else:
            z"""), ["3\n","[1, 2]\n","'a'\n'b'\n'b'\n"])

class TestIntrospect:
    # test names end with SMC issue number
    def test_sage_autocomplete_1188(self, execintrospect):
        execintrospect('2016.fa', ["ctor","ctorial"], "fa")
    def test_sage_autocomplete_295_setup(self, exec2):
        exec2("aaa=Rings()._super_categories_for_classes;len(aaa[0].axioms())","6\n")
    def test_sage_autocomplete_295a(self, execintrospect):
        execintrospect('for a in aa', ["a"], "aa")
    def test_sage_autocomplete_295b(self, execintrospect):
        execintrospect('3 * aa', ["a"], "aa")
    def test_sage_autocomplete_701_setup(self, exec2):
        exec2(dedent("""
        class Xyz:
            numerical_attribute = 42
        x1 = Xyz()
        x1.numerical_attribute.next_prime()"""),"43\n")
    def test_sage_autocomplete_701a(self, execintrospect):
        execintrospect('3 / x1.nu', ["merical_attribute"], "nu")
    def test_sage_autocomplete_701b(self, execintrospect):
        execintrospect('aa', ["a"], "aa")
    def test_sage_autocomplete_701c(self, execintrospect):
        execintrospect('[aa', ["a"], "aa")
    def test_sage_autocomplete_701d(self, execintrospect):
        execintrospect('( aa', ["a"], "aa")
    def test_sage_autocomplete_734a(self, execintrospect):
        f = '*_factors'
        execintrospect(f, ["cunningham_prime_factors", "prime_factors"], f)
    def test_sage_autocomplete_734b(self, execintrospect):
        f = '*le_pr*'
        execintrospect(f, ["next_probable_prime"], f)
    def test_sage_autocomplete_734c(self, execintrospect):
        execintrospect('list.re*e', ["remove", "reverse"], 're*e')
    def test_sage_autocomplete_1225a(self, execintrospect):
        execintrospect('z = 12.5 * units.len', ["gth"], 'len')
    def test_sage_autocomplete_1225b_setup(self, exec2):
        exec2(dedent("""
        class TC:
            def __init__(self, xval):
                self.x = xval
        y = TC(49)
        """))
    def test_sage_autocomplete_1225b(self, execintrospect):
        execintrospect('z = 12 * y.', ["x"], '')
    def test_sage_autocomplete_1252a(self, execintrospect):
        execintrospect('2*sqr', ["t"], 'sqr')
    def test_sage_autocomplete_1252b(self, execintrospect):
        execintrospect('2+sqr', ["t"], 'sqr')

class TestAttach:
    def test_define_paf(self, exec2):
        exec2(dedent(r"""
        def paf():
            print("attached files: %d"%len(attached_files()))
            print("\n".join(attached_files()))
        paf()"""),"attached files: 0\n\n")
    def test_attach_sage_1(self, exec2, test_ro_data_dir):
        fn = os.path.join(test_ro_data_dir, 'a.sage')
        exec2("%attach {}\npaf()".format(fn), pattern="attached files: 1\n.*/a.sage\n")
    def test_attach_sage_2(self, exec2):
        exec2("f1('foo')","f1 arg = 'foo'\ntest f1 1\n")
    def test_attach_py_1(self, exec2, test_ro_data_dir):
        fn = os.path.join(test_ro_data_dir, 'a.py')
        exec2("%attach {}\npaf()".format(fn), pattern="attached files: 2\n.*/a.py\n.*/a.sage\n")
    def test_attach_py_2(self, exec2):
        exec2("f2('foo')","test f2 1\n")
    def test_attach_html_1(self, execblob, test_ro_data_dir):
        fn = os.path.join(test_ro_data_dir, 'a.html')
        execblob("%attach {}".format(fn), want_html=False, want_javascript=True, file_type='html')
    def test_attach_html_2(self, exec2):
        exec2("paf()", pattern="attached files: 3\n.*/a.html\n.*/a.py\n.*/a.sage\n")
    def test_detach_1(self, exec2):
        exec2("detach(attached_files())")
    def test_detach_2(self, exec2):
        exec2("paf()","attached files: 0\n\n")

class TestSearchSrc:
    def test_search_src_simple(self, execinteract):
        execinteract('search_src("convolution")')

    def test_search_src_max_chars(self, execinteract):
        execinteract('search_src("full cremonadatabase", max_chars = 1000)')

class TestIdentifiers:
    """
    see SMC issue #63
    """
    def test_ident_set_file_env(self, exec2):
        """emulate initial code block sent from UI, needed for first show_identifiers"""
        code = "os.chdir(salvus.data[\'path\']);__file__=salvus.data[\'file\']"
        exec2(code)
    def test_show_identifiers_initial(self, exec2):
        exec2("show_identifiers()","[]\n")

    def test_show_identifiers_vars(self, exec2):
        code = dedent(r"""
        k = ['a','b','c']
        A = {'a':'foo','b':'bar','c':'baz'}
        z = 99
        sorted(show_identifiers())""")
        exec2(code, "['A', 'k', 'z']\n")

    def test_save_and_reset(self,exec2,data_path):
        code = dedent(r"""
        save_session('%s')
        reset()
        show_identifiers()""")%data_path.join('session').strpath
        exec2(code,"[]\n")
    def test_load_session1(self,exec2,data_path):
        code = dedent(r"""
        pretty_print = 8
        view = 9
        load_session('%s')
        sorted(show_identifiers())""")%data_path.join('session').strpath
        output = "['A', 'k', 'pretty_print', 'view', 'z']\n"
        exec2(code,output)
    def test_load_session2(self,exec2):
        exec2("pretty_print,view","(8, 9)\n")

    def test_redefine_sage(self,exec2):
        code = dedent(r"""
        reset()
        sage=1
        show_identifiers()""")
        exec2(code,"['sage']\n")
<commit_msg>add pytest cases for python decorators<commit_after># test_sagews.py
# basic tests of sage worksheet using TCP protocol with sage_server
import socket
import conftest
import os
import re

from textwrap import dedent

class TestDecorators:
    def test_simple_dec(self, exec2):
        code = dedent(r"""
        def d2(f): return lambda x: f(x)+'-'+f(x)
        @d2
        def s(str): return str.upper()
        s('spam')""")
        exec2(code, "'SPAM-SPAM'\n")

    def test_multiple_dec(self, exec2):
        code = dedent(r"""
        def dummy(f): return f
        @dummy
        @dummy
        def f(x): return 2*x+1
        f(2)""")
        exec2(code, "5\n")

class TestErr:
    def test_non_ascii(self, test_id, sagews):
        # assign x to hbar to trigger non-ascii warning
        code = ("x = " + unichr(295) + "\nx").encode('utf-8')
        m = conftest.message.execute_code(code = code, id = test_id)
        sagews.send_json(m)
        # expect 3 messages from worksheet client, including final done:true
        # 1 stderr Error in lines 1-1
        typ, mesg = sagews.recv()
        assert typ == 'json'
        assert mesg['id'] == test_id
        assert 'stderr' in mesg
        assert 'Error in lines 1-1' in mesg['stderr']
        # 2 stderr WARNING: Code contains non-ascii characters
        typ, mesg = sagews.recv()
        assert typ == 'json'
        assert mesg['id'] == test_id
        assert 'stderr' in mesg
        assert 'WARNING: Code contains non-ascii characters' in mesg['stderr']
        # 3 done
        conftest.recv_til_done(sagews, test_id)

class TestLinearAlgebra:
    def test_solve_right(self, exec2):
        code = dedent(r"""
        A=matrix([[1,2,6],[1,2,0],[1,-2,3]])
        b=vector([1,-1,1])
        A.solve_right(b)""")
        exec2(code,"(-1/2, -1/4, 1/3)\n")

    def test_kernel(self, exec2):
        code = dedent(r"""
        A=matrix([[1,2,3],[1,2,3],[1,2,3]])
        kernel(A)""")
        pat = "\[ 1  0 -1\]\n\[ 0  1 -1\]"
        exec2(code, pattern = pat)

    def test_charpoly(self, exec2):
        code = dedent(r"""
        A=matrix([[1,2,3],[1,2,3],[1,2,3]])
        A.charpoly()""")
        exec2(code, "x^3 - 6*x^2\n")

    def test_eigenvalues(self, exec2):
        code = dedent(r"""
        A=matrix([[1,2,3],[1,2,3],[1,2,3]])
        A=matrix([[1,2,3],[1,2,3],[1,2,3]])
        A.eigenvalues()""")
        exec2(code, "[6, 0, 0]\n")

class TestBasic:
    def test_connection_type(self, sagews):
        print("type %s"%type(sagews))
        assert isinstance(sagews, conftest.ConnectionJSON)
        return

    def test_set_file_env(self, exec2):
        code = "os.chdir(salvus.data[\'path\']);__file__=salvus.data[\'file\']"
        exec2(code)

    def test_sage_assignment(self, exec2):
        code = "x = 42\nx\n"
        output = "42\n"
        exec2(code, output)

    def test_issue70(self, exec2):
        code = dedent(r"""
        for i in range(1):
            pass
        'x'
        """)
        output = dedent(r"""
        'x'
        """).lstrip()
        exec2(code, output)

    def test_issue819(self, exec2):
        code = dedent(r"""
        def never_called(a):
            print 'should not execute 1', a
            # comment
        # comment at indent 0
            print 'should not execute 2', a
        22
        """)
        output = "22\n"
        exec2(code, output)

    def test_search_doc(self, exec2):
        code = "search_doc('laurent')"
        html = "https://www.google.com/search\?q=site%3Adoc.sagemath.org\+laurent\&oq=site%3Adoc.sagemath.org"
        exec2(code, html_pattern = html)

    def test_show_doc(self, test_id, sagews):
        # issue 476
        code = "show?"
        patn = "import smc_sagews.graphics\nsmc_sagews.graphics.graph_to_d3_jsonable?"
        m = conftest.message.execute_code(code = code, id = test_id)
        sagews.send_json(m)
        typ, mesg = sagews.recv()
        assert typ == 'json'
        assert mesg['id'] == test_id
        assert 'code' in mesg
        assert 'source' in mesg['code']
        assert re.sub('\s+','',patn) in re.sub('\s+','',mesg['code']['source'])
        conftest.recv_til_done(sagews, test_id)

    # https://github.com/sagemathinc/smc/issues/1107
    def test_sage_underscore_1(self, exec2):
        exec2("2/5","2/5\n")
    def test_sage_underscore_2(self, exec2):
        exec2("_","2/5\n")

    # https://github.com/sagemathinc/smc/issues/978
    def test_mode_comments_1(self, exec2):
        exec2(dedent("""
        def f(s):
            print "s='%s'"%s"""))
    def test_mode_comments_2(self, exec2):
        exec2(dedent("""
        %f
        123
        # foo
        456"""), dedent("""
        s='123
        # foo
        456'
        """).lstrip())

    def test_block_parser(self, exec2):
        """
        .. NOTE::

            This function supplies a list of expected outputs to `exec2`.
        """
        exec2(dedent("""
        pi.n().round()
        [x for x in [1,2,3] if x<3]
        for z in ['a','b']:
            z
        else:
            z"""), ["3\n","[1, 2]\n","'a'\n'b'\n'b'\n"])

class TestIntrospect:
    # test names end with SMC issue number
    def test_sage_autocomplete_1188(self, execintrospect):
        execintrospect('2016.fa', ["ctor","ctorial"], "fa")
    def test_sage_autocomplete_295_setup(self, exec2):
        exec2("aaa=Rings()._super_categories_for_classes;len(aaa[0].axioms())","6\n")
    def test_sage_autocomplete_295a(self, execintrospect):
        execintrospect('for a in aa', ["a"], "aa")
    def test_sage_autocomplete_295b(self, execintrospect):
        execintrospect('3 * aa', ["a"], "aa")
    def test_sage_autocomplete_701_setup(self, exec2):
        exec2(dedent("""
        class Xyz:
            numerical_attribute = 42
        x1 = Xyz()
        x1.numerical_attribute.next_prime()"""),"43\n")
    def test_sage_autocomplete_701a(self, execintrospect):
        execintrospect('3 / x1.nu', ["merical_attribute"], "nu")
    def test_sage_autocomplete_701b(self, execintrospect):
        execintrospect('aa', ["a"], "aa")
    def test_sage_autocomplete_701c(self, execintrospect):
        execintrospect('[aa', ["a"], "aa")
    def test_sage_autocomplete_701d(self, execintrospect):
        execintrospect('( aa', ["a"], "aa")
    def test_sage_autocomplete_734a(self, execintrospect):
        f = '*_factors'
        execintrospect(f, ["cunningham_prime_factors", "prime_factors"], f)
    def test_sage_autocomplete_734b(self, execintrospect):
        f = '*le_pr*'
        execintrospect(f, ["next_probable_prime"], f)
    def test_sage_autocomplete_734c(self, execintrospect):
        execintrospect('list.re*e', ["remove", "reverse"], 're*e')
    def test_sage_autocomplete_1225a(self, execintrospect):
        execintrospect('z = 12.5 * units.len', ["gth"], 'len')
    def test_sage_autocomplete_1225b_setup(self, exec2):
        exec2(dedent("""
        class TC:
            def __init__(self, xval):
                self.x = xval
        y = TC(49)
        """))
    def test_sage_autocomplete_1225b(self, execintrospect):
        execintrospect('z = 12 * y.', ["x"], '')
    def test_sage_autocomplete_1252a(self, execintrospect):
        execintrospect('2*sqr', ["t"], 'sqr')
    def test_sage_autocomplete_1252b(self, execintrospect):
        execintrospect('2+sqr', ["t"], 'sqr')

class TestAttach:
    def test_define_paf(self, exec2):
        exec2(dedent(r"""
        def paf():
            print("attached files: %d"%len(attached_files()))
            print("\n".join(attached_files()))
        paf()"""),"attached files: 0\n\n")
    def test_attach_sage_1(self, exec2, test_ro_data_dir):
        fn = os.path.join(test_ro_data_dir, 'a.sage')
        exec2("%attach {}\npaf()".format(fn), pattern="attached files: 1\n.*/a.sage\n")
    def test_attach_sage_2(self, exec2):
        exec2("f1('foo')","f1 arg = 'foo'\ntest f1 1\n")
    def test_attach_py_1(self, exec2, test_ro_data_dir):
        fn = os.path.join(test_ro_data_dir, 'a.py')
        exec2("%attach {}\npaf()".format(fn), pattern="attached files: 2\n.*/a.py\n.*/a.sage\n")
    def test_attach_py_2(self, exec2):
        exec2("f2('foo')","test f2 1\n")
    def test_attach_html_1(self, execblob, test_ro_data_dir):
        fn = os.path.join(test_ro_data_dir, 'a.html')
        execblob("%attach {}".format(fn), want_html=False, want_javascript=True, file_type='html')
    def test_attach_html_2(self, exec2):
        exec2("paf()", pattern="attached files: 3\n.*/a.html\n.*/a.py\n.*/a.sage\n")
    def test_detach_1(self, exec2):
        exec2("detach(attached_files())")
    def test_detach_2(self, exec2):
        exec2("paf()","attached files: 0\n\n")

class TestSearchSrc:
    def test_search_src_simple(self, execinteract):
        execinteract('search_src("convolution")')

    def test_search_src_max_chars(self, execinteract):
        execinteract('search_src("full cremonadatabase", max_chars = 1000)')

class TestIdentifiers:
    """
    see SMC issue #63
    """
    def test_ident_set_file_env(self, exec2):
        """emulate initial code block sent from UI, needed for first show_identifiers"""
        code = "os.chdir(salvus.data[\'path\']);__file__=salvus.data[\'file\']"
        exec2(code)
    def test_show_identifiers_initial(self, exec2):
        exec2("show_identifiers()","[]\n")

    def test_show_identifiers_vars(self, exec2):
        code = dedent(r"""
        k = ['a','b','c']
        A = {'a':'foo','b':'bar','c':'baz'}
        z = 99
        sorted(show_identifiers())""")
        exec2(code, "['A', 'k', 'z']\n")

    def test_save_and_reset(self,exec2,data_path):
        code = dedent(r"""
        save_session('%s')
        reset()
        show_identifiers()""")%data_path.join('session').strpath
        exec2(code,"[]\n")
    def test_load_session1(self,exec2,data_path):
        code = dedent(r"""
        pretty_print = 8
        view = 9
        load_session('%s')
        sorted(show_identifiers())""")%data_path.join('session').strpath
        output = "['A', 'k', 'pretty_print', 'view', 'z']\n"
        exec2(code,output)
    def test_load_session2(self,exec2):
        exec2("pretty_print,view","(8, 9)\n")

    def test_redefine_sage(self,exec2):
        code = dedent(r"""
        reset()
        sage=1
        show_identifiers()""")
        exec2(code,"['sage']\n")
<|endoftext|>


<commit_before>###########################################################
#
# Copyright (c) 2009, Southpaw Technology
#                     All Rights Reserved
#
# PROPRIETARY INFORMATION.  This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#
__all__ = ["StaticTableLayoutWdg"]

from pyasm.common import Common
from pyasm.search import Search, SearchKey
from pyasm.web import DivWdg, Table
from pyasm.widget import ThumbWdg, IconWdg

from table_layout_wdg import FastTableLayoutWdg
from tactic.ui.widget import SingleButtonWdg

class StaticTableLayoutWdg(FastTableLayoutWdg):

    ARGS_KEYS = {

        "mode": {
            'description': "Determines whether to draw with widgets or just use the raw data",
            'type': 'SelectWdg',
            'values': 'widget|raw',
            'order': 0,
            'category': 'Required'
        },

        "search_type": {
            'description': "search type that this panels works with",
            'type': 'TextWdg',
            'order': 1,
            'category': 'Required'
        },
        "view": {
            'description': "view to be displayed",
            'type': 'TextWdg',
            'order': 2,
            'category': 'Required'
        },
        "element_names": {
            'description': "Comma delimited list of elemnent to view",
            'type': 'TextWdg',
            'order': 0,
            'category': 'Optional'
        },
        "show_shelf": {
            'description': "Determines whether or not to show the action shelf",
            'type': 'SelectWdg',
            'values': 'true|false',
            'order': 1,
            'category': 'Optional'
        },

    }

    def get_display(my):
        #my.chunk_size = 10000

        if my.kwargs.get("do_search") != "false":
            my.handle_search()

        my.mode = my.kwargs.get("mode")
        if my.mode != 'raw':
            my.mode = 'widget'

        # extraneous variables inherited from TableLayoutWdg
        my.edit_permission = False

        top = DivWdg()
        my.set_as_panel(top)
        top.add_class("spt_sobject_top")

        inner = DivWdg()
        top.add(inner)
        inner.add_color("background", "background")
        inner.add_color("color", "color")
        inner.add_class("spt_table")
        inner.add_class("spt_layout")
        inner.add_attr("spt_version", "2")

        table = my.table
        table.add_class("spt_table_table")
        # set the sobjects to all the widgets then preprocess
        if my.mode == 'widget':
            for widget in my.widgets:
                widget.handle_layout_behaviors(table)
                widget.set_sobjects(my.sobjects)
                widget.set_parent_wdg(my)
                # preprocess the elements
                widget.preprocess()
        else:
            for i, widget in enumerate(my.widgets):
                #widget.handle_layout_behaviors(table)
                widget.set_sobjects(my.sobjects)
                #widget.set_parent_wdg(my)
                # preprocess the elements
                widget.preprocess()

        my.process_groups()
        my.order_sobjects()
        my.remap_sobjects()

        my.attributes = []
        for i, widget in enumerate(my.widgets):
            element_name = widget.get_name()

            if element_name and element_name != "None":
                attrs = my.config.get_element_attributes(element_name)
            else:
                attrs = {}

            my.attributes.append(attrs)

        is_refresh = my.kwargs.get("is_refresh")
        if my.kwargs.get("show_shelf") not in ['false', False]:
            action = my.get_action_wdg()
            inner.add(action)

        index = 0

        table.add_attr("spt_view", my.kwargs.get("view") )
        table.set_attr("spt_search_type", my.kwargs.get('search_type'))

        table.set_id(my.table_id)

        table.add_style("width: 100%")
        inner.add(table)
        table.add_color("color", "color")

        # initialize the spt.table js
        #my.handle_table_behaviors(table)

        my.handle_headers(table)

        border_color = table.get_color("table_border", default="border")
        for row, sobject in enumerate(my.sobjects):

            # put in a group row
            if my.is_grouped:
                my.handle_groups(table, row, sobject)

            tr = table.add_row()
            if row % 2:
                background = tr.add_color("background", "background")
            else:
                background = tr.add_color("background", "background", -7)

            tr.add_class("spt_table_row")
            tr.add_attr("spt_search_key", sobject.get_search_key())

            for i, widget in enumerate(my.widgets):

                value_div = DivWdg()
                value_div.add_style("padding: 3px")
                td = table.add_cell(value_div)
                td.add_style("vertical-align: top")
                td.add_style("border: solid 1px %s" % border_color)

                if my.mode == 'widget':
                    widget.set_current_index(row)
                    value_div.add(widget.get_buffer_display())
                else:
                    element_name = widget.get_name()
                    value = sobject.get_value(element_name, no_exception=True)
                    value_div.add(value)

        top.add_class("spt_table_top");
        class_name = Common.get_full_class_name(my)
        top.add_attr("spt_class_name", class_name)

        table.add_class("spt_table_content");
        inner.add_attr("spt_search_type", my.kwargs.get('search_type'))
        inner.add_attr("spt_view", my.kwargs.get('view'))

        # extra ?? Doesn't really work to keep the mode
        inner.add_attr("spt_mode", my.mode)
        top.add_attr("spt_mode", my.mode)

        inner.add("
")

        if my.kwargs.get("is_refresh") == 'true':
            return inner
        else:
            return top

    def handle_headers(my, table):

        # this comes from refresh
        widths = my.kwargs.get("column_widths")

        # Add the headers
        tr = table.add_row()
        tr.add_class("spt_table_header_row")
        for i, widget in enumerate(my.widgets):
            widget_name = widget.get_name()
            th = table.add_header()
            th.add_style("text-align: left")
            th.add_attr("spt_element_name", widget_name)
            header_div = DivWdg()
            th.add(header_div)
            th.add_style("padding: 3px")
            th.add_gradient("background", "background", -5, -10)
            th.add_border()

            if my.mode == 'widget':
                value = widget.get_title()
            else:
                element = widget_name
                value = Common.get_display_title(element)
            header_div.add(value)

            if widths and len(widths) > i:
                th.add_style("width", widths[i])
                width_set = True
                width = widths[i]

            else: # get width from definition
                width = my.attributes[i].get("width")
                if width:
                     th.add_style("width", width)
                     width_set = True
            if width:
                th.add_style("min-width", width)
            else:
                th.add_style("overflow","hidden")

            widget.handle_th(th, i)

    def handle_group(my, table, i, sobject, group_name, group_value):

        tr, td = table.add_row_cell()
        tr.add_color("background", "background3", 5)
        tr.add_color("color", "color3")

        if group_value == '__NONE__':
            label = '(unknown)'
        else:
            label = Common.process_unicode_string(group_value)

        td.add(label)

        td.add_style("height: 25px")
        td.add_style("padding-left: %spx" % (i*15+5))
        td.add_style("border-style: solid")
        border_color = td.get_color("border")
        td.add_style("border-width: 0px 0px 0px 1px")
        td.add_style("border-color: %s" % border_color)
        td.add_style("font-weight: bold")

<commit_msg>BRANCH 4.1: changed (unknown) to "---"<commit_after>###########################################################
#
# Copyright (c) 2009, Southpaw Technology
#                     All Rights Reserved
#
# PROPRIETARY INFORMATION.  This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#
__all__ = ["StaticTableLayoutWdg"]

from pyasm.common import Common
from pyasm.search import Search, SearchKey
from pyasm.web import DivWdg, Table
from pyasm.widget import ThumbWdg, IconWdg

from table_layout_wdg import FastTableLayoutWdg
from tactic.ui.widget import SingleButtonWdg

class StaticTableLayoutWdg(FastTableLayoutWdg):

    ARGS_KEYS = {

        "mode": {
            'description': "Determines whether to draw with widgets or just use the raw data",
            'type': 'SelectWdg',
            'values': 'widget|raw',
            'order': 0,
            'category': 'Required'
        },

        "search_type": {
            'description': "search type that this panels works with",
            'type': 'TextWdg',
            'order': 1,
            'category': 'Required'
        },
        "view": {
            'description': "view to be displayed",
            'type': 'TextWdg',
            'order': 2,
            'category': 'Required'
        },
        "element_names": {
            'description': "Comma delimited list of elemnent to view",
            'type': 'TextWdg',
            'order': 0,
            'category': 'Optional'
        },
        "show_shelf": {
            'description': "Determines whether or not to show the action shelf",
            'type': 'SelectWdg',
            'values': 'true|false',
            'order': 1,
            'category': 'Optional'
        },

    }

    def get_display(my):
        #my.chunk_size = 10000

        if my.kwargs.get("do_search") != "false":
            my.handle_search()

        my.mode = my.kwargs.get("mode")
        if my.mode != 'raw':
            my.mode = 'widget'

        # extraneous variables inherited from TableLayoutWdg
        my.edit_permission = False

        top = DivWdg()
        my.set_as_panel(top)
        top.add_class("spt_sobject_top")

        inner = DivWdg()
        top.add(inner)
        inner.add_color("background", "background")
        inner.add_color("color", "color")
        inner.add_class("spt_table")
        inner.add_class("spt_layout")
        inner.add_attr("spt_version", "2")

        table = my.table
        table.add_class("spt_table_table")
        # set the sobjects to all the widgets then preprocess
        if my.mode == 'widget':
            for widget in my.widgets:
                widget.handle_layout_behaviors(table)
                widget.set_sobjects(my.sobjects)
                widget.set_parent_wdg(my)
                # preprocess the elements
                widget.preprocess()
        else:
            for i, widget in enumerate(my.widgets):
                #widget.handle_layout_behaviors(table)
                widget.set_sobjects(my.sobjects)
                #widget.set_parent_wdg(my)
                # preprocess the elements
                widget.preprocess()

        my.process_groups()
        my.order_sobjects()
        my.remap_sobjects()

        my.attributes = []
        for i, widget in enumerate(my.widgets):
            element_name = widget.get_name()

            if element_name and element_name != "None":
                attrs = my.config.get_element_attributes(element_name)
            else:
                attrs = {}

            my.attributes.append(attrs)

        is_refresh = my.kwargs.get("is_refresh")
        if my.kwargs.get("show_shelf") not in ['false', False]:
            action = my.get_action_wdg()
            inner.add(action)

        index = 0

        table.add_attr("spt_view", my.kwargs.get("view") )
        table.set_attr("spt_search_type", my.kwargs.get('search_type'))

        table.set_id(my.table_id)

        table.add_style("width: 100%")
        inner.add(table)
        table.add_color("color", "color")

        # initialize the spt.table js
        #my.handle_table_behaviors(table)

        my.handle_headers(table)

        border_color = table.get_color("table_border", default="border")
        for row, sobject in enumerate(my.sobjects):

            # put in a group row
            if my.is_grouped:
                my.handle_groups(table, row, sobject)

            tr = table.add_row()
            if row % 2:
                background = tr.add_color("background", "background")
            else:
                background = tr.add_color("background", "background", -7)

            tr.add_class("spt_table_row")
            tr.add_attr("spt_search_key", sobject.get_search_key())

            for i, widget in enumerate(my.widgets):

                value_div = DivWdg()
                value_div.add_style("padding: 3px")
                td = table.add_cell(value_div)
                td.add_style("vertical-align: top")
                td.add_style("border: solid 1px %s" % border_color)

                if my.mode == 'widget':
                    widget.set_current_index(row)
                    value_div.add(widget.get_buffer_display())
                else:
                    element_name = widget.get_name()
                    value = sobject.get_value(element_name, no_exception=True)
                    value_div.add(value)

        top.add_class("spt_table_top");
        class_name = Common.get_full_class_name(my)
        top.add_attr("spt_class_name", class_name)

        table.add_class("spt_table_content");
        inner.add_attr("spt_search_type", my.kwargs.get('search_type'))
        inner.add_attr("spt_view", my.kwargs.get('view'))

        # extra ?? Doesn't really work to keep the mode
        inner.add_attr("spt_mode", my.mode)
        top.add_attr("spt_mode", my.mode)

        inner.add("
")

        if my.kwargs.get("is_refresh") == 'true':
            return inner
        else:
            return top

    def handle_headers(my, table):

        # this comes from refresh
        widths = my.kwargs.get("column_widths")

        # Add the headers
        tr = table.add_row()
        tr.add_class("spt_table_header_row")
        for i, widget in enumerate(my.widgets):
            widget_name = widget.get_name()
            th = table.add_header()
            th.add_style("text-align: left")
            th.add_attr("spt_element_name", widget_name)
            header_div = DivWdg()
            th.add(header_div)
            th.add_style("padding: 3px")
            th.add_gradient("background", "background", -5, -10)
            th.add_border()

            if my.mode == 'widget':
                value = widget.get_title()
            else:
                element = widget_name
                value = Common.get_display_title(element)
            header_div.add(value)

            if widths and len(widths) > i:
                th.add_style("width", widths[i])
                width_set = True
                width = widths[i]

            else: # get width from definition
                width = my.attributes[i].get("width")
                if width:
                     th.add_style("width", width)
                     width_set = True
            if width:
                th.add_style("min-width", width)
            else:
                th.add_style("overflow","hidden")

            widget.handle_th(th, i)

    def handle_group(my, table, i, sobject, group_name, group_value):

        tr, td = table.add_row_cell()
        tr.add_color("background", "background3", 5)
        tr.add_color("color", "color3")

        if group_value == '__NONE__':
            label = '---'
        else:
            label = Common.process_unicode_string(group_value)

        td.add(label)

        td.add_style("height: 25px")
        td.add_style("padding-left: %spx" % (i*15+5))
        td.add_style("border-style: solid")
        border_color = td.get_color("border")
        td.add_style("border-width: 0px 0px 0px 1px")
        td.add_style("border-color: %s" % border_color)
        td.add_style("font-weight: bold")

<|endoftext|>


<commit_before>"""Select2 widget implementation module."""

try:
    from functools import lru_cache
except:
    lru_cache = None

from dal.widgets import (
    QuerySetSelectMixin,
    Select,
    SelectMultiple,
    WidgetMixin
)

from django import forms
from django.conf import settings
try:
    # SELECT2_TRANSLATIONS is Django 2.x only
    from django.contrib.admin.widgets import SELECT2_TRANSLATIONS
except ImportError:
    SELECT2_TRANSLATIONS = {}
from django.contrib.staticfiles import finders
from django.utils import translation
from django.utils.itercompat import is_iterable

import six

I18N_PATH = 'vendor/select2/dist/js/i18n/'

def get_i18n_name(lang_code):
    """Ensure lang_code is supported by Select2."""
    lower_lang = lang_code.lower()
    split_lang = lang_code.split('-')[0]
    # Use the SELECT2_TRANSLATIONS if available
    if SELECT2_TRANSLATIONS:
        if lower_lang in SELECT2_TRANSLATIONS:
            return SELECT2_TRANSLATIONS.get(lower_lang)
        elif split_lang in SELECT2_TRANSLATIONS:
            return SELECT2_TRANSLATIONS.get(split_lang)
    # Otherwise fallback to manually checking if the static file exists
    if finders.find('%s%s.js' % (I18N_PATH, lang_code)):
        return lang_code
    elif finders.find('%s%s.js' % (I18N_PATH, split_lang)):
        return lang_code.split('-')[0]

if lru_cache:
    get_i18n_name = lru_cache()(get_i18n_name)
else:
    import warnings
    warnings.warn('Python2: no cache on get_i18n_name until contribution')

class Select2WidgetMixin(object):
    """Mixin for Select2 widgets."""

    def build_attrs(self, *args, **kwargs):
        """Set data-autocomplete-light-language."""
        attrs = super(Select2WidgetMixin, self).build_attrs(*args, **kwargs)
        lang_code = self._get_language_code()
        if lang_code:
            attrs.setdefault('data-autocomplete-light-language', lang_code)
        return attrs

    def _get_language_code(self):
        """Return language code or None."""
        lang_code = translation.get_language()
        if lang_code:
            lang_code = get_i18n_name(
                translation.to_locale(lang_code).replace('_', '-')
            )
        return lang_code

    @property
    def media(self):
        """Return JS/CSS resources for the widget."""
        extra = '' if settings.DEBUG else '.min'
        i18n_name = self._get_language_code()
        i18n_file = (
            '%s%s.js' % (I18N_PATH, i18n_name),
        ) if i18n_name else ()

        return forms.Media(
            js=(
                'admin/js/vendor/jquery/jquery%s.js' % extra,
                'autocomplete_light/jquery.init.js',
                'vendor/select2/dist/js/select2.full%s.js' % extra,
            ) + i18n_file + (
                'autocomplete_light/autocomplete.init.js',
                'autocomplete_light/forward.js',
                'autocomplete_light/select2.js',
                'autocomplete_light/jquery.post-setup.js',
            ),
            css={
                'screen': (
                    'vendor/select2/dist/css/select2%s.css' % extra,
                    'admin/css/autocomplete.css',
                    'autocomplete_light/select2.css',
                ),
            },
        )

    autocomplete_function = 'select2'

class Select2(Select2WidgetMixin, Select):
    """Select2 widget for regular choices."""

class Select2Multiple(Select2WidgetMixin, SelectMultiple):
    """Select2Multiple widget for regular choices."""

class ListSelect2(WidgetMixin, Select2WidgetMixin, forms.Select):
    """Select widget for regular choices and Select2."""

class ModelSelect2(QuerySetSelectMixin,
                   Select2WidgetMixin,
                   forms.Select):
    """Select widget for QuerySet choices and Select2."""

class ModelSelect2Multiple(QuerySetSelectMixin,
                           Select2WidgetMixin,
                           forms.SelectMultiple):
    """SelectMultiple widget for QuerySet choices and Select2."""

class TagSelect2(WidgetMixin,
                 Select2WidgetMixin,
                 forms.SelectMultiple):
    """Select2 in tag mode."""

    def build_attrs(self, *args, **kwargs):
        """Automatically set data-tags=1."""
        attrs = super(TagSelect2, self).build_attrs(*args, **kwargs)
        attrs.setdefault('data-tags', 1)
        return attrs

    def value_from_datadict(self, data, files, name):
        """Return a comma-separated list of options.

        This is needed because Select2 uses a multiple select even in tag mode,
        and the model field expects a comma-separated list of tags.
        """
        values = super(TagSelect2, self).value_from_datadict(data, files, name)
        return six.text_type(',').join(values)

    def option_value(self, value):
        """Return the HTML option value attribute for a value."""
        return value

    def format_value(self, value):
        """Return the list of HTML option values for a form field value."""
        if not isinstance(value, (tuple, list)):
            value = [value]

        values = set()
        for v in value:
            if not v:
                continue

            v = v.split(',') if isinstance(v, six.string_types) else v
            v = [v] if not is_iterable(v) else v
            for t in v:
                values.add(self.option_value(t))
        return values

    def options(self, name, value, attrs=None):
        """Return only select options."""
        # When the data hasn't validated, we get the raw input
        if isinstance(value, six.text_type):
            value = value.split(',')

        for v in value:
            if not v:
                continue

            real_values = v.split(',') if hasattr(v, 'split') else v
            real_values = [real_values] if not is_iterable(real_values) else real_values
            for rv in real_values:
                yield self.option_value(rv)

    def optgroups(self, name, value, attrs=None):
        """Return a list of one optgroup and selected values."""
        default = (None, [], 0)
        groups = [default]

        for i, v in enumerate(self.options(name, value, attrs)):
            default[1].append(
                self.create_option(v, v, v, True, i)
            )
        return groups
<commit_msg>Do not load jquery in Select2WidgetMixin<commit_after>"""Select2 widget implementation module."""

try:
    from functools import lru_cache
except:
    lru_cache = None

from dal.widgets import (
    QuerySetSelectMixin,
    Select,
    SelectMultiple,
    WidgetMixin
)

from django import forms
from django.conf import settings
try:
    # SELECT2_TRANSLATIONS is Django 2.x only
    from django.contrib.admin.widgets import SELECT2_TRANSLATIONS
except ImportError:
    SELECT2_TRANSLATIONS = {}
from django.contrib.staticfiles import finders
from django.utils import translation
from django.utils.itercompat import is_iterable

import six

I18N_PATH = 'vendor/select2/dist/js/i18n/'

def get_i18n_name(lang_code):
    """Ensure lang_code is supported by Select2."""
    lower_lang = lang_code.lower()
    split_lang = lang_code.split('-')[0]
    # Use the SELECT2_TRANSLATIONS if available
    if SELECT2_TRANSLATIONS:
        if lower_lang in SELECT2_TRANSLATIONS:
            return SELECT2_TRANSLATIONS.get(lower_lang)
        elif split_lang in SELECT2_TRANSLATIONS:
            return SELECT2_TRANSLATIONS.get(split_lang)
    # Otherwise fallback to manually checking if the static file exists
    if finders.find('%s%s.js' % (I18N_PATH, lang_code)):
        return lang_code
    elif finders.find('%s%s.js' % (I18N_PATH, split_lang)):
        return lang_code.split('-')[0]

if lru_cache:
    get_i18n_name = lru_cache()(get_i18n_name)
else:
    import warnings
    warnings.warn('Python2: no cache on get_i18n_name until contribution')

class Select2WidgetMixin(object):
    """Mixin for Select2 widgets."""

    def build_attrs(self, *args, **kwargs):
        """Set data-autocomplete-light-language."""
        attrs = super(Select2WidgetMixin, self).build_attrs(*args, **kwargs)
        lang_code = self._get_language_code()
        if lang_code:
            attrs.setdefault('data-autocomplete-light-language', lang_code)
        return attrs

    def _get_language_code(self):
        """Return language code or None."""
        lang_code = translation.get_language()
        if lang_code:
            lang_code = get_i18n_name(
                translation.to_locale(lang_code).replace('_', '-')
            )
        return lang_code

    @property
    def media(self):
        """Return JS/CSS resources for the widget."""
        extra = '' if settings.DEBUG else '.min'
        i18n_name = self._get_language_code()
        i18n_file = (
            '%s%s.js' % (I18N_PATH, i18n_name),
        ) if i18n_name else ()

        return forms.Media(
            js=(
                'autocomplete_light/jquery.init.js',
                'vendor/select2/dist/js/select2.full%s.js' % extra,
            ) + i18n_file + (
                'autocomplete_light/autocomplete.init.js',
                'autocomplete_light/forward.js',
                'autocomplete_light/select2.js',
                'autocomplete_light/jquery.post-setup.js',
            ),
            css={
                'screen': (
                    'vendor/select2/dist/css/select2%s.css' % extra,
                    'admin/css/autocomplete.css',
                    'autocomplete_light/select2.css',
                ),
            },
        )

    autocomplete_function = 'select2'

class Select2(Select2WidgetMixin, Select):
    """Select2 widget for regular choices."""

class Select2Multiple(Select2WidgetMixin, SelectMultiple):
    """Select2Multiple widget for regular choices."""

class ListSelect2(WidgetMixin, Select2WidgetMixin, forms.Select):
    """Select widget for regular choices and Select2."""

class ModelSelect2(QuerySetSelectMixin,
                   Select2WidgetMixin,
                   forms.Select):
    """Select widget for QuerySet choices and Select2."""

class ModelSelect2Multiple(QuerySetSelectMixin,
                           Select2WidgetMixin,
                           forms.SelectMultiple):
    """SelectMultiple widget for QuerySet choices and Select2."""

class TagSelect2(WidgetMixin,
                 Select2WidgetMixin,
                 forms.SelectMultiple):
    """Select2 in tag mode."""

    def build_attrs(self, *args, **kwargs):
        """Automatically set data-tags=1."""
        attrs = super(TagSelect2, self).build_attrs(*args, **kwargs)
        attrs.setdefault('data-tags', 1)
        return attrs

    def value_from_datadict(self, data, files, name):
        """Return a comma-separated list of options.

        This is needed because Select2 uses a multiple select even in tag mode,
        and the model field expects a comma-separated list of tags.
        """
        values = super(TagSelect2, self).value_from_datadict(data, files, name)
        return six.text_type(',').join(values)

    def option_value(self, value):
        """Return the HTML option value attribute for a value."""
        return value

    def format_value(self, value):
        """Return the list of HTML option values for a form field value."""
        if not isinstance(value, (tuple, list)):
            value = [value]

        values = set()
        for v in value:
            if not v:
                continue

            v = v.split(',') if isinstance(v, six.string_types) else v
            v = [v] if not is_iterable(v) else v
            for t in v:
                values.add(self.option_value(t))
        return values

    def options(self, name, value, attrs=None):
        """Return only select options."""
        # When the data hasn't validated, we get the raw input
        if isinstance(value, six.text_type):
            value = value.split(',')

        for v in value:
            if not v:
                continue

            real_values = v.split(',') if hasattr(v, 'split') else v
            real_values = [real_values] if not is_iterable(real_values) else real_values
            for rv in real_values:
                yield self.option_value(rv)

    def optgroups(self, name, value, attrs=None):
        """Return a list of one optgroup and selected values."""
        default = (None, [], 0)
        groups = [default]

        for i, v in enumerate(self.options(name, value, attrs)):
            default[1].append(
                self.create_option(v, v, v, True, i)
            )
        return groups
<|endoftext|>


<commit_before>import asyncio
from functools import wraps
from inspect import signature, Parameter
from typing import Callable, Any, Sequence, get_type_hints, Type, NamedTuple, Optional, List, Dict
from typing import Tuple
from typing import Union
from venom.fields import RepeatField, MapField, Field

from venom.converter import Converter

from venom.message import Empty, Message
from venom.rpc.resolver import Resolver

MessageFunction = NamedTuple('MessageFunction', [
    ('request', Type[Message]),
    ('response', Type[Message]),
    ('invokable', Callable[[Any, Message, Optional['asyncio.BaseEventLoop']], Message])  # TODO update to typing.Coroutine in Python 3.6
])

# TODO name arg for use with auto-generation
def magic_normalize(func: Callable[..., Any],
                    request: Type[Message] = None,
                    response: Type[Message] = None,
                    converters: Sequence[Converter] = (),
                    # TODO replace with Sequence[Union[Converter, Type[Converter]]] see https://github.com/python/typing/issues/266
                    additional_args: Sequence[Resolver] = ()) -> MessageFunction:
    """

    :param func:
    :param request:
    :param response:
    :param converters:
    :param additional_args: additional arguments that are resolved during invocation.
    :return:
    """
    # TODO parameters supplied by the service implementation through a context; session etc.

    additional_args = [resolver() if isinstance(resolver, type) else resolver for resolver in additional_args]
    converters = [converter() if isinstance(converter, type) else converter for converter in converters]

    func_signature = signature(func)
    func_type_hints = get_type_hints(func)

    if len(func_signature.parameters) == 0:
        # no "self" parameter: func()
        raise RuntimeError("At least one argument expected in {}".format(func))

    request_converter = None
    func_parameters = tuple(func_signature.parameters.items())[1 + len(additional_args):]

    if len(func_parameters):
        name, param = func_parameters[0]
        type_ = func_type_hints.get(name, Any)

        unpack_request = False  # type: Union[bool, Tuple[str, ...]]

        if issubclass(type_, Message) and name == 'request':
            # func(self, request: MessageType, ...)
            if request is None:
                request = type_
            elif request != type_ and 'request' not in request.__fields__:
                raise RuntimeError("Bad argument in {}: "
                                   "'{}' should be {}, but got {}".format(func, name, request, type_))

            for name, param in func_parameters[1:]:
                if param.default is Parameter.empty:
                    raise RuntimeError("Unexpected required argument in {}: '{}'".format(func, name))
        elif request is not None and name == 'request':
            for converter in converters:
                if converter.wire == request and converter.python == type_:
                    request_converter = converter
                    break

            if not request_converter:
                raise RuntimeError("Unable to coerce request message to python format: "
                                   "'{}' in {}".format(type_, func))
        else:  # func(self, arg: ?, ...)
            required_params, remaining_params = {}, {}
            for name, param in func_parameters:

                if param.default is Parameter.empty:
                    required_params[name] = func_type_hints.get(name, Any)
                else:
                    remaining_params[name] = (func_type_hints.get(name, Any), param.default)

            if request:  # unpack from request message
                message_params = set()

                for name, type_ in required_params.items():
                    if name not in request.__fields__:
                        raise RuntimeError("Unexpected required argument in {}: "
                                           "'{}' is not a field of {}".format(func, name, request))

                    field = request.__fields__[name]

                    # TODO support other mappings and sequences
                    # XXX support for list-within-a-list situations?
                    if isinstance(field, RepeatField):
                        field_type = List[field.items.type]
                    elif isinstance(field, MapField):
                        field_type = Dict[str, field.values.type]
                    elif isinstance(field, Field):
                        field_type = field.type
                    else:
                        # TODO support Dict[str, ?] for MapField
                        raise NotImplementedError

                    if type_ not in (Any, field_type):
                        raise RuntimeError("Bad argument in {}: "
                                           "'{}' should be {}, but got {}".format(func, name, field_type, type_))

                    message_params.add((name, None))

                for name, (type_, default) in remaining_params.items():
                    if name in request.__fields__:
                        field = request.__fields__[name]

                        # TODO support other mappings and sequences
                        # XXX support for list-within-a-list situations?
                        if isinstance(field, RepeatField):
                            field_type = List[field.items.type]
                        elif isinstance(field, MapField):
                            field_type = Dict[str, field.values.type]
                        elif isinstance(field, Field):
                            field_type = field.type
                        else:
                            # TODO support Dict[str, ?] for MapField
                            raise NotImplementedError

                        if type_ not in (Any, field_type):
                            raise RuntimeError("Bad argument in {}: "
                                               "'{}' should be {}, but got {}".format(func, name, field_type, type_))

                        message_params.add((name, default))

                if message_params == set(request.__fields__.keys()):
                    unpack_request = True
                else:
                    unpack_request = tuple(message_params)

            else:  # auto-generate message from params (uses converters where necessary)
                unpack_request = True
                raise NotImplementedError  # TODO
    else:  # func(self)
        if request is None:
            request = Empty

        # either request is Empty, or otherwise all request fields are discarded -- strange, but valid
        unpack_request = ()

    return_type = func_type_hints.get('return', Any)
    response_converter = None

    if response is None:
        if return_type in (Any, None, type(None)):  # None for Python 3.5 compatibility
            # TODO warn if Any: missing return type annotation (will discard return value)
            response = Empty
        elif issubclass(return_type, Message):
            response = return_type
        else:
            for converter in converters:
                if converter.python == return_type:
                    response_converter = converter
                    response = converter.wire
                    break

            if not response_converter:
                raise RuntimeError("Unable to coerce return value to wire format: "
                                   "'{}' in {}".format(return_type, func))
    elif return_type == Any:
        # NOTE missing return type annotation (assuming 'response' here)
        pass
    elif return_type in (None, type(None)):  # None for Python 3.5 compatibility
        response = Empty
    elif response != return_type:
        for converter in converters:
            if converter.wire == response and converter.python == return_type:
                response_converter = converter
                break

        if not response_converter:
            raise RuntimeError("Unable to coerce return value to wire format: "
                               "'{}' in {}".format(return_type, func))

    wrap_args = lambda req: (req,)
    wrap_kwargs = lambda req: {}

    if unpack_request is True:
        wrap_args = lambda req: ()
        wrap_kwargs = lambda req: req
    elif unpack_request is False:
        if request_converter:
            wrap_args = lambda req: (request_converter.convert(req),)
    elif unpack_request == ():
        wrap_args = lambda req: ()
    else:
        wrap_args = lambda req: ()
        wrap_kwargs = lambda req: {f: req.get(f, d) for f, d in unpack_request}

    if response == Empty and return_type != Empty:
        wrap_response = lambda res: Empty()
    elif response_converter:
        wrap_response = lambda res: response_converter.format(res)
    else:
        wrap_response = lambda res: res

    if not asyncio.iscoroutinefunction(func):
        func_ = func

        @wraps(func)
        async def func(*args, **kwargs):
            return func_(*args, **kwargs)

    # TODO (optimization) do not wrap what does not need to be wrapped
    if additional_args:
        async def invokable(inst, req: Message, loop: 'asyncio.BaseEventLoop' = None) -> Message:
            return wrap_response(await func(inst,
                                            *(await asyncio.gather(*[arg.resolve(inst, req)
                                                                     for arg in additional_args], loop=loop)),
                                            *wrap_args(req),
                                            **wrap_kwargs(req)))
    else:
        async def invokable(inst, req: Message, loop: 'asyncio.BaseEventLoop' = None) -> Message:
            return wrap_response(await func(inst, *wrap_args(req), **wrap_kwargs(req)))

    return MessageFunction(request, response, wraps(func)(invokable))
<commit_msg>Remove TODO<commit_after>import asyncio
from functools import wraps
from inspect import signature, Parameter
from typing import Callable, Any, Sequence, get_type_hints, Type, NamedTuple, Optional, List, Dict
from typing import Tuple
from typing import Union
from venom.fields import RepeatField, MapField, Field

from venom.converter import Converter

from venom.message import Empty, Message
from venom.rpc.resolver import Resolver

MessageFunction = NamedTuple('MessageFunction', [
    ('request', Type[Message]),
    ('response', Type[Message]),
    ('invokable', Callable[[Any, Message, Optional['asyncio.BaseEventLoop']], Message])  # TODO update to typing.Coroutine in Python 3.6
])

# TODO name arg for use with auto-generation
def magic_normalize(func: Callable[..., Any],
                    request: Type[Message] = None,
                    response: Type[Message] = None,
                    converters: Sequence[Converter] = (),
                    # TODO replace with Sequence[Union[Converter, Type[Converter]]] see https://github.com/python/typing/issues/266
                    additional_args: Sequence[Resolver] = ()) -> MessageFunction:
    """

    :param func:
    :param request:
    :param response:
    :param converters:
    :param additional_args: additional arguments that are resolved during invocation.
    :return:
    """
    # TODO parameters supplied by the service implementation through a context; session etc.

    additional_args = [resolver() if isinstance(resolver, type) else resolver for resolver in additional_args]
    converters = [converter() if isinstance(converter, type) else converter for converter in converters]

    func_signature = signature(func)
    func_type_hints = get_type_hints(func)

    if len(func_signature.parameters) == 0:
        # no "self" parameter: func()
        raise RuntimeError("At least one argument expected in {}".format(func))

    request_converter = None
    func_parameters = tuple(func_signature.parameters.items())[1 + len(additional_args):]

    if len(func_parameters):
        name, param = func_parameters[0]
        type_ = func_type_hints.get(name, Any)

        unpack_request = False  # type: Union[bool, Tuple[str, ...]]

        if issubclass(type_, Message) and name == 'request':
            # func(self, request: MessageType, ...)
            if request is None:
                request = type_
            elif request != type_ and 'request' not in request.__fields__:
                raise RuntimeError("Bad argument in {}: "
                                   "'{}' should be {}, but got {}".format(func, name, request, type_))

            for name, param in func_parameters[1:]:
                if param.default is Parameter.empty:
                    raise RuntimeError("Unexpected required argument in {}: '{}'".format(func, name))
        elif request is not None and name == 'request':
            for converter in converters:
                if converter.wire == request and converter.python == type_:
                    request_converter = converter
                    break

            if not request_converter:
                raise RuntimeError("Unable to coerce request message to python format: "
                                   "'{}' in {}".format(type_, func))
        else:  # func(self, arg: ?, ...)
            required_params, remaining_params = {}, {}
            for name, param in func_parameters:

                if param.default is Parameter.empty:
                    required_params[name] = func_type_hints.get(name, Any)
                else:
                    remaining_params[name] = (func_type_hints.get(name, Any), param.default)

            if request:  # unpack from request message
                message_params = set()

                for name, type_ in required_params.items():
                    if name not in request.__fields__:
                        raise RuntimeError("Unexpected required argument in {}: "
                                           "'{}' is not a field of {}".format(func, name, request))

                    field = request.__fields__[name]

                    # TODO support other mappings and sequences
                    # XXX support for list-within-a-list situations?
                    if isinstance(field, RepeatField):
                        field_type = List[field.items.type]
                    elif isinstance(field, MapField):
                        field_type = Dict[str, field.values.type]
                    elif isinstance(field, Field):
                        field_type = field.type
                    else:
                        raise NotImplementedError

                    if type_ not in (Any, field_type):
                        raise RuntimeError("Bad argument in {}: "
                                           "'{}' should be {}, but got {}".format(func, name, field_type, type_))

                    message_params.add((name, None))

                for name, (type_, default) in remaining_params.items():
                    if name in request.__fields__:
                        field = request.__fields__[name]

                        # TODO support other mappings and sequences
                        # XXX support for list-within-a-list situations?
                        if isinstance(field, RepeatField):
                            field_type = List[field.items.type]
                        elif isinstance(field, MapField):
                            field_type = Dict[str, field.values.type]
                        elif isinstance(field, Field):
                            field_type = field.type
                        else:
                            raise NotImplementedError

                        if type_ not in (Any, field_type):
                            raise RuntimeError("Bad argument in {}: "
                                               "'{}' should be {}, but got {}".format(func, name, field_type, type_))

                        message_params.add((name, default))

                if message_params == set(request.__fields__.keys()):
                    unpack_request = True
                else:
                    unpack_request = tuple(message_params)

            else:  # auto-generate message from params (uses converters where necessary)
                unpack_request = True
                raise NotImplementedError  # TODO
    else:  # func(self)
        if request is None:
            request = Empty

        # either request is Empty, or otherwise all request fields are discarded -- strange, but valid
        unpack_request = ()

    return_type = func_type_hints.get('return', Any)
    response_converter = None

    if response is None:
        if return_type in (Any, None, type(None)):  # None for Python 3.5 compatibility
            # TODO warn if Any: missing return type annotation (will discard return value)
            response = Empty
        elif issubclass(return_type, Message):
            response = return_type
        else:
            for converter in converters:
                if converter.python == return_type:
                    response_converter = converter
                    response = converter.wire
                    break

            if not response_converter:
                raise RuntimeError("Unable to coerce return value to wire format: "
                                   "'{}' in {}".format(return_type, func))
    elif return_type == Any:
        # NOTE missing return type annotation (assuming 'response' here)
        pass
    elif return_type in (None, type(None)):  # None for Python 3.5 compatibility
        response = Empty
    elif response != return_type:
        for converter in converters:
            if converter.wire == response and converter.python == return_type:
                response_converter = converter
                break

        if not response_converter:
            raise RuntimeError("Unable to coerce return value to wire format: "
                               "'{}' in {}".format(return_type, func))

    wrap_args = lambda req: (req,)
    wrap_kwargs = lambda req: {}

    if unpack_request is True:
        wrap_args = lambda req: ()
        wrap_kwargs = lambda req: req
    elif unpack_request is False:
        if request_converter:
            wrap_args = lambda req: (request_converter.convert(req),)
    elif unpack_request == ():
        wrap_args = lambda req: ()
    else:
        wrap_args = lambda req: ()
        wrap_kwargs = lambda req: {f: req.get(f, d) for f, d in unpack_request}

    if response == Empty and return_type != Empty:
        wrap_response = lambda res: Empty()
    elif response_converter:
        wrap_response = lambda res: response_converter.format(res)
    else:
        wrap_response = lambda res: res

    if not asyncio.iscoroutinefunction(func):
        func_ = func

        @wraps(func)
        async def func(*args, **kwargs):
            return func_(*args, **kwargs)

    # TODO (optimization) do not wrap what does not need to be wrapped
    if additional_args:
        async def invokable(inst, req: Message, loop: 'asyncio.BaseEventLoop' = None) -> Message:
            return wrap_response(await func(inst,
                                            *(await asyncio.gather(*[arg.resolve(inst, req)
                                                                     for arg in additional_args], loop=loop)),
                                            *wrap_args(req),
                                            **wrap_kwargs(req)))
    else:
        async def invokable(inst, req: Message, loop: 'asyncio.BaseEventLoop' = None) -> Message:
            return wrap_response(await func(inst, *wrap_args(req), **wrap_kwargs(req)))

    return MessageFunction(request, response, wraps(func)(invokable))
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# (C) 2005 British Broadcasting Corporation and Kamaelia Contributors(1)
#     All Rights Reserved.
#
# You may only modify and redistribute this under the terms of any of the
# following licenses(2): Mozilla Public License, V1.1, GNU General
# Public License, V2.0, GNU Lesser General Public License, V2.1
#
# (1) Kamaelia Contributors are listed in the AUTHORS file and at
#     http://kamaelia.sourceforge.net/AUTHORS - please extend this file,
#     not this notice.
# (2) Reproduced in the COPYING file, and at:
#     http://kamaelia.sourceforge.net/COPYING
# Under section 3.5 of the MPL, we are using this text since we deem the MPL
# notice inappropriate for this file. As per MPL/GPL/LGPL removal of this
# notice is prohibited.
#
# Please contact us via: kamaelia-list-owner@lists.sourceforge.net
# to discuss alternative licensing.
# -------------------------------------------------------------------------
"""\
=====================
Checkers Piece
=====================
TODO
"""

import Axon
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import sys
sys.path.append("../../Kamaelia/UI/OpenGL/")

from OpenGLComponent import *

class CheckersPiece(OpenGLComponent):

    def __init__(self, **argd):
        super(CheckersPiece, self).__init__(**argd)

        self.colour = argd.get("colour", (0,0,0))

    def setup(self):
        self.quadric = gluNewQuadric()

    def draw(self):
        glColor(0,0,0)
        gluCylinder(self.quadric,0.4,0.4,0.3,32,32);
        glColor(0.6,0,0)
        gluDisk(self.quadric,0,0.4,32,32);
        glTranslate(0,0,0.3)
        glColor(0.6,0,0)
        gluDisk(self.quadric,0,0.4,32,32);

if __name__=='__main__':

    from LiftTranslationInteractor import *
    from CheckersBoard import *

    display = OpenGLDisplay(viewerposition=(0,-10,0), lookat=(0,0,-15)).activate()
    OpenGLDisplay.setDisplayService(display)

    o1 = CheckersPiece(position=(0,0,-15)).activate()
    i1 = LiftTranslationInteractor(victim=o1, liftheight=0.2).activate()
    board = CheckersBoard(position=(0,0,-15)).activate()

    o1.link( (o1, "position"), (i1, "position"))
    i1.link( (i1, "outbox"), (o1, "rel_position"))

    Axon.Scheduler.scheduler.run.runThreads()
<commit_msg>Updated example to use CheckersInteractor.<commit_after>#!/usr/bin/env python
#
# (C) 2005 British Broadcasting Corporation and Kamaelia Contributors(1)
#     All Rights Reserved.
#
# You may only modify and redistribute this under the terms of any of the
# following licenses(2): Mozilla Public License, V1.1, GNU General
# Public License, V2.0, GNU Lesser General Public License, V2.1
#
# (1) Kamaelia Contributors are listed in the AUTHORS file and at
#     http://kamaelia.sourceforge.net/AUTHORS - please extend this file,
#     not this notice.
# (2) Reproduced in the COPYING file, and at:
#     http://kamaelia.sourceforge.net/COPYING
# Under section 3.5 of the MPL, we are using this text since we deem the MPL
# notice inappropriate for this file. As per MPL/GPL/LGPL removal of this
# notice is prohibited.
#
# Please contact us via: kamaelia-list-owner@lists.sourceforge.net
# to discuss alternative licensing.
# -------------------------------------------------------------------------
"""\
=====================
Checkers Piece
=====================
TODO
"""

import Axon
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import sys
sys.path.append("../../Kamaelia/UI/OpenGL/")

from OpenGLComponent import *

class CheckersPiece(OpenGLComponent):

    def __init__(self, **argd):
        super(CheckersPiece, self).__init__(**argd)

        self.colour = argd.get("colour", (0,0,0))

    def setup(self):
        self.quadric = gluNewQuadric()

    def draw(self):
        glColor(0,0,0)
        gluCylinder(self.quadric,0.4,0.4,0.3,32,32);
        glColor(0.6,0,0)
        gluDisk(self.quadric,0,0.4,32,32);
        glTranslate(0,0,0.3)
        glColor(0.6,0,0)
        gluDisk(self.quadric,0,0.4,32,32);

if __name__=='__main__':

    from LiftTranslationInteractor import *
    from CheckersBoard import *
    from CheckersInteractor import *

    display = OpenGLDisplay(viewerposition=(0,-10,0), lookat=(0,0,-15)).activate()
    OpenGLDisplay.setDisplayService(display)

    o1 = CheckersPiece(position=(0,0,-15)).activate()
    i1 = CheckersInteractor(victim=o1, liftheight=0.2).activate()
    board = CheckersBoard(position=(0,0,-15)).activate()

    o1.link( (o1, "position"), (i1, "position"))
    i1.link( (i1, "outbox"), (o1, "rel_position"))

    Axon.Scheduler.scheduler.run.runThreads()
<|endoftext|>


<commit_before># -*- encoding:utf-8 -*-
import os
import logging
from multiprocessing import Process

from db_models.datasets import Dataset
from db_models.models import Model
import deeplearning.prepare.prepare_for_imagenet
import deeplearning.prepare.prepare_for_lstm
import deeplearning.train.train_imagenet
import deeplearning.train.train_lstm

logger = logging.getLogger(__name__)

def run_imagenet_train(
    prepared_data_root,
    output_dir_root,
    dataset_id,
    model_id,
    epoch,
    pretrained_model,
    gpu_num,
    resize_mode,
    channels,
    avoid_flipping,
    batchsize
):
    dataset = Dataset.query.get(dataset_id)
    model = Model.query.get(model_id)
    model.dataset = dataset
    model.epoch = epoch
    model.resize_mode = resize_mode
    model.channels = channels
    model.gpu = gpu_num
    model.batchsize = batchsize
    model, train_image_num, val_image_num = deeplearning.prepare.prepare_for_imagenet.do(model, prepared_data_root)
    if model.framework == 'chainer':
        train_process = Process(
            target=deeplearning.train.train_imagenet.do_train_by_chainer,
            args=(
                model,
                output_dir_root,
                250,  # val_batchsize
                20,   # loader_job
                pretrained_model,
                avoid_flipping
            )
        )
    elif model.framework == 'tensorflow':
        train_process = Process(
            target=deeplearning.train.train_imagenet.do_train_by_tensorflow,
            args=(
                model,
                output_dir_root,
                500,  # val_batchsize
                pretrained_model,
                train_image_num,
                val_image_num,
                avoid_flipping
            )
        )
    else:
        raise Exception('Unknown framework')
    train_process.start()
    model.pid = train_process.pid
    model.update_and_commit()
    logging.info('start imagenet training. PID: ', model.pid)
    return

def run_lstm_train(
    prepared_data_root,
    output_dir_root,
    dataset_id,
    model_id,
    epoch,
    pretrained_model,
    gpu_num,
    use_wakatigaki,
    batchsize=50
):
    dataset = Dataset.query.get(dataset_id)
    model = Model.query.get(model_id)
    model.dataset = dataset
    model.epoch = epoch
    model.gpu = gpu_num
    model.enable_wakatigaki(use_wakatigaki)
    model.batchsize = batchsize
    (input_data_path, pretrained_vocab, model) = deeplearning.prepare.prepare_for_lstm.do(
        model, prepared_data_root, pretrained_model, use_wakatigaki)
    train_process = Process(
        target=deeplearning.train.train_lstm.do_train,
        args=(
            model,
            output_dir_root,
            input_data_path,
            pretrained_vocab,
            use_wakatigaki,
            pretrained_model,
            None,       # resume
            128,        # runsize
            2e-3,       # learning_rate
            0.97,       # learning_rate_decay
            10,         # learning_rate_decay_after
            0.95,       # decay_rate
            0.0,        # dropout
            50,         # seq_length
            batchsize,  # batchsize
            5           # grad_clip
        )
    )
    train_process.start()
    model.pid = train_process.pid
    model.update_and_commit()
    logging.info('start LSTM training. PID: ', model.pid)
<commit_msg>関数の呼び出し前にdb_modelの更新<commit_after># -*- encoding:utf-8 -*-
import os
import logging
from multiprocessing import Process

from db_models.datasets import Dataset
from db_models.models import Model
import deeplearning.prepare.prepare_for_imagenet
import deeplearning.prepare.prepare_for_lstm
import deeplearning.train.train_imagenet
import deeplearning.train.train_lstm

logger = logging.getLogger(__name__)

def run_imagenet_train(
    prepared_data_root,
    output_dir_root,
    dataset_id,
    model_id,
    epoch,
    pretrained_model,
    gpu_num,
    resize_mode,
    channels,
    avoid_flipping,
    batchsize
):
    dataset = Dataset.query.get(dataset_id)
    model = Model.query.get(model_id)
    model.dataset = dataset
    model.epoch = epoch
    model.resize_mode = resize_mode
    model.channels = channels
    model.gpu = gpu_num
    model.batchsize = batchsize
    model.update_and_commit()
    model, train_image_num, val_image_num = deeplearning.prepare.prepare_for_imagenet.do(model, prepared_data_root)
    if model.framework == 'chainer':
        train_process = Process(
            target=deeplearning.train.train_imagenet.do_train_by_chainer,
            args=(
                model,
                output_dir_root,
                250,  # val_batchsize
                20,   # loader_job
                pretrained_model,
                avoid_flipping
            )
        )
    elif model.framework == 'tensorflow':
        train_process = Process(
            target=deeplearning.train.train_imagenet.do_train_by_tensorflow,
            args=(
                model,
                output_dir_root,
                500,  # val_batchsize
                pretrained_model,
                train_image_num,
                val_image_num,
                avoid_flipping
            )
        )
    else:
        raise Exception('Unknown framework')
    train_process.start()
    model.pid = train_process.pid
    model.update_and_commit()
    logging.info('start imagenet training. PID: ', model.pid)
    return

def run_lstm_train(
    prepared_data_root,
    output_dir_root,
    dataset_id,
    model_id,
    epoch,
    pretrained_model,
    gpu_num,
    use_wakatigaki,
    batchsize=50
):
    dataset = Dataset.query.get(dataset_id)
    model = Model.query.get(model_id)
    model.dataset = dataset
    model.epoch = epoch
    model.gpu = gpu_num
    model.enable_wakatigaki(use_wakatigaki)
    model.batchsize = batchsize
    (input_data_path, pretrained_vocab, model) = deeplearning.prepare.prepare_for_lstm.do(
        model, prepared_data_root, pretrained_model, use_wakatigaki)
    train_process = Process(
        target=deeplearning.train.train_lstm.do_train,
        args=(
            model,
            output_dir_root,
            input_data_path,
            pretrained_vocab,
            use_wakatigaki,
            pretrained_model,
            None,       # resume
            128,        # runsize
            2e-3,       # learning_rate
            0.97,       # learning_rate_decay
            10,         # learning_rate_decay_after
            0.95,       # decay_rate
            0.0,        # dropout
            50,         # seq_length
            batchsize,  # batchsize
            5           # grad_clip
        )
    )
    train_process.start()
    model.pid = train_process.pid
    model.update_and_commit()
    logging.info('start LSTM training. PID: ', model.pid)
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
#  stat_tests.py:  testing the svn stat command
#
#  Subversion is a tool for revision control.
#  See http://subversion.tigris.org for more information.
#
# ====================================================================
# Copyright (c) 2000-2001 CollabNet.  All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.  The terms
# are also available at http://subversion.tigris.org/license-1.html.
# If newer versions of this license are posted there, you may use a
# newer version instead, at your option.
#
######################################################################

# General modules
import string, sys, os.path

# Our testing module
import svntest

Item = svntest.wc.StateItem

######################################################################
# Tests
#
#   Each test must return 0 on success or non-zero on failure.

#----------------------------------------------------------------------

def stat_unversioned_file_in_current_dir(sbox):
  "stat an unversioned file in the current directory"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir

  was_cwd = os.getcwd()
  try:
    os.chdir(wc_dir)

    svntest.main.file_append('foo', 'a new file')

    stat_output, err_output = svntest.main.run_svn(None, 'stat', 'foo')

    if len(stat_output) != 1:
      return 1

    if len(err_output) != 0:
      return 1

    return 0
  finally:
    os.chdir(was_cwd)

#----------------------------------------------------------------------

# regression for issue #590

def status_update_with_nested_adds(sbox):
  "run 'status -u' when nested additions are pending"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir

  # Make a backup copy of the working copy
  wc_backup = wc_dir + 'backup'
  svntest.actions.duplicate_dir(wc_dir, wc_backup)

  # Create newdir and newfile
  newdir_path = os.path.join(wc_dir, 'newdir')
  newfile_path = os.path.join(wc_dir, 'newdir', 'newfile')
  os.makedirs(newdir_path)
  svntest.main.file_append (newfile_path, 'new text')

  # Schedule newdir and newfile for addition
  svntest.main.run_svn(None, 'add', newdir_path)
  svntest.main.run_svn(None, 'add', newfile_path)

  # Created expected output tree for commit
  expected_output = svntest.wc.State(wc_dir, {
    'newdir' : Item(verb='Adding'),
    'newdir/newfile' : Item(verb='Adding'),
    })

  # Create expected status tree; all local revisions should be at 1,
  # but newdir and newfile should be at revision 2.
  expected_status = svntest.actions.get_virginal_state(wc_dir, 2)
  expected_status.tweak(wc_rev=1)
  expected_status.add({
    'newdir' : Item(status='_ ', wc_rev=2, repos_rev=2),
    'newdir/newfile' : Item(status='_ ', wc_rev=2, repos_rev=2),
    })

  # Commit.
  if svntest.actions.run_and_verify_commit (wc_dir, expected_output,
                                            expected_status, None,
                                            None, None, None, None, wc_dir):
    return 1

  # Now we go to the backup working copy, still at revision 1.
  # We will run 'svn st -u', and make sure that newdir/newfile is reported
  # as a nonexistent (but pending) path.

  # Create expected status tree; all local revisions should be at 1,
  # but newdir and newfile should be present with 'blank' attributes.
  expected_status = svntest.actions.get_virginal_state(wc_backup, 2)
  expected_status.tweak(wc_rev=1)

  # Verify status.  Notice that we're running status *without* the
  # --quiet flag, so the unversioned items will appear.
  # Unfortunately, the regexp that we currently use to parse status
  # output is unable to parse a line that has no working revision!  If
  # an error happens, we'll catch it here.  So that's a good enough
  # regression test for now.  Someday, though, it would be nice to
  # positively match the mostly-empty lines.
  return svntest.actions.run_and_verify_unquiet_status(wc_backup,
                                                       expected_status)

########################################################################
# Run the tests

# list all tests here, starting with None:
test_list = [ None,
              stat_unversioned_file_in_current_dir,
              status_update_with_nested_adds,
             ]

if __name__ == '__main__':
  svntest.main.run_tests(test_list)
  # NOTREACHED

### End of file.
# local variables:
# eval: (load-file "../../../../tools/dev/svn-dev.el")
# end:
<commit_msg>* subversion/tests/clients/cmdline/stat_tests.py<commit_after>#!/usr/bin/env python
#
#  stat_tests.py:  testing the svn stat command
#
#  Subversion is a tool for revision control.
#  See http://subversion.tigris.org for more information.
#
# ====================================================================
# Copyright (c) 2000-2001 CollabNet.  All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.  The terms
# are also available at http://subversion.tigris.org/license-1.html.
# If newer versions of this license are posted there, you may use a
# newer version instead, at your option.
#
######################################################################

# General modules
import string, sys, os.path

# Our testing module
import svntest

Item = svntest.wc.StateItem

######################################################################
# Tests
#
#   Each test must return 0 on success or non-zero on failure.

#----------------------------------------------------------------------

def stat_unversioned_file_in_current_dir(sbox):
  "stat an unversioned file in the current directory"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir

  was_cwd = os.getcwd()
  try:
    os.chdir(wc_dir)

    svntest.main.file_append('foo', 'a new file')

    stat_output, err_output = svntest.main.run_svn(None, 'stat', 'foo')

    if len(stat_output) != 1:
      return 1

    if len(err_output) != 0:
      return 1

    return 0
  finally:
    os.chdir(was_cwd)

#----------------------------------------------------------------------

# regression for issue #590

def status_update_with_nested_adds(sbox):
  "run 'status -u' when nested additions are pending"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir

  # Make a backup copy of the working copy
  wc_backup = wc_dir + 'backup'
  svntest.actions.duplicate_dir(wc_dir, wc_backup)

  # Create newdir and newfile
  newdir_path = os.path.join(wc_dir, 'newdir')
  newfile_path = os.path.join(wc_dir, 'newdir', 'newfile')
  os.makedirs(newdir_path)
  svntest.main.file_append (newfile_path, 'new text')

  # Schedule newdir and newfile for addition
  svntest.main.run_svn(None, 'add', newdir_path)
  svntest.main.run_svn(None, 'add', newfile_path)

  # Created expected output tree for commit
  expected_output = svntest.wc.State(wc_dir, {
    'newdir' : Item(verb='Adding'),
    'newdir/newfile' : Item(verb='Adding'),
    })

  # Create expected status tree; all local revisions should be at 1,
  # but newdir and newfile should be at revision 2.
  expected_status = svntest.actions.get_virginal_state(wc_dir, 2)
  expected_status.tweak(wc_rev=1)
  expected_status.add({
    'newdir' : Item(status='_ ', wc_rev=2, repos_rev=2),
    'newdir/newfile' : Item(status='_ ', wc_rev=2, repos_rev=2),
    })

  # Commit.
  if svntest.actions.run_and_verify_commit (wc_dir, expected_output,
                                            expected_status, None,
                                            None, None, None, None, wc_dir):
    return 1

  # Now we go to the backup working copy, still at revision 1.
  # We will run 'svn st -u', and make sure that newdir/newfile is reported
  # as a nonexistent (but pending) path.

  # Create expected status tree; all local revisions should be at 1,
  # but newdir and newfile should be present with 'blank' attributes.
  expected_status = svntest.actions.get_virginal_state(wc_backup, 2)
  expected_status.tweak(wc_rev=1)

  # Verify status.  Notice that we're running status *without* the
  # --quiet flag, so the unversioned items will appear.
  # Unfortunately, the regexp that we currently use to parse status
  # output is unable to parse a line that has no working revision!  If
  # an error happens, we'll catch it here.  So that's a good enough
  # regression test for now.  Someday, though, it would be nice to
  # positively match the mostly-empty lines.
  return svntest.actions.run_and_verify_unquiet_status(wc_backup,
                                                       expected_status)

#----------------------------------------------------------------------

# svn status -vN should include all entries in a directory
def status_shows_all_in_current_dir(sbox):
  "stat -vN and test if all items in the current directory show up"

  if sbox.build():
    return 1

  wc_dir = sbox.wc_dir
  was_cwd = os.getcwd()

  os.chdir(wc_dir)

  stat_output, err_output = svntest.main.run_svn(None, 'stat', '-vN')
  entries_in_wc = len(os.listdir("."))

  os.chdir(was_cwd)

  if (len(stat_output) != entries_in_wc):
    return 1

  return 0

########################################################################
# Run the tests

# list all tests here, starting with None:
test_list = [ None,
              stat_unversioned_file_in_current_dir,
              status_update_with_nested_adds,
              status_shows_all_in_current_dir,
             ]

if __name__ == '__main__':
  svntest.main.run_tests(test_list)
  # NOTREACHED

### End of file.
# local variables:
# eval: (load-file "../../../../tools/dev/svn-dev.el")
# end:
<|endoftext|>


<commit_before>'''
Created on June 14, 2012

@package: superdesk media archive image
@copyright: 2012 Sourcefabric o.p.s.
@license: http://www.gnu.org/licenses/gpl-3.0.txt
@author: Mugur Rus
'''

# --------------------------------------------------------------------

from setuptools import setup, find_packages

# --------------------------------------------------------------------

setup(
    name="media_archive_image",
    version="1.0",
    packages=find_packages(),
    install_requires=['ally_api >= 1.0', 'ally_core_sqlalchemy >= 1.0', 'gui_action >= 1.0',
                      'gui_core >= 1.0', 'internationalization >= 1.0'],
    platforms=['all'],
    zip_safe=True,
    package_data={
        '': ['*.gif', '*.png', '*.jpg', '*.jpeg', '*.js', '*.css', '*.less', '*.dust', '*.html', '*.jar'],
    },

    # metadata for upload to PyPI
    author="Gabriel Nistor",
    author_email="gabriel.nistor@sourcefabric.org",
    description="Media archive image plugin",
    long_description='Image files management functionality',
    license="GPL v3",
    keywords="Ally REST framework plugin Livedesk",
    url="http://www.sourcefabric.org/en/superdesk/", # project home page
)
<commit_msg>NR-436 : Image metadata reading tool not inserted in egg<commit_after>'''
Created on June 14, 2012

@package: superdesk media archive image
@copyright: 2012 Sourcefabric o.p.s.
@license: http://www.gnu.org/licenses/gpl-3.0.txt
@author: Mugur Rus
'''

# --------------------------------------------------------------------

from setuptools import setup, find_packages

# --------------------------------------------------------------------

setup(
    name="media_archive_image",
    version="1.0",
    packages=find_packages(),
    install_requires=['ally_api >= 1.0', 'ally_core_sqlalchemy >= 1.0', 'gui_action >= 1.0',
                      'gui_core >= 1.0', 'internationalization >= 1.0'],
    platforms=['all'],
    zip_safe=True,
    package_data={
        '': ['*.gif', '*.png', '*.jpg', '*.jpeg', '*.js', '*.css', '*.less', '*.dust', '*.html', '*.jar', '*.exe', '*.so*', '*.a*'],
    },

    # metadata for upload to PyPI
    author="Gabriel Nistor",
    author_email="gabriel.nistor@sourcefabric.org",
    description="Media archive image plugin",
    long_description='Image files management functionality',
    license="GPL v3",
    keywords="Ally REST framework plugin Livedesk",
    url="http://www.sourcefabric.org/en/superdesk/", # project home page
)
<|endoftext|>