<commit_before># -*- coding: utf-8 -*-

# This class resolves USB devices (e.g. /dev/sdb) to USB paths (e.g. 00/1.2)

import os, re, time

class usb_path_mapper (object):
    def __init__ (self):
        self.cache = {}

    def get_usb_path (self, device, retry_count=0):
        if device in self.cache:
            return self.cache[device]

        dev_files = os.listdir("/dev/disk/by-path/")
        for this_file in dev_files:
            if os.path.realpath("/dev/disk/by-path/%s" % this_file) == device:
                re_devids = re.search(r'pci-\d+:([a-f0-9]+):.+usb-\d+:([\.\d]+):', this_file)
                if re_devids:
                    devid = re_devids.group(1)+'/'+re_devids.group(2)
                    self.cache[device] = devid
                    return devid
                else:
                    return ""

        if retry_count < 20:
            time.sleep(0.5)
            return self.get_usb_path(device, retry_count+1)

        return ""

    def changed (self, device):
        if device in self.cache:
            del self.cache[device]
<commit_msg>Fix: Don't die if /dev/disk/by-path is missing<commit_after># -*- coding: utf-8 -*-

# This class resolves USB devices (e.g. /dev/sdb) to USB paths (e.g. 00/1.2)

import os, re, time

class usb_path_mapper (object):
    def __init__ (self):
        self.cache = {}

    def get_usb_path (self, device, retry_count=0):
        if device in self.cache:
            return self.cache[device]

        try:
            dev_files = os.listdir("/dev/disk/by-path/")
        except OSError:
            dev_files = []

        for this_file in dev_files:
            if os.path.realpath("/dev/disk/by-path/%s" % this_file) == device:
                re_devids = re.search(r'pci-\d+:([a-f0-9]+):.+usb-\d+:([\.\d]+):', this_file)
                if re_devids:
                    devid = re_devids.group(1)+'/'+re_devids.group(2)
                    self.cache[device] = devid
                    return devid
                else:
                    return ""

        if retry_count < 20:
            time.sleep(0.5)
            return self.get_usb_path(device, retry_count+1)

        return ""

    def changed (self, device):
        if device in self.cache:
            del self.cache[device]

{'fraction_non_alphanumeric': 0.13815789473684212, 'fraction_numerical': 0.013157894736842105, 'mean_word_length': 1.6523255813953488, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1758651', 'n_tokens_mistral': 779, 'n_tokens_neox': 737, 'n_words': 166}