Esempio n. 1
0
 def setupCertificate():
     if not Text.isTrue(Web.cocoscats.cfg["Web"]["UseHttps"]):
         return
     if Text.isTrue(Web.cocoscats.cfg["Web"]["RefreshCertificate"]):
         Security.deleteCertsAndKeys()
     if not Security.certsAndKeysExist():
         Security.createCertsAndKeys(Web.cocoscats.cfg["Web"]["Host"])
Esempio n. 2
0
 def __parseContentForOutputSRT(self, rawContent):
     tc = self.getTranslatorContentAsJson()
     counters = []
     markers = []
     subtitles = []
     for token in rawContent.split("\n"):
         token = token.strip()
         if Text.isNothing(token):
             continue
         if token.isdigit():
             counters.append(token)
         elif re.search("-->", token):
             markers.append(token)
     tc["L1L2"] = tc["L1L2"].strip()
     subtitle = ""
     for token in tc["L1L2"].split("\n"):
         token = token.strip()
         if Text.isNothing(token):
             subtitles.append(subtitle)
             subtitle = ""
         else:
             subtitle = "{0}\n{1}".format(subtitle, token)
     subtitles.append(subtitle)
     content = ""
     for i in range(0, len(counters)):
         content = "{0}\n{1}\n{2}{3}\n".format(content, counters[i],
                                               markers[i], subtitles[i])
     content = content.lstrip()
     return content
Esempio n. 3
0
 def verify(self):
     for name, value in self.cfg.items():
         if name == "ProjectID":
             if len(value) > 256 or Text.isNothing(value):
                 Error.raiseException(
                     "{0} can only be 256 characters or less: {1}".format(
                         name, value))
             if re.search(r'[^A-Za-z0-9_\-\\]', value):
                 Error.raiseException(
                     "{0} contains invalid characters: {1}".format(
                         name, value))
         if Text.isNothing(value):
             Error.raiseException("Missing '{0}' value in {1}".format(
                 name, self.cfgPath))
     pluginLookupMap = []
     for plugin in self.cfg["Plugin"]:
         pluginMethods = self.getPluginMethods(plugin["Type"],
                                               plugin["Name"])
         for pluginMethod in pluginMethods["Method"]:
             if not Framework.hasPluginClassMethod(
                     plugin["Type"], plugin["Name"], pluginMethod["Name"]):
                 Error.raiseException("Can't find {0}::{1}::{2}()".format(
                     plugin["Type"], plugin["Name"], pluginMethod["Name"]))
             pluginLookupMap.append("{0}{1}{2}".format(
                 plugin["Type"], plugin["Name"], pluginMethod["Name"]))
     if len(self.cfg["Workflow"]["Demo"]["Plugin"]) != len(
             self.cfg["Workflow"]["Demo"]["Method"]):
         Error.raiseException(
             "Mismatched number of demo plugins and methods")
     workflowPluginLookupMap = []
     for workflowPluginType, workflowPluginCfg in self.cfg[
             "Workflow"].items():
         pluginType = self.pluginTypeAlias[workflowPluginType]
         if pluginType != "Demo":
             workflowPluginLookupMap.append("{0}{1}{2}".format(
                 pluginType, workflowPluginCfg["Plugin"],
                 workflowPluginCfg["Method"]))
         else:
             for i in range(0, len(workflowPluginCfg["Plugin"])):
                 key = "{0}{1}{2}".format(pluginType,
                                          workflowPluginCfg["Plugin"][i],
                                          workflowPluginCfg["Method"][i])
                 if key not in pluginLookupMap:
                     Error.raiseException(
                         "Can't find workflow plugin {0}::{1}::{2}()".
                         format(workflowPluginType,
                                workflowPluginCfg["Plugin"][i],
                                workflowPluginCfg["Method"][i]))
Esempio n. 4
0
 def __runSetup(self):
     sourceURL = self.getWorkflowSource()
     url = self.__parseURL(sourceURL)
     if url is None:
         Exception("Invalid YouTube URL. Unable to ascertain Video ID: {0}".
                   format(sourceURL))
     params = {
         "CaptionID":
         None,
         "CaptionName":
         self.getPluginParamValue("CaptionName"),
         "Format":
         self.getPluginParamValue("Format"),
         "IsDraft":
         Text.isTrue(self.getPluginParamValue("IsDraft")),
         "L1":
         self.getPluginParamValue("L1"),
         "RefreshOAUTH2AccessToken":
         self.getPluginParamValue("RefreshOAUTH2AccessToken").lower() ==
         "true",
         "VaultPath":
         self.getVaultPath(),
         "URL":
         url,
     }
     api = YouTubeGoogleCaptionsApiCode()
     api.initialize(params)
     api.setCaptionID()
     return api
Esempio n. 5
0
    def __runDemo():
        if not Cli.cocoscats.isWorkflowDemoEnabled() or \
            Cli.cocoscats.getWorkflowDemoPluginCount() < 1:
            return
        Msg.show("Execute: Demo Stage")
        choices = Cli.cocoscats.getWorkflowDemoPluginChoices()
        menu = """-----------------------------------------------
Please make a selection to run demo or 'x' to exit.\n"""
        i = 1
        for choice in choices:
            menu = """{0}
[{1}]: {2}::{3}()""".format(menu, i, choice["Name"], choice["Method"])
            i += 1
        menu = """{0}
[x]: Exit
""".format(menu)
        errMsg = "Error: Valid options are: [1-{0}]".format(i - 1)
        while True:
            Msg.showRaw(menu)
            response = input()
            if response == "x" or response == "X":
                break
            if not Text.isInt(response):
                Msg.showRaw(errMsg)
                continue
            response = int(response)
            if response < 1 or response >= i:
                Msg.showRaw(errMsg)
                continue
            j = response - 1
            ret = Cli.cocoscats.runDemo(choices[j]["Name"],
                                        choices[j]["Method"])
            if ret:
                Msg.showWarning("Demo returned an error")
Esempio n. 6
0
 def setupPassword():
     if not Web.useAuthentication:
         return
     if Text.isTrue(Web.cocoscats.cfg["Web"]["RefreshPassword"]):
         Security.deletePassword()
     if not Security.hasPasswordFile():
         Security.createPassword()
Esempio n. 7
0
    def run(cocoscats):
        Web.cocoscats = cocoscats
        Web.useHttps = Text.isTrue(Web.cocoscats.cfg["Web"]["UseHttps"])
        Web.useAuthentication = Text.isTrue(
            Web.cocoscats.cfg["Web"]["UseAuthentication"])
        sessionOptions = {
            "session.type": "memory",
            "session.cookie_expires": 300,
            "session.auto": True
        }

        if Text.isTrue(Web.cocoscats.cfg["Web"]["Debug"]):
            WebSecurity.getSubresourceIntegrityHashes(True)

        Web.setupPassword()
        Web.setupCertificate()

        if Web.useHttps:
            Web.scheme = "https"
            Web.url = "{0}://{1}:{2}/".format(Web.scheme,
                                              Web.cocoscats.cfg["Web"]["Host"],
                                              Web.cocoscats.cfg["Web"]["Port"])
            server = WebSecurity(host=Web.cocoscats.cfg["Web"]["Host"],
                                 port=Web.cocoscats.cfg["Web"]["Port"])

            threading.Thread(target=bottle.run,
                             kwargs=dict(
                                 app=SessionMiddleware(bottle.app(),
                                                       sessionOptions),
                                 debug=Text.toTrueOrFalse(
                                     Web.cocoscats.cfg["Web"]["Debug"]),
                                 reloader=Text.toTrueOrFalse(
                                     Web.cocoscats.cfg["Web"]["Reloader"]),
                                 server=server)).start()
        else:
            Web.scheme = "http"
            Web.url = "{0}://{1}:{2}/".format(Web.scheme,
                                              Web.cocoscats.cfg["Web"]["Host"],
                                              Web.cocoscats.cfg["Web"]["Port"])
            threading.Thread(
                target=bottle.run,
                kwargs=dict(
                    app=SessionMiddleware(bottle.app(), sessionOptions),
                    debug=Text.toTrueOrFalse(
                        Web.cocoscats.cfg["Web"]["Debug"]),
                    host=Web.cocoscats.cfg["Web"]["Host"],
                    port=Web.cocoscats.cfg["Web"]["Port"],
                    reloader=Text.toTrueOrFalse(
                        Web.cocoscats.cfg["Web"]["Reloader"]))).start()
        Msg.flush()
        for client in Web.cocoscats.cfg["Web"]["Browser"]:
            if Text.isNothing(client) or client.lower() == "default":
                if webbrowser.open(Web.url):
                    break
            else:
                if webbrowser.get(client).open(Web.url):
                    break
Esempio n. 8
0
    def __init__(self, _scenesArray, _sceneIndex, _gameWindow):
        self.scenesArray = _scenesArray
        self.activeScene = self.scenesArray[_sceneIndex]
        self.window = _gameWindow
        self.activeScene.Start(self.window, self)

        ### DEBUG MODE ###
        self.debug = False
        self.mousePosIsoText = Text("FPS Text", 25, pygame.Vector2(1800, 0),
                                    self.window.display, (255, 0, 0))
Esempio n. 9
0
 def __Demo():
     WebApp.checkAuthentication()
     header = WebApp.getHeader("Demo")
     footer = WebApp.getFooter()
     if not Text.isTrue(Web.cocoscats.cfg["Workflow"]["Demo"]["Enable"]):
         return {"Error": True, "Message": "No demo found"}
     pluginName = Web.cocoscats.cfg["Workflow"]["Demo"]["Plugin"][0]
     pluginMethod = Web.cocoscats.cfg["Workflow"]["Demo"]["Method"][0]
     content = Web.cocoscats.runDemo(pluginName, pluginMethod)
     body = "Running demo"
     return """  """.format(header, body, footer)
Esempio n. 10
0
 def getHeader(title, meta="", css="", js=""):
     replaceHeader = {"title": title, "meta": meta, "css": css, "js": js}
     replaceMenu = {"LoginStatus": ""}
     replaceTitle = {"title": title}
     if Web.useAuthentication:
         if Text.isTrue(WebApp.__getSession("Authenticated")):
             replaceMenu[
                 "LoginStatus"] = """ | <a href="/Logout">Logout</a>"""
         else:
             replaceMenu[
                 "LoginStatus"] = """ | <a href="/Login">Login</a>"""
     return """{0}{1}{2}""".format(
         bottle.template("Web/Tpl/Header.tpl", replaceHeader),
         bottle.template("Web/Tpl/Menu.tpl", replaceMenu),
         bottle.template("Web/Tpl/Title.tpl", replaceTitle))
Esempio n. 11
0
 def __parseContentForInputSRT(self, rawContent):
     content = []
     newLineTracker = 0
     for token in rawContent.split("\n"):
         token = token.strip()
         if re.search("-->",
                      token) or token.isdigit() or Text.isNothing(token):
             if newLineTracker < 1:
                 content.append("")
             newLineTracker += 1
         else:
             content.append(token)
             newLineTracker = 0
     content = "\n".join(content).strip()
     return content
Esempio n. 12
0
 def __runView(projectID=None):
     WebApp.checkAuthentication()
     script = """<script src="/Web/Js/CocoscatsView.js"></script>"""
     header = WebApp.getHeader("View")
     footer = WebApp.getFooter(script)
     navigation = WebApp.getNavigation("View", 4, "View")
     demoHTML = ""
     if Text.isTrue(Web.cocoscats.cfg["Workflow"]["Demo"]["Enable"]):
         demoHTML = bottle.template("Web/Tpl/Demo.tpl", {})
     replace = {
         "projectID": Web.cocoscats.getProjectID(),
         "runDemo": demoHTML
     }
     body = """{0}{1}""".format(
         navigation, bottle.template("Web/Tpl/View.tpl", replace))
     return "{0}{1}{2}".format(header, body, footer)
Esempio n. 13
0
 def getWorkflowSourcePath(self):
     if Text.isNothing(self.cfg["Workflow"]["Input"]["Source"]):
         return None
     return self.cfg["Workflow"]["Input"]["Source"]
Esempio n. 14
0
 def getWorkflowInputSource(self):
     source = self.getWorkflowPlugin("Input")["Source"]
     if Text.isNothing(source):
         return None
     return source
Esempio n. 15
0
 def deleteCfg(self):
     if Text.isNone(self.cfgPath):
         return
     File.delete(self.cfgPath)
Esempio n. 16
0
 def __isAuthenticated():
     return Text.isTrue(WebApp.__getSession("Authenticated"))
Esempio n. 17
0
    def updateDatabase(self):
        #Database.connect()
        #projects = Database.getProject("MyProjectID")
        #print(projects)
        #Database.disconnect()
        #sys.exit()
        if not Text.isTrue(self.cfg["Database"]["Enable"]):
            Msg.showWarning("Database is NOT enabled in {0}".format(
                self.cfgPath))
            return
        Database.connect()
        Database.setDebug(Text.toTrueOrFalse(self.cfg["Database"]["Debug"]))
        with Database.ORM.db_session:
            records = Database.Table.Project.get(ID=self.getProjectID())
            if records is not None:
                records.delete()
                Database.commit()

            projectTable = Database.Table.Project(
                ID=self.getProjectID(),
                Title=Database.sanitize(self.cfg["Title"]),
                Description=Database.sanitize(self.cfg["Description"]),
                DateTime=self.frameworkParams["dateTime"],
                Workflow=self.cfg["Workflow"])

            inputTable = Database.Table.Input(
                ProjectID=projectTable,
                Content=Database.sanitize(
                    File.getContent(self.frameworkParams["inputPath"])),
                Source=Database.sanitize(
                    self.cfg["Workflow"]["Input"]["Source"]),
                PluginName=Database.sanitize(
                    self.cfg["Workflow"]["Input"]["Plugin"]),
                PluginMethod=Database.sanitize(
                    self.cfg["Workflow"]["Input"]["Method"]),
                Plugin=self.cfg["Workflow"]["Input"])

            analyzerTable = Database.Table.Analyzer(
                ProjectID=projectTable,
                Content=Database.sanitize(
                    File.getContent(self.frameworkParams["analyzerPath"])),
                PluginName=Database.sanitize(
                    self.cfg["Workflow"]["Analyzer"]["Plugin"]),
                PluginMethod=Database.sanitize(
                    self.cfg["Workflow"]["Analyzer"]["Method"]),
                Plugin=self.cfg["Workflow"]["Analyzer"])

            content = Database.sanitize(
                File.getContent(self.frameworkParams["translatorPath"]))
            translatorTable = Database.Table.Translator(
                ProjectID=projectTable,
                Content=content,
                ContentParsed=Result.parseTranslatorContent(content),
                PluginName=Database.sanitize(
                    self.cfg["Workflow"]["Translator"]["Plugin"]),
                PluginMethod=Database.sanitize(
                    self.cfg["Workflow"]["Translator"]["Method"]),
                Plugin=self.cfg["Workflow"]["Translator"])

            outputTable = Database.Table.Output(
                ProjectID=projectTable,
                Content=Database.sanitize(
                    File.getContent(self.frameworkParams["outputPath"])),
                Target=Database.sanitize(
                    self.cfg["Workflow"]["Output"]["Target"]),
                PluginName=Database.sanitize(
                    self.cfg["Workflow"]["Output"]["Plugin"]),
                PluginMethod=Database.sanitize(
                    self.cfg["Workflow"]["Output"]["Method"]),
                Plugin=self.cfg["Workflow"]["Output"])

        Database.disconnect()
Esempio n. 18
0
 def getWorkflowOutputTarget(self):
     target = self.getWorkflowPlugin("Output")["Target"]
     if Text.isNothing(target):
         return None
     return target
Esempio n. 19
0
 def getWorkflowTargetPath(self):
     if Text.isNothing(self.cfg["Workflow"]["Output"]["Target"]):
         return None
     return self.cfg["Workflow"]["Output"]["Target"]
Esempio n. 20
0
 def isWorkflowDemoEnabled(self):
     return Text.isTrue(self.cfg["Workflow"]["Demo"]["Enable"])
Esempio n. 21
0
    def initialize(self, params):
        self.__PARAMS = params
        self.__ASSETS_DIR = "{0}/__YouTube".format(
            os.path.dirname(os.path.realpath(__file__)).replace('\\', '/'))
        self.__CLIENT_API_CAPTIONS_FILE = "{0}/youtube-v3-api-captions.json".format(
            self.__ASSETS_DIR)
        if not os.path.isfile(self.__CLIENT_API_CAPTIONS_FILE):
            raise Exception(
                "Missing YouTube API file file. Download it from Google and put in this path: {0}"
                .format(self.__CLIENT_API_CAPTIONS_FILE))
        self.__DOWNLOADED_PATH = "{0}/downloadedCaptions.srt".format(
            self.__ASSETS_DIR)
        self.__TRANSLATED_PATH = "{0}/translatedCaptions.txt".format(
            self.__ASSETS_DIR)
        self.__CLIENT_SECRETS_FILE = "{0}/client_secrets.json".format(
            self.__PARAMS["VaultPath"])
        if not os.path.isfile(self.__CLIENT_SECRETS_FILE):
            raise Exception(
                "Missing client_secrets.json file. Download it from your Google account and put under the Cocoscat's Vault directory"
            )
        self.__CLIENT_OAUTH2_ACCESS_TOKEN_FILE = "{0}/client_secrets_oauth2.json".format(
            self.__PARAMS["VaultPath"])
        parser = argparse.ArgumentParser(parents=[argparser])
        parser.add_argument(
            "--videoid",
            help="ID for video for which the caption track will be uploaded.",
            default=self.__PARAMS["URL"]["VideoID"])
        parser.add_argument("--name",
                            help="Caption track name",
                            default=self.__PARAMS["CaptionName"])
        parser.add_argument("--file", help="Captions track file to upload")
        parser.add_argument("--language",
                            help="Caption track language",
                            default=self.__PARAMS["L1"])
        parser.add_argument(
            "--captionid",
            help="Required; ID of the caption track to be processed")
        parser.add_argument("--action", help="Action", default="all")

        # Using argsparse is dumb idea in a class but it's not my design. I need to include main client's args
        # until I can figure a better way here to do this here
        parser.add_argument("-c",
                            "--cfg",
                            metavar="'cfg'",
                            type=str,
                            default="cfg.json",
                            help="JSON configuration file")
        parser.add_argument("-C",
                            "--cli",
                            action="store_true",
                            help="Run command line interface")
        parser.add_argument("-W",
                            "--web",
                            action="store_true",
                            help="Run web interface")

        self.__ARGS_PARSER = parser

        if Text.isTrue(self.__PARAMS["RefreshOAUTH2AccessToken"]):
            File.delete(self.__CLIENT_OAUTH2_ACCESS_TOKEN_FILE)

        if not File.exists(self.__CLIENT_OAUTH2_ACCESS_TOKEN_FILE):
            self.generateOAUTH2AccessToken()
Esempio n. 22
0
 def isWorkflowDebugTrue(self, pluginType):
     if pluginType == "Demo":
         return False
     return Text.isTrue(self.cfg["Workflow"][pluginType]["Debug"])
Esempio n. 23
0
 def __initializeDatabase(self):
     Database.setName(self.cfg["Database"]["Name"])
     if Text.isTrue(self.cfg["Database"]["Rebuild"]):
         Database.drop()
     if not Database.exists():
         Database.create(True)
Esempio n. 24
0
 def getPluginParamValueAsTrueOrFalse(self, name):
     return Text.toTrueOrFalse(self.getPluginParamValue(name))