File: common.py

package info (click to toggle)
python-plyer 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,808 kB
  • sloc: python: 13,395; sh: 217; makefile: 177
file content (76 lines) | stat: -rw-r--r-- 1,978 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'''
Common objects for testing
==========================

* :class:`PlatformTest` - used as a decorator, allows running a test function
  only on a specific platform (see `plyer.utils.platform`).
* :func:`platform_import` - manual import of a platform specific class instead
  of using `plyer.facades.*` proxies.
'''

import traceback
from os import sep
from os.path import normpath, splitdrive
from plyer.utils import platform as plyer_platform


class PlatformTest:
    '''
    Class for the @PlatformTest decorator to prevent running tests
    calling platform dependent API on different platforms.
    '''

    def __init__(self, platform):
        self.platform = platform

    def __call__(self, func):
        platform = self.platform

        if platform != plyer_platform:
            print("Skipping test '{}' - not on '{}'".format(
                func.__name__, platform
            ))
            func = self.eat
        return func

    @staticmethod
    def eat(*args, **kwargs):
        '''
        Simply eat all positional and keyword arguments
        and return None as an empty function.
        '''


def platform_import(platform, module_name, whereis_exe=None):
    '''
    Import platform API directly instead of through Proxy.
    '''

    try:
        module = 'plyer.platforms.{}.{}'.format(
            platform, module_name
        )
        mod = __import__(module, fromlist='.')

    except ImportError as exc:
        print(vars(exc))
        traceback.print_exc()

    if whereis_exe:
        mod.whereis_exe = whereis_exe
    return mod


def splitpath(path):
    '''
    Split string path into a list of folders (+ file if available).
    '''
    if path[0] == sep and path[1] != sep:
        path = path[1:]
        path = normpath(path).split(sep)
    else:
        drive, path = splitdrive(path)
        if path[0] == sep and path[1] != sep:
            path = path[1:]
        path = [drive, ] + normpath(path).split(sep)
    return path