示例#1
0
import pickle
import json
import time
import os
import hashlib
from olm.logger import get_logger
from olm.constants import CacheTypes, ArticleStatus
from collections import namedtuple

logger = get_logger('olm.cache')
comparison = namedtuple('comparison', ['added', 'removed', 'modified'])


def dict_compare(d1, d2):
    d1_keys = set(d1.keys())
    d2_keys = set(d2.keys())
    intersect_keys = d1_keys.intersection(d2_keys)
    added_keys = d1_keys - d2_keys
    removed_keys = d2_keys - d1_keys
    modified = {o: (d1[o], d2[o]) for o in intersect_keys if d1[o] != d2[o]}
    same = set(o for o in intersect_keys if d1[o] == d2[o])
    added = {}
    removed = {}
    for key in added_keys:
        added[key] = d1[key]
    for key in removed_keys:
        removed[key] = d2[key]
    return comparison(added=added, removed=removed, modified=modified)


def hash_object(thing):
示例#2
0
import logging
import re
import os
from olm.logger import get_logger

logger = get_logger('olm.plugins.metainserter')


def meta_inserter(sender, context, Writer):
    for afile in context['all_files']:
        if hasattr(afile, 'same_as_cache'):
            if afile.same_as_cache:
                continue
        content = afile.content
        metadata = afile.metadata

        # Useful bit of metadata to have access to
        if afile.source_filepath is not None:
            metadata['source_path'] = afile.source_filepath.replace(
                os.path.join(os.getcwd(), ""), "")

        # Replace {{ tags }} with the data they should have
        def replacer(match):
            key = match.group(1).strip().replace(">", ">")

            if 'data' in dir(afile) and key in afile.data:
                return afile.data[key] + "\n"
            elif key in context:
                return context[key] + "\n"
            else:
                logger.warn("Failed to replace %s in file '%s %s'",
示例#3
0
文件: __init__.py 项目: rhyst/olm
                    action="store",
                    default="NOTICE",
                    help='Set log level')
args = parser.parse_args()

from olm.logger import get_logger, set_log_level
set_log_level(args.log_level)

# Imports here in order to get correct log level
from olm.context import load_context, load_context_from_file, load_default_context
from olm.plugins import Plugins
from olm.signals import Signal, signals
from olm.website import Site
from olm.quickstart import quickstart

logger = get_logger('olm')


def main():
    """Main olm function"""
    if args.init:
        logger.notice("Setting up basic website")
        base_path = os.path.abspath(args.src)
        if os.listdir(base_path):
            logger.error("The directory must be empty in order to initialise")
            return
        quickstart(base_path)
        logger.success("Done! Run 'olm {}'".format(args.src))
        return

    time_all = time.time()
示例#4
0
文件: plugins.py 项目: rhyst/olm
import os
import sys
import imp
from blinker import signal
from olm.logger import get_logger

logger = get_logger('olm.plugins')


class Plugins:
    def __init__(self, context):
        self.receivers = []
        self.load_plugins(context)

    def load_plugins(self, context):
        logger.info('Loading plugins')
        for plugin in context['PLUGINS']:
            logger.debug('Loading plugin %s', plugin)
            sys_path = sys.path
            try:
                path = os.path.join(context.PLUGINS_FOLDER, plugin)
                sys.path.append(path)
                pyfile = os.path.join(path, plugin + '.py')
                py_mod = imp.load_source(plugin, pyfile)
                registrations = getattr(py_mod, 'register')()
                if type(registrations) is tuple:
                    registrations = [registrations]
                for registration in registrations:
                    # TODO: Check if valid signal
                    sender = signal(registration[0])
                    receiver = sender.connect(registration[1])
示例#5
0
from collections import namedtuple, defaultdict, OrderedDict
import os
from datetime import date, datetime
import time
import string
import sys
import re
import copy
from urllib.parse import quote

from olm.logger import get_logger
from olm.constants import ArticleStatus
from bios import generate_cave_pages, generate_person_pages, construct_bios

logger = get_logger('olm.plugins.cavepeep')

# =====================Functions============================


def parse_metadata(metadata, article):
    # Create list of people, caves they have been to and the articles that the
    # trip is recorded in. Also create array of caves and the articles that
    # refer to the cave (could have a 'people who have been in this cave' thing
    # as well but I didn't think it was useful
    trips_for_insert = {}
    cavepeep = []  # Set up list to hold named tuples
    row = namedtuple('row', 'date cave person article')
    # Ensure the metadata is a list. It will be a string if there is
    # just one entry
    article_metadata = [metadata
                        ] if not isinstance(metadata, list) else metadata
示例#6
0
文件: website.py 项目: rhyst/olm
from jsmin import jsmin
from shutil import copyfile

from olm.context import load_context, load_default_context
from olm.plugins import Plugins
from olm.article import Article
from olm.index import Index
from olm.page import Page
from olm.constants import ArticleStatus
from olm.helper import Map
from olm.writer import Writer
from olm.signals import Signal, signals
from olm.logger import get_logger
from olm.cache import check_cache

logger = get_logger('olm.site')


class Site:
    def __init__(self, context):
        self.context = context
        self.subsites = []

    def build_site(self):
        CONTEXT = self.context
        all_files = []
        articles = []
        draft_articles = []
        unlisted_articles = []
        pages = []
        subsites = set()
示例#7
0
from datetime import date, datetime
import re
from olm.logger import get_logger

logger = get_logger('olm.plugins.cavepeep')

def parse_metadata(metadata, article=None):
    trips = []

    # Ensure the metadata is a list. It will be a string if there is
    # just one entry
    title            = article.title if article else ""
    date             = article.date if article else datetime.now()
    cavepeeps        = [metadata] if not isinstance(metadata, list) else metadata

    # Look at all the entries. Each will describe a trip that happened
    # on a date.
    c = re.compile(r"""\s*DATE=\s*(\d\d\d\d-\d\d-\d\d)\s*;\s*CAVE=\s*([\s\w\D][^;]*)\s*;\s*PEOPLE=\s*([\s\w\D][^;]*);*[\n\t\r]*""")
    c2 = re.compile(r"""\s*NOCAVE=\s*([\s\w\D][^;]*);*[\n\t\r]*""")
    for entry in cavepeeps:
        # Create key/value relationship between trip identifier (Date + Cave) and list of cavers
        item_date = None
        item_caves = None
        item_people = None
        m = c.match(entry)
        m2 = c2.match(entry)
        if m:
            item_date=datetime.strptime(m.group(1), '%Y-%m-%d')
            item_caves=m.group(2)
            item_people=m.group(3).split(',')
        elif m2:
示例#8
0
from collections import namedtuple, OrderedDict
import os
import re
import codecs
import time
from olm.helper import Map
from olm.reader import Reader
from olm.source import Source
from olm.logger import get_logger
from olm.writer import Writer
from olm.helper import merge_dictionaries


logger = get_logger('olm.plugins.wiki')

def add_to_structure(structure, path_list):
    folders = structure["folders"]
    articles = structure["articles"]
    subdir = path_list[0]
    rest = path_list[1:]

    if len(rest) > 1:
        if subdir in folders:
            folders[subdir] = add_to_structure(folders[subdir], rest)
        else:
            folders[subdir] = add_to_structure({"folders":{},"articles":[]}, rest)
    else:
       if subdir in folders:
           folders[subdir]["articles"] += rest
       else:
           folders[subdir] = { "folders": {}, "articles": rest }
示例#9
0
from collections import namedtuple, OrderedDict
import os
import re
import codecs
import time
from olm.helper import Map
from olm.reader import Reader
from olm.source import Source
from olm.logger import get_logger

logger = get_logger('olm.plugins.wiki')

def add_to_structure(structure, path_list):
    folders = structure["folders"]
    articles = structure["articles"]
    subdir = path_list[0]
    rest = path_list[1:]

    if len(rest) > 1:
        if subdir in folders:
            folders[subdir] = add_to_structure(folders[subdir], rest)
        else:
            folders[subdir] = add_to_structure({"folders":{},"articles":[]}, rest)
    else:
       if subdir in folders:
           folders[subdir]["articles"] += rest
       else:
           folders[subdir] = { "folders": {}, "articles": rest }
    
    return { "folders": folders, "articles": articles }
示例#10
0
import os
import sys
import imp
import mistune
import re
from collections import OrderedDict
from olm.helper import Map, merge_ordered_dictionaries
from olm.logger import get_logger
from jinja2 import Environment, FileSystemLoader, select_autoescape

logger = get_logger('olm.context')

def load_default_context(path):
    logger.info('Loading default context')
    BASE_FOLDER          = os.path.abspath(path)
    CACHE_LOCATION       = os.path.abspath(os.path.join(BASE_FOLDER)) + os.sep + 'cache.pickle'
    SOURCE_FOLDER        = os.path.abspath(os.path.join(BASE_FOLDER, 'src'))
    STATIC_FOLDER        = os.path.abspath(os.path.join(BASE_FOLDER, 'theme', 'static'))
    CSS_FOLDER           = os.path.abspath(os.path.join(BASE_FOLDER, 'theme', 'static', 'css'))
    JS_FOLDER            = os.path.abspath(os.path.join(BASE_FOLDER, 'theme', 'static', 'js'))
    TEMPLATES_FOLDER     = os.path.abspath(os.path.join(BASE_FOLDER, 'theme', 'templates'))
    PLUGINS_FOLDER       = os.path.abspath(os.path.join(BASE_FOLDER, 'plugins',))
    NO_SCAN              = []
    MD                   = mistune.Markdown(escape=False)
    JINJA_ENV            = Environment(
                            loader=FileSystemLoader([TEMPLATES_FOLDER])
                            )
    ARTICLE_TYPES        = ['article']
    INDEX_TYPES          = ['index']
    PLUGINS              = []
    SITEURL              = ""
示例#11
0
文件: signals.py 项目: rhyst/olm
import time
from blinker import signal
from olm.helper import Map
from olm.logger import get_logger

logger = get_logger('olm.signal')

signals = Map({
    'INITIALISED':              "INITIALISED",             # args: context
    'SITE_INITIALISED':         "SITE_INITIALISED",        # args: context
    "AFTER_SOURCE_INITIALISED": "AFTER_SOURCE_INITIALISED",# args: context, source      
    'BEFORE_CACHING':           "BEFORE_CACHING",          # args: context, articles
    'AFTER_ARTICLE_READ':       "AFTER_ARTICLE_READ",      # args: context, article
    'AFTER_PAGE_READ':          "AFTER_PAGE_READ",         # args: context, article
    'AFTER_ALL_ARTICLES_READ':  "AFTER_ALL_ARTICLES_READ", # args: context, articles
    'BEFORE_WRITING':           "BEFORE_WRITING",          # args: context
    'BEFORE_MD_CONVERT':        "BEFORE_MD_CONVERT",       # args: context, article
    'BEFORE_SOURCE_WRITE':      "BEFORE_SOURCE_WRITE",     # args: context, article
    'AFTER_WRITING':            "AFTER_WRITING"            # args: context, article
})

class Signal:
    def __init__(self, signal_value):
        logger.spam('Instantiating new Signal "%s"', signal_value)
        if signal_value not in signals:
            logger.error('No such signal %s', signal_value)
            raise Exception('No such signal %s', signal_value)
        self.signal_value = signal_value
        self.sender = signal(signal_value)

    def send(self, **kwargs):