URI: 
       atellier.dcgi - bitreich-bitmasquerade - Scripts for bitreich's new years eve bitmasqurade
   DIR Log
   DIR Files
   DIR Refs
       ---
       atellier.dcgi (4739B)
       ---
            1 #!/usr/bin/env python
            2 # coding=utf-8
            3 
            4 from functools import reduce
            5 import re
            6 import sys
            7 
            8 geomyidae_host = "localhost"
            9 geomyidae_port = "70"
           10 bitmask_selection = [ '010101010101', '110001100011', '001110011100',
           11                       '101010101010', '100000100001', '011111111110',
           12                       '000010010000', '100010001001', '010010001000',
           13                       '100110001110', '111111101111', '011000100010']
           14 bitwise_selection = ['Flip', 'Set', 'Clear']
           15 bitwise_operations = {'Flip' : lambda a, b: a ^ b,
           16                       'Set' : lambda a, b: a | b,
           17                       'Clear' : lambda a, b: a & b}
           18 
           19 def string_to_binary(s, bits):
           20     return ''.join([format(ord(c), '0{}b'.format(bits)) for c in s])
           21 
           22 def strip_char_to_ascii_range(s, start, end):
           23     range = [format(ord(a), 'x') for a in [start, end]]
           24     patt = r'[^\x{}-\x{}]+'.format(*range)
           25     return re.sub(patt, '', s)
           26 
           27 def strip_to_max_len(s):
           28     return s[:16]
           29 
           30 def print_nick_fail():
           31     print("Pardon, but I'll need your name")
           32 
           33 def print_step_1_template(nick):
           34     gph_menu_link = lambda s: \
           35         '[1|{}|/atellier.dcgi?{} {}|{}|{}]'.format(s, nick, s,
           36                                                       geomyidae_host, geomyidae_port)
           37     gph_links = '\n'.join(map(gph_menu_link, bitmask_selection))
           38     with open('step_1.txt', 'r') as file:
           39         print(file.read().format(nick, gph_links))
           40 
           41 def print_step_2_template(nick, bitmask):
           42     gph_menu_link = lambda s: \
           43         '[1|{}|/atellier.dcgi?{} {} {}|{}|{}]'.format(s, nick, bitmask, s,
           44                                                       geomyidae_host, geomyidae_port)
           45     gph_links = '\n'.join(map(gph_menu_link, bitwise_selection))
           46     with open('step_2.txt', 'r') as file:
           47         print(file.read().format(nick, gph_links))
           48 
           49 def print_completion(nick, bitmask, bitwise, mask):
           50     with open('step_complete.txt', 'r') as file:
           51         print(file.read().format(nick, bitmask, bitwise, mask))
           52 
           53 def make_mask(nick, bitmask, bitwise):
           54     bits = len(nick)
           55     binary_nick = string_to_binary(nick, bits)
           56     # print(binary_nick)
           57     def bit_picker(remaining, accm=""):
           58         if len(accm) == 0:
           59             # Recursion entry point: accumulate first element of
           60             # the list.
           61             return bit_picker(remaining[1:], remaining[0:1])
           62         if remaining == "":
           63             # Recursion exit point: return the accumulated elements.
           64             return accm
           65         # Main loop: return the current element that corresponds
           66         # with the current multiple of the remaining chars
           67         multiple = int(len(remaining)/bits)
           68         if (len(remaining) % bits) == multiple:
           69             accm = accm + remaining[0:1]
           70             # print(remaining)
           71             return bit_picker(remaining[1:], accm)
           72         return bit_picker(remaining[1:], accm)
           73     selected_bits = bit_picker(binary_nick)
           74     bitmask = bitmask[:len(selected_bits)]
           75     bin_1 = int(selected_bits, 2)
           76     bin_2 = int(bitmask, 2)
           77     return bin(bitwise_operations[bitwise](bin_2, bin_1))\
           78         [2:].zfill(len(selected_bits))
           79 
           80 def print_args_fail():
           81     print("Pardon, but something's not working.")
           82 
           83 def print_bitmask_fail():
           84     print("Pardon, but that bitmask isn't available.")
           85 
           86 def main(args):
           87     geomyidae_search = args[1]
           88     geomyidae_args = args[2].split(' ') # tab splitting
           89     if geomyidae_search:
           90         # Get the search result, which will be this
           91         # person's nick, and ask them to pick a bitmask from
           92         # the list printed out in the step 1 template.
           93         nick = strip_char_to_ascii_range(geomyidae_search, '!', '~')
           94         nick = strip_to_max_len(nick)
           95         if nick != '':
           96             print_step_1_template(nick)
           97         else:
           98             print_nick_fail()
           99         sys.exit()
          100     # Validate and clear up the input from the bitmask
          101     # selection.
          102     if len(geomyidae_args) < 2:
          103         print_args_fail()
          104         sys.exit()
          105     nick = strip_char_to_ascii_range(geomyidae_args[0], '!', '~')
          106     nick = strip_to_max_len(nick)
          107     bitmask = geomyidae_args[1]
          108     if nick == '':
          109         print_nick_fail()
          110         sys.exit()
          111     if bitmask not in bitmask_selection:
          112         print_bitmask_fail()
          113         sys.exit()
          114     if len(geomyidae_args) == 2:
          115         # Get the prson to choose their bitwise operation.
          116         print_step_2_template(nick, bitmask)
          117     else:        #
          118         bitwise = geomyidae_args[2]
          119         if bitwise not in bitwise_selection:
          120             print_bitmask_fail()
          121             sys.exit()
          122         # Finish up by making combining the nick, bitmask,
          123         # and bitwise operation.
          124         mask = make_mask(nick, bitmask, bitwise)
          125         print_completion(nick, bitmask[:len(nick)], bitwise, mask)
          126     sys.exit()
          127 
          128 if __name__ == "__main__":
          129     sys.exit(main(sys.argv))