Skip to content

Module concentration.run

Concentration

A very simple command line application to maintain focus by blocking distracting sites.

View Source
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python

"""Concentration

A very simple command line application to maintain focus by blocking distracting sites.

"""

import codecs

import subprocess  # nosec

import sys

import time

import hug

from . import output, settings

def reset_network(message):

    """Resets the users network to make changes take effect"""

    for command in settings.RESTART_NETWORK:

        try:

            subprocess.check_call(command)  # nosec

        except Exception:  # nosec

            pass

    print(message)

@hug.cli()

def improve():

    """Disables access to websites that are defined as 'distractors'"""

    with open(settings.HOSTS_FILE, "r+") as hosts_file:

        contents = hosts_file.read()

        if settings.START_TOKEN not in contents and settings.END_TOKEN not in contents:

            hosts_file.write(settings.START_TOKEN + "\n")

            for site in set(settings.DISTRACTORS):

                hosts_file.write("{0}\t{1}\n".format(settings.REDIRECT_TO, site))

                for sub_domain in settings.SUB_DOMAINS:

                    hosts_file.write(

                        "{0}\t{1}.{2}\n".format(settings.REDIRECT_TO, sub_domain, site)

                    )

            hosts_file.write(settings.END_TOKEN + "\n")

    reset_network("Concentration is now improved :D!")

@hug.cli()

def lose():

    """Enables access to websites that are defined as 'distractors'"""

    changed = False

    with open(settings.HOSTS_FILE, "r") as hosts_file:

        new_file = []

        in_block = False

        for line in hosts_file:

            if in_block:

                if line.strip() == settings.END_TOKEN:

                    in_block = False

                    changed = True

            elif line.strip() == settings.START_TOKEN:

                in_block = True

            else:

                new_file.append(line)

    if changed:

        with open(settings.HOSTS_FILE, "w") as hosts_file:

            hosts_file.write("".join(new_file))

    reset_network("Concentration is now lost :(.")

@hug.cli("break")

def take_break(minutes: hug.types.number = 5, now: bool = False):

    """Enables temporarily breaking concentration"""

    if not now:

        print("")

        print(output.banner("ARE YOU SURE?"))

        try:

            for remaining in range(60, -1, -1):

                sys.stdout.write("\r")

                sys.stdout.write("{:2d} seconds to change your mind. ".format(remaining))

                sys.stdout.write("Won't you prefer programming? Or a book?")

                sys.stdout.flush()

                time.sleep(1)

        except KeyboardInterrupt:

            print("")

            print("")

            print(":D :D :D\nGood on you! <3")

            return

    # The user insisted on breaking concentration.

    lose()

    print("")

    print(output.banner("TAKING A BREAK"))

    try:

        for remaining in range(minutes * 60, -1, -1):

            sys.stdout.write("\r")

            sys.stdout.write("{:2d} seconds remaining without concentration.".format(remaining))

            sys.stdout.flush()

            time.sleep(1)

    except KeyboardInterrupt:

        pass

    finally:

        sys.stdout.write(

            "\rEnough distraction!                                                            \n"

        )

        print(output.banner("BREAK OVER :)"))

        print("")

        improve()

@hug.cli()

def blocked():

    """Returns the configured list of blocked sites"""

    return settings.DISTRACTORS

@hug.cli("64")

def game():

    """Basic game implementation"""

    print(codecs.encode("Sbe Nznaqn, gur ybir bs zl yvsr", "rot_13"))

Functions

blocked

def blocked(

)

Returns the configured list of blocked sites

View Source
@hug.cli()

def blocked():

    """Returns the configured list of blocked sites"""

    return settings.DISTRACTORS

game

def game(

)

Basic game implementation

View Source
@hug.cli("64")

def game():

    """Basic game implementation"""

    print(codecs.encode("Sbe Nznaqn, gur ybir bs zl yvsr", "rot_13"))

improve

def improve(

)

Disables access to websites that are defined as 'distractors'

View Source
@hug.cli()

def improve():

    """Disables access to websites that are defined as 'distractors'"""

    with open(settings.HOSTS_FILE, "r+") as hosts_file:

        contents = hosts_file.read()

        if settings.START_TOKEN not in contents and settings.END_TOKEN not in contents:

            hosts_file.write(settings.START_TOKEN + "\n")

            for site in set(settings.DISTRACTORS):

                hosts_file.write("{0}\t{1}\n".format(settings.REDIRECT_TO, site))

                for sub_domain in settings.SUB_DOMAINS:

                    hosts_file.write(

                        "{0}\t{1}.{2}\n".format(settings.REDIRECT_TO, sub_domain, site)

                    )

            hosts_file.write(settings.END_TOKEN + "\n")

    reset_network("Concentration is now improved :D!")

lose

def lose(

)

Enables access to websites that are defined as 'distractors'

View Source
@hug.cli()

def lose():

    """Enables access to websites that are defined as 'distractors'"""

    changed = False

    with open(settings.HOSTS_FILE, "r") as hosts_file:

        new_file = []

        in_block = False

        for line in hosts_file:

            if in_block:

                if line.strip() == settings.END_TOKEN:

                    in_block = False

                    changed = True

            elif line.strip() == settings.START_TOKEN:

                in_block = True

            else:

                new_file.append(line)

    if changed:

        with open(settings.HOSTS_FILE, "w") as hosts_file:

            hosts_file.write("".join(new_file))

    reset_network("Concentration is now lost :(.")

reset_network

def reset_network(
    message
)

Resets the users network to make changes take effect

View Source
def reset_network(message):

    """Resets the users network to make changes take effect"""

    for command in settings.RESTART_NETWORK:

        try:

            subprocess.check_call(command)  # nosec

        except Exception:  # nosec

            pass

    print(message)

take_break

def take_break(
    minutes: <hug.types.create.<locals>.new_type_handler.<locals>.NewType object at 0x7f56c879b730> = 5,
    now: bool = False
)

Enables temporarily breaking concentration

View Source
@hug.cli("break")

def take_break(minutes: hug.types.number = 5, now: bool = False):

    """Enables temporarily breaking concentration"""

    if not now:

        print("")

        print(output.banner("ARE YOU SURE?"))

        try:

            for remaining in range(60, -1, -1):

                sys.stdout.write("\r")

                sys.stdout.write("{:2d} seconds to change your mind. ".format(remaining))

                sys.stdout.write("Won't you prefer programming? Or a book?")

                sys.stdout.flush()

                time.sleep(1)

        except KeyboardInterrupt:

            print("")

            print("")

            print(":D :D :D\nGood on you! <3")

            return

    # The user insisted on breaking concentration.

    lose()

    print("")

    print(output.banner("TAKING A BREAK"))

    try:

        for remaining in range(minutes * 60, -1, -1):

            sys.stdout.write("\r")

            sys.stdout.write("{:2d} seconds remaining without concentration.".format(remaining))

            sys.stdout.flush()

            time.sleep(1)

    except KeyboardInterrupt:

        pass

    finally:

        sys.stdout.write(

            "\rEnough distraction!                                                            \n"

        )

        print(output.banner("BREAK OVER :)"))

        print("")

        improve()