예제 #1
0
    def login(self, sock):
        # read the msg that should have login code plus username
        try:
            msg = json.loads(myrecv(sock))
            if len(msg) > 0:

                if msg["action"] == "login":
                    name = msg["name"]
                    if self.group.is_member(name) != True:
                        self.new_clients.remove(sock)
                        self.logged_name2sock[name] = sock
                        self.logged_sock2name[sock] = name
                        if name not in self.indices.keys():
                            try:
                                self.indices[name] = pkl.load(
                                    open(name + '.idx', 'rb'))
                            except IOError:  # chat index does not exist, then create one
                                self.indices[name] = indexer.Index(name)
                        print(name + ' logged in')
                        self.group.join(name)
                        mysend(sock, json.dumps(
                            {"action": "login", "status": "okay"}))
                    else:
                        mysend(sock, json.dumps(
                            {"action": "login", "status": "duplicate"}))
                        print(name + ' duplicate login attempt')
                else:
                    print('wrong code received')
            else:
                self.logout(sock)
        except:
            self.all_sockets.remove(sock)
    def login(self, sock):
        #read the msg that should have login code plus username
        msg = myrecv(sock)
        if len(msg) > 0:
            code = msg[0]

            if code == M_LOGIN:
                name = msg[1:]
                if self.group.is_member(name) != True:
                    #move socket from new clients list to logged clients
                    self.new_clients.remove(sock)
                    #add into the name to sock mapping
                    self.logged_name2sock[name] = sock
                    self.logged_sock2name[sock] = name
                    #load chat history of that user
                    if name not in self.indices.keys():
                        try:
                            self.indices[name]=pkl.load(open(name+'.idx','rb'))
                        except IOError: #chat index does not exist, then create one
                            self.indices[name] = indexer.Index(name)
                    print(name + ' logged in')
                    self.group.join(name)
                    mysend(sock, M_LOGIN + 'ok')
                else: #a client under this name has already logged in
                    mysend(sock, M_LOGIN + 'duplicate')
                    print(name + ' duplicate login attempt')
            else:
                print ('wrong code received')
        else: #client died unexpectedly
            self.logout(sock)
    def login(self, sock):
        # read the msg that should have login code plus username
        try:
            msg = json.loads(myrecv(sock))
            if len(msg) > 0:

                if msg["action"] == "login":
                    name = msg["name"]
                    if self.group.is_member(name) != True:
                        # move socket from new clients list to logged clients
                        self.new_clients.remove(sock)
                        # add into the name to sock mapping
                        self.logged_name2sock[name] = sock
                        self.logged_sock2name[sock] = name
                        # load chat history of that user
                        if name not in self.indices.keys():
                            try:
                                self.indices[name] = pkl.load(
                                    open(name + '.idx', 'rb'))
                            except IOError:  # chat index does not exist, then create one
                                self.indices[name] = indexer.Index(name)
                        print(name + ' logged in')
                        self.group.join(name)
                        mysend(sock, json.dumps(
                            {"action": "login", "status": "ok"}))
                    else:  # a client under this name has already logged in
                        mysend(sock, json.dumps(
                            {"action": "login", "status": "duplicate"}))
                        print(name + ' duplicate login attempt')
                else:
                    print('wrong code received')
            else:  # client died unexpectedly
                self.logout(sock)
        except:
            self.all_sockets.remove(sock)
예제 #4
0
    def login(self, sock):
        # read the msg that should have login code plus username
        try:
            # username = self.logged_sock2name[sock]
            msg = json.loads(myrecv(sock, 'server', '__OFFLINE__'))
            if len(msg) > 0:
                # print(msg)
                if msg["action"] == "login":
                    name = msg["name"]
                    if self.group.is_member(name) != True:

                        path = './user/' + name + '/'

                        if msg["step"] == 'name':
                            if os.path.exists(path) and os.path.exists(path + 'key') and os.path.exists(path + name + '.pwd'):
                                name_status = 'name_ok'
                            else:
                                name_status = 'name_fail'
                            mysend(sock, json.dumps(
                                {"action": "login", "status": name_status}), '__OFFLINE__')

                        elif msg["step"] == 'pwd':
                            pwd = pkl.load(open(path + name + '.pwd', 'rb'))
                            if pwd.check_pwd(msg["pwd"]):
                                # move socket from new clients list to logged clients
                                self.new_clients.remove(sock)
                                # add into the name to sock mapping
                                self.logged_name2sock[name] = sock
                                self.logged_sock2name[sock] = name
                                # load chat history of that user
                                if name not in self.indices.keys():
                                    try:
                                        self.indices[name] = pkl.load(
                                            open('./user/' + name + '/' + name + '.idx', 'rb'))
                                    except IOError:  # chat index does not exist, then create one
                                        self.indices[name] = indexer.Index(name)
                                print('[' + name + '] logged in')
                                self.group.join(name)
                                mysend(sock, json.dumps(
                                    {"action": "login", "status": "pwd_ok"}), '__OFFLINE__')
                            else:
                                # self.new_clients.remove(sock)
                                mysend(sock, json.dumps(
                                    {"action": "login", "status": "pwd_fail"}), '__OFFLINE__')

                    else:  # a client under this name has already logged in
                        mysend(sock, json.dumps(
                            {"action": "login", "status": "duplicate"}), '__OFFLINE__')
                        print(name + ' duplicate login attempt')
                else:
                    print('wrong code received')
            else:  # client died unexpectedly
                self.logout(sock)
        except Exception as err:
            print(err)
            self.all_sockets.remove(sock)
예제 #5
0
 def setUpClass(cls):
     print("Testing document index", file=sys.stderr)
     print("Load data and build temporary index", file=sys.stderr)
     with open("test/data.json") as f:
         cls.test_data = json.load(f)
     cls.index_dir = tempfile.TemporaryDirectory()
     index_path = os.path.join(cls.index_dir.name, "index")
     cls.index = indexer.Index(index_path, "TEMP_TEST_INDEX")
     for d in cls.test_data:
         cls.index.add_document(d)
     print("Index ready", file=sys.stderr)
예제 #6
0
 def register(self, sock, msg):
     name = msg["name"]
     if name in self.users:
         mysend(sock,json.dumps({"server_response":"Name has been used"}))
     else:
         password = msg["password"]
         #hash
         self.users[name] = SHA256.new((password).encode('utf-8')).hexdigest()
         self.indices[name] = indexer.Index(name)
         print(name + ' has registered')
         mysend(sock, json.dumps({"server_response":"register_succeed"}))
def client_indexing(msg):
    [term, str_start_line, str_lines] = msg.split('|')
    start_line = int(str_start_line)
    lines = str_lines.split('\n')

    client_indexer = indexer.Index('ClientIndexer', start_line)
    for line in lines:
        client_indexer.add_msg_and_index(line)
    msg = json.dumps(client_indexer.search_idx(term)) # convert list to string

    return msg
예제 #8
0
def main():
    global files
    files = list(glob.glob('*.txt'))
    v = list()

    for filename in files:
        with open(filename) as f:
            text = f.read()

            p = preprocess.Preprocess(text)
            v.append(p.get_list())

    # Creating Index
    idx = indexer.Index(v)

    display_menu(idx)
예제 #9
0
 def login(self, sock, msg):
     # read the msg that should have login code plus username
             
     name = msg["name"]
     password =SHA256.new((msg["password"]).encode('utf-8')).hexdigest()
     if name in self.users:
         if password == self.users[name]:
             mysend(sock, json.dumps({"server_response":"login succeed"}))
             status = S_LOGGEDIN
         
         else:
             mysend(sock, json.dumps({"server_response":"wrong password"}))
             return None
     else:
    
         mysend(sock,json.dumps({"server_response":'fail'}))
         return None
                             
                             
     if status == S_LOGGEDIN:
                 # move socket from new clients list to logged clients
                 #   if self.group.is_member(name) != True:
         self.new_clients.remove(sock)
                 # add into the name to sock mapping
         self.logged_name2sock[name] = sock
         self.logged_sock2name[sock] = name
         print(name + ' has logged in')
         self.group.join(name)
                 # load chat history of that user
         if name not in self.indices.keys():
             try:
                 self.indices[name] = pkl.load(
                 open(name + '.idx', 'rb'))
             except IOError:  # chat index does not exist, then create one
                 self.indices[name] = indexer.Index(name)
         
         else:
             mysend(sock, json.dumps({"server_response": 'fail'}))
예제 #10
0
"""Indexer module

To get the index do "from indexer import index".
See Index class for methods to call on it
"""

import indexer
index = indexer.Index()
예제 #11
0
    def window_login(self, sock):
        #laod in msg
        #check if in our dictionary of users
        #return soemthing conditional on if msg is in dictionary
        try:
            msg = json.loads(myrecv(sock))
            if len(msg) > 0:
                if msg["action"] == "login":
                    if msg["name"] in self.users.keys() and self.users[
                            msg["name"]] == msg["password"]:
                        name = msg["name"]
                        #remove from new_clients so they are not relogged in
                        self.new_clients.remove(sock)
                        #add to these dictionaries to use later
                        self.logged_name2sock[name] = sock
                        self.logged_sock2name[sock] = name
                        if name not in self.indices.keys():
                            try:
                                self.indices[name] = pkl.load(
                                    open(name + '.idx', 'rb'))
                            except IOError:  # chat index does not exist, then create one
                                self.indices[name] = indexer.Index(name)
                        self.group.join(name)
                        mysend(
                            sock,
                            json.dumps({
                                "action": "login",
                                "status": "success"
                            }))

                    else:
                        mysend(
                            sock,
                            json.dumps({
                                "action":
                                "login",
                                "status":
                                "error",
                                "errormsg":
                                "login error.\nTry again, or register an account."
                            }))

                elif msg["action"] == "register":

                    if msg["name"] in self.users.keys():
                        mysend(
                            sock,
                            json.dumps({
                                "action":
                                "register",
                                "status":
                                "error",
                                "errormsg":
                                "this username alreay exists, please try another."
                            }))

                    elif msg["password"] != msg["cpassword"]:
                        mysend(
                            sock,
                            json.dumps({
                                "action":
                                "register",
                                "status":
                                "error",
                                "errormsg":
                                "Passwords do not match, please try again"
                            }))

                    else:
                        self.users[msg["name"]] = msg["password"]
                        print(self.users)
                        file = open("up3_user_database.txt", "w")
                        self.users = json.dumps(self.users)
                        file.write(self.users)
                        file.close()
                        self.users = self.get_users()
                        mysend(
                            sock,
                            json.dumps({
                                "action":
                                "register",
                                "status":
                                "success",
                                "msg":
                                "Registration successful! Please log in."
                            }))
                else:
                    self.logout(sock)
                    print("client died unexpectadly")

            else:
                print("how did you get here?")
        except:
            pass
 def set_indexing(self):
     if self.me not in self.indices.keys():
         try:
             self.indices[self.me] = pkl.load(open(self.me + '.idx', 'rb'))
         except IOError:  # chat index does not exist, then create one
             self.indices[self.me] = indexer.Index(self.me)
예제 #13
0
 def setUp(self):
   baseDir = os.path.dirname(os.path.realpath(__file__))
   self.index = indexer.Index(baseDir + '/../test_data.csv.gz')
    def proc(self, my_msg, peer_code, peer_msg):
        self.out_msg = ''
        if self.state == S_LOGGEDIN:
            if len(my_msg) > 0:

                if my_msg == 'q':
                    self.out_msg += 'See you next time!\n'
                    self.state = S_OFFLINE

                elif my_msg == 'time':
                    mysend(self.s, M_TIME)
                    time_in = myrecv(self.s)
                    self.out_msg += "Time is: " + time_in

                elif my_msg == 'who':
                    mysend(self.s, M_LIST)
                    logged_in = myrecv(self.s)
                    self.out_msg += 'Here are all the users in the system:\n'
                    self.out_msg += logged_in

                elif my_msg[0] == 'c':
                    peer = my_msg[1:]
                    peer = peer.strip()
                    if self.connect_to(peer) == True:
                        self.state = S_CHATTING

                        self.chatting_start_time = time.time()

                        print("Current encryption status: ", self.is_encrypted)
                        self.out_msg += 'Connect to ' + peer + '. Chat away!\n\n'
                        self.out_msg += '-----------------------------------\n'
                    else:
                        self.out_msg += 'Connection unsuccessful\n'

                elif my_msg[0] == '?':
                    term = my_msg[1:].strip()
                    mysend(self.s, M_SEARCH + term)
                    search_rslt_server = myrecv(self.s)[1:].strip()
                    search_rslt_prs_history = ''

                    client_name = self.me
                    if client_name not in self.personal_history.keys():
                        try:
                            self.personal_history[client_name] = pkl.load(
                                open(client_name + '.idx', 'rb'))

                        except IOError:
                            self.personal_history[self.me] = indexer.Index(
                                self.me)

                    search_rslt_prs_history += (
                        self.personal_history[client_name].search(term)
                    ).strip()

                    total_search = search_rslt_server + search_rslt_prs_history
                    if (len(total_search)) > 0:
                        self.out_msg += 'Public conversation history:' + '\n' + search_rslt_server + '\n\n' + \
                                        'Private conversation history:' + '\n' + search_rslt_prs_history + '\n\n'

                    else:
                        self.out_msg += '\'' + term + '\'' + ' not found\n\n'

                elif my_msg == 'all history':
                    mysend(self.s, M_HISTORY)
                    rslt = myrecv(self.s)[1:].strip()
                    if (len(rslt)) > 0:
                        self.out_msg += rslt + '\n\n'
                    else:
                        self.out_msg += '\'' + term + '\'' + ' not found\n\n'

                elif my_msg[0] == 'p':
                    poem_idx = my_msg[1:].strip()
                    mysend(self.s, M_POEM + poem_idx)
                    poem = myrecv(self.s)[1:].strip()
                    if (len(poem) > 0):
                        self.out_msg += poem + '\n\n'
                    else:
                        self.out_msg += 'Sonnet ' + poem_idx + ' not found\n\n'

            if len(peer_msg) > 0:
                if peer_code == M_CONNECT:
                    self.peer = peer_msg
                    self.out_msg += 'Request from ' + self.peer + '\n'
                    self.out_msg += 'You are connected with ' + self.peer
                    print('Current encryption status: ', self.is_encrypted)
                    self.out_msg += '. Chat away!\n\n'
                    self.out_msg += '------------------------------------\n'
                    self.state = S_CHATTING

        elif self.state == S_CHATTING:
            if len(my_msg) > 0:  # my stuff going out

                if my_msg.rstrip(my_msg[20:]) == "secure my chat with ":
                    if self.is_encrypted == True:
                        print("please unsecure yourself first")
                    else:
                        name = my_msg[20:]
                        public_private_number = str(
                            self.compute_PPN_1(self.base, self.clock_size,
                                               self.private_number))
                        print("Securing the chat...")
                        mysend(self.s,
                               M_SECURING + public_private_number + name)
                        self.is_encrypted = True
                        print("...")
                        print("...")
                        print("...")
                        print("SECURED")

                if my_msg.rstrip(
                        my_msg[19:]) != "secure my chat with" and my_msg.lower(
                        ) != "unsecure my chat":

                    if self.is_encrypted:
                        to_cript = "[-SECURED-" + self.me + "] " + my_msg
                        if self.me not in self.personal_history.keys():
                            try:
                                self.personal_history[self.me] = pkl(
                                    open(self.me + '.idx', 'rb'))
                            except:
                                self.personal_history[self.me] = indexer.Index(
                                    self.me)
                        to_save = text_proc(to_cript, '')
                        self.personal_history[self.me].add_msg_and_index(
                            to_save)
                        cripted_msg = self.cripta(to_cript, self.shared_secret)
                        mysend(self.s, M_SECURE_EXCHANGE + cripted_msg)
                    else:
                        mysend(self.s,
                               M_EXCHANGE + "[" + self.me + "] " + my_msg)

                if my_msg.lower() == "unsecure my chat":
                    print()
                    print("Unsecuring the chat...")
                    self.is_encrypted = False
                    self.shared_secret = 0
                    print("...")
                    print("...")
                    print("...")
                    print("UNSECURED")
                    mysend(self.s, M_UNSECURING)

                if my_msg == 'bye':
                    if self.is_encrypted == True:
                        print("Unsecure before leaving the chat room")
                    else:

                        self.preference = pkl.load(open(
                            self.me + '.txt', 'rb'))
                        self.chatting_ending_time = time.time()
                        duration = self.chatting_ending_time - self.chatting_start_time

                        tml = list()
                        for key in self.preference:
                            tml.append(key)
                        if self.peer in tml:
                            self.preference[self.peer] += duration
                        else:
                            self.preference[self.peer] = duration

                        pkl.dump(self.preference, open(self.me + '.txt', 'wb'))

                        self.disconnect()
                        self.state = S_LOGGEDIN
                        self.is_encrypted = False
                        self.shared_secret = 0
                        self.peer = ''

            if len(peer_msg) > 0:
                if peer_code == M_CONNECT:
                    self.out_msg += "(" + peer_msg + " joined)\n"
                    print("Current encryption status: ", self.is_encrypted)

            #Securing-Unsecuring
                if peer_code == M_SECURING:

                    #print(peer_msg[1:])
                    counter = 0
                    for alpha in peer_msg:
                        counter += 1
                        if alpha.isalpha():
                            break

                    received_PPN = int(peer_msg[:counter - 1])
                    #                   print(received_PPN)
                    name = peer_msg[counter - 1:]
                    #                   print(name)

                    self.shared_secret = self.compute_PPN_2(
                        received_PPN, self.clock_size, self.private_number)
                    if self.is_encrypted == False:
                        public_private_number = str(
                            self.compute_PPN_1(self.base, self.clock_size,
                                               self.private_number))
                        mysend(self.s,
                               M_SECURING + public_private_number + name)
                        self.is_encrypted = True
                        print("Securing the chat...")
                        print("...")
                        print("...")
                        print("...")
                        print("SECURED")

                if peer_code == M_UNSECURING:
                    print()
                    self.is_encrypted = False
                    self.shared_secret = 0
                    print("Unscuring the chat...")
                    print("...")
                    print("...")
                    print("...")
                    print("UNSECURED")

                if peer_code == M_EXCHANGE:
                    self.out_msg += peer_msg

                if peer_code == M_SECURE_EXCHANGE:
                    if self.is_encrypted == True:
                        msg_to_decrypt = peer_msg[1:]
                        decrypted_msg = self.decripta(msg_to_decrypt,
                                                      self.shared_secret)
                        if self.me not in self.personal_history.keys():
                            try:
                                self.personal_history[self.me] = pkl.load(
                                    open(self.me + '.idx', 'rb'))
                            except:
                                self.personal_history[self.me] = indexer.Index(
                                    self.me)
                        to_save = text_proc(decrypted_msg, '')
                        self.personal_history[self.me].add_msg_and_index(
                            to_save)
                        self.out_msg += decrypted_msg
                    else:
                        self.out_msg += peer_msg

            if peer_code == M_DISCONNECT:
                self.state = S_LOGGEDIN
                self.is_encrypted = False
                self.shared_secret = 0

            if self.state == S_LOGGEDIN:
                self.out_msg += menu
        else:
            self.out_msg += 'How did you wind up here??\n'
            print_state(self.state)

        return self.out_msg
예제 #15
0
               spider_output_path(spider_name), "--logfile", "scrapy.log")
    print("Crawl with spider '{}'".format(spider_name))
    subprocess.run(command, check=True)
os.chdir(start_path)

print("Crawling complete, joining crawl output")
scraped_data = []
for output in map(spider_output_path, spiders):
    with open(output) as f:
        scraped_data.extend(json.load(f))
    os.remove(output)

index_path = os.path.join(start_path, data_dir, settings.INDEX_DIRNAME)
print("Building index")
if not os.path.exists(index_path):
    print("Previous index does not exist at '{}', creating".format(index_path))
    indexer.create_new_index(index_path, settings.INDEX_NAME,
                             settings.TOKENIZER_OPTIONS)
else:
    print("Previous index exists at '{}', not creating a new one".format(
        index_path))

index = indexer.Index(index_path, settings.INDEX_NAME,
                      settings.TOKENIZER_OPTIONS)

print("Add crawled output to index")
for data in scraped_data:
    index.add_documents(data)

print("All done")
예제 #16
0
def load_index():
    return indexer.Index(settings.INDEX_DIRNAME, settings.INDEX_NAME,
                         settings.TOKENIZER_OPTIONS)
예제 #17
0
    def handle_msg(self, from_sock):
        #read msg code
        msg = myrecv(from_sock)
        if len(msg) > 0:
            #==============================================================================
            # handle connect request this is implemented for you
            #==============================================================================
            msg = json.loads(msg)
            if msg["action"] == "connect":
                to_name = msg["target"]
                from_name = self.logged_sock2name[from_sock]
                if to_name == from_name:
                    msg = json.dumps({"action": "connect", "status": "self"})
                # connect to the peer
                elif self.group.is_member(to_name):
                    to_sock = self.logged_name2sock[to_name]
                    self.group.connect(from_name, to_name)
                    the_guys = self.group.list_me(from_name)
                    msg = json.dumps({
                        "action": "connect",
                        "status": "success"
                    })
                    for g in the_guys[1:]:
                        to_sock = self.logged_name2sock[g]
                        mysend(
                            to_sock,
                            json.dumps({
                                "action": "connect",
                                "status": "request",
                                "from": from_name
                            }))
                else:
                    msg = json.dumps({
                        "action": "connect",
                        "status": "no-user"
                    })
                mysend(from_sock, msg)
#==============================================================================
# handle messeage exchange: IMPLEMENT THIS
#==============================================================================
            elif msg["action"] == "exchange":

                from_name = self.logged_sock2name[from_sock]
                raw_message = msg["message"]
                message = text_proc(raw_message, from_name)

                if from_name in self.indices.keys():
                    self.indices[from_name].add_msg_and_index(message)
                else:
                    index = indexer.Index(from_name)
                    self.indices[from_name] = index
                    self.indices[from_name].add_msg_ane_index(message)

                the_guys = self.group.list_me(from_name)[1:]

                for g in the_guys:
                    to_sock = self.logged_name2sock[g]
                    #self.indices[g].add_msg_ane_index(message)
                    mysend(
                        to_sock,
                        json.dumps({
                            "action": "exchange",
                            "message": raw_message,
                            "from": "[" + from_name + "]"
                        }))

#==============================================================================
# the "from" guy has had enough (talking to "to")!
#=============================================================================
            elif msg["action"] == "disconnect":
                from_name = self.logged_sock2name[from_sock]
                the_guys = self.group.list_me(from_name)
                self.group.disconnect(from_name)
                the_guys.remove(from_name)
                if len(the_guys) == 1:  # only one left
                    g = the_guys.pop()
                    to_sock = self.logged_name2sock[g]
                    mysend(to_sock, json.dumps({"action": "disconnect"}))
#==============================================================================
#                 listing available peers: IMPLEMENT THIS---
#
#==============================================================================
            elif msg["action"] == "list":
                from_name = self.logged_sock2name[from_sock]
                msg = self.group.list_all(from_name)
                mysend(from_sock, json.dumps({
                    "action": "list",
                    "results": msg
                }))
#==============================================================================
#             retrieve a sonnet : IMPLEMENT THIS
#done  uncheck!!!
#==============================================================================
            elif msg["action"] == "poem":
                poem_idx = int(msg["target"])
                poem = " " + self.sonnet.get_sect(poem_idx)
                print('here:\n', poem)
                mysend(from_sock,
                       json.dumps({
                           "action": "poem",
                           "results": poem
                       }))

#==============================================================================
#                 time
#==============================================================================
            elif msg["action"] == "time":
                ctime = time.strftime('%d.%m.%y,%H:%M', time.localtime())
                mysend(from_sock,
                       json.dumps({
                           "action": "time",
                           "results": ctime
                       }))
#==============================================================================
#                 search: : IMPLEMENT THIS
#==============================================================================
            elif msg["action"] == "search":

                search = msg["target"]
                search_rslt = " "
                for index in self.indices.values():
                    if index.search(search) not in search_rslt:
                        search_rslt += index.search(search)
                print('server side search: ' + search_rslt)
                mysend(
                    from_sock,
                    json.dumps({
                        "action": "search",
                        "results": search_rslt
                    }))

            elif msg['action'] == 'play':
                self.dic[msg['name']] = msg['target']
                mess = 'You are the only player now.'
                if len(self.dic) == 1:
                    mysend(from_sock,
                           json.dumps({
                               "action": "play",
                               "results": mess
                           }))
                if len(self.dic) == 2:
                    mysend(from_sock,
                           json.dumps({
                               "action": "play",
                               "results": self.dic
                           }))

#==============================================================================
#                 the "from" guy really, really has had enough
#==============================================================================

        else:
            #client died unexpectedly
            self.logout(from_sock)
예제 #18
0
    def handle_msg(self, from_sock):
        # read msg code
        msg = myrecv(from_sock)
        if len(msg) > 0:
            # ==============================================================================
            # handle connect request this is implemented for you
            # ==============================================================================
            msg = json.loads(msg)
            if msg["action"] == "connect":
                to_name = msg["target"]
                from_name = self.logged_sock2name[from_sock]
                if to_name == from_name:
                    msg = json.dumps({"action": "connect", "status": "self"})
                # connect to the peer
                elif self.group.is_member(to_name):
                    to_sock = self.logged_name2sock[to_name]
                    self.group.connect(from_name, to_name)
                    the_guys = self.group.list_me(from_name)
                    msg = json.dumps({
                        "action": "connect",
                        "status": "success"
                    })
                    for g in the_guys[1:]:
                        to_sock = self.logged_name2sock[g]
                        mysend(
                            to_sock,
                            json.dumps({
                                "action": "connect",
                                "status": "request",
                                "from": from_name
                            }))
                else:
                    msg = json.dumps({
                        "action": "connect",
                        "status": "no-user"
                    })
                mysend(from_sock, msg)
# ==============================================================================
# handle message exchange: IMPLEMENT THIS
# ==============================================================================
            elif msg["action"] == "exchange":
                from_name = self.logged_sock2name[from_sock]
                """
                Finding the list of people to send to and index message
                """
                # IMPLEMENTATION
                # ---- start your code ---- #
                ctime = time.strftime('%H:%M:%S', time.localtime())
                msg_sent = '(' + from_name + ')' + ctime + ' ' + msg["message"]
                if from_name not in self.indices.keys():
                    new_index = indexer.Index(from_name)
                    self.indices[from_name] = new_index
                self.indices[from_name].add_msg_and_index(msg_sent)
                # ---- end of your code --- #

                the_guys = self.group.list_me(from_name)[1:]
                for g in the_guys:
                    to_sock = self.logged_name2sock[g]

                    # IMPLEMENTATION
                    # ---- start your code ---- #
                    msg_sent = msg["message"]
                    mysend(
                        to_sock,
                        json.dumps({
                            "action": "exchange",
                            "message": msg_sent,
                            "from": from_name
                        }))
                    # ---- end of your code --- #

# ==============================================================================
# the "from" guy has had enough (talking to "to")!
# ==============================================================================
            elif msg["action"] == "disconnect":
                from_name = self.logged_sock2name[from_sock]
                the_guys = self.group.list_me(from_name)
                self.group.disconnect(from_name)
                the_guys.remove(from_name)
                if len(the_guys) == 1:  # only one left
                    g = the_guys.pop()
                    to_sock = self.logged_name2sock[g]
                    mysend(
                        to_sock,
                        json.dumps({
                            "action": "disconnect",
                            "msg": "everyone left, you are alone"
                        }))
# ==============================================================================
#                 listing available peers: IMPLEMENT THIS
# ==============================================================================
            elif msg["action"] == "list":

                # IMPLEMENTATION
                # ---- start your code ---- #
                from_name = self.logged_sock2name[from_sock]
                msg = self.group.list_all(from_name)
                # ---- end of your code --- #
                mysend(from_sock, json.dumps({
                    "action": "list",
                    "results": msg
                }))
# ==============================================================================
#             retrieve a sonnet : IMPLEMENT THIS
# ==============================================================================
            elif msg["action"] == "poem":

                # IMPLEMENTATION
                # ---- start your code ---- #
                index = int(msg["target"])
                poem = self.sonnet.get_poem(index)
                print('here:\n', poem)
                poem = '\n'.join(poem)
                # ---- end of your code --- #

                mysend(from_sock,
                       json.dumps({
                           "action": "poem",
                           "results": poem
                       }))
# ==============================================================================
#                 time
# ==============================================================================
            elif msg["action"] == "time":
                ctime = time.strftime('%d.%m.%y,%H:%M', time.localtime())
                mysend(from_sock,
                       json.dumps({
                           "action": "time",
                           "results": ctime
                       }))
# ==============================================================================
#                 search: : IMPLEMENT THIS
# ==============================================================================
            elif msg["action"] == "search":

                # IMPLEMENTATION
                # ---- start your code ---- #
                target = msg["target"]
                search_rslt = []
                real_result = []
                for key in self.indices.keys():
                    result = self.indices[key].search(target)
                    if result:
                        real_result = [i[1] for i in result]
                        print("******", real_result)
                    search_rslt.extend(real_result)
                search_rslt = '\n'.join(search_rslt)
                print('server side search: ' + search_rslt)

                # ---- end of your code --- #
                mysend(
                    from_sock,
                    json.dumps({
                        "action": "search",
                        "results": search_rslt
                    }))

# ==============================================================================
#                 the "from" guy really, really has had enough
# ==============================================================================

        else:
            # client died unexpectedly
            self.logout(from_sock)
예제 #19
0
import indexer, tempfile, ast
import ast_parser
from whoosh.query import Term

with tempfile.TemporaryDirectory() as td:
    print("create index")
    i = indexer.Index(td + "index", "asd")
    w = i.index.writer()
    print("add document")
    w.add_document(title=u"hello",
                   url=u"code.com",
                   content=u"def f(x):\n return x - 2")
    w.commit()
    print("committed")
    print("parse query")
    for query_code in (
            u"class A:\n def __init__(self):\n  pass",
            u"a + 2 - 3",
            u"def function(x):\n pass",
            u"def function(x, y):\n return x - 2",
            u"class A(x, y):\n def __init__(self, x):\n  x += 1\n  return list.sort",
    ):
        subtrees = ast_parser.dump(ast.parse(query_code),
                                   tokenize_leaves=False)
        q = Term(u"content", next(subtrees))
        for st in subtrees:
            q |= Term(u"content", st)
        with i.index.searcher() as searcher:
            for result in searcher.search(q):
                print(result)