Python string.printable() Examples

The following are 30 code examples of string.printable(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module string , or try the search function .
Example #1
Source File: parsers.py    From rekall with GNU General Public License v2.0 7 votes vote down vote up
def anything_beetween(opener_and_closer):
    """Builds a (pyparsing) parser for the content inside delimiters.

    Args:
    opener_and_closer: a string containing two elements: opener and closer

    Returns:
      A (pyparsing) parser for the content inside delimiters.
    """
    opener = pyparsing.Literal(opener_and_closer[0])
    closer = pyparsing.Literal(opener_and_closer[1])
    char_removal_mapping = dict.fromkeys(map(ord, opener_and_closer))
    other_chars = unicode(string.printable).translate(char_removal_mapping)
    word_without_delimiters = pyparsing.Word(other_chars).setName(
        "other_chars")
    anything = pyparsing.Forward()
    delimited_block = opener + anything + closer
    # pylint: disable=expression-not-assigned
    anything << pyparsing.ZeroOrMore(
        word_without_delimiters.setName("word_without_delimiters")
        | delimited_block.setName("delimited_block")
    )

    # Combine all the parts into a single string.
    return pyparsing.Combine(anything) 
Example #2
Source File: yara_support.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def anything_beetween(opener_and_closer):
    """Builds a (pyparsing) parser for the content inside delimiters.

    Args:
    opener_and_closer: a string containing two elements: opener and closer

    Returns:
      A (pyparsing) parser for the content inside delimiters.
    """
    opener = pyparsing.Literal(opener_and_closer[0])
    closer = pyparsing.Literal(opener_and_closer[1])
    char_removal_mapping = dict.fromkeys(list(map(ord, opener_and_closer)))
    other_chars = str(string.printable).translate(char_removal_mapping)
    word_without_delimiters = pyparsing.Word(other_chars).setName(
        "other_chars")
    anything = pyparsing.Forward()
    delimited_block = opener + anything + closer
    # pylint: disable=expression-not-assigned
    anything << pyparsing.ZeroOrMore(
        word_without_delimiters.setName("word_without_delimiters")
        | delimited_block.setName("delimited_block")
    )

    # Combine all the parts into a single string.
    return pyparsing.Combine(anything) 
Example #3
Source File: strings.py    From Karta with MIT License 6 votes vote down vote up
def nextGlobalString(self, ea):
        """Find the next possible address for a global string, given the beginning of the current global string.

        Args:
            ea (int): effective start address of the current global string.

        Return Value:
            Possible start address for the next global string
        """
        str_content = self.getAsciiString(ea)
        if str_content is None:
            return ea + self._global_alignment
        elif idc.get_wide_byte(ea + len(str_content)) != ord('\0'):
            return ea + max(self._global_alignment, pad(len(str_content), self._global_alignment))
        else:
            for offset in range(len(str_content) - 1, -1, -1):
                if chr(str_content[offset]) not in string.printable:
                    return ea + max(self._global_alignment, pad(offset, self._global_alignment))
        return ea + self._global_alignment 
Example #4
Source File: test_enabling_brick_mux.py    From glusto-tests with GNU General Public License v3.0 6 votes vote down vote up
def test_enabling_brick_mux_with_wrong_values(self):
        """
        Test Case:
        - Create a gluster cluster
        - Set cluster.brick-multiplex value to random string(Must fail)
        - Set cluster.brick-multiplex value to random int(Must fail)
        - Set cluster.brick-multiplex value to random
          special characters(Must fail)
        """
        # Creation of random data for cluster.brick-multiplex option
        # Data has: alphabets, numbers, punctuations and their combinations
        key = 'cluster.brick-multiplex'
        for char_type in (string.ascii_letters, string.punctuation,
                          string.printable, string.digits):

            temp_val = self.get_random_string(char_type)
            value = "{}".format(temp_val)
            ret = set_volume_options(self.mnode, 'all', {key: value})
            self.assertFalse(ret, "Unexpected: Erroneous value {}, to option "
                             "{} should result in failure".format(value, key))
            g.log.info("Expected: Erroneous value %s, to option "
                       "%s resulted in failure", value, key) 
Example #5
Source File: log.py    From privacyidea with GNU Affero General Public License v3.0 6 votes vote down vote up
def format(self, record):
        try:
            message = super(SecureFormatter, self).format(record)
        except TypeError:
            # In pyhton 2.6 the Formatter does not seem to 
            # be defined as 
            # class Formatter(object)
            # Using it in the super-statement this will raise a TypeError
            message = Formatter.format(self, record)
        secured = False

        s = ""
        for c in message:
            if c in string.printable:
                s += c
            else:
                s += '.'
                secured = True

        if secured:
            s = "!!!Log Entry Secured by SecureFormatter!!! " + s

        return s 
Example #6
Source File: libfuzzer.py    From clusterfuzz with Apache License 2.0 6 votes vote down vote up
def _restart_qemu(self):
    """Restart QEMU."""
    logs.log_warn(
        'Connection to fuzzing VM lost. Restarting.',
        processes=process_handler.get_runtime_snapshot())
    stop_qemu()

    # Do this after the stop, to make sure everything is flushed
    if os.path.exists(QemuProcess.LOG_PATH):
      with open(QemuProcess.LOG_PATH) as f:
        # Strip non-printable characters at beginning of qemu log
        qemu_log = ''.join(c for c in f.read() if c in string.printable)
        logs.log_warn(qemu_log)
    else:
      logs.log_error('Qemu log not found in {}'.format(QemuProcess.LOG_PATH))

    start_qemu()
    self._setup_device_and_fuzzer() 
Example #7
Source File: pescanner.py    From CapTipper with GNU General Public License v3.0 6 votes vote down vote up
def check_yara(self, data):
        ret = []
        if self.rules:
            yarahits = self.rules.match(data=data)
            if yarahits:
              for hit in yarahits:
                ret.append("YARA: %s" % hit.rule)
                #for key, val in hit.strings.iteritems():
                for (key,stringname,val) in hit.strings:
                    makehex = False
                    for char in val:
                        if char not in string.printable:
                            makehex = True
                            break
                    if makehex == True:
                        ret.append("   %s => %s" % (hex(key), binascii.hexlify(val)))
                    else:
                        ret.append("   %s => %s" % (hex(key), val))
        return '\n'.join(ret) 
Example #8
Source File: argparser.py    From streamlink with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def convert_arg_line_to_args(self, line):
        # Strip any non-printable characters that might be in the
        # beginning of the line (e.g. Unicode BOM marker).
        match = _printable_re.search(line)
        if not match:
            return
        line = line[match.start():].strip()

        # Skip lines that do not start with a valid option (e.g. comments)
        option = _option_re.match(line)
        if not option:
            return

        name, value = option.group("name", "value")
        if name and value:
            yield u"--{0}={1}".format(name, value)
        elif name:
            yield u"--{0}".format(name) 
Example #9
Source File: constraints_checker.py    From asn1tools with MIT License 6 votes vote down vote up
def encode(self, data):
        length = len(data)

        if not self.is_in_range(length):
            raise ConstraintsError(
                'Expected between {} and {} characters, but got {}.'.format(
                    self.minimum,
                    self.maximum,
                    length))

        if self.permitted_alphabet is None:
            return

        for character in data:
            if character not in self.permitted_alphabet:
                raise ConstraintsError(
                    "Expected a character in '{}', but got '{}' (0x{:02x}).".format(
                        ''.join([c if c in string.printable[:-5] else '.'
                                 for c in self.permitted_alphabet]),
                        character if character in string.printable else '.',
                        ord(character))) 
Example #10
Source File: __init__.py    From CapTipper with GNU General Public License v3.0 6 votes vote down vote up
def handle_string(self, token_text):
        if self.last_type in ['TK_START_BLOCK', 'TK_END_BLOCK', 'TK_SEMICOLON']:
            self.append_newline()
        elif self.last_type == 'TK_WORD':
            self.append(' ')

        # Try to replace readable \x-encoded characters with their equivalent,
        # if it is possible (e.g. '\x41\x42\x43\x01' becomes 'ABC\x01').
        def unescape(match):
            block, code = match.group(0, 1)
            char = chr(int(code, 16))
            if block.count('\\') == 1 and char in string.printable:
                return char
            return block

        token_text = re.sub(r'\\{1,2}x([a-fA-F0-9]{2})', unescape, token_text)

        self.append(token_text) 
Example #11
Source File: tabview.py    From OpenTrader with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _search_validator(self, ch):
        """Fix Enter and backspace for textbox.

        Used as an aux function for the textpad.edit method

        """
        if ch == curses.ascii.NL:  # Enter
            return curses.ascii.BEL
        elif ch == 127:  # Backspace
            self.search_str = self.textpad.gather().strip().lower()[:-1]
            return 8
        else:
            if 0 < ch < 256:
                c = chr(ch)
                if c in string.printable:
                    res = self.textpad.gather().strip().lower()
                    self.search_str = res + chr(ch)
                    self.search_results(look_in_cur=True)
                    self.display()
            return ch 
Example #12
Source File: corpus.py    From tmtoolkit with Apache License 2.0 6 votes vote down vote up
def filter_characters(self, allow_chars=string.printable, drop_chars=None):
        """
        Filter the document strings by removing all characters but those in `allow_chars` or, if `allow_chars` evaluates
        to False, remove those in `drop_chars`.

        :param allow_chars: set (like ``{'a', 'b', 'c'}`` or string sequence (like ``'abc'``)
        :param drop_chars: set or string sequence of characters to remove (if `allow_chars` evaluates to False)
        :return: this instance
        """

        if allow_chars is not None:
            if not isinstance(allow_chars, set):
                allow_chars = set(allow_chars)

            drop_chars = ''.join(self.unique_characters - allow_chars)
        else:
            if isinstance(drop_chars, (set, list, tuple)):
                drop_chars = ''.join(drop_chars)

            if not isinstance(drop_chars, str):
                raise ValueError('`drop_chars` must be a sequence, set or string if `allow_chars` is not given')

        return self.replace_characters(str.maketrans(drop_chars, drop_chars, drop_chars)) 
Example #13
Source File: backup.py    From qubes-core-admin with GNU Lesser General Public License v2.1 6 votes vote down vote up
def _monitor_process(proc, error_message):
        try:
            yield from proc.wait()
        except:
            proc.terminate()
            raise

        if proc.returncode:
            if proc.stderr is not None:
                proc_stderr = (yield from proc.stderr.read())
                proc_stderr = proc_stderr.decode('ascii', errors='ignore')
                proc_stderr = ''.join(
                    c for c in proc_stderr if c in string.printable and
                                              c not in '\r\n%{}')
                error_message += ': ' + proc_stderr
            raise qubes.exc.QubesException(error_message) 
Example #14
Source File: avclass_common.py    From BASS with GNU General Public License v2.0 6 votes vote down vote up
def get_sample_info(vt_rep, from_vt):
        '''Parse and extract sample information from JSON line
           Returns a SampleInfo named tuple: md5, sha1, sha256, label_pairs 
        '''
        label_pairs = []
        if from_vt:
            try:
                scans = vt_rep['scans']
            except KeyError:
                return None
            for av, res in scans.items():
                if res['detected']:
                    label = res['result']
                    clean_label = filter(lambda x: x in string.printable,
                                         label).strip().encode('utf-8').strip()
                    label_pairs.append((av, clean_label))
        else:
            label_pairs = vt_rep['av_labels']

        return SampleInfo(vt_rep['md5'], vt_rep['sha1'], vt_rep['sha256'],
                          label_pairs) 
Example #15
Source File: vnc_action_space.py    From universe with MIT License 6 votes vote down vote up
def __init__(self, keys=None, buttonmasks=None, screen_shape=(1024, 728)):
        self.keys = []
        if keys is None:
            keys = [c for c in string.printable] + list(constants.KEYMAP.keys())
        for key in (keys or []):
            down = vnc_event.KeyEvent.by_name(key, down=True)
            up = vnc_event.KeyEvent.by_name(key, down=False)
            self.keys.append(down)
            self.keys.append(up)
        self._key_set = set(self.keys)

        self.screen_shape = screen_shape
        if self.screen_shape is not None:
            self.buttonmasks = []
            if buttonmasks is None:
                buttonmasks = range(256)
            for buttonmask in buttonmasks:
                self.buttonmasks.append(buttonmask)
            self._buttonmask_set = set(self.buttonmasks) 
Example #16
Source File: test_change_reserve_limit_to_wrong_values.py    From glusto-tests with GNU General Public License v3.0 5 votes vote down vote up
def test_change_reserve_limit_to_wrong_value(self):
        """
        Test Case:
        1) Create and start a distributed-replicated volume.
        2) Give different inputs to the storage.reserve volume set options
        3) Validate the command behaviour on wrong inputs
        """

        # Creation of random data for storage.reserve volume option
        # Data has: alphabets, numbers, punctuations and their combinations
        key = 'storage.reserve'
        for char_type in (string.ascii_letters, string.punctuation,
                          string.printable):

            # Remove quotes from the generated string
            temp_val = self.get_random_string(char_type)
            temp_val = temp_val.translate({39: None, 35: None})

            value = "'{}'".format(temp_val)
            ret = set_volume_options(self.mnode, self.volname, {key: value})
            self.assertFalse(ret, "Unexpected: Erroneous value {}, to option "
                             "{} should result in failure".format(value, key))

        # Passing an out of range value
        for value in ('-1%', '-101%', '101%', '-1', '-101'):
            ret = set_volume_options(self.mnode, self.volname, {key: value})
            self.assertFalse(ret, "Unexpected: Erroneous value {}, to option "
                             "{} should result in failure".format(value, key)) 
Example #17
Source File: util.py    From ands with MIT License 5 votes vote down vote up
def generate_random_string(size):
    return "".join(random.choice(string.printable) for _ in range(size)) 
Example #18
Source File: export_dataset.py    From seq2seq-keyphrase with MIT License 5 votes vote down vote up
def load_text(doclist, textdir):
    for filename in os.listdir(textdir):
        with open(textdir+filename) as textfile:
            doc = Document()
            doc.name = filename[:filename.find('.txt')]

            import string
            printable = set(string.printable)

            # print((filename))
            try:
                lines = textfile.readlines()

                lines = [filter(lambda x: x in printable, l) for l in lines]

                title = lines[0].encode('ascii', 'ignore').decode('ascii', 'ignore')
                # the 2nd line is abstract title
                text  = (' '.join(lines[2:])).encode('ascii', 'ignore').decode('ascii', 'ignore')

                # if lines[1].strip().lower() != 'abstract':
                #     print('Wrong title detected : %s' % (filename))

                doc.title = title
                doc.text  = text
                doclist.append(doc)

            except UnicodeDecodeError:
                print('UnicodeDecodeError detected! %s' % filename )
    return doclist 
Example #19
Source File: test_kubernetes_executor.py    From airflow with Apache License 2.0 5 votes vote down vote up
def _gen_random_string(seed, str_len):
        char_list = []
        for char_seed in range(str_len):
            random.seed(str(seed) * char_seed)
            char_list.append(random.choice(string.printable))
        return ''.join(char_list) 
Example #20
Source File: test_project.py    From signac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_get_project_all_printable_characters(self):
        root = self._tmp_dir.name
        with pytest.raises(LookupError):
            signac.get_project(root=root)
        project_name = 'testproject' + string.printable
        project = signac.init_project(name=project_name, root=root)
        with pytest.deprecated_call():
            assert project.get_id() == project_name 
Example #21
Source File: user_command.py    From airflow with Apache License 2.0 5 votes vote down vote up
def users_create(args):
    """Creates new user in the DB"""
    appbuilder = cached_app().appbuilder  # pylint: disable=no-member
    role = appbuilder.sm.find_role(args.role)
    if not role:
        valid_roles = appbuilder.sm.get_all_roles()
        raise SystemExit('{} is not a valid role. Valid roles are: {}'.format(args.role, valid_roles))

    if args.use_random_password:
        password = ''.join(random.choice(string.printable) for _ in range(16))
    elif args.password:
        password = args.password
    else:
        password = getpass.getpass('Password:')
        password_confirmation = getpass.getpass('Repeat for confirmation:')
        if password != password_confirmation:
            raise SystemExit('Passwords did not match!')

    if appbuilder.sm.find_user(args.username):
        print('{} already exist in the db'.format(args.username))
        return
    user = appbuilder.sm.add_user(args.username, args.firstname, args.lastname,
                                  args.email, role, password)
    if user:
        print('{} user {} created.'.format(args.role, args.username))
    else:
        raise SystemExit('Failed to create user.') 
Example #22
Source File: keyphrase_test_dataset.py    From seq2seq-keyphrase with MIT License 5 votes vote down vote up
def load_text(self, textdir):
        for filename in os.listdir(textdir):
            with open(textdir+filename) as textfile:
                doc = Document()
                doc.name = filename[:filename.find('.txt')]

                import string
                printable = set(string.printable)

                # print((filename))
                try:
                    lines = textfile.readlines()

                    lines = [list(filter(lambda x: x in printable, l)) for l in lines]

                    title = ''.join(lines[1]).encode('ascii', 'ignore').decode('ascii', 'ignore')
                    # the 2nd line is abstract title
                    text  = ''.join(lines[3]).encode('ascii', 'ignore').decode('ascii', 'ignore')

                    # if lines[1].strip().lower() != 'abstract':
                    #     print('Wrong title detected : %s' % (filename))

                    doc.title = title
                    doc.text  = text
                    self.doclist.append(doc)

                except UnicodeDecodeError:
                    print('UnicodeDecodeError detected! %s' % filename ) 
Example #23
Source File: file_dir_ops.py    From glusto-tests with GNU General Public License v3.0 5 votes vote down vote up
def overwrite(args):
    """
    Truncates everything present and overwrites the file with new data.
    """
    dir_path = os.path.abspath(args.dir)
    if not path_exists(args.dir):
        return 1
    rc = 0

    for dir_name, subdir_list, file_list in os.walk(dir_path, topdown=False):
        for fname in file_list:
            new_size = sizes_dict[
                random.choice(list(sizes_dict.keys()))]
            try:
                file = os.path.join(dir_name, fname)
                with open(file, "w+") as fd:
                    try:
                        fd.write(''.join(random.choice(string.printable)
                                         for x in range(new_size)))
                        fd.flush()
                    except IOError as e:
                        print("Unable to write to file '%s' : %s" %
                              (file, e.strerror))
                        rc = 1
            except OSError:
                rc = 1
    return rc 
Example #24
Source File: file_dir_ops.py    From glusto-tests with GNU General Public License v3.0 5 votes vote down vote up
def append(args):
    """
    Appends all files under 'dir' with randomly sized data.
    """
    dir_path = os.path.abspath(args.dir)
    if not path_exists(args.dir):
        return 1
    rc = 0

    for dir_name, subdir_list, file_list in os.walk(dir_path, topdown=False):
        for fname in file_list:
            append_size = sizes_dict[
                random.choice(list(sizes_dict.keys()))]
            try:
                file = os.path.join(dir_name, fname)
                with open(file, "a") as fd:
                    try:
                        fd.write(''.join(random.choice(string.printable)
                                         for x in range(append_size)))
                        fd.flush()
                    except IOError as e:
                        print("Unable to append to file '%s' : %s" %
                              (file, e.strerror))
                        rc = 1

            except OSError:
                rc = 1

    return rc 
Example #25
Source File: file_dir_ops.py    From glusto-tests with GNU General Public License v3.0 5 votes vote down vote up
def _create_file(file_abs_path, file_type, file_size):
    rc = 0

    if file_type == 'txt':
        file_abs_path += ".txt"

        with open(file_abs_path, "w+") as new_file:
            try:
                new_file.write(''.join(
                    np.random.choice(list(string.printable), file_size)))
                new_file.flush()
                new_file.close()
            except IOError as err:
                print("Unable to write to file '%s' : %s" % (
                    file_abs_path, err.strerror))
                rc = 1

    elif file_type == 'docx':
        file_abs_path += ".docx"
        try:
            document = Document()
            str_to_write = list(string.ascii_letters + string.digits)
            file_str = ''.join(np.random.choice(str_to_write, file_size))
            document.add_paragraph(file_str)
            document.save(file_abs_path)
        except Exception as err:
            print("Unable to write to file '%s' : %s" % (
                file_abs_path, err.strerror))
            rc = 1

    elif file_type == 'empty_file':
        try:
            with open(file_abs_path, "w+") as new_file:
                new_file.close()
        except IOError as err:
            print("Unable to write to file '%s' : %s" % (
                file_abs_path, err.strerror))
            rc = 1

    return rc 
Example #26
Source File: pdf2pdfocr.py    From pdf2pdfocr with Apache License 2.0 5 votes vote down vote up
def get_tesseract_version(self):
        # Inspired by the great lib 'pytesseract' - https://github.com/madmaze/pytesseract/blob/master/src/pytesseract.py
        try:
            version_info = subprocess.check_output([self.path_tesseract, '--version'], stderr=subprocess.STDOUT).decode('utf-8').split()
            version_info = version_info[1].lstrip(string.printable[10:])
            l_version_info = LooseVersion(version_info)
            result = int(l_version_info.version[0])
            self.log("Tesseract version: {0}".format(result))
            return result
        except Exception as e:
            self.log("Error checking tesseract version. Trying to continue assuming legacy version 3. Exception was {0}".format(e))
            return 3 
Example #27
Source File: test_BST.py    From ands with MIT License 5 votes vote down vote up
def test_insert_many(self):
        for letter in string.printable:
            self.t.insert(letter)

        self.assertEqual(self.t.size, len(string.printable))

        for letter in string.printable:
            self.assertTrue(self.t.contains(letter)) 
Example #28
Source File: types.py    From blueflower with GNU General Public License v3.0 5 votes vote down vote up
def is_text(data):
    """True if data is text content, False otherwise"""
    return not bool(data.translate(None, string.printable)) 
Example #29
Source File: utils_misc.py    From avocado-vt with GNU General Public License v2.0 5 votes vote down vote up
def cpu_str_to_list(origin_str):
    """
    Convert the cpu string to a list. The string may include comma and
    hyphen.

    :param origin_str: the cpu info string read from system
    :type origin_str: string
    :return: A list of the cpu ids
    :rtype: builtin.list
    """
    if isinstance(origin_str, six.string_types):
        origin_str = "".join([_ for _ in origin_str if _ in string.printable])
        cpu_list = []
        for cpu in origin_str.strip().split(","):
            if "-" in cpu:
                start, end = cpu.split("-")
                for cpu_id in range(int(start), int(end) + 1):
                    cpu_list.append(cpu_id)
            else:
                try:
                    cpu_id = int(cpu)
                    cpu_list.append(cpu_id)
                except ValueError:
                    logging.error("Illegimate string in cpu "
                                  "informations: %s" % cpu)
                    cpu_list = []
                    break
        cpu_list.sort()
        return cpu_list 
Example #30
Source File: _testtools.py    From tmtoolkit with Apache License 2.0 5 votes vote down vote up
def strategy_texts_printable(*args, **kwargs):
    return strategy_texts(string.printable)