Exemple #1
0
    def get_known_wifis(self):
        from vorta.models import WifiSettingModel
        plist_path = '/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist'

        try:
            plist_file = open(plist_path, 'rb')
            wifis = plistlib.load(plist_file).get('KnownNetworks')
        except xml.parsers.expat.ExpatError:
            logger.error('Unable to parse list of Wifi networks.')
            return None

        result = []
        if wifis is not None:
            for wifi in wifis.values():
                raw_last_connected = wifi.get('LastConnected', None)
                last_connected = None if not raw_last_connected \
                    else format_date_time(raw_last_connected, WifiSettingModel.last_connected.formats)
                ssid = wifi.get('SSIDString', None)

                if ssid is None:
                    continue

                result.append(
                    SystemWifiInfo(ssid=ssid, last_connected=last_connected))

        return result
Exemple #2
0
def pretty_bytes(size, metric=True, sign=False, precision=1):
    if not isinstance(size, int):
        return ''
    prefix = '+' if sign and size > 0 else ''
    power, units = (10**3, ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']) if metric else \
                   (2**10, ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'])
    n = 0
    while abs(round(size, precision)) >= power and n + 1 < len(units):
        size /= power
        n += 1
    try:
        unit = units[n]
        return f'{prefix}{round(size, precision)} {unit}B'
    except KeyError as e:
        logger.error(e)
        return "NaN"
Exemple #3
0
 def set_password(self, service, repo_url, password):
     try:
         asyncio.set_event_loop(asyncio.new_event_loop())
         collection = secretstorage.get_default_collection(self.connection)
         attributes = {
             'application': 'Vorta',
             'service': service,
             'repo_url': repo_url,
             'xdg:schema': 'org.freedesktop.Secret.Generic'
         }
         collection.create_item(repo_url,
                                attributes,
                                password,
                                replace=True)
     except secretstorage.exceptions.ItemNotFoundException:
         logger.error("SecretStorage writing failed",
                      exc_info=sys.exc_info())
Exemple #4
0
def pretty_bytes(size):
    """from https://stackoverflow.com/questions/12523586/
            python-format-size-application-converting-b-to-kb-mb-gb-tb/37423778"""
    if not isinstance(size, int):
        return ''
    power = 1000  # GiB is base 2**10, GB is base 10**3.
    n = 0
    Dic_powerN = {0: '', 1: 'K', 2: 'M', 3: 'G', 4: 'T'}
    while size >= power:
        size /= power
        n += 1
    try:
        unit = Dic_powerN[n]
        return f'{round(size, 1)} {unit}B'
    except KeyError as e:
        logger.error(e)
        return "NaN"
Exemple #5
0
def get_sorted_wifis(profile):
    """Get SSIDs from OS and merge with settings in DB."""

    from vorta.models import WifiSettingModel

    if sys.platform == 'darwin':
        plist_path = '/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist'

        try:
            plist_file = open(plist_path, 'rb')
            wifis = plistlib.load(plist_file).get('KnownNetworks')
        except xml.parsers.expat.ExpatError:
            logger.error('Unable to parse list of Wifi networks.')
            return []

        if wifis is not None:
            for wifi in wifis.values():
                timestamp = wifi.get('LastConnected', None)
                ssid = wifi.get('SSIDString', None)

                if ssid is None:
                    continue

                db_wifi, created = WifiSettingModel.get_or_create(
                    ssid=ssid,
                    profile=profile.id,
                    defaults={
                        'last_connected': timestamp,
                        'allowed': True
                    })

                # update last connected time
                if not created and db_wifi.last_connected != timestamp:
                    db_wifi.last_connected = timestamp
                    db_wifi.save()

            # remove Wifis that were deleted in the system.
            deleted_wifis = WifiSettingModel.select() \
                .where(WifiSettingModel.ssid.not_in([w['SSIDString'] for w in wifis.values()]))
            for wifi in deleted_wifis:
                wifi.delete_instance()

    return WifiSettingModel.select() \
        .where(WifiSettingModel.profile == profile.id).order_by(-WifiSettingModel.last_connected)