def gen_iplum(words=None, paragraphs=None): """Return a lorem ipsum string. If no arguments are passed, then return the entire default lorem ipsum string. :param int words: The number of words to return. :param int paragraphs: The number of paragraphs to return. :raises: ``ValueError`` if ``words`` is not a valid positive integer. :returns: A ``lorem ipsum`` string containing either the number of ``words`` or ``paragraphs``, extending and wrapping around the text as needed to make sure that it has the specified length. :rtype: str """ # Check parameters if words is None or words == 0: words = len(LOREM_IPSUM_TEXT.split()) if paragraphs is None: paragraphs = 1 if not isinstance(words, int) or words < 0: raise ValueError( 'Cannot generate a string with negative number of words.') is_positive_int(paragraphs) # Original Lorem Ipsum string all_words = LOREM_IPSUM_TEXT.split() # How many words do we need? total_words_needed = words * paragraphs quotient = int(total_words_needed / len(all_words)) modulus = total_words_needed % len(all_words) # Pool of words to use all_words = all_words * (quotient + modulus) result = u'' start_pos = 0 for _ in range(0, paragraphs): sentence = u' '.join(all_words[start_pos:start_pos + words]) # Remove comma from the end, if it exists if sentence.endswith(','): sentence = sentence.rstrip(',') # Remove period from the end, if it exists if sentence.endswith('.'): sentence = sentence.rstrip('.') # Each sentence should be properly capitalized cap_sentence = [ frag.capitalize() + u'.' for frag in sentence.split('. ') ] # Add newline at the end result += ' '.join(cap_sentence) + u'\n' # Increment positional counter start_pos += words return result.rstrip()
def add_processor_info(count=None): """Generate fake processor facts. :param int count: Number of processors for a system. :returns: A dictionary containing fake processor facts. :rtype: dict """ if count is None: count = gen_choice(range(2, 16, 2)) else: is_positive_int(count) processors = { 'physicalprocessorcount': count, 'processorcount': count, 'cpu::topology_source': 'kernel /sys cpu sibling lists', 'cpu::cpu(s)': count, 'lscpu::cpu(s)': count, 'processors': { 'models': [], 'count': count, 'physicalcount': count, }, } # Add processors info based on total processors for idx in range(count): processors['processor{}'.format( idx)] = 'Intel(R) Xeon(R) CPU E31220 @ 3.10GHz' processors['processors']['models'].append( 'Intel(R) Xeon(R) CPU E31220 @ 3.10GHz') return processors
def add_memory_info(count=None): """Generate fake memory facts. :param int count: The total amount of RAM for a system. :returns: A dictionary representing memory facts. :rtype: dict """ if count is None: count = gen_choice(range(4, 128, 2)) else: is_positive_int(count) free_ram = (gen_integer(min_value=4, max_value=count)) return { 'dmi::memory::size': '{}'.format(free_ram * 1024), 'memoryfree': '{} GB'.format(free_ram), 'memoryfree_mb': '{}'.format(free_ram * 1024), 'memorysize': '{} GB'.format(count), 'memorysize_mb': '{}'.format(count * 1024), 'swapfree': '{} GB'.format(free_ram), 'swapfree_mb': '{}'.format(free_ram * 1024), 'swapsize': '{} GB'.format(count), 'swapsize_mb': '{}'.format(count * 1024), }
def add_partitions(extra_partitions=None): """Generate fake partitions facts.""" partitions = { 'partitions': { 'sda1': { 'uuid': gen_uuid(), 'size': '1024000', 'mount': '/boot', 'filesystem': 'xfs' }, } } if extra_partitions is not None: is_positive_int(extra_partitions) for idx in range(extra_partitions): device_id = idx + 1 partitions['partitions'].update({ 'sdb{}'.format(device_id): { 'size': '975747072', 'filesystem': 'LVM2_member', } }) return partitions
def test_positive_value(length): """Positive values are allowed.""" assert is_positive_int(length) is None
def test_non_numeric_value(length): """Non-numeric values are not allowed.""" with pytest.raises(ValueError): is_positive_int(length)
def test_empty_value(): """Empty list is not an allowed value.""" with pytest.raises(ValueError): is_positive_int([])
def test_none_value(): """None is not an allowed value.""" with pytest.raises(ValueError): is_positive_int(None)
def test_zero_value(): """Zero is not an allowed value.""" with pytest.raises(ValueError): is_positive_int(0)
def test_negative_value(length): """Negative values are not allowed.""" with pytest.raises(ValueError): is_positive_int(length)