Example #1
0
def prompt(message, default_value=None, valid_list=None):
    value = None

    if valid_list is not None:
        list_str = '{%s}' % '|'.join(map(str, valid_list))
        message = '%s %s' % (message, list_str)

    if default_value is not None:
        message = '%s [default=%s]' % (message, default_value)

    if supports_color():
        message = colorize('[ECM] ', fg='cyan', opts=('bold',)) + colorize(message, fg='magenta') + ' '
    else:
        message = '[ECM] ' + message + ' '

    if valid_list is not None:
        while value not in valid_list:
            value = raw_input(message)
            if not value:
                value = default_value
    else:
        while not value:
            value = raw_input(message)
            if not value:
                value = default_value

    return value
 def colors(self):
     b = supports_color() or os.environ.get(
         'PYCHARM_HOSTED') or os.environ.get('PYCHARM_DJANGO_MANAGE_MODULE')
     return {
         'yellow': '\033[033m' if b else '',
         'reset': '\033[0m' if b else ''
     }
Example #3
0
def prompt(message, default_value=None, valid_list=None):
    value = None

    if valid_list is not None:
        list_str = '{%s}' % '|'.join(map(str, valid_list))
        message = '%s %s' % (message, list_str)

    if default_value is not None:
        message = '%s [default=%s]' % (message, default_value)

    if supports_color():
        message = colorize('[ECM] ', fg='cyan', opts=('bold', )) + colorize(
            message, fg='magenta') + ' '
    else:
        message = '[ECM] ' + message + ' '

    if valid_list is not None:
        while value not in valid_list:
            value = raw_input(message)
            if not value:
                value = default_value
    else:
        while not value:
            value = raw_input(message)
            if not value:
                value = default_value

    return value
Example #4
0
def color_style():
    if color.supports_color():
        style = color.color_style()
        style.METRIC = termcolors.make_style(opts=("bold",))
        style.ES_TEMPLATE = termcolors.make_style(fg="yellow")
    else:
        style = no_style()
    return style
Example #5
0
def color_style():
    style = color.color_style()
    if color.supports_color():
        style.URL = termcolors.make_style(fg='green', opts=('bold',))
        style.MODULE = termcolors.make_style(fg='yellow')
        style.MODULE_NAME = termcolors.make_style(opts=('bold',))
        style.URL_NAME = termcolors.make_style(fg='red')
    return style
Example #6
0
def color_style():
    style = color.color_style()
    if color.supports_color():
        style.URL = termcolors.make_style(fg='green', opts=('bold', ))
        style.MODULE = termcolors.make_style(fg='yellow')
        style.MODULE_NAME = termcolors.make_style(opts=('bold', ))
        style.URL_NAME = termcolors.make_style(fg='red')
    return style
Example #7
0
def color_style():
    style = color.color_style()
    if color.supports_color():
        style.INFO = termcolors.make_style(fg="green")
        style.WARN = termcolors.make_style(fg="yellow")
        style.BOLD = termcolors.make_style(opts=("bold",))
        style.URL = termcolors.make_style(fg="green", opts=("bold",))
        style.MODULE = termcolors.make_style(fg="yellow")
        style.MODULE_NAME = termcolors.make_style(opts=("bold",))
        style.URL_NAME = termcolors.make_style(fg="red")
    return style
Example #8
0
def color_style():
    if color.supports_color():
        style = color.color_style()
        style.INFO = termcolors.make_style(fg='green')
        style.WARN = termcolors.make_style(fg='yellow')
        style.BOLD = termcolors.make_style(opts=('bold', ))
        style.URL = termcolors.make_style(fg='green', opts=('bold', ))
        style.MODULE = termcolors.make_style(fg='yellow')
        style.MODULE_NAME = termcolors.make_style(opts=('bold', ))
        style.URL_NAME = termcolors.make_style(fg='red')
    else:
        style = no_style()
    return style
Example #9
0
def color_style():
    if color.supports_color():
        style = color.color_style()
        style.INFO = termcolors.make_style(fg='green')
        style.WARN = termcolors.make_style(fg='yellow')
        style.BOLD = termcolors.make_style(opts=('bold',))
        style.URL = termcolors.make_style(fg='green', opts=('bold',))
        style.MODULE = termcolors.make_style(fg='yellow')
        style.MODULE_NAME = termcolors.make_style(opts=('bold',))
        style.URL_NAME = termcolors.make_style(fg='red')
    else:
        style = no_style()
    return style
Example #10
0
 def __init__(self, *args, **kwargs):
     super(CommandMixin, self).__init__(*args, **kwargs)
     self.argv = kwargs.get("argv") or sys.argv[:]
     self.package = self.__class__.__module__.split(".")[0]
     self.basecommand = self.argv[1]
     try:
         self.usercommand = self.argv[2]
     except IndexError:
         self.usercommand = ""
     if supports_color():
         opts = ('bold',)
         self.style.DESTROY = termcolors.make_style(fg='red', opts=opts)
         self.style.NOTFOUND = termcolors.make_style(fg='white', opts=opts)
         self.style.NOTEMPTY = termcolors.make_style(fg='black', opts=opts)
Example #11
0
 def __init__(self, *args, **kwargs):
     super(CommandMixin, self).__init__(*args, **kwargs)
     self.argv = kwargs.get("argv") or sys.argv[:]
     self.package = self.__class__.__module__.split(".")[0]
     self.basecommand = self.argv[1]
     try:
         self.usercommand = self.argv[2]
     except IndexError:
         self.usercommand = ""
     if supports_color():
         opts = ('bold', )
         self.style.DESTROY = termcolors.make_style(fg='red', opts=opts)
         self.style.NOTFOUND = termcolors.make_style(fg='white', opts=opts)
         self.style.NOTEMPTY = termcolors.make_style(fg='black', opts=opts)
Example #12
0
def color_style():
    style = color.color_style()
    if color.supports_color():
        style.INFO = termcolors.make_style(fg='green')
        style.WARN = termcolors.make_style(fg='yellow')
        style.BOLD = termcolors.make_style(opts=('bold',))
        style.URL = termcolors.make_style(fg='green', opts=('bold',))
        style.MODULE = termcolors.make_style(fg='yellow')
        style.MODULE_NAME = termcolors.make_style(opts=('bold',))
        style.URL_NAME = termcolors.make_style(fg='red')
    else:
        for role in ('INFO', 'WARN', 'BOLD', 'URL', 'MODULE', 'MODULE_NAME', 'URL_NAME'):
            if not hasattr(style, role):
                setattr(style, role, _dummy_style_func)
    return style
Example #13
0
 def __init__(self, *args, **kwargs):
     super(VerboseCommandMixin, self).__init__()
     self.dry_run = False
     if supports_color():
         opts = ('bold',)
         self.style.EXISTS = \
             termcolors.make_style(fg='blue', opts=opts)
         self.style.APPEND = \
             termcolors.make_style(fg='yellow', opts=opts)
         self.style.CREATE = \
             termcolors.make_style(fg='green', opts=opts)
         self.style.REVERT = \
             termcolors.make_style(fg='magenta', opts=opts)
         self.style.BACKUP = \
             termcolors.make_style(fg='cyan', opts=opts)
Example #14
0
def color_style():
    style = color.color_style()
    if color.supports_color():
        style.INFO = termcolors.make_style(fg="green")
        style.WARN = termcolors.make_style(fg="yellow")
        style.BOLD = termcolors.make_style(opts=("bold",))
        style.URL = termcolors.make_style(fg="green", opts=("bold",))
        style.MODULE = termcolors.make_style(fg="yellow")
        style.MODULE_NAME = termcolors.make_style(opts=("bold",))
        style.URL_NAME = termcolors.make_style(fg="red")
    else:
        for role in ("INFO", "WARN", "BOLD", "URL", "MODULE", "MODULE_NAME", "URL_NAME"):
            if not hasattr(style, role):
                setattr(style, role, _dummy_style_func)
    return style
 def __init__(self, *args, **kwargs):
     super(VerboseCommandMixin, self).__init__(*args, **kwargs)
     self.dry_run = False
     if supports_color():
         opts = ('bold',)
         self.style.EXISTS = \
             termcolors.make_style(fg='blue', opts=opts)
         self.style.APPEND = \
             termcolors.make_style(fg='yellow', opts=opts)
         self.style.CREATE = \
             termcolors.make_style(fg='green', opts=opts)
         self.style.REVERT = \
             termcolors.make_style(fg='magenta', opts=opts)
         self.style.BACKUP = \
             termcolors.make_style(fg='cyan', opts=opts)
Example #16
0
def color_style():
    """Returns a Style object with the Django color scheme."""
    from django.core.management.color import supports_color
    from django.utils import termcolors
    if not supports_color():        
        return no_style()
    class dummy: 
        pass
    style = dummy()
    style.ERROR = termcolors.make_style(fg='red', opts=('bold',))
    style.ERROR_OUTPUT = termcolors.make_style(fg='red', opts=('bold',))
    style.NOTICE = termcolors.make_style(fg='red')
    style.NORMAL = termcolors.make_style()
    style.INFO = termcolors.make_style(fg='white', opts=('bold',))
    style.OK = termcolors.make_style(fg='yellow', opts=('bold',))
    return style
Example #17
0
def color_style():
    style = color.color_style()
    if color.supports_color():
        style.INFO = termcolors.make_style(fg='green')
        style.WARN = termcolors.make_style(fg='yellow')
        style.BOLD = termcolors.make_style(opts=('bold', ))
        style.URL = termcolors.make_style(fg='green', opts=('bold', ))
        style.MODULE = termcolors.make_style(fg='yellow')
        style.MODULE_NAME = termcolors.make_style(opts=('bold', ))
        style.URL_NAME = termcolors.make_style(fg='red')
    else:
        for role in ('INFO', 'WARN', 'BOLD', 'URL', 'MODULE', 'MODULE_NAME',
                     'URL_NAME'):
            if not hasattr(style, role):
                setattr(style, role, _dummy_style_func)
    return style
    def __init__(self, app: App,
                 print_task_names: bool = False,
                 colour: bool = True,
                 django_settings: str = '',
                 requirements_file: str = 'requirements/dev.txt',
                 verbosity: Parameter.constraint_from_choices(int, (0, 1, 2)) = 1):
        self.app = app

        self.print_task_names = print_task_names
        self.verbosity = verbosity
        self.use_colour = colour and supports_color()

        self.requirements_file = requirements_file
        self.django_settings = django_settings or '%s.settings' % app.django_app_name
        self._setup_django = False

        self.env = os.environ.copy()
        self.overidden_tasks = []
Example #19
0
 def __init__(
     self,
     *,
     template_name=None,
     template=None,
     pretty=False,
     limit_body=None,
     colors=True,
 ):
     if not template_name and not template:
         raise RuntimeError(
             "DjangoTemplateResponseFormatter requires a template_name or "
             "template setting")
     self._template_name = template_name
     self._template = template
     self.pretty = pretty
     self.limit_body = limit_body
     self.colors = colors and supports_color()
    def __init__(self,
                 app: App,
                 print_task_names: bool = True,
                 print_task_paths: bool = True,
                 colour: bool = True,
                 django_settings: str = '',
                 requirements_file: str = 'requirements/dev.txt',
                 verbosity: Parameter.constraint_from_choices(int,
                                                              (0, 1, 2)) = 2):
        self.app = app

        self.print_task_names = print_task_names
        self.print_task_paths = print_task_paths
        self.verbosity = verbosity
        self.use_colour = colour and supports_color()

        self.requirements_file = requirements_file
        self.django_settings = django_settings or f'{app.django_app_name}.settings'
        self._setup_django = False

        self.env = os.environ.copy()
        self.overidden_tasks = []
Example #21
0
def banner():
    # Database name - this is just the ``vendor`` atrribute of
    # the connection backend, with some exceptions where we
    # replace it with something else, such as microsoft -> sql server.
    conn = connection

    db_name = {
        "microsoft": "sql server",
    }.get(conn.vendor, conn.vendor)
    db_name = "%s%s" % (db_name[:1].upper(), db_name.replace(
        "sql", "SQL").replace("db", "DB")[1:])

    # Database version - vendor names mapped to functions that
    # retrieve the version, which should be a sequence of things
    # to join with dots.
    db_version_func = {
        "postgresql":
        lambda: (
            conn.pg_version // 10000,
            conn.pg_version // 100 % 100,
            conn.pg_version % 100,
        ),
        "mysql":
        lambda: conn.mysql_version,
        "sqlite":
        lambda: conn.Database.sqlite_version_info,
        # The remaining backends haven't actually been tested,
        # and so their version logic has been gleaned from glancing
        # at the code for each backend.
        "oracle":
        lambda: [conn.oracle_version],
        "microsoft":
        lambda: [conn._DatabaseWrapper__get_dbms_version()],
        "firebird":
        lambda: conn.server_version.split(" ")[-1].split("."),
    }.get(conn.vendor, lambda: [])

    db_version = ".".join(map(str, db_version_func()))

    # The raw banner split into lines.
    lines = ("""

              .....
          _d^^^^^^^^^b_
       .d''           ``b.
     .p'                `q.
    .d'                   `b.
   .d'                     `b.   * Backend %(backend_version)s
   ::                       ::   * Django %(django_version)s
   ::     B A C K E N D     ::   * Python %(python_version)s
   ::                       ::   * %(db_name)s %(db_version)s
   `p.                     .q'   * %(os_name)s %(os_version)s
    `p.                   .q'
     `b.                 .d'
       `q..          ..p'
          ^q........p^
              ''''


""" % {
        "backend_version": __version__,
        "django_version": get_version(),
        "python_version": sys.version.split(" ", 1)[0],
        "db_name": db_name,
        "db_version": db_version,
        "os_name": platform.system(),
        "os_version": platform.release(),
    }).splitlines()[2:]

    if not supports_color():
        return "\n".join(lines)

    # Pairs of function / colorize args for coloring the banner.
    # These are each of the states moving from left to right on
    # a single line of the banner. The function represents whether
    # the current char in a line should trigger the next state.
    color_states = [
        (lambda c: c != " ", {}),
        (lambda c: c == " ", {
            "fg": "red"
        }),
        (lambda c: c != " " and not c.isupper(), {
            "fg": "white",
            "bg": "red",
            "opts": ["bold"]
        }),
        (lambda c: c == " ", {
            "fg": "red"
        }),
        (lambda c: c == "*", {}),
        (lambda c: c != "*", {
            "fg": "red"
        }),
        (lambda c: False, {}),
    ]

    # Colorize the banner.
    for i, line in enumerate(lines):
        chars = []
        color_state = 0

        for char in line:
            color_state += color_states[color_state][0](char)
            chars.append(colorize(char, **color_states[color_state][1]))

        lines[i] = "".join(chars)

    return "\n".join(lines)
Example #22
0
 def __init__(self, *args, **kwargs):
     self.palette = colorpalette if supports_color() else bandwpalette
     super().__init__(*args, **kwargs)
    def handle(self, *args, **options):
        self.extra_prints = options['no_color'] or not supports_color()
        self.stdout.write(
            """This program is for finding and replacing deadnames present on the website.
This comes with a few disclaimers, which you """ + self.style.NOTICE("must") +
            """ heed:
1. This does a simple find-and-replace over various parts of the website -
   newsletters, hosted pages (including Exec History) and meeting minutes.
   This does _not_ edit parts of the website that aren't "official society
   communication". Those edits will be up to individuals to make.
   Since this is just a find-and-replace, it only replaces names and not
   pronouns. You will have to deal with pronouns yourself for the most part.
2. You should only perform a find-and-replace if you are sure that the name
   in question is the correct one.
   It turns out multiple people can have the same name! Some context will be
   provided to help discern this, as well as a link to the full original
   source.
3. This does not attempt any optical character recognition, so cannot read
   images. You'll have to check images of things like tournament standings
   yourself.
   Alt text containing the deadname will be flagged up as such, as you'll
   likely have to change the image also.
   You can check for additional alt text keywords to identify images where
   the person may have been deadnames (e.g. 'mtg', 'netrunner', 'tournament').
   This does not guarantee all offending images will be found however, so you
   should still manually check afterwards.
4. Only perform this process if the person has actively asked for it.
   There are a variety of reasons that someone might not want this process to
   have been done, such as not being out to employers who want evidence of work
   done at the society.
5. This does not perform any changes on Discord or Facebook. You'll have to do 
   those yourself.
6. For names with complex title casing (e.g. MacDonald), please restart the
   program and enter the command line arguments with that case. This tool
   assumes that the input given is of the correct case, or if the input is all
   lowercase then it assumes that the default (of capitalising the first
   letter) is correct.

Please type out 'yes' to confirm that you understand this.""")
        self.must_be("Confirm: ", "yes")
        self.stdout.write("Thank you!")
        self.stdout.write(
            "We'll start with the newsletters. Searching through all newsletters!"
        )
        dead, real = options['deadname'], options['realname']
        if dead == dead.lower():
            dead = titlecase(dead)
        if real == real.lower():
            real = titlecase(real)
        for n in Newsletter.objects.all():
            n.title, sub1 = self.perform_substitutions(
                identify_newsletter("Title", n), dead, real, n.title)
            n.summary, sub2 = self.perform_substitutions(
                identify_newsletter("Summary", n), dead, real, n.summary)
            n.body, sub3 = self.perform_substitutions(
                identify_newsletter("Body", n), dead, real, n.body)
            if sub1 or sub2 or sub3:
                n.save()
        self.stdout.write("""Would you like to look for specific alt-text in
newsletter images?
This can help identify images that might contain deadnames.
Example alt-text you might look for is 'mtg' or 'magic' for a Magic player to
try and find tournament standings.

Enter your search terms separated by commas if you'd like to look for them, or
leave it blank if you want to skip this step. For example, for the above Magic
player you might enter:
mtg,magic,tournament
""")
        term_str = input("Search terms:")
        terms = term_str.split(",")
        for term in terms:
            if term != "":
                for n in Newsletter.objects.all():
                    if re.search(f"\[.+?{term}.+?\]\(.+?\)", n.body):
                        id_string = identify_newsletter("Body", n)
                        self.stdout.write(
                            f"Found term {term} in alt-text in: {id_string}!")
                        self.keyword_matches.append(
                            f"(found term {term}) {id_string}")

        self.stdout.write("Done with newsletters!")
        self.stdout.write(
            "Cool, now doing pages. Please be careful when applying substitutions directly to HTML!"
        )
        for p in Page.objects.all():
            p.title, sub1 = self.perform_substitutions(
                identify_page("Title", p), dead, real, p.title)
            p.page_title, sub2 = self.perform_substitutions(
                identify_page("Page Title", p), dead, real, p.page_title)
            p.body, sub3 = self.perform_substitutions(identify_page("Body", p),
                                                      dead, real, p.body)
            if sub1 or sub2 or sub3:
                p.save()
        self.stdout.write("Done with pages!")
        self.stdout.write(
            "Cool, finally doing meeting minutes. Searching through all minutes!"
        )
        for m in Meeting.objects.all():
            m.title, sub1 = self.perform_substitutions(
                identify_minutes("Title", m), dead, real, m.title)
            m.body, sub2 = self.perform_substitutions(
                identify_minutes("Body", m), dead, real, m.body)
            if sub1 or sub2:
                m.save()
        self.stdout.write(
            "Deadname removal completed. Report of changes made:")
        for sub in self.subs_sources:
            self.stdout.write(
                f"{sub[1]}/{sub[2]} substitutions made: {sub[0]}")
        for sub in self.keyword_matches:
            self.stdout.write(sub)
Example #24
0
 def __init__(self, *args, **kwargs):
     self.palette = colorpalette if supports_color() else bandwpalette
     super().__init__(*args, **kwargs)
Example #25
0
def banner():

    # Database name - this is just the ``vendor`` atrribute of
    # the connection backend, with some exceptions where we
    # replace it with something else, such as microsoft -> sql server.
    conn = connection
    db_name = {
        "microsoft": "sql server",
    }.get(conn.vendor, conn.vendor)
    db_name = "%s%s" % (db_name[:1].upper(),
        db_name.replace("sql", "SQL").replace("db", "DB")[1:])

    # Database version - vendor names mapped to functions that
    # retrieve the version, which should be a sequence of things
    # to join with dots.
    db_version_func = {
        "postgresql": lambda: (
            conn.pg_version // 10000,
            conn.pg_version // 100 % 100,
            conn.pg_version % 100,
        ),
        "mysql": lambda: conn.mysql_version,
        "sqlite": lambda: conn.Database.sqlite_version_info,
        # The remaining backends haven't actually been tested,
        # and so their version logic has been gleaned from glancing
        # at the code for each backend.
        "oracle": lambda: [conn.oracle_version],
        "microsoft": lambda: [conn._DatabaseWrapper__get_dbms_version()],
        "firebird": lambda: conn.server_version.split(" ")[-1].split("."),
    }.get(conn.vendor, lambda: [])
    db_version = ".".join(map(str, db_version_func()))

    # The raw banner split into lines.
    lines = ("""

              .....
          _d^^^^^^^^^b_
       .d''           ``b.
     .p'                `q.
    .d'                   `b.
   .d'                     `b.   * Mezzanine %(mezzanine_version)s
   ::                       ::   * Django %(django_version)s
  ::    M E Z Z A N I N E    ::  * Python %(python_version)s
   ::                       ::   * %(db_name)s %(db_version)s
   `p.                     .q'   * %(os_name)s %(os_version)s
    `p.                   .q'
     `b.                 .d'
       `q..          ..p'
          ^q........p^
              ''''


""" % {
        "mezzanine_version": mezzanine.__version__,
        "django_version": django.get_version(),
        "python_version": sys.version.split(" ", 1)[0],
        "db_name": db_name,
        "db_version": db_version,
        "os_name": platform.system(),
        "os_version": platform.release(),
    }).splitlines()[2:]

    if not supports_color():
        return "\n".join(lines)

    # Pairs of function / colorize args for coloring the banner.
    # These are each of the states moving from left to right on
    # a single line of the banner. The function represents whether
    # the current char in a line should trigger the next state.
    color_states = [
        (lambda c: c != " ", {}),
        (lambda c: c == " ", {"fg": "red"}),
        (lambda c: c != " " and not c.isupper(),
            {"fg": "white", "bg": "red", "opts": ["bold"]}),
        (lambda c: c == " ", {"fg": "red"}),
        (lambda c: c == "*", {}),
        (lambda c: c != "*", {"fg": "red"}),
        (lambda c: False, {}),
    ]

    # Colorize the banner.
    for i, line in enumerate(lines):
        chars = []
        color_state = 0
        for char in line:
            color_state += color_states[color_state][0](char)
            chars.append(colorize(char, **color_states[color_state][1]))
        lines[i] = "".join(chars)

    return "\n".join(lines)
"""Makes console output colored by success / failure.
"""
try:
    import unittest2 as unittest
except ImportError:
    try:
        from django.utils import unittest
    except ImportError:
        import unittest

if hasattr(unittest, 'runner'):
    from django.core.management.color import color_style, supports_color
    from django.utils.termcolors import make_style
    import new
    style = color_style()
    if supports_color():
        style.SUCCEEDING = make_style(fg='green')
        style.FAILING = make_style(fg='red')
        style.ERROR = make_style(opts=('bold',),fg='yellow', bg='black')
        style.FAILURE = make_style(opts=('bold',),fg='yellow', bg='black')
    
    def write(self, arg):
        if arg:
            if 'ERROR' in arg or arg=="E":
                self.stream.write(style.ERROR(arg))
                self.failed_before=True
            elif 'FAIL' in arg or arg=="F":
                self.stream.write(style.FAILURE(arg))
                self.failed_before=True
            elif self.failed_before:
                self.stream.write(style.FAILING(arg))            
Example #27
0
def log(message, *args):
    message = message % args
    if supports_color():
        print colorize('[ECM] ', fg='cyan', opts=('bold',)) +  message
    else:
        print '[ECM] ' + message
Example #28
0
def log(message, *args):
    message = message % args
    if supports_color():
        print colorize('[ECM] ', fg='cyan', opts=('bold', )) + message
    else:
        print '[ECM] ' + message