<commit_before>from lab.decorators import section


class CloudImage(object):
    IMAGES = {'sqe-iperf': 'http://172.29.173.233/cloud-images/os-sqe-localadmin-ubuntu.qcow2',
              'CSR1KV': 'http://172.29.173.233/cloud-images/csr1000v-universalk9.03.16.00.S.155-3.S-ext.qcow2',
              'nfvbenchvm': 'http://172.29.173.233/cloud-images/testpmdvm-latest.qcow2'}

    SQE_PERF = 'sqe-perf'
    FOR_CSR = 'sqe-csr'

    def __init__(self, cloud, image_dic):
        self.cloud = cloud
        self._dic = image_dic

    @property
    def id(self):
        return self._dic['id']

    @property
    def name(self):
        import re

        if '(' in self._dic['name']:
            self._dic['name'], self._dic['id'] = re.findall('(.*) \((.*)\)', self._dic['name'])[0]
        return self._dic['name']

    @property
    def url(self):
        if 'url' not in self._dic:
            self._read_image_properties()
        return self._dic['url']

    @property
    def checksum(self):
        if 'checksum' not in self._dic:
            self._read_image_properties()
        return self._dic['checksum']

    @property
    def size(self):
        if 'size' not in self._dic:
            self._read_image_properties()
        return self._dic['size']

    @property
    def loc_abs_path(self):
        if 'loc_abs_path' not in self._dic:
            self._read_image_properties()
        return self._dic['loc_abs_path']

    @property
    def username(self):
        if 'username' not in self._dic:
            self._read_image_properties()
        return self._dic['username']

    @property
    def password(self):
        if 'password' not in self._dic:
            self._read_image_properties()
        return self._dic['password']

    @staticmethod
    def read_image_properties(name):
        from os import path
        import requests

        try:
            url = CloudImage.IMAGES[name]
            url_txt = url + '.txt'
        except KeyError:
            return 'fake', 'fake', 'fake', 'fake'

        try:
            r = requests.get(url=url_txt)
            if not r.ok:
                raise RuntimeError(r.url + ': ' + r.reason)
            checksum, _, size, username, password = r.text.split()
            return url, checksum, size, username, password, path.basename(url)
        except ValueError:
            raise ValueError(url_txt + ' has wrong body, expected checksum file_name size username password')

    def _read_image_properties(self):
        self._dic['url'], self._dic['checksum'], self._dic['size'], self._dic['username'], self._dic['password'], self._dic['loc_abs_path'] = self.read_image_properties(name=self.name)
<commit_msg>cloud image: fix wrong number of tuple elements when reading properties for unknown image<commit_after>from lab.decorators import section


class CloudImage(object):
    IMAGES = {'sqe-iperf': 'http://172.29.173.233/cloud-images/os-sqe-localadmin-ubuntu.qcow2',
              'CSR1KV': 'http://172.29.173.233/cloud-images/csr1000v-universalk9.03.16.00.S.155-3.S-ext.qcow2',
              'nfvbenchvm': 'http://172.29.173.233/cloud-images/testpmdvm-latest.qcow2'}

    SQE_PERF = 'sqe-perf'
    FOR_CSR = 'sqe-csr'

    def __init__(self, cloud, image_dic):
        self.cloud = cloud
        self._dic = image_dic

    @property
    def id(self):
        return self._dic['id']

    @property
    def name(self):
        import re

        if '(' in self._dic['name']:
            self._dic['name'], self._dic['id'] = re.findall('(.*) \((.*)\)', self._dic['name'])[0]
        return self._dic['name']

    @property
    def url(self):
        if 'url' not in self._dic:
            self._read_image_properties()
        return self._dic['url']

    @property
    def checksum(self):
        if 'checksum' not in self._dic:
            self._read_image_properties()
        return self._dic['checksum']

    @property
    def size(self):
        if 'size' not in self._dic:
            self._read_image_properties()
        return self._dic['size']

    @property
    def loc_abs_path(self):
        if 'loc_abs_path' not in self._dic:
            self._read_image_properties()
        return self._dic['loc_abs_path']

    @property
    def username(self):
        if 'username' not in self._dic:
            self._read_image_properties()
        return self._dic['username']

    @property
    def password(self):
        if 'password' not in self._dic:
            self._read_image_properties()
        return self._dic['password']

    @staticmethod
    def read_image_properties(name):
        from os import path
        import requests

        url = CloudImage.IMAGES[name]
        url_txt = url + '.txt'

        try:
            r = requests.get(url=url_txt)
            if not r.ok:
                raise RuntimeError(r.url + ': ' + r.reason)
            checksum, _, size, username, password = r.text.split()
            return url, checksum, size, username, password, path.basename(url)
        except ValueError:
            raise ValueError(url_txt + ' has wrong body, expected checksum file_name size username password')

    def _read_image_properties(self):
        self._dic['url'], self._dic['checksum'], self._dic['size'], self._dic['username'], self._dic['password'], self._dic['loc_abs_path'] = self.read_image_properties(name=self.name) if self.name in self.IMAGES else 6 * ['fake']

{'fraction_non_alphanumeric': 0.13238024507983662, 'fraction_numerical': 0.01986632008911994, 'mean_word_length': 2.3212083847102343, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6780853', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1714, 'n_words': 362}