# Python utils for migmig project

import random


def parse_doc_arguments(arguments):
    options, args, command = {}, {}, None
    for key, value in arguments.items():
        if key[0:2] == "--":
            options[key[2:]] = value
        elif key[0] == "<":
            args[key] = value
        else:
            if value:
                command = key

    return command, args, options


def calc_bytes_range(chunk_size, threads_count):
    """
        calculate "start byte" and "end byte" to give to each thread.
        return a list including tuples: (start, end)
    """
    # 6291456
    bytes_range = []

    mini_chunk = chunk_size // threads_count
    pos = 0
    for i in range(threads_count):
        start_byte = pos
        end_byte = start_byte + mini_chunk
        if end_byte > chunk_size - 1:
            end_byte = chunk_size

        pos = end_byte + 1
        bytes_range.append((start_byte, end_byte))

    return bytes_range


def get_random_useragent():
    """
    Returns a random popular user-agent.
    Taken from `here <http://techblog.willshouse.com/2012/01/03/most-common-user-agents/>`_, last updated on 01/02/2014.

    :returns: user-agent
    :rtype: string
    """
    l = [
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11',
        'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36',
        'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36',
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:26.0) Gecko/20100101 Firefox/26.0',
        'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53',
        'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
        'Mozilla/5.0 (Windows NT 5.1; rv:26.0) Gecko/20100101 Firefox/26.0',
    ]
    return random.choice(l)

@@ -411,26 +411,26 @@
 s_range(
-chunk_size
+start, end
 , thread
@@ -582,22 +582,8 @@
 %22%22%22%0A
-    # 6291456%0A
     
@@ -598,16 +598,49 @@
 nge = %5B%5D
+%0A    chunk_size = end - start + 1
 %0A%0A    mi
@@ -688,17 +688,21 @@
   pos = 
-0
+start
 %0A    for
@@ -819,26 +819,19 @@
 _byte %3E 
-chunk_size
+end
  - 1:%0A  
@@ -851,26 +851,19 @@
 _byte = 
-chunk_size
+end
 %0A%0A      

Modify "calc_bytes_range" in utils module.