Пример #1
0
def _createPackage(templateDir, packageName):

    # Create new package output directory
    pkgRoot = os.path.join(config.bldDir(), packageName)
    os.mkdir(pkgRoot)
    formatSubs = {ixtemplate.tag: packageName}

    # Copy files over
    templateDirLen = len(templateDir) + 1
    for root, dirs, files in os.walk(templateDir):
        # determine the new root name and make the appropriate name substitution
        newRoot, _ = ixre.subf(os.path.join(pkgRoot, root[templateDirLen:]),
                               formatSubs)
        # Rename new directories that are created
        for d in dirs:
            newDir, _ = ixre.subf(d, formatSubs)
            os.mkdir(os.path.join(newRoot, newDir))

        # Any files that are copied over have a format substitution
        for f in files:
            # Read the old file
            text = ixfs.read(os.path.join(root, f))

            # Make the substitutions in new text
            text, _ = ixre.subf(text, formatSubs)

            # Get the new filename
            filename, _ = ixre.subf(f, formatSubs)

            # Save in a new file
            ixfs.write(os.path.join(newRoot, filename), text)

    return pkgRoot
Пример #2
0
def append_to_line(fp, line_no, append_text):
  file_text = ixfs.read(fp)

  for ndx, match in enumerate(_line_pttrn.finditer(file_text, 0)):

    if ndx == line_no - 1:
      ixfs.write(fp, "".join([
          file_text[:match.start()],
          append_text,
          file_text[match.start():]]))
      return

  print(f"Unable to find line number {line_no} in ({fp}")
Пример #3
0
def subr(directory, file_regex, search_regex, replace_regex):
  """Recursively search through a directory and search and replace in each file.

  Params:
    directory: str
      Directory to start the search
    search_regex: str
      pattern to match in the file
    replace_regex: str
      pattern to replace in the file
    file_regex: str
      regex pattern to match files
  """

  # Compile patterns
  search_pttrn = re.compile(search_regex)
  file_pttrn = re.compile(file_regex)

  # Recursively search through directory
  total_subs = 0
  for root, _, files in os.walk(directory):
    for f in files:

      # If a pattern is supplied and it isn't matched, skip the file
      if file_pttrn is not None and file_pttrn.search(f) is None:
        continue

      # Read the file
      filepath = os.path.join(root, f)
      text = fs.read(filepath)

      # Make the appropriate substitutions
      text, n = search_pttrn.subn(replace_regex, text)
      if n > 0:
        print(f"({n}) substitutions in file({filepath})")
        total_subs += n

        fs.write(filepath, text)

  print(f"Total substitutions: {total_subs}")
Пример #4
0
def apply_template(template, src_fp, dst_dir, fmt_subs=None):

    # Substitution map
    if fmt_subs is None:
        fmt_subs = {tag: template}
    else:
        fmt_subs[tag] = template

    # read the file
    text = ixfs.read(src_fp)

    # make the substitutions
    text, _ = ixre.subf(text, fmt_subs)

    # Get the templated name
    dst_fp = os.path.join(dst_dir,
                          ixre.subf(os.path.basename(src_fp), fmt_subs)[0])

    # write out the destination file
    ixfs.write(dst_fp, text)

    return dst_fp
Пример #5
0
def multiTemplate(srcFilename, dstFilename, *templates):
    """Create multiple files from one template"""

    # Creat substitution map
    fmt_subs = {tag: ""}

    # Get outfile name
    filepath = os.path.join(config.bldDir(), dstFilename)
    ixfs.write(filepath, "")  # clear the file

    for template in templates:
        # Build sub map
        fmt_subs[tag] = template

        # read the file
        text = ixfs.read(ixfs.findr(config.srcDir(), srcFilename))

        # make the substitutions
        text, _ = ixre.subf(text, fmt_subs)

        # append to file
        ixfs.append(filepath, text)

    return filepath
Пример #6
0
def generateTemplate(src_fp, dst_dir, key, value=None):

    # read the file
    text = ixfs.read(src_fp)

    # check if there is a different substitution value
    if value is None:
        value_camel = tag
        value_pascal = Tag
        value_const = TAG
    else:
        value_camel = ixre.toCamel(value)
        value_pascal = ixre.toPascal(value)
        value_const = ixre.toConst(value)

    # Make the substitutions
    text = ixre.to_template(ixre.toCamel(key), value_camel, text)
    text = ixre.to_template(ixre.toPascal(key), value_pascal, text)
    text = ixre.to_template(ixre.toConst(key), value_const, text)

    filename = ixre.to_template(key, Tag, os.path.basename(src_fp))

    # write out the file
    ixfs.write(os.path.join(dst_dir, filename), text)
Пример #7
0
def substitute_multi_template(dst_dir, key, value, *src_fps):
    subs = ixre.to_fmt_dict(key, value)
    for src_fp in src_fps:
        ixfs.write(
            os.path.join(dst_dir, ixre.subkv(subs, os.path.basename(src_fp))),
            ixre.subkv(subs, ixfs.read(src_fp)))