# Path: saulify/sitespec.py
# def load_rules(f):
#     """
#     Reads scraping rules from Instapaper spec file.
# 
#     The scraping rules are stored in a dictionary. For simple directives
#     like `strip`, the rules are stored in a list using the directive name as
#     the key. Exceptions to this storage format are detailed below.
# 
#     `find_string` / `replace_string` :
#         These directives come in pairs; one find regular expression and one
#         replace. Stored as a list of 2-tuples under the key `"find_replace"`.
# 
#     Boolean properties such as `prune`:
#         Stored as `True` or `False` according to the last line specifying
#         the value.
# 
#     Args:
#       f (file): Spec file object
# 
#     Returns:
#       Dictionary containing scraper rules.
#     """
# 
#     rules = collections.defaultdict(list)
#     find_string = None
# 
#     boolean_props = ["prune"]
# 
#     for label, content in parse_specfile(f):
# 
#         if label.startswith("test_"):
#             continue
# 
#         if label == "find_string":
#             find_string = content
# 
#         elif label == "replace_string":
#             if not find_string:
#                 raise Exception("Invalid spec file")
#             rules["find_replace"].append((find_string, content))
# 
#         elif label in boolean_props:
#             if content == "yes":
#                 rules[label] = True
#             elif content == "no":
#                 rules[label] = False
#             else:
#                 raise Exception("Invalid spec file")
# 
#         else:
#             rules[label].append(content)
# 
#     return rules


import io
import os
import urlparse
import markdown2
import html2text
from flask import Markup
from . import download
from . import instapaper
from . import newspaper
from saulify.sitespec import load_rules
    try:
        d = load_sitespec(hostname)
    except IOError:
        d = {}

    # If `d` is empty, file for `hostname` could not be loaded or was empty;
    # in either case, we go on to the next superdomain.
    if not d:
        super_domain = hostname.partition(".")[2]
        if super_domain:
            return load_superdomains(super_domain)
        else:
            return None

    return d


def load_sitespec(hostname):
    """ Load sitespec file for a given host.

    Args:
        hostname (str): Hostname for which to load associated spec file.

    Raises:
        IOError if there is no sitespec file for the exact hostname.
    """

    fpath = os.path.join("sitespecs", hostname + ".txt")

    with io.open(fpath) as f:

# Path: saulify/sitespec.py
# def load_rules(f):
#     """
#     Reads scraping rules from Instapaper spec file.
# 
#     The scraping rules are stored in a dictionary. For simple directives
#     like `strip`, the rules are stored in a list using the directive name as
#     the key. Exceptions to this storage format are detailed below.
# 
#     `find_string` / `replace_string` :
#         These directives come in pairs; one find regular expression and one
#         replace. Stored as a list of 2-tuples under the key `"find_replace"`.
# 
#     Boolean properties such as `prune`:
#         Stored as `True` or `False` according to the last line specifying
#         the value.
# 
#     Args:
#       f (file): Spec file object
# 
#     Returns:
#       Dictionary containing scraper rules.
#     """
# 
#     rules = collections.defaultdict(list)
#     find_string = None
# 
#     boolean_props = ["prune"]
# 
#     for label, content in parse_specfile(f):
# 
#         if label.startswith("test_"):
#             continue
# 
#         if label == "find_string":
#             find_string = content
# 
#         elif label == "replace_string":
#             if not find_string:
#                 raise Exception("Invalid spec file")
#             rules["find_replace"].append((find_string, content))
# 
#         elif label in boolean_props:
#             if content == "yes":
#                 rules[label] = True
#             elif content == "no":
#                 rules[label] = False
#             else:
#                 raise Exception("Invalid spec file")
# 
#         else:
#             rules[label].append(content)
# 
#     return rules


# Path: saulify/scrapers/cascade.py
import io
import os
import urlparse
import markdown2
import html2text
from flask import Markup
from . import download
from . import instapaper
from . import newspaper
from saulify.sitespec import load_rules

    try:
        d = load_sitespec(hostname)
    except IOError:
        d = {}

    # If `d` is empty, file for `hostname` could not be loaded or was empty;
    # in either case, we go on to the next superdomain.
    if not d:
        super_domain = hostname.partition(".")[2]
        if super_domain:
            return load_superdomains(super_domain)
        else:
            return None

    return d


def load_sitespec(hostname):
    """ Load sitespec file for a given host.

    Args:
        hostname (str): Hostname for which to load associated spec file.

    Raises:
        IOError if there is no sitespec file for the exact hostname.
    """

    fpath = os.path.join("sitespecs", hostname + ".txt")

    with io.open(fpath) as f:

        return load_rules(f)