예제 #1
0
    def get_all(self):
        """Gets the complete list of colors."""
        if self._collection is None:
            file_path = self.get_data_path()

            # read the colors.json file (this simulates the data access, without data access layer)
            file_data = Scribe.read(file_path)
            self._collection = json.loads(file_data)

        return self._collection
    def get_all(self):
        """Gets the complete list of colors."""
        if self._collection is None:
            file_path = self.get_data_path()

            # read the colors.json file (this simulates the data access, without data access layer)
            file_data = Scribe.read(file_path)
            self._collection = json.loads(file_data)

        return self._collection
예제 #3
0
def load_resources_config():
    """
    Loads the resources configuration.
    """
    try:
        base_path = path.abspath(path.join(path.dirname(__file__), pardir))
        scripts_config = path.join(base_path, "configuration", "scripts.js")
        contents = Scribe.read(scripts_config)
        # extract the information that we care about
        contents = remove_comments(contents).replace("module.exports = {", "{")
        contents = Text.remove_line_breaks(contents)
        contents = re.sub(";\s*$", "", contents)
        data = json.loads(contents)
        return data
    except Exception as ex:
        print("ERROR: while loading the scripts configuration for the resources helper.")
        print(str(ex))
        raise
def load_resources_config():
    """
    Loads the resources configuration.
    :rtype : object
    """
    try:
        base_path = path.abspath(path.join(path.dirname(__file__), pardir))
        scripts_config = path.join(base_path, "configuration", "scripts.js")
        contents = Scribe.read(scripts_config)
        # extract the information that we care about
        contents = remove_comments(contents).replace("module.exports = {", "{")
        contents = Text.remove_line_breaks(contents)
        contents = re.sub(";\s*$", "", contents)
        data = json.loads(contents)
        return data
    except Exception as ex:
        print("ERROR: while loading the scripts configuration for the resources helper.")
        print(ex.message)
        raise
예제 #5
0
def get_templates_for_angular(path, all_html_files, appname, underscore_js_compile, comment):
    """
        Creates templates.js files for AngularJs
    """
    f = []
    pat = r"<!--template=\"([a-zA-Z0-9\\-]+)\"-->"
    f.append("//")
    f.append("//Knight generated templates file.")
    if comment is not None:
        f.append("// * ")
        f.append("// * " + comment)
        f.append("// * ")
    f.append("//")
    f.append("(function () {")

    f.append("  var o = {")
    k = len(all_html_files)
    i = 0
    for h in all_html_files:
        i += 1
        # get file content
        txt = Scribe.read(h)

        # check if the rx matches the contents
        m = re.search(pat, txt)
        if m:
            # get the template name from the group
            name = m.group(1)
            # remove the template name comment
            txt = re.sub(pat, "", txt)
        else:
            # get the filename with extension
            name = os.path.basename(h)
            # remove extension
            name = os.path.splitext(name)[0]

        # escape single quotes
        txt = re.sub("'", "\\'", txt)
        # condensate
        txt = Text.condensate(txt)

        f.append("    \'{0}\': \'{1}\'{2}".format(name, txt, "," if i < k else ""))
        
    f.append("  };")

    f.append("  var f = function(a) {")
    if underscore_js_compile is None or underscore_js_compile == "":
        #plain templates
        f.append("    var x;")
        f.append("    for (x in o) {")
        f.append("      a.put(x, o[x]);")
        f.append("    }")
    else:
        #templates run into UnderscoreJs template function
        f.append("    var ctx = {};".format(underscore_js_compile))
        f.append("    _.each(o, function (v, k) {")
        f.append("      a.put(k, _.template(v, ctx));")
        f.append("    });")

    f.append("  };")
    f.append("  f.$inject = ['$templateCache'];")
    f.append("  {0}.run(f);".format(appname))
    f.append("})();")

    code = "\n".join(f)
    # save templates.js
    outputPath = os.path.join(path, "templates.js")
    print("...saving file {}".format(outputPath))
    Scribe.write(code, outputPath)
예제 #6
0
def get_templates_for_angular(path, all_html_files, appname,
                              underscore_js_compile, comment):
    """
        Creates templates.js files for AngularJs
    """
    f = []
    pat = r"<!--template=\"([a-zA-Z0-9\\-]+)\"-->"
    f.append("//")
    f.append("//Knight generated templates file.")
    if comment is not None:
        f.append("// * ")
        f.append("// * " + comment)
        f.append("// * ")
    f.append("//")
    f.append("(function () {")

    f.append("  var o = {")
    k = len(all_html_files)
    i = 0
    for h in all_html_files:
        i += 1
        # get file content
        txt = Scribe.read(h)

        # check if the rx matches the contents
        m = re.search(pat, txt)
        if m:
            # get the template name from the group
            name = m.group(1)
            # remove the template name comment
            txt = re.sub(pat, "", txt)
        else:
            # get the filename with extension
            name = os.path.basename(h)
            # remove extension
            name = os.path.splitext(name)[0]

        # escape single quotes
        txt = re.sub("'", "\\'", txt)
        # condensate
        txt = Text.condensate(txt)

        f.append("    \'{0}\': \'{1}\'{2}".format(name, txt,
                                                  "," if i < k else ""))

    f.append("  };")

    f.append("  var f = function(a) {")
    if underscore_js_compile is None or underscore_js_compile == "":
        #plain templates
        f.append("    var x;")
        f.append("    for (x in o) {")
        f.append("      a.put(x, o[x]);")
        f.append("    }")
    else:
        #templates run into UnderscoreJs template function
        f.append("    var ctx = {};".format(underscore_js_compile))
        f.append("    _.each(o, function (v, k) {")
        f.append("      a.put(k, _.template(v, ctx));")
        f.append("    });")

    f.append("  };")
    f.append("  f.$inject = ['$templateCache'];")
    f.append("  {0}.run(f);".format(appname))
    f.append("})();")

    code = "\n".join(f)
    # save templates.js
    outputPath = os.path.join(path, "templates.js")
    print("...saving file {}".format(outputPath))
    Scribe.write(code, outputPath)
예제 #7
-1
def get_templates(path, all_html_files, underscore_js_compile, templates_variable, comment):
    """
        Creates templates.js files for KnockOut
    """
    f = []
    pat = r"<!--template=\"([a-zA-Z0-9\\-]+)\"-->"
    f.append("//")
    f.append("//Knight generated templates file.")
    if comment is not None:
        f.append("// * ")
        f.append("// * " + comment)
        f.append("// * ")
    f.append("//")
    f.append("if (!" + templates_variable + ") " + templates_variable + " = {};")
    f.append("(function (templates) {")

    f.append("  var o = {")

    k = len(all_html_files)
    i = 0
    for h in all_html_files:
        i += 1
        # get file content
        txt = Scribe.read(h)

        # check if the rx matches the contents
        m = re.search(pat, txt)
        if m:
            # get the template name from the group
            name = m.group(1)
            # remove the template name comment
            txt = re.sub(pat, "", txt)
        else:
            # get the filename with extension
            name = os.path.basename(h)
            # remove extension
            name = os.path.splitext(name)[0]

        # escape single quotes
        txt = re.sub("'", "\\'", txt)

        # condensate
        txt = Text.condensate(txt)

        f.append("    '{}': '{}'{}".format(name, txt, "," if i < k else ""))

    f.append("  };")

    if underscore_js_compile is None or underscore_js_compile == "":
        # plain templates
        f.append("  var x;")
        f.append("  for (x in o) {")
        f.append("    templates[x] = o[x];")
        f.append("  }")
    else:
        # templates run into UnderscoreJs template function
        f.append("  var ctx = {};".format(underscore_js_compile))
        f.append("  _.each(o, function (v, k) {")
        f.append("    x[k] = _.template(v, ctx);")
        f.append("  });")

    f.append("})(" + templates_variable + ");")

    code = "\n".join(f)

    # save templates.js
    outputPath = os.path.join(path, "templates.js")
    print("...saving file {}".format(outputPath))
    Scribe.write(code, outputPath)