Exemple #1
0
def csharp_parameters(parameters: List[str]) -> StringBuilder:
    parameters = parameters if parameters else []
    parameters_len = len(parameters)
    sb = StringBuilder()
    if parameters_len > 1:
        sb.nl()
        for i, p in enumerate(parameters):
            sb.wt(p, addtl_indent_len=1)
            sb.wl("," if i < parameters_len - 1 else "")
    elif parameters_len == 1:
        p = parameters[0]
        sb.wt(p)
    return sb
Exemple #2
0
def csharp_doc(*args: str) -> StringBuilder:
    sb = StringBuilder()
    sb.wl("/// <summary>")
    for arg in args:
        sb.wl(f"/// {sb.indent}{arg}")
    sb.wl("/// </summary>")
    return sb
Exemple #3
0
 def __init__(self,
              name: str,
              attributes: Dict[str, str] = None,
              content: Union[str, List[XmlElementBuilder]] = None,
              indent_len: int = None,
              indent_space_len: int = None,
              indent_type: IndentType = None,
              rstrip: bool = None) -> None:
     attributes = attributes if attributes is not None else {}
     StringBuilder.__init__(self,
                            indent_len=indent_len,
                            indent_space_len=indent_space_len,
                            indent_type=indent_type,
                            rstrip=rstrip)
     self.name = name
     self.attributes = attributes
     self.content = content
Exemple #4
0
def markdown_comment(*args: str, padded: bool = True) -> StringBuilder:
    sb = StringBuilder()
    if args:
        if padded:
            sb.nl()
        for arg in args:
            sb.wl(f"[//]: # ({arg})")
        if padded:
            sb.nl()
    return sb
Exemple #5
0
def xml_attributes(attributes: List[str]) -> StringBuilder:
    attributes = attributes if attributes else []
    attributes_len = len(attributes)
    sb = StringBuilder()
    if attributes_len > 1:
        sb.nl()
        for i, a in enumerate(attributes):
            sb.wl(a, addtl_indent_len=1)
    elif attributes_len == 1:
        a = attributes[0]
        sb.wt(a)
    return sb
Exemple #6
0
def csharp_usings(*args: str, **kwargs: str) -> StringBuilder:
    sb = StringBuilder()
    for arg in args:
        sb.wl(f"using {arg};")
    for k, v in kwargs.items():
        sb.wl(f"using {k} = {v};")
    return sb
Exemple #7
0
def csharp_try_catch(
    catch_var_name: str = "Exception ex"
) -> (StringBuilder, StringBuilder, StringBuilder):
    sb = StringBuilder()
    csb_try = CSharpBlockBuilder("try")
    sb.wb(csb_try)
    csb_catch = CSharpBlockBuilder(f"catch ({catch_var_name})")
    sb.wb(csb_catch)
    return sb, csb_try, csb_catch
Exemple #8
0
def get_text(template: str,
             expressions: Dict[str, str],
             template_name: str = None) -> str:
    sb = StringBuilder()

    # Add a timestamp when the template is a markdown file.
    if template_name.casefold().endswith(".md"):
        from scriptgen.templates.markdown import markdown_autogen
        sb.wb(markdown_autogen())
        sb.nl()

    # Replace placeholders.
    # i.e. replace placeholders found in the text with values found in the expressions dictionary.
    # ex: ${SOME_KEY} → ACTUAL_VALUE
    interpolated_text = interpolate_text(template, expressions)

    # Write the interpolated text into the builder.
    sb.wl(interpolated_text)

    return str(sb)
Exemple #9
0
def csharp_comment(*args: str) -> StringBuilder:
    sb = StringBuilder()
    for arg in args:
        sb.wl(f"// {arg}")
    return sb
Exemple #10
0
def get_text(template: str,
             expressions: Dict[str, str],
             template_name: str = None) -> str:
    sb = StringBuilder()

    # Add a timestamps.
    if template_name.casefold().endswith(".md"):
        from scriptgen.templates.markdown import markdown_autogen
        sb.wb(markdown_autogen())
        sb.nl()
    elif template_name.casefold().endswith(".py"):
        from scriptgen import timestamp
        sb.wls(["#!/usr/bin/env python3", "# -*- coding: utf-8 -*-"])
        sb.nl()
        sb.wl(f"# Auto-generated: {timestamp()}")
        sb.nl()

    # Replace placeholders.
    # i.e. replace placeholders found in the text with values found in the expressions dictionary.
    # ex: ${SOME_KEY} → ACTUAL_VALUE
    interpolated_text = interpolate_text(template, expressions)

    # Write the interpolated text into the builder.
    sb.wl(interpolated_text)

    return str(sb)
Exemple #11
0
def xml_declaration(version: str = "1.0",
                    encoding: str = "UTF-8") -> StringBuilder:
    sb = StringBuilder()
    sb.wl(f'<?xml version="{version}" encoding="{encoding}" ?>')
    return sb
Exemple #12
0
def xml_comment(*args: str) -> StringBuilder:
    sb = StringBuilder()
    if args:
        for arg in args:
            sb.wl(f"<!-- {arg} -->")
    return sb
Exemple #13
0
 def build(self, rstrip: bool = None) -> str:
     rstrip = self.rstrip if rstrip is None else rstrip
     attributes = [f'{k}="{v}"' for k, v in self.attributes.items()]
     attributes_len = len(attributes)
     sb = StringBuilder()
     sb.wt(f"<{self.name}")
     if attributes:
         sb.wt("" if attributes_len > 1 else " ")
         sb.wb(xml_attributes(attributes), rstrip=True)
     if self.content is None:
         sb.wl(" />")
     else:
         sb.wt(">")
         if isinstance(self.content, str):
             if attributes_len > 1:
                 sb.nl()
             sb.wt(f"{self.content}",
                   addtl_indent_len=1 if attributes_len > 1 else 0)
             if attributes_len > 1:
                 sb.nl()
         elif isinstance(self.content, List) and \
                 all(isinstance(c, XmlElementBuilder) for c in self.content):
             sb.nl()
             bb = BlockBuilder()
             for c in self.content:
                 bb.wb(c, rstrip)
             sb.wb(bb)
         sb.wl(f"</{self.name}>")
     return sb.build(rstrip)