<commit_before>#coding=utf-8
from __future__ import with_statement

import sys
import os
import logging
import shutil

from tornado.util import ObjectDict
from catsup.options import config, g
from catsup.utils import call


def read_meta(path):
    """
    :param path: path for the theme.
    :return: Theme meta read in path.
    """
    if not os.path.exists(path):
        return
    meta = os.path.join(path, 'theme.py')
    if not os.path.exists(meta):
        logging.warn("%s is not a catsup theme." % path)
        return
    theme = ObjectDict(
        name='',
        author='',
        homepage='',
        pages=[],
        has_index=False,
        path=path,
        vars={},
    )
    execfile(meta, {}, theme)
    templates_path = os.path.join(path, 'templates')
    for page in theme.pages:
        if page == 'page.html':
            theme.has_index = True
            # If your theme does not have index page,
            # catsup will rename page/1.html to page.html.
        if not os.path.exists(os.path.join(templates_path, page)):
            logging.warning("%s announces a page %s"
                         " which not exists." % (theme.name, page))
    theme.name = theme.name.lower()
    return theme


def find(theme_name=''):
    if not theme_name:
        theme_name = config.theme.name
    theme_name = theme_name.lower()
    theme_gallery = [
        os.path.join(os.path.abspath('themes'), theme_name),
        os.path.join(g.catsup_path, 'themes', theme_name),
    ]
    for path in theme_gallery:
        theme = read_meta(path)
        if theme:
            return theme

    raise Exception("Can't find theme: %s" % theme_name)


def list():
    theme_gallery = [
        os.path.abspath('themes'),
        os.path.join(g.catsup_path, 'themes'),
    ]
    themes = set()
    for path in theme_gallery:
        if not os.path.exists(path):
            continue
        names = os.listdir(path)
        for theme in names:
            themes.add(theme)
    print('Available themes: \n')
    for name in themes:
        theme = find(name)
        print('Name: %s' % theme.name)
        print('Author: %s' % theme.author)
        print('HomePage: %s' % theme.homepage)
        print('----')


def install(path, g):
    theme = find(path)
    if theme:
        # Update theme
        if not os.path.exists(os.path.join(theme.path, '.git')):
            logging.warn("%s is not installed via git."
                         "Can't update it." % theme.name)
        else:
            logging.info("Updating theme %s" % theme.name)
            call('git pull', cwd=theme.path)
        sys.exit(0)

    themes_path = os.path.abspath('themes')
    if g:
        themes_path = os.path.join(g.catsup_path, 'themes')

    logging.info('Installing theme from %s' % path)

    if not os.path.exists(themes_path):
        os.makedirs(themes_path)

    if os.path.exists(path):
        meta = read_meta(path)<commit_msg>fix a bug about installing theme<commit_after>#coding=utf-8
from __future__ import with_statement

import sys
import os
import logging
import shutil

from tornado.util import ObjectDict
from catsup.options import config, g
from catsup.utils import call


def read_meta(path):
    """
    :param path: path for the theme.
    :return: Theme meta read in path.
    """
    if not os.path.exists(path):
        return
    meta = os.path.join(path, 'theme.py')
    if not os.path.exists(meta):
        logging.warn("%s is not a catsup theme." % path)
        return
    theme = ObjectDict(
        name='',
        author='',
        homepage='',
        pages=[],
        has_index=False,
        path=path,
        vars={},
    )
    execfile(meta, {}, theme)
    templates_path = os.path.join(path, 'templates')
    for page in theme.pages:
        if page == 'page.html':
            theme.has_index = True
            # If your theme does not have index page,
            # catsup will rename page/1.html to page.html.
        if not os.path.exists(os.path.join(templates_path, page)):
            logging.warning("%s announces a page %s"
                         " which not exists." % (theme.name, page))
    theme.name = theme.name.lower()
    return theme


def find(theme_name=''):
    if not theme_name:
        theme_name = config.theme.name
    theme_name = theme_name.lower()
    theme_gallery = [
        os.path.join(os.path.abspath('themes'), theme_name),
        os.path.join(g.catsup_path, 'themes', theme_name),
    ]
    for path in theme_gallery:
        theme = read_meta(path)
        if theme:
            return theme

    raise Exception("Can't find theme: %s" % theme_name)


def list():
    theme_gallery = [
        os.path.abspath('themes'),
        os.path.join(g.catsup_path, 'themes'),
    ]
    themes = set()
    for path in theme_gallery:
        if not os.path.exists(path):
            continue
        names = os.listdir(path)
        for theme in names:
            themes.add(theme)
    print('Available themes: \n')
    for name in themes:
        theme = find(name)
        print('Name: %s' % theme.name)
        print('Author: %s' % theme.author)
        print('HomePage: %s' % theme.homepage)
        print('----')


def install(path, g):
    try:
        theme = find(path)
    except Exception:
        pass
    else:
        # Update theme
        if not os.path.exists(os.path.join(theme.path, '.git')):
            logging.warn("%s is not installed via git."
                         "Can't update it." % theme.name)
        else:
            logging.info("Updating theme %s" % theme.name)
            call('git pull', cwd=theme.path)
        sys.exit(0)

    themes_path = os.path.abspath('themes')
    if g:
        themes_path = os.path.join(g.catsup_path, 'themes')

    logging.info('Installing theme from %s' % path)

    if not os.path.exists(themes_path):
        os.makedirs(themes_path)

    if os.path.exists(path):
        meta = read_meta(path)
{'fraction_non_alphanumeric': 0.12447434819175777, 'fraction_numerical': 0.001009251471825063, 'mean_word_length': 2.0792335577421026, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4391949', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1831, 'n_words': 483}