<filename>qtstyles/sheet.py<gh_stars>1-10
'''
Defines -

Sheet: a class representing a style sheet object
with attributes such as path and contents.

get_style_sheets: a function that returns a dictionary
with style sheet names as keys and sheet objects as values.
'''
import os
from qtstyles import errors


class Sheet(object):
    '''
    Keeps key information related to style sheets, particularly the path
    and contents as a string.
    >>> import os
    >>> dirpath = os.path.dirname(os.path.abspath(__file__))
    >>> path = os.path.join(dirpath, "style_sheets", "default.qss")
    >>> sheet = Sheet(path)
    >>> isinstance(sheet.contents, str)
    True
    '''
    def __init__(self, path):
        ''' This constructor only takes one argument being the sheet path.
            path = style sheet file path ending with '.qss'.
        '''
        if not isinstance(path, str):
            raise errors.SheetPathTypeError
        if not path.endswith(".qss"):
            raise errors.SheetPathValueError
        if not os.path.isfile(path):
            raise errors.SheetPathFileDoesntExist
        self._path = path
        self._contents = None  # to be loaded on request

    @property
    def path(self):
        ''' Collect the path as a sheet attribute. '''
        return self._path

    @property
    def contents(self):
        ''' The style sheet contents will load only once when needed. '''
        if self._contents is None:
            self._load_contents()
        return self._contents

    def _load_contents(self):
        ''' Loads the style sheet contents (if not already loaded). '''
        with open(self.path, "r") as qss_file:
            self._contents = qss_file.read()


def get_style_sheets():
    '''
    Returns a dictionary with the style sheet names as keys and
    associated Sheet objects as values.
    There must be a sheet called 'default' which is empty.
    >>> sheets = get_style_sheets()
    >>> isinstance(sheets, dict) # returns a dictionary
    True
    >>> sheet_object = sheets["default"]
    >>> sheet_object.path.endswith(".qss")
    True
    '''
    dirpath = os.path.dirname(os.path.abspath(__file__))

    sheets = {}
    for name in os.listdir(os.path.join(dirpath, "style_sheets")):
        if "__" in name:
            # exclude any files with a double underscore
            # (e.g. __init__, __pycache__)
            continue
        path = os.path.join(dirpath, "style_sheets", name)
        sheets[name.replace(".qss", "")] = Sheet(path)
    return sheets

<filename>qtstyles/sheet.py<gh_stars>1-10
'''
Defines -

Sheet: a class representing a style sheet object
with attributes such as path and contents.

get_style_sheets: a function that returns a dictionary
with style sheet names as keys and sheet objects as values.
'''
import os
from qtstyles import errors


class Sheet(object):
    '''
    Keeps key information related to style sheets, particularly the path
    and contents as a string.
    >>> import os
    >>> dirpath = os.path.dirname(os.path.abspath(__file__))
    >>> path = os.path.join(dirpath, "style_sheets", "default.qss")
    >>> sheet = Sheet(path)
    >>> isinstance(sheet.contents, str)
    True
    '''
    def __init__(self, path):
        ''' This constructor only takes one argument being the sheet path.
            path = style sheet file path ending with '.qss'.
        '''
        if not isinstance(path, str):
            raise errors.SheetPathTypeError
        if not path.endswith(".qss"):
            raise errors.SheetPathValueError
        if not os.path.isfile(path):
            raise errors.SheetPathFileDoesntExist
        self._path = path
        self._contents = None  # to be loaded on request

    @property
    def path(self):
        ''' Collect the path as a sheet attribute. '''
        return self._path

    @property
    def contents(self):
        ''' The style sheet contents will load only once when needed. '''
        if self._contents is None:
            self._load_contents()
        return self._contents

    def _load_contents(self):
        ''' Loads the style sheet contents (if not already loaded). '''
        with open(self.path, "r") as qss_file:
            self._contents = qss_file.read()


def get_style_sheets():
    '''
    Returns a dictionary with the style sheet names as keys and
    associated Sheet objects as values.
    There must be a sheet called 'default' which is empty.
    >>> sheets = get_style_sheets()
    >>> isinstance(sheets, dict) # returns a dictionary
    True
    >>> sheet_object = sheets["default"]
    >>> sheet_object.path.endswith(".qss")
    True
    '''
    dirpath = os.path.dirname(os.path.abspath(__file__))

    sheets = {}
    for name in os.listdir(os.path.join(dirpath, "style_sheets")):
        if "__" in name:
            # exclude any files with a double underscore
            # (e.g. __init__, __pycache__)
            continue
        path = os.path.join(dirpath, "style_sheets", name)
        sheets[name.replace(".qss", "")] = Sheet(path)
    return sheets

Defines -
 
 Sheet: a class representing a style sheet object
 with attributes such as path and contents.
 
 get_style_sheets: a function that returns a dictionary
 with style sheet names as keys and sheet objects as values. Keeps key information related to style sheets, particularly the path
     and contents as a string.
     >>> import os
     >>> dirpath = os.path.dirname(os.path.abspath(__file__))
     >>> path = os.path.join(dirpath, "style_sheets", "default.qss")
     >>> sheet = Sheet(path)
     >>> isinstance(sheet.contents, str)
     True This constructor only takes one argument being the sheet path.
             path = style sheet file path ending with '.qss'. # to be loaded on request Collect the path as a sheet attribute. The style sheet contents will load only once when needed. Loads the style sheet contents (if not already loaded). Returns a dictionary with the style sheet names as keys and
     associated Sheet objects as values.
     There must be a sheet called 'default' which is empty.
     >>> sheets = get_style_sheets()
     >>> isinstance(sheets, dict) # returns a dictionary
     True
     >>> sheet_object = sheets["default"]
     >>> sheet_object.path.endswith(".qss")
     True # exclude any files with a double underscore # (e.g. __init__, __pycache__)