def _serialize_frames(filename,
                      function,
                      pre_context,
                      post_context,
                      context_line,
                      variables,
                      lineno,
                      pre_context_lineno,
                      is_opened=False,
                      **opts):

    if filename:
        description = wl.Row([
            wl.Button(
                wl.Style(filename,
                         wl.RGBColor(0.25, 0.48, 1),
                         wl.Small,
                         FontFamily="Courier"),
                wl.If(wl.FileExistsQ(filename), wl.SystemOpen(filename)),
                Appearance="Frameless",
            ),
            " in ",
            function,
        ])
    else:
        description = function

    yield wl.OpenerView(
        [
            description,
            wl.Column(
                iterate(
                    (wl.Column(
                        iterate(
                            (_paginate(pre_context_lineno + i, l)
                             for i, l in enumerate(pre_context)),
                            [
                                wl.Item(
                                    _paginate(lineno, context_line),
                                    Background=wl.LightYellow,
                                )
                            ],
                            (_paginate(lineno + i + 1, l)
                             for i, l in enumerate(post_context)),
                        ),
                        Background=[[wl.GrayLevel(0.95),
                                     wl.GrayLevel(1)]],
                        Frame=wl.LightGray,
                    ), ),
                    _serialize_variables(variables),
                )),
        ],
        is_opened,
    )
Example #2
0
 def pull(self):
     assert weval(wl.FileExistsQ(wl.CloudObject(self.file.abspath)))
     self.file.deleteIfExists()
     self.file.mkparents()
     if self.file.isdir:
         return weval(
             wl.CopyDirectory(CloudObject(self.file.abspath),
                              self.file.abspath))
     else:
         return weval(
             wl.CopyFile(wl.CloudObject(self.file.abspath),
                         self.file.abspath))
Example #3
0
 def push(self, permissions=PERMISSIONS.PRIVATE):
     if self.file.isdir:
         if weval(wl.DirectoryQ(CloudObject(self.file.abspath))):
             weval(
                 wl.DeleteDirectory(CloudObject(self.file.abspath),
                                    Rule(wl.DeleteContents, True)))
         return weval(
             wl.CopyDirectory(
                 self.file.abspath,
                 CloudObject(self.file.abspath,
                             Rule(wl.Permissions, permissions))))
     else:
         if weval(wl.FileExistsQ(wl.CloudObject(self.file.abspath))):
             weval(wl.DeleteFile(wl.CloudObject(self.file.abspath)))
         return weval(
             wl.CopyFile(
                 self.file.abspath,
                 wl.CloudObject(self.file.abspath,
                                wl.Rule(wl.Permissions, permissions))))
Example #4
0
    def __init__(self,
                 database,
                 *args,
                 allow_get=True,
                 allow_set=True,
                 password=None,
                 **kwargs):
        apiFile = self.apifile_for(database.file)

        self.database = database

        self.allow_get = allow_get
        self.allow_set = allow_set

        self.password = password

        super().__init__(apiFile, *args, **kwargs)

        if not self.offline_mode:
            weval(
                If(
                    wl.Not(
                        wl.FileExistsQ(
                            wl.CloudObject("APIPasswords",
                                           wlexpr("$CloudSymbolBase")))),
                    wlexpr('CloudSymbol["APIPasswords"]=<||>')))
            weval(
                wlexpr(
                    f'pwds = CloudSymbol["APIPasswords"]; pwds["{apiFile.abspath}"] = "{self.password}"; CloudSymbol["APIPasswords"] = pwds;'
                ))
        else:
            warn(
                f'not pushing password for {self} because offline mode is switched one'
            )
        apiFile.parent[f"{apiFile.name_pre_ext}_api_doc.text"].write(
            f"HTTP GET: {apiFile.wcurl}?<url encoded json obj>")
Example #5
0
def FileExistsQ(*args):
    return wl.FileExistsQ(*args)
Example #6
0
File: api.py Project: mgroth0/mlib
    def __post_init__(self):
        self.service = WolframService()
        self.build_api_fun(self.service)
        expression = APIFunction(
            [
                APIRule("message", "String"),
                APIRule("index", "Integer"),
                APIRule("total", "Integer"),
                APIRule("messageID", "String"),
            ],
            Function(
                wlexprc('xx'),
                wlblock(
                    wlexpr('CloudSymbol["APILog"] = "started api base"'),
                    If(
                        wl.Not(
                            wl.FileExistsQ(
                                wl.CloudObject("APIMessages",
                                               wlexpr("$CloudSymbolBase")))),
                        wlexpr('CloudSymbol["APIMessages"]=<||>')),
                    wl.For(
                        wlexpr('i=1'),
                        wlexpr('i<=Length@Keys[CloudSymbol["APIMessages"]]'),
                        wlexpr('i++'),
                        If(
                            wlexpr(
                                '((UnixTime[] * 1000) - ToExpression[StringSplit[(Keys[CloudSymbol["APIMessages"]])[[i]],"***"][[2]]]) > 60000'
                            ),
                            wlexpr(
                                'apiMessages = CloudSymbol["APIMessages"]; apiMessages = KeyDrop[apiMessages,Keys[CloudSymbol["APIMessages"]][[i]]]; CloudSymbol["APIMessages"] = apiMessages;'
                            ))),
                    If(
                        wl.Not(
                            wl.KeyExistsQ(wl.CloudSymbol("APIMessages"),
                                          wlexpr('xx["messageID"]'))),
                        wlexpr(
                            'APIMessages=CloudSymbol["APIMessages"]; APIMessages[xx["messageID"]] = {}; CloudSymbol["APIMessages"] = APIMessages;'
                        )),
                    wlexpr(
                        'thisMessageData = <|"i"->xx["index"],"t"->xx["total"],"m"->xx["message"]|>; APIMessages=CloudSymbol["APIMessages"]; myMessages = APIMessages[xx["messageID"]]; myMessages = Append[myMessages,thisMessageData]; APIMessages[xx["messageID"]] = myMessages; CloudSymbol["APIMessages"] = APIMessages;'
                    ),
                    If(
                        wlexpr('thisMessageData["i"]==thisMessageData["t"]'),
                        wlblock(
                            wlexpr(
                                'fullMessage = StringJoin[Map[Function[xxxx,xxxx["m"]],CloudSymbol["APIMessages"][xx["messageID"]]]]'
                            ),
                            wlexpr(
                                'fullMessage = ImportString[fullMessage,"RawJSON"]'
                            ),
                            *self.service.build_expressions(),
                        )),
                )))
        expression = FormatWLInput(inputForm(expression))

        self.apiFile = File(self.apiFile)
        assert self.apiFile.ext == 'wl', f'extension of {self.apiFile} is {self.apiFile.ext}'
        self.apiURL = self.apiFile.wcurl
        self.apiFile.write(expression)
        if not self.offline_mode:
            MWL.cloud_deploy(expression, self.apiFile, PERMISSIONS.PUBLIC)
        else:
            warn(f'not deploying {self} because offline mode is switched one')
        log(f'{self.apiURL=}')