コード例 #1
0
ファイル: service.py プロジェクト: zaibacu/wutu
def create_service_js(stream, module):
    """
    Creates AngularJS service from module
    :param stream:
    :param module:
    :return:
    """
    from wutu.util import get_implemented_methods
    from wutu.compiler.grammar import Provider, Function, Object, SimpleDeclare, String, Expression, unwraps
    stream.write("wutu.factory('{0}Service', ['$http', ".format(module.__class__.__name__))
    http = Provider("$http")
    params = module.get_identifier()
    obj = Object()
    extended = "+ \"/\" + {0} + \"/\"".format(" + \"/\" + ".join(params)) if len(params) > 0 else ""
    full_url = "base_url() + url {0}".format(extended)
    obj.add_member("list", Function(None, *unwraps(http.get("base_url() + url"))))
    obj.add_member("get", Function(params + ("result",), *unwraps(http.get(full_url), "result")))
    obj.add_member("put", Function(("data", "result",), *unwraps(http.put("base_url() + url", "data"), "result")))
    obj.add_member("post", Function(params=params + ("data",), returns=http.post(full_url, "data")))
    obj.add_member("delete", Function(params=params, returns=http.delete(full_url)))
    impl = Function([http.name],
                    body=[SimpleDeclare("url", String(module.__name__), private=True), SimpleDeclare("service", obj, private=True)],
                    returns=Expression("service"))
    stream.write(impl.compile())
    stream.write("]);\n")
コード例 #2
0
ファイル: compiler_tests.py プロジェクト: zaibacu/wutu
 def test_function(self):
     from wutu.compiler.grammar import Function, String, SimpleDeclare, Expression
     fun = Function(["name"], [SimpleDeclare("hello_str", String("Hello, "))], Expression("hello_str + \" \" + name"))
     expected = """
     function(name){
         hello_str = "Hello, ";
         return hello_str + " " + name;
     }
     """
     compare(self.assertEqual, expected, fun.compile())
コード例 #3
0
ファイル: compiler_tests.py プロジェクト: zaibacu/wutu
 def test_function(self):
     from wutu.compiler.grammar import Function, String, SimpleDeclare, Expression
     fun = Function(["name"],
                    [SimpleDeclare("hello_str", String("Hello, "))],
                    Expression("hello_str + \" \" + name"))
     expected = """
     function(name){
         hello_str = "Hello, ";
         return hello_str + " " + name;
     }
     """
     compare(self.assertEqual, expected, fun.compile())
コード例 #4
0
ファイル: controller.py プロジェクト: zaibacu/wutu
def create_controller_js(stream, module):
    """
    Creates AngularJS controller from module
    :param stream:
    :param module:
    :return:
    """
    from wutu.util import get_implemented_methods
    from wutu.compiler.grammar import Provider, Function, SimpleDeclare
    stream.write("wutu.controller('{0}Controller', ".format(module.__class__.__name__))
    service = Provider("{0}Service".format(module.__class__.__name__))
    scope = Provider("$scope")
    methods = get_implemented_methods(module)
    entity_name = "{0}_list".format(module.get_entity_name())
    params = module.get_identifier()
    post_params = params + ("data",)
    put_params = ("data",)

    scope[entity_name] = service.list()
    scope.refresh = Function(None, body=[SimpleDeclare(scope[entity_name].compile(), service.list())])
    scope["get_{0}".format(module.get_entity_name())] = Function(params, returns=service.get(*params))
    scope["create_{0}".format(module.get_entity_name())] = Function(put_params, body=[service.put(*put_params), scope.refresh()])
    scope["update_{0}".format(module.get_entity_name())] = Function(post_params, body=[service.post(*post_params), scope.refresh()])
    scope["remove_{0}".format(module.get_entity_name())] = Function(params, body=[service.delete(*params), scope.refresh()])
    impl = Function([scope.name, service.name], body=scope.assignments)
    stream.write(impl.compile())
    stream.write(");")
コード例 #5
0
ファイル: compiler_tests.py プロジェクト: zaibacu/wutu
    def test_unwrap(self):
        from wutu.compiler.grammar import Function, Promise, Provider, String, unwraps
        http = Provider("$http")
        promise = http.get(String("http://google.com").compile())
        result = Function([], *unwraps(promise)).compile()
        expected = """
        function(){
            var result = [];
            if(result != undefined){
                $http.get("http://google.com").then(function(response){
                    angular.forEach(response.data,
                            function(val){
                                result.push(val);
                            })
                });
            }
            else {
                return $http.get("http://google.com").then(function(response){
                    return response.data;
                });
            }
            return result;
        }
        """

        compare(self.assertEqual, expected, result)
コード例 #6
0
ファイル: compiler_tests.py プロジェクト: zaibacu/wutu
 def test_promise(self):
     from wutu.compiler.grammar import Provider, Function, SimpleDeclare, String, Expression
     http = Provider("$http")
     result = http.get(String("http://google.com").compile()).resolve(
         Function(
             ["result"],
             body=[SimpleDeclare("$scope.test",
                                 Expression("result.data"))]))
     expected = """
     $http.get("http://google.com").then(function(result){
         $scope.test = result.data;
     });
     """
     compare(self.assertEqual, expected, result)
コード例 #7
0
ファイル: service.py プロジェクト: zaibacu/wutu
def create_service_js(stream, module):
    """
    Creates AngularJS service from module
    :param stream:
    :param module:
    :return:
    """
    from wutu.util import get_implemented_methods
    from wutu.compiler.grammar import Provider, Function, Object, SimpleDeclare, String, Expression, unwraps
    stream.write("wutu.factory('{0}Service', ['$http', ".format(
        module.__class__.__name__))
    http = Provider("$http")
    params = module.get_identifier()
    obj = Object()
    extended = "+ \"/\" + {0} + \"/\"".format(
        " + \"/\" + ".join(params)) if len(params) > 0 else ""
    full_url = "base_url() + url {0}".format(extended)
    obj.add_member("list",
                   Function(None, *unwraps(http.get("base_url() + url"))))
    obj.add_member(
        "get",
        Function(params + ("result", ), *unwraps(http.get(full_url),
                                                 "result")))
    obj.add_member(
        "put",
        Function((
            "data",
            "result",
        ), *unwraps(http.put("base_url() + url", "data"), "result")))
    obj.add_member(
        "post",
        Function(params=params + ("data", ),
                 returns=http.post(full_url, "data")))
    obj.add_member("delete",
                   Function(params=params, returns=http.delete(full_url)))
    impl = Function([http.name],
                    body=[
                        SimpleDeclare("url",
                                      String(module.__name__),
                                      private=True),
                        SimpleDeclare("service", obj, private=True)
                    ],
                    returns=Expression("service"))
    stream.write(impl.compile())
    stream.write("]);\n")