Beispiel #1
0
    def generate_config_file(self, config_file):
        conf = ConfigParser.RawConfigParser()
        conf.read(config_file)

        # COLOR
        conf.add_section('colors')
        for c in self.colors:
            conf.set('colors', c, self.colors[c]['c'])
        conf.set('colors', 'bold', '')
        # KEYS
        conf.add_section('keys')
        for k in self.keys:
            conf.set('keys', k, self.keys[k])
        # PARAMS
        conf.add_section('params')
        for p in self.params:
            if self.params[p] == True:
                value = 1
            elif self.params[p] == False:
                value = 0
            elif self.params[p] == None:
                continue
            else:
                value = self.params[p]

            conf.set('params', p, value)

        with open(config_file, 'wb') as config:
            conf.write(config)

        print encode(_('Genereting configuration file in %s')) % config_file
Beispiel #2
0
 def get_bold_colors(self, str):
     bolds = str.split(' ')
     for bold in bolds:
         try:
             self.colors[bold]['b'] = True
         except:
             print encode(_('The param "%s" does not exist for bold colors')) % bold
Beispiel #3
0
 def listdir(self,directory):
     files = []
     dirs = []
 
     if(not directory.startswith('/')):
         directory = '/' + directory
     
     #get the id of this folder
     parentFolder = self._getGoogleFile(directory)
 
     #need to do this after
     if(not directory.endswith('/')):
             directory = directory + '/'
 
     if(parentFolder != None):
     
         fileList = self.drive.ListFile({'q':"'" + parentFolder['id'] + "' in parents and trashed = false"}).GetList()
    
         for aFile in fileList:
             if(aFile['mimeType'] == self.FOLDER_TYPE):
                 dirs.append(utils.encode(aFile['title']))
             else:
                 files.append(utils.encode(aFile['title']))
             
 
     return [dirs,files]    
Beispiel #4
0
	def _raise_exception():
		if raise_exception:
			if flags.rollback_on_exception:
				db.rollback()
			import inspect
			if inspect.isclass(raise_exception) and issubclass(raise_exception, Exception):
				raise raise_exception, encode(msg)
			else:
				raise ValidationError, encode(msg)
Beispiel #5
0
    def test_incomplete_task(self):
        """Send task messages without all required keys."""
        c = encode(source(constant))
        msg1 = {"module": c, "params": encode(())}
        msg2 = {"module": c, "dataset": []}
        msg3 = {"dataset": [], "params": encode(())}

        for msg in (msg1, msg2, msg3):
            self.write_command("task", msg)
            assert_true("missing key" in self.read_error_string())
Beispiel #6
0
 def check_for_default_config(self):
     default_dir = '/tyrs'
     default_file = '/tyrs/tyrs.cfg'
     if not os.path.isfile(self.xdg_config + default_file):
         if not os.path.exists(self.xdg_config + default_dir):
             try:
                 os.makedirs(self.xdg_config + default_dir)
             except:
                 print encode(_('Couldn\'t create the directory in %s/tyrs')) % self.xdg_config
         self.generate_config_file(self.xdg_config + default_file)
Beispiel #7
0
def print_ask_service(token_file):
    print ''
    print encode(_('Couldn\'t find any profile.'))
    print ''
    print encode(_('It should reside in: %s')) % token_file
    print encode(_('If you want to setup a new account, then follow these steps'))
    print encode(_('If you want to skip this, just press return or ctrl-C.'))
    print ''

    print ''
    print encode(_('Which service do you want to use?'))
    print ''
    print '1. Twitter'
    print '2. Identi.ca'
    print ''
Beispiel #8
0
def print_ask_service(config_file):
    print ''
    print encode(_('There is no profile detected.'))
    print ''
    print encode(_('It should be in %s')) % config_file
    print encode(_('If you want to setup a new account, let\'s go through some basic steps'))
    print encode(_('If you want to skip this, just press return or ctrl-C.'))
    print ''

    print ''
    print encode(_('Which service do you want to use?'))
    print ''
    print '1. Twitter'
    print '2. Identi.ca'
    print ''
Beispiel #9
0
    def listdir(self,directory):
        if(self.client != None and self.exists(directory)):
            files = []
            dirs = []
            metadata = self.client.metadata(directory)

            for aFile in metadata['contents']:
                if(aFile['is_dir']):
                    dirs.append(utils.encode(aFile['path'][len(directory):]))
                else:
                    files.append(utils.encode(aFile['path'][len(directory):]))

            return [dirs,files]
        else:
            return [[],[]]
Beispiel #10
0
    def get_header(self, status):
        retweeted = ""
        reply = ""
        retweet_count = ""
        retweeter = ""
        source = self.get_source(status)
        nick = self.get_nick(status)
        time = self.get_time(status)

        if self.is_reply(status):
            reply = u" \u2709"
        if status.rt:
            retweeted = u" \u267b "
            retweeter = nick
            nick = self.origin_of_retweet(status)

        if self.get_retweet_count(status):
            retweet_count = str(self.get_retweet_count(status))

        header_template = self.conf.params["header_template"]
        header = unicode(header_template).format(
            time=time,
            nick=nick,
            reply=reply,
            retweeted=retweeted,
            source=source,
            retweet_count=retweet_count,
            retweeter=retweeter,
        )

        return encode(header)
Beispiel #11
0
    def display(self):
        """show status bar"""

        self.win.erase()
        adir = self.app.act_pane.act_tab
        maxw = self.app.maxw
        if len(adir.selections) > 0:
            if maxw >= 45:
                size = 0
                for f in adir.selections:
                    size += adir.files[f][files.FT_SIZE]
                self.win.addstr('    %s bytes in %d files' % \
                                (num2str(size), len(adir.selections)))
        else:
            if maxw >= 80:
                self.win.addstr('File: %4d of %-4d' % \
                                (adir.file_i + 1, adir.nfiles))
                filename = adir.sorted[adir.file_i]
                if adir.vfs:
                    realpath = os.path.join(vfs.join(self.app.act_pane.act_tab),
                                            filename)
                else:
                    realpath = files.get_realpath(adir.path, filename,
                                                  adir.files[filename][files.FT_TYPE])
                path = (len(realpath)>maxw-35) and \
                    '~' + realpath[-(maxw-37):] or realpath
                self.win.addstr(0, 20, 'Path: ' + utils.encode(path))
        if maxw > 10:
            try:
                self.win.addstr(0, maxw-8, 'F1=Help')
            except:
                pass
        self.win.refresh()
Beispiel #12
0
 def __run(self, cmd, path, mode):
     if mode == PowerCLI.RUN_NEEDCURSESWIN:
         curses.endwin()
         try:
             msg = utils.get_shell_output3('cd "%s" && %s' % (path, cmd))
         except KeyboardInterrupt:
             os.system('reset')
             msg = 'Stopped by user'
         st = -1 if msg else 0
     elif mode == PowerCLI.RUN_BACKGROUND:
         utils.run_in_background(cmd, path)
         st, msg = 0, ''
     else: # PowerCLI.RUN_NORMAL
         st, msg = utils.ProcessFunc('Executing PowerCLI', cmd,
                                     utils.run_shell, cmd, path, True).run()
     if st == -1:
         messages.error('Cannot execute PowerCLI command:\n  %s\n\n%s' % \
                            (cmd, str(msg)))
     elif st != -100 and msg is not None and msg != '':
         if self.app.prefs.options['show_output_after_exec']:
             curses.curs_set(0)
             if messages.confirm('Executing PowerCLI', 'Show output', 1):
                 lst = [(l, 2) for l in msg.split('\n')]
                 pyview.InternalView('Output of "%s"' % utils.encode(cmd),
                                     lst, center=0).run()
     return st
Beispiel #13
0
def resolve(query, dnsIP='192.55.83.30'):   # Default IP is local host
    _, Qs, _, _, _ = utils.dissectDNS(query)
    name = Qs[0][0]
    if name in cache:
        return cache[name]

    ID = struct.unpack('!H', query[0:2])[0]
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.sendto(query, (dnsIP, 53))

    whatReady = select.select([sock], [], [])
    if whatReady:  # Pythonic way of saying list is not empty
        rcvd = sock.recv(512*8)
        print utils.isAA(rcvd)
        sock.close()
        oneStep = doOneStep(rcvd)

        if oneStep is not None:
            if len(oneStep) == 2:
                if oneStep[1] == NOCHANGE:  # Continue our journey
                    return resolve(query, oneStep[0])
                elif oneStep[1] == ANSWER:
                    _, Qs, _, _, _ = utils.dissectDNS(query)
                    return oneStep[0]
            else:
                # CNAMEQUERY: make query with new address
                address = oneStep[2]
                query = utils.encode(address, False, ID)
                if oneStep[0] is not None:
                    return resolve(query, oneStep[0])
                else:
                    return resolve(query)
Beispiel #14
0
 def ask_root_url(self):
     message.print_ask_root_url()
     url = raw_input(encode(_('Your choice? > ')))
     if url == '':
         self.base_url = 'https://identi.ca/api'
     else:
         self.base_url = url
Beispiel #15
0
 def send_refs(self, refs, worker=None):
     if worker is None:
         worker = self._worker
     # Encode the objects in the refs.
     refs = [(x, encode(y)) for (x, y) in refs]
     msg = {"refs": refs}
     self.write_command("refs", msg, worker=worker)
Beispiel #16
0
    def get_nick(self, status):
        if hasattr(status, 'user'):
            nick = status.user.screen_name
        else:
            nick = status.sender_screen_name

        return encode(nick)
Beispiel #17
0
 def put(self,source,dest):
     
     aFile = xbmcvfs.File(xbmc.translatePath(source),'r')
     
     self.zip.writestr(utils.encode(dest),aFile.read())
     
     return True
Beispiel #18
0
    def get_header(self, status):
        retweeted = ''
        reply = ''
        retweet_count = ''
        retweeter = ''
        source = self.get_source(status)
        nick = self.get_nick(status)
        timer = self.get_time(status)

        if self.is_reply(status):
            reply = u' \u2709'
        if status.rt:
            retweeted = u" \u267b "
            retweeter = nick
            nick = self.origin_of_retweet(status)

        if self.get_retweet_count(status):
            retweet_count = str(self.get_retweet_count(status))
            
        tyrs.container['completion'].add(get_exact_nick(nick))
        header_template = self.conf.params['header_template'] 
        header = unicode(header_template).format(
            time = timer,
            nick = nick,
            reply = reply,
            retweeted = retweeted,
            source = source,
            retweet_count = retweet_count,
            retweeter = retweeter
            )

        return encode(header)
Beispiel #19
0
 def open_shell(self):
     curses.endwin()
     if self.stdin_flag:
         os.system('sh')
     else:
         os.system('cd \"%s\"; sh' % encode(os.path.dirname(self.fc.filename)))
     curses.curs_set(0)
Beispiel #20
0
    def run_sftp(self, distfile, location):
        if not self.process.quiet:
            print 'running sftp_upload'
            self.delay()
            name = basename(distfile)
            print 'Uploading dist/%(name)s to %(location)s' % locals()

        with tempfile.NamedTemporaryFile() as file:
            cmds = 'put "%(distfile)s"\nbye\n' % locals()
            if sys.version_info[0] >= 3:
                cmds = encode(cmds)
            file.write(cmds)
            file.flush()
            cmdfile = file.name

            try:
                rc, lines = self.process.popen(
                    'sftp -b "%(cmdfile)s" "%(location)s"' % locals(),
                    echo=False)
                if rc == 0:
                    if not self.process.quiet:
                        print 'OK'
                    return rc
            except KeyboardInterrupt:
                pass
            err_exit('ERROR: sftp failed')
Beispiel #21
0
 def put(self,source,dest):
     
     aFile = xbmcvfs.File(xbmc.translatePath(source),'r')
     
     self.zip.writestr(utils.encode(dest),aFile.read(),compress_type=zipfile.ZIP_DEFLATED)
     
     return True
Beispiel #22
0
 def __find(self, title):
     self.pattern = messages.Entry(title, 'Type search string', '', True, False).run()
     if self.pattern is None or self.pattern == '':
         return -1
     filename = os.path.abspath(self.fc.filename)
     mode = (self.mode==MODE_TEXT) and 'n' or 'b'
     try:
         cmd = '%s -i%c \"%s\" \"%s\"' % (sysprogs['grep'], mode,
                                          self.pattern, filename)
         st, buf = run_shell(encode(cmd), path=u'.', return_output=True)
     except OSError:
         self.show()
         messages.error('Find error: Can\'t open file')
         return -1
     if st == -1:
         self.show()
         messages.error('Find error\n' + buf)
         self.matches = []
         return -1
     else:
         try:
             self.matches = [int(l.split(':')[0]) for l in buf.split('\n') if l]
         except (ValueError, TypeError):
             self.matches = []
         return 0
Beispiel #23
0
    def listdir(self,directory):
        directory = self._fix_slashes(directory)
        
        if(self.client != None and self.exists(directory)):
            files = []
            dirs = []
            metadata = self.client.files_list_folder(directory)

            for aFile in metadata.entries:
                if(isinstance(aFile,dropbox.files.FolderMetadata)):
                    dirs.append(utils.encode(aFile.name))
                else:
                    files.append(utils.encode(aFile.name))

            return [dirs,files]
        else:
            return [[],[]]
Beispiel #24
0
    def display_text(self, panel, status):
        '''needed to cut words properly, as it would cut it in a midle of a
        world without. handle highlighting of '#' and '@' tags.
        '''
        text = self.get_text(status)
        words = text.split(' ')
        margin = self.conf.params['margin']
        padding = self.conf.params['padding']
        myself = self.api.myself.screen_name
        curent_x = padding
        line = 1

        hashtag = encode('#')
        attag = encode('@')


        for word in words:
            word = encode(word)
            if curent_x + len(word) > self.maxyx[1] - (margin + padding)*2:
                line += 1
                curent_x = padding

            if word != '':
                # The word is an HASHTAG ? '#'
                if word[0] == hashtag:
                    panel.addstr(line, curent_x, word, self.get_color('hashtag'))
                # Or is it an 'AT TAG' ? '@'
                elif word[0] == attag:
                    # The AT TAG is,  @myself
                    if word == attag + myself or word == attag + myself+ encode(':'):
                        panel.addstr(line, curent_x, word, self.get_color('highlight'))
                    # @anyone
                    else:
                        panel.addstr(line, curent_x, word, self.get_color('attag'))
                # It's just a normal word
                else:
                    try:
                        panel.addstr(line, curent_x, word, self.get_color('text'))
                    except curses.error:
                        pass
                curent_x += len(word) + 1

                # We check for ugly empty spaces
                while panel.inch(line, curent_x -1) == ord(' ') and panel.inch(line, curent_x -2) == ord(' '):
                    curent_x -= 1
Beispiel #25
0
    def display_cursorbar(self):
        if self == self.app.act_pane:
            attr_noselected = curses.color_pair(3)
            attr_selected = curses.color_pair(11) | curses.A_BOLD
        else:
            if self.app.prefs.options['manage_otherpane']:
                attr_noselected = curses.color_pair(20)
                attr_selected = curses.color_pair(21)
            else:
                return
        if self.mode == PANE_MODE_FULL:
            cursorbar = curses.newpad(1, self.maxw)
        else:
            cursorbar = curses.newpad(1, self.dims[1]-1)
        cursorbar.bkgd(curses.color_pair(3))
        cursorbar.erase()

        tab = self.act_tab
        filename = tab.sorted[tab.file_i]
        try:
            tab.selections.index(filename)
        except ValueError:
            attr = attr_noselected
        else:
            attr = attr_selected

        res = files.get_fileinfo_dict(tab.path, filename, tab.files[filename])
        if self.mode == PANE_MODE_FULL:
            buf = tab.get_fileinfo_str_long(res, self.maxw)
            cursorbar.addstr(0, 0, utils.encode(buf), attr)
            cursorbar.refresh(0, 0,
                              tab.file_i % self.dims[0] + 1, 0,
                              tab.file_i % self.dims[0] + 1, self.maxw-2)
        else:
            buf = tab.get_fileinfo_str_short(res, self.dims[1], self.pos_col1)
            cursorbar.addstr(0, 0, utils.encode(buf), attr)
            cursorbar.addch(0, self.pos_col1-1, curses.ACS_VLINE, attr)
            cursorbar.addch(0, self.pos_col2-1, curses.ACS_VLINE, attr)
            row = tab.file_i % (self.dims[0]-3) + 3
            if self.mode == PANE_MODE_LEFT:
                cursorbar.refresh(0, 0,
                                  row, 1, row, int(self.maxw/2)-2)
            else:
                cursorbar.refresh(0, 0,
                                  row, int(self.maxw/2)+1, row, self.maxw-2)
Beispiel #26
0
    def __getitem__(self, idx):
        source = encode(
            "generate prompt: " + self.data.iloc[idx]["source"],
            self.tokenizer,
            config.MAX_INPUT_LEN,
        )
        target = encode(self.data.iloc[idx]["target"], self.tokenizer,
                        config.MAX_TARGET_LEN)

        target = target["input_ids"]
        input_ids = source["input_ids"]
        mask = source["attention_mask"]

        return {
            "input_ids": torch.tensor(input_ids, dtype=torch.long),
            "mask": torch.tensor(mask, dtype=torch.long),
            "target": torch.tensor(target, dtype=torch.long),
        }
Beispiel #27
0
def get_hf_len(counter):
    enc = utils._encode2dict(utils.encode(counter))
    l = 0
    h = 0
    for k,v in counter.items():
        l+= len(enc[k])*v
        h+= len(enc[k])
        
    return (16*len(enc), h, l)
Beispiel #28
0
 def post(self):
     data = request.get_json()
     password = data.get('password')
     password = encode(password)
     email = data.get('email')
     username = data.get('username')
     email_code = data.get('email_code')
     res = db.create_user(password, email, username, email_code)
     return dumps(res, ensure_ascii=False)
Beispiel #29
0
 def _display_info(self):
     info = {
         'location': encode(self.user.location),
         'description': encode(self.user.description),
         'url': encode(self.user.url),
         'time zone': encode(self.user.time_zone),
         'status': self.user.status,
         'friends': self.user.friends_count,
         'follower': self.user.followers_count,
         'tweets': self.user.statuses_count,
         'verified': self.user.verified,
         'created at': self.user.created_at,
         }
     i=0
     for item in info:
         self.screen.addstr(4+i, 5, '%s' % item)
         self.screen.addstr(4+i, 20, '%s' % info[item])
         i += 1
Beispiel #30
0
    def onmessage(self, msg):

        print 'Got message: {0}'.format(msg)

        try:
           msg = encode(str(int(msg)+1), opcode=Opcode.TEXT)
           self.client.send(msg)
        except ValueError:
            pass
Beispiel #31
0
 def _prepare_data(self, nonce):
     data_lst = [
         str(self._block.block_header.prev_block_hash),
         str(self._block.block_header.hash_merkle_root),
         str(self._block.block_header.timestamp),
         str(self._block.block_header.height),
         str(nonce)
     ]
     return utils.encode(''.join(data_lst))
Beispiel #32
0
    def challenge_7():
        print(f"\n-- Challenge 7 - AES in ECB mode --")

        key = encode("YELLOW SUBMARINE")
        data = b64decode(ut.import_data("data_S1C7.txt"))
        plaintext = ocl.AESECB(key).decrypt(data)

        print(f"Key    : {decode(key)}")
        print(f"Secret : \n{decode(plaintext[:90])}...")
def handwave(receiver_socket, seq_num, ack_num):
    receiver_socket.sendto(utils.encode(seq_num, ack_num, 2),
                           config['sender_addr'])
    log('snd', 2, seq_num, 0, ack_num)

    receiver_socket.sendto(utils.encode(seq_num, ack_num, 1),
                           config['sender_addr'])
    log('snd', 1, seq_num, 0, ack_num)
    seq_num += 1

    raw_data, addr = receiver_socket.recvfrom(65536)
    ret = utils.decode(raw_data)
    assert (ret is not None)
    assert (ret['ack_num'] == seq_num)
    assert (ret['flag'] == 2)
    log('rcv', ret['flag'], ret['seq_num'], len(ret['data']), ret['ack_num'])

    receiver_socket.close()
Beispiel #34
0
def encode_file(filename, key):
    with open(filename, "rb") as fi, open(filename + ".enc", "wb") as fo:
        while fi.readable():
            chunk = fi.read(CHUNK_SIZE)
            if len(chunk) == 0:
                break
            fo.write(encode(chunk, key))
    remove(filename)
    rename(filename + ".enc", filename)
Beispiel #35
0
 def _display_info(self):
     info = {
         'location': encode(self.user.location),
         'description': encode(self.user.description),
         'url': encode(self.user.url),
         'time zone': encode(self.user.time_zone),
         'status': self.user.status,
         'friends': self.user.friends_count,
         'follower': self.user.followers_count,
         'tweets': self.user.statuses_count,
         'verified': self.user.verified,
         'created at': self.user.created_at,
     }
     i = 0
     for item in info:
         self.screen.addstr(4 + i, 5, '%s' % item)
         self.screen.addstr(4 + i, 20, '%s' % info[item])
         i += 1
Beispiel #36
0
    def put(self, source, dest):

        aFile = xbmcvfs.File(xbmc.translatePath(source), 'r')

        self.zip.writestr(utils.encode(dest),
                          aFile.read(),
                          compress_type=zipfile.ZIP_DEFLATED)

        return True
Beispiel #37
0
 def _prepare_data(self, nonce):
     data_lst = [
         str(self._block._prev_block_hash),
         str(self._block._transaction),
         str(self._block._height),
         str(self._block._timestamp),
         str(self._block._bits),
         str(nonce)
     ]
     return utils.encode(''.join(data_lst))
Beispiel #38
0
    def get_url(self):
        if self.cluster is None:
            raise QueryArgumentError("cluster query needs cluster ID")

        urlargs = {"cluster": self.cluster, "num": self.num_results or ScholarConf.MAX_PAGE_RESULTS}

        for key, val in urlargs.items():
            urlargs[key] = quote(encode(val))

        return self.SCHOLAR_CLUSTER_URL % urlargs
Beispiel #39
0
    def createTokenFile(self):

        if not os.path.isdir(self.tyrs_path):
            try:
                os.mkdir(self.tyrs_path)
            except:
                print encode(_('Error creating directory .config/tyrs'))

        conf = ConfigParser.RawConfigParser()
        conf.add_section('token')
        conf.set('token', 'service', self.service)
        conf.set('token', 'base_url', self.base_url)
        conf.set('token', 'oauth_token', self.oauth_token)
        conf.set('token', 'oauth_token_secret', self.oauth_token_secret)

        with open(self.token_file, 'wb') as tokens:
            conf.write(tokens)

        print encode(_('your account has been saved'))
Beispiel #40
0
    def display_cursorbar(self):
        if self == self.app.act_pane:
            attr_noselected = curses.color_pair(3)
            attr_selected = curses.color_pair(11) | curses.A_BOLD
        else:
            if self.app.prefs.options['manage_otherpane']:
                attr_noselected = curses.color_pair(20)
                attr_selected = curses.color_pair(21)
            else:
                return
        if self.mode == PANE_MODE_FULL:
            cursorbar = curses.newpad(1, self.maxw)
        else:
            cursorbar = curses.newpad(1, self.dims[1] - 1)
        cursorbar.bkgd(curses.color_pair(3))
        cursorbar.erase()

        tab = self.act_tab
        filename = tab.sorted[tab.file_i]
        try:
            tab.selections.index(filename)
        except ValueError:
            attr = attr_noselected
        else:
            attr = attr_selected

        res = files.get_fileinfo_dict(tab.path, filename, tab.files[filename])
        if self.mode == PANE_MODE_FULL:
            buf = tab.get_fileinfo_str_long(res, self.maxw)
            cursorbar.addstr(0, 0, utils.encode(buf), attr)
            cursorbar.refresh(0, 0, tab.file_i % self.dims[0] + 1, 0,
                              tab.file_i % self.dims[0] + 1, self.maxw - 2)
        else:
            buf = tab.get_fileinfo_str_short(res, self.dims[1], self.pos_col1)
            cursorbar.addstr(0, 0, utils.encode(buf), attr)
            cursorbar.addch(0, self.pos_col1 - 1, curses.ACS_VLINE, attr)
            cursorbar.addch(0, self.pos_col2 - 1, curses.ACS_VLINE, attr)
            row = tab.file_i % (self.dims[0] - 3) + 3
            if self.mode == PANE_MODE_LEFT:
                cursorbar.refresh(0, 0, row, 1, row, int(self.maxw / 2) - 2)
            else:
                cursorbar.refresh(0, 0, row,
                                  int(self.maxw / 2) + 1, row, self.maxw - 2)
Beispiel #41
0
    def createTokenFile(self):

        if not os.path.isdir(self.tyrs_path):
            try:
                os.mkdir(self.tyrs_path)
            except:
                print encode(_('Error creating directory .config/tyrs'))

        conf = ConfigParser.RawConfigParser()
        conf.add_section('token')
        conf.set('token', 'service', self.service)
        conf.set('token', 'base_url', self.base_url)
        conf.set('token', 'oauth_token', self.oauth_token)
        conf.set('token', 'oauth_token_secret', self.oauth_token_secret)

        with open(self.token_file, 'wb') as tokens:
            conf.write(tokens)

        print encode(_('your account has been saved'))
Beispiel #42
0
    def generate_user_token(user):
        "Generate a temp token"
        if not isinstance(user, datamodel.User):
            user = SecurityService.userid_to_user(user)
        if not user:
            return None

        token_store = UserTokenStore()
        token = token_store.create(user)
        return utils.encode(token.value)
Beispiel #43
0
def handshake(sender_socket):
    seq_num = 0
    ack_num = 0
    config['time'] = time.time()
    sender_socket.sendto(utils.encode(seq_num, ack_num, 4), (config['receiver_host_ip'], config['receiver_port']))
    log('snd', 4, seq_num, 0, ack_num)
    seq_num += 1

    ret = sender_recv(sender_socket)
    assert(ret is not None)
    assert(ret['ack_num'] == seq_num)
    assert(ret['flag'] == 6)
    log('rcv', ret['flag'], ret['seq_num'], len(ret['data']), ret['ack_num'])
    ack_num = ret['seq_num'] + 1

    sender_socket.sendto(utils.encode(seq_num, ack_num, 2), (config['receiver_host_ip'], config['receiver_port']))
    log('snd', 2, seq_num, 0, ack_num)

    return seq_num, ack_num
Beispiel #44
0
 def test_encode(self):
     assert utils.encode('abc') == 'abc'
     assert utils.encode(u'abc') == 'abc'
     assert utils.encode(u'\u4f60\u597d') == '\xe4\xbd\xa0\xe5\xa5\xbd'
     assert utils.encode('\xe4\xbd\xa0\xe5\xa5\xbd') == \
         '\xe4\xbd\xa0\xe5\xa5\xbd'
     assert utils.encode('abc', 'shift_jis') == 'abc'
     assert utils.encode(u'abc', 'shift_jis') == 'abc'
     assert utils.encode(u'\uffe3\u2015', 'shift_jis') == '\x81P\x81\\'
Beispiel #45
0
def set_credentials(username, password):
    config = ConfigParser.ConfigParser()
    config.read([utils.get_config_file()])
    try:
        config.add_section('user')
    except ConfigParser.DuplicateSectionError:
        pass
    config.set('user', 'username', username)
    config.set('user', 'password', utils.encode(password))
    with open(utils.get_config_file(), 'wb') as configfile:
        config.write(configfile)
Beispiel #46
0
 def post(self):
     res = {"state": "fail"}
     try:
         data = request.get_json()
         mail = data.get('mail')
         password = encode(data.get('password'))
         res = db.expert_set_password(mail, password)
     except:
         pass
     finally:
         return jsonify(res)
Beispiel #47
0
    def get_url(self):
        if self.cluster is None:
            raise QueryArgumentError('cluster query needs cluster ID')

        urlargs = {'cluster': self.cluster,
                   'num': self.num_results or ScholarConf.MAX_PAGE_RESULTS}

        for key, val in urlargs.items():
            urlargs[key] = quote(encode(val))

        return self.SCHOLAR_CLUSTER_URL % urlargs
Beispiel #48
0
    async def get_classes(cls):
        query = "?api_key={}".format(config.get("warcraftlogs_token"))
        url = encode("{}/class".format(cls.BASE), query)

        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                if response.status == 200:
                    return await response.json()
                else:
                    logger.error("Failed to get classes from warcraftlogs.")
                    return None
Beispiel #49
0
    def ask_service(self):
        message.print_ask_service(self.token_file)
        choice = raw_input(encode(_('Your choice? > ')))

        if choice == '1':
            self.service = 'twitter'
        elif choice == '2':
            self.service = 'identica'
        else:
            sys.exit(1)
        return choice
Beispiel #50
0
    async def get_weekly_affixes(cls):
        query = "?region={}&locale={}".format(REGION, LOCALE)
        url = encode("{}/mythic-plus/affixes".format(cls.BASE), query)

        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                if response.status == 200:
                    return await response.json()
                else:
                    logger.error("Failed to get weekly affixes from raider.")
                    return None
Beispiel #51
0
    def __init__(self, txid=None, vout=None, sig=None, pubkey=None):
        self._tx_id = 0
        self._vout = 0
        self._sig = None
        self._public_key = None

        if txid != None and vout != None and sig != None and pubkey != None:
            self._tx_id = utils.encode(txid)
            self._vout = vout
            self._sig = sig
            self._public_key = pubkey
Beispiel #52
0
def main():
    cmd_args = parse_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = cmd_args.device
    if cmd_args.seed is not None:
        random.seed(cmd_args.seed)
        np.random.seed(cmd_args.seed)
        tf.set_random_seed(cmd_args.seed)

    print("Reading data...")
    train_data, val_data, test_data = read_data(cmd_args.x, cmd_args.y,
                                                cmd_args.split)
    train_val_data = train_data + val_data

    print("Building model...")
    model = CNNPredictor(
        path=cmd_args.output_path,
        input_len=train_val_data["x"].shape[1],
        input_channel=train_val_data["x"].shape[2],
        kernel_num=1000,
        kernel_len=5,
        fc_depth=2,
        fc_dim=1000,
        final_fc_dim=100,
        class_num=train_val_data["y"].shape[1],
        class_weights=[
            (0.5 * train_val_data.size() /
             (train_val_data.size() - train_val_data["y"][:, i].sum()),
             0.5 * train_val_data.size() / train_val_data["y"][:, i].sum())
            for i in range(train_val_data["y"].shape[1])
        ])
    model.compile(lr=1e-4)
    if os.path.exists(os.path.join(cmd_args.output_path, "final")):
        print("Loading existing weights...")
        model.load(os.path.join(cmd_args.output_path, "final"))

    if not cmd_args.no_fit:
        print("Fitting model...")
        model.fit(train_data,
                  val_data,
                  batch_size=128,
                  epoch=1000,
                  patience=10)
        model.save(os.path.join(cmd_args.output_path, "final"))

    print("Evaluating result...")
    evaluate(model, train_val_data, test_data, cmd_args.output_path)
    if cmd_args.save_hidden:
        all_data = train_val_data + test_data
        hidden = model.fetch(model.final_fc, all_data)
        with h5py.File(os.path.join(cmd_args.output_path, "hidden.h5"),
                       "w") as f:
            f.create_dataset("mat", data=hidden)
            f.create_dataset("protein_id",
                             data=utils.encode(all_data["protein_id"]))
Beispiel #53
0
 def post(self):
     data = request.get_json()
     password = data.get('password')
     password = encode(password)
     email = data.get('email')
     res = {"state": "fail"}
     try:
         res = db.compare_password(password, email)
         return dumps(res, ensure_ascii=False)
     except:
         return dumps(res, ensure_ascii=False)
Beispiel #54
0
def main(args):

    train_sentences_raw = seq2seq_utils.load_data(args.train_file)
    dev_sentences_raw = utils.load_data(args.dev_file)
    args.num_train = len(train_sentences_raw)
    args.num_dev = len(dev_sentences_raw)
    word_dict, args.vocab_size = utils.load_dict(args.vocab_file)
    train_sentences = seq2seq_utils.encode(train_sentences_raw, word_dict)
    train_sentences = seq2seq_utils.gen_examples(train_sentences,
                                                 args.batch_size)
    dev_sentences = utils.encode(dev_sentences_raw, word_dict)
    dev_sentences = utils.gen_examples(dev_sentences, args.batch_size)

    if os.path.exists(args.model_file):
        model = torch.load(args.model_file)

    train_encodings = []

    for idx, (mb_x, mb_x_mask, mb_y, mb_y_mask) in enumerate(train_sentences):
        batch_size = mb_x.shape[0]
        mb_x = Variable(torch.from_numpy(mb_x)).long()
        mb_x_mask = Variable(torch.from_numpy(mb_x_mask)).long()
        hidden = model.init_hidden(batch_size)
        hiddens, _ = model.encode(mb_x, mb_x_mask, hidden)
        train_encodings.append(hiddens)

    train_encodings = torch.cat(train_encodings, 0)

    # code.interact(local=locals())

    idx = 0
    mb_x, mb_x_mask = dev_sentences[0]
    batch_size = mb_x.shape[0]
    mb_x = Variable(torch.from_numpy(mb_x)).long()
    mb_x_mask = Variable(torch.from_numpy(mb_x_mask)).long()
    hidden = model.init_hidden(batch_size)
    hiddens, _ = model.encode(mb_x, mb_x_mask, hidden)
    similarity = F.linear(hiddens, train_encodings)
    # code.interact(local=locals())
    similarity = similarity / torch.norm(hiddens, 2, 1).expand_as(similarity)
    similarity = similarity / torch.norm(train_encodings, 2, 1).transpose(
        1, 0).expand_as(similarity)
    similarity = similarity.data.numpy()
    nearest = similarity.argpartition(10)[:, -10:]
    for i in range(10):
        # code.interact(local=locals())
        print("\\\\dev: " +
              " ".join(dev_sentences_raw[idx * args.batch_size + i]).replace(
                  "<", "$<$").replace(">", "$>$") + "\\\\")
        for k in range(10):
            print("nearest neighbor in training: " +
                  " ".join(train_sentences_raw[nearest[i][k]][0]).replace(
                      "<", "$<$").replace(">", "$>$") + "\\\\")
Beispiel #55
0
 def get_rdev(f):
     """'ls -la' to get mayor and minor number of devices"""
     try:
         buf = get_shell_output('ls -la %s' % encode(f))
     except:
         return 0
     else:
         try:
             return int(buf[4][:-1]), int(buf[5])
         except:
             # HACK: found 0xff.. encoded device numbers, ignore them...
             return 0, 0
Beispiel #56
0
 def _prepare_data(self, nonce):
     data_lst = [
         self._block.prev_block_hash, self._block.timestamp,
         str(self.target_bits),
         str(nonce)
     ]
     # data_lst = [self._block.prev_block_hash,
     #             self._block.hash_transactions(),
     #             self._block.timestamp,
     #             str(self.target_bits),
     #             str(nonce)]
     return utils.encode(''.join(data_lst))
Beispiel #57
0
    async def check_access_token(cls):
        query = "?token={}".format(cls._token)
        url = encode("{}/check_token".format(cls.OAUTH_BASE), query)

        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                if response.status == 400:
                    await cls.change_access_token()
                    return True
                elif response.status == 200:
                    return True
        return False
Beispiel #58
0
def handwave(sender_socket, seq_num, ack_num):
    sender_socket.sendto(utils.encode(seq_num, ack_num, 1), (config['receiver_host_ip'], config['receiver_port']))
    log('snd', 1, seq_num, 0, ack_num)
    seq_num += 1

    ret = sender_recv(sender_socket)
    assert(ret is not None)
    assert(ret['ack_num'] == seq_num) #
    assert(ret['flag'] == 2)
    log('rcv', ret['flag'], ret['seq_num'], len(ret['data']), ret['ack_num'])

    ret = sender_recv(sender_socket)
    assert(ret is not None)
    assert(ret['seq_num'] == ack_num)
    assert(ret['flag'] == 1)
    log('rcv', ret['flag'], ret['seq_num'], len(ret['data']), ret['ack_num'])
    ack_num += 1

    sender_socket.sendto(utils.encode(seq_num, ack_num, 2), (config['receiver_host_ip'], config['receiver_port']))
    log('snd', 2, seq_num, 0, ack_num)
    sender_socket.close()
Beispiel #59
0
    def __init__(self, robot_id: str, curr_node_id: int):
        self.host, self.port = '127.0.0.1', 2000
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.curr_task: Task = Task(TaskType.NO_TASK, {})
        self.task_queue = queue.Queue()

        # Connect to server
        self.socket.connect((self.host, self.port))
        self.socket.send(
            utils.encode(f"{robot_id};{curr_node_id}"))  # send ID to server
        print("Connected successfully!")
        threading.Thread(target=self.receive_messages, daemon=True).start()
Beispiel #60
0
    def __init__(self):
        super(ComicStreamerConfig, self).__init__()

        self.csfolder = AppFolders.settings()

        # make sure folder exisits
        if not os.path.exists(self.csfolder):
            os.makedirs(self.csfolder)

        # set up initial values
        self.filename = os.path.join(self.csfolder, "comicstreamer")
        if platform.system() == "Windows":
            self.filename += ".ini"
        else:  #if platform.system() == "Darwin":
            self.filename += ".conf"

        self.configspec = io.StringIO(ComicStreamerConfig.configspec)
        self.encoding = "UTF8"

        # since some stuff in the configobj has to happen during object initialization,
        # use a temporary delegate,  and them merge it into self
        tmp = ConfigObj(self.filename,
                        configspec=self.configspec,
                        encoding=self.encoding)
        validator = Validator()
        tmp.validate(validator, copy=True)

        if tmp['format.ebook']['calibre'] == '':
            if platform.system() == "Darwin":
                calibre = '/Applications/calibre.app/Contents/MacOS/ebook-convert'
                if os.path.isfile(calibre):
                    tmp['format.ebook']['calibre'] = calibre

        # set up the install ID
        if tmp['general']['install_id'] == '':
            tmp['general']['install_id'] = uuid.uuid4().hex

        #set up the cookie secret
        if tmp['security']['cookie_secret'] == '':
            tmp['security']['cookie_secret'] = base64.b64encode(
                uuid.uuid4().bytes + uuid.uuid4().bytes)

        # normalize the folder list
        tmp['general']['folder_list'] = [
            os.path.abspath(os.path.normpath(unicode(a)))
            for a in tmp['general']['folder_list']
        ]
        tmp['database.mysql']['password'] = utils.encode(
            tmp['general']['install_id'], 'comic')

        self.merge(tmp)
        if not os.path.exists(self.filename):
            self.write()