Skip to content

Instantly share code, notes, and snippets.

@heyitsjames
Created April 22, 2022 18:12
Show Gist options
  • Select an option

  • Save heyitsjames/52ebe7d353471a2b7ee66a86aa61a60a to your computer and use it in GitHub Desktop.

Select an option

Save heyitsjames/52ebe7d353471a2b7ee66a86aa61a60a to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
import sys
hosts_path = '/Users/james/Desktop/hosts'
def main(argv):
is_commented = False
hosts_file = open(hosts_path, 'r')
hosts_file_contents = hosts_file.readlines()
block_end = -1
block_start = -1
with open(hosts_path, 'r') as f:
for (i, line) in enumerate(f):
if "start_focustime" in line:
block_start = i
if "end_focustime" in line:
block_end = i
if block_start > -1 and i == block_start + 1 and '# ' in line:
is_commented = True
if argv == 'on' and is_commented:
print('Work hard and do a good job')
for (i, line) in enumerate(hosts_file_contents, block_start):
if 'start_focustime' in line:
continue
if "end_focustime" in line:
break;
hosts_file_contents[i] = line[2:]
if argv == 'off' and not is_commented:
print('Time to relax responsibly')
for (i, line) in enumerate(hosts_file_contents, block_start):
if 'start_focustime' in line:
continue
if "end_focustime" in line:
break;
hosts_file_contents[i] = '# ' + line
with open(hosts_path,'w') as f:
for line in hosts_file_contents:
f.writelines(line)
if __name__ == "__main__":
main(sys.argv[1:2][0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment