text:
import argparse
import datetime
import os
import re
import shutil
import StringIO
import sys
import urlparse

import pygments
import pygments.formatters
import pygments.lexers
import yaml
import Cheetah.Template as template

import errors
import model
import model_parser
import output
import utils

class Config(object):
    def __init__(self, template_homepage_path, template_postpage_path, template_feedpage_path,
                 template_foundation_dir, template_blogula_css_path, template_img_dir,
                 template_sitemap_xml_path, template_robots_txt_path, template_humans_txt_path):
        assert isinstance(template_homepage_path, str)
        assert isinstance(template_postpage_path, str)
        assert isinstance(template_feedpage_path, str)
        assert isinstance(template_foundation_dir, str)
        assert isinstance(template_blogula_css_path, str)
        assert isinstance(template_img_dir, str)
        assert isinstance(template_sitemap_xml_path, str)
        assert isinstance(template_robots_txt_path, str)
        assert isinstance(template_humans_txt_path, str)

        self._template_homepage_path = template_homepage_path
        self._template_postpage_path = template_postpage_path
        self._template_feedpage_path = template_feedpage_path
        self._template_foundation_dir = template_foundation_dir
        self._template_blogula_css_path = template_blogula_css_path
        self._template_img_dir = template_img_dir
        self._template_sitemap_xml_path = template_sitemap_xml_path
        self._template_robots_txt_path = template_robots_txt_path
        self._template_humans_txt_path = template_humans_txt_path
        self._presentation_title_heading_level = 1
        self._presentation_article_title_heading_level = 2
        self._presentation_article_subtitle_heading_level_min = 3
        self._presentation_article_subtitle_heading_level_max = 6

    @property
    def template_homepage_path(self):
        return self._template_homepage_path

    @property
    def template_postpage_path(self):
        return self._template_postpage_path

    @property
    def template_feedpage_path(self):
        return self._template_feedpage_path

    @property
    def template_foundation_dir(self):
        return self._template_foundation_dir

    @property
    def template_blogula_css_path(self):
        return self._template_blogula_css_path

    @property
    def template_img_dir(self):
        return self._template_img_dir

    @property
    def template_sitemap_xml_path(self):
        return self._template_sitemap_xml_path

    @property
    def template_robots_txt_path(self):
        return self._template_robots_txt_path

    @property
    def template_humans_txt_path(self):
        return self._template_humans_txt_path

    @property
    def presentation_title_heading_level(self):
        return self._presentation_title_heading_level

    @property
    def presentation_article_title_heading_level(self):
        return self._presentation_article_title_heading_level

    @property
    def presentation_article_subtitle_heading_level_min(self):
        return self._presentation_article_subtitle_heading_level_min

    @property
    def presentation_article_subtitle_heading_level_max(self):
        return self._presentation_article_subtitle_heading_level_max

def _ParseConfig(config_path):
    config_text = utils.QuickRead(config_path)

    try:
        config_raw = yaml.safe_load(config_text)
    except yaml.YAMLError as e:
        raise errors.Error(str(e))

    if not isinstance(config_raw, dict):
        raise errors.Error('Invalid config file structure')

    templates_raw = utils.Extract(config_raw, 'Templates', dict)

    template_homepage_path = utils.Extract(templates_raw, 'HomePagePath', str)
    template_postpage_path = utils.Extract(templates_raw, 'PostPagePath', str)
    template_feedpage_path = utils.Extract(templates_raw, 'FeedPagePath', str)
    template_foundation_dir = utils.Extract(templates_raw, 'FoundationDir', str)
    template_blogula_css_path = utils.Extract(templates_raw, 'BlogulaCSSPath', str)
    template_img_dir = utils.Extract(templates_raw, 'ImgDir', str)
    template_sitemap_xml_path = utils.Extract(templates_raw, 'SitemapXmlPath', str)
    template_robots_txt_path = utils.Extract(templates_raw, 'RobotsTxtPath', str)
    template_humans_txt_path = utils.Extract(templates_raw, 'HumansTxtPath', str)

    return Config(template_homepage_path=template_homepage_path, template_postpage_path=template_postpage_path,
                  template_feedpage_path=template_feedpage_path, template_foundation_dir=template_foundation_dir,
                  template_blogula_css_path=template_blogula_css_path, template_img_dir=template_img_dir,
                  template_sitemap_xml_path=template_sitemap_xml_path, template_robots_txt_path=template_robots_txt_path,
                  template_humans_txt_path=template_humans_txt_path)

class SiteBuilder(object):
    def __init__(self, info_path, config, info, post_db):
        assert isinstance(info_path, str)
        assert isinstance(config, Config)
        assert isinstance(info, model.Info)
        assert isinstance(post_db, model.PostDB)

        self._info_path = info_path
        self._config = config
        self._info = info
        self._post_db = post_db

    @staticmethod
    def _UniformPath(path):
        def EliminateNonAlpha(word):
            good_word_file = StringIO.StringIO()

            for ch in word:
                if re.match('[a-z0-9_]', ch):
                    good_word_file.write(ch)

            good_word = good_word_file.getvalue()
            good_word_file.close()

            return good_word

        return '_'.join(EliminateNonAlpha(w) for w in path.lower().split())

    def _UrlForPost(self, post):
        return os.path.join(
            '/',
            self._info.output_posts_dir,
            SiteBuilder._UniformPath(SiteBuilder._EvaluateTextToText(post.title))) + '.html'

    def _GenerateHomepage(self):
        homepage_template_text = utils.QuickRead(self._config.template_homepage_path)
        homepage_template = template.Template(homepage_template_text)
        homepage_template.info = {}
        homepage_template.info['title_text'] = SiteBuilder._EvaluateTextToText(self._info.title)
        homepage_template.info['title_html'] = SiteBuilder._EvaluateTextToHTML(self._info.title)
        homepage_template.info['author'] = self._info.author
        homepage_template.info['avatar_url'] = '/img/avatar.jpg'
        homepage_template.info['description_text'] = SiteBuilder._EvaluateTextToText(self._info.description)
        homepage_template.info['description_html'] = SiteBuilder._EvaluateTextToHTML(self._info.description)
        homepage_template.posts = []

        for post in self._post_db.post_map.itervalues():
            homepage_template.posts.append({})
            homepage_template.posts[-1]['title_html'] = SiteBuilder._EvaluateTextToHTML(post.title)
            homepage_template.posts[-1]['description_html'] = SiteBuilder._EvaluateTextToHTML(post.description)
            homepage_template.posts[-1]['tags_html'] = [SiteBuilder._EvaluateTextToHTML(t) for t in post.tags]
            homepage_template.posts[-1]['url'] = self._UrlForPost(post)
            homepage_template.posts[-1]['date_str'] = post.date.strftime('%d %B %Y')

        homepage_template.posts.reverse()

        homepage_template.presentation = {}
        homepage_template.presentation['title_heading_level'] = \
            self._config.presentation_title_heading_level
        homepage_template.presentation['article_title_heading_level'] = \
            self._config.presentation_article_title_heading_level

        homepage_text = str(homepage_template)
        return output.File('text/html', output.CrawlMode.CRAWLABLE, homepage_text)

    def _GeneratePostpage(self, post):
        (line_units, extra_image_units) = SiteBuilder._LinearizeSectionToLineUnits(self._info_path, self._config, post.root_section, 0)
        postpage_template_text = utils.QuickRead(self._config.template_postpage_path)
        postpage_template = template.Template(postpage_template_text)

        postpage_template.info = {}
        postpage_template.info['title_text'] = SiteBuilder._EvaluateTextToText(self._info.title)
        postpage_template.info['title_html'] = SiteBuilder._EvaluateTextToHTML(self._info.title)
        postpage_template.info['author'] = self._info.author
        postpage_template.info['avatar_url'] = '/img/avatar.jpg'
        postpage_template.info['description_html'] = SiteBuilder._EvaluateTextToHTML(self._info.description)

        postpage_template.post = {}
        postpage_template.post['title_text'] = SiteBuilder._EvaluateTextToText(post.title)
        postpage_template.post['title_html'] = SiteBuilder._EvaluateTextToHTML(post.title)
        postpage_template.post['description_text'] = SiteBuilder._EvaluateTextToText(post.description)
        postpage_template.post['lineunits'] = line_units
        postpage_template.post['tags_html'] = [SiteBuilder._EvaluateTextToHTML(t) for t in post.tags]

        if post.prev_post is not None:
            postpage_template.post['prev_post'] = {}
            postpage_template.post['prev_post']['url'] = self._UrlForPost(post.prev_post)
            postpage_template.post['prev_post']['title_html'] = SiteBuilder._EvaluateTextToHTML(post.prev_post.title)
        else:
            postpage_template.post['prev_post'] = None

        if post.next_post is not None:
            postpage_template.post['next_post'] = {}
            postpage_template.post['next_post']['url'] = self._UrlForPost(post.next_post)
            postpage_template.post['next_post']['title_html'] = SiteBuilder._EvaluateTextToHTML(post.next_post.title)
        else:
            postpage_template.post['next_post'] = None

        postpage_template.post['series'] = []

        for s in post.series:
            postpage_template.post['series'].append({})
            postpage_template.post['series'][-1]['title_html'] = SiteBuilder._EvaluateTextToHTML(s)

            if post.prev_post_by_series[s] is not None:
                postpage_template.post['series'][-1]['prev_post'] = {}
                postpage_template.post['series'][-1]['prev_post']['url'] = self._UrlForPost(post.prev_post_by_series[s])
                postpage_template.post['series'][-1]['prev_post']['title_html'] = SiteBuilder._EvaluateTextToHTML(post.prev_post_by_series[s].title)
            else:
                postpage_template.post['series'][-1]['prev_post'] = None

            if post.next_post_by_series[s] is not None:
                postpage_template.post['series'][-1]['next_post'] = {}
                postpage_template.post['series'][-1]['next_post']['url'] = self._UrlForPost(post.next_post_by_series[s])
                postpage_template.post['series'][-1]['next_post']['title_html'] = SiteBuilder._EvaluateTextToHTML(post.next_post_by_series[s].title)
            else:
                postpage_template.post['series'][-1]['next_post'] = None

        postpage_template.presentation = {}
        postpage_template.presentation['title_heading_level'] = \
            self._config.presentation_title_heading_level
        postpage_template.presentation['article_title_heading_level'] = \
            self._config.presentation_article_title_heading_level

        postpage_text = str(postpage_template)
        return (output.File('text/html', output.CrawlMode.CRAWLABLE, postpage_text), extra_image_units)

    def _GenerateFeed(self):
        feedpage_template_text = utils.QuickRead(self._config.template_feedpage_path)
        feedpage_template = template.Template(feedpage_template_text)
        feedpage_template.info = {}
        feedpage_template.info['title_text'] = SiteBuilder._EvaluateTextToText(self._info.title)
        feedpage_template.info['description_text'] = SiteBuilder._EvaluateTextToText(self._info.description)
        feedpage_template.info['url'] = self._info.url
        feedpage_template.info['copyright_year'] = datetime.datetime.now().year
        feedpage_template.info['author'] = self._info.author
        feedpage_template.info['email'] = self._info.email
        feedpage_template.info['build_date_str'] = datetime.datetime.now().strftime('%A, %d %B %Y %S:%M:%H %Z')
        feedpage_template.posts = []

        for post in self._post_db.post_map.values()[-self._info.nr_of_posts_in_feed:]:
            feedpage_template.posts.append({})
            feedpage_template.posts[-1]['title_text'] = SiteBuilder._EvaluateTextToText(post.title)
            feedpage_template.posts[-1]['url'] = self._UrlForPost(post)
            feedpage_template.posts[-1]['description_text'] = SiteBuilder._EvaluateTextToText(post.description)
            feedpage_template.posts[-1]['tags_text'] = [SiteBuilder._EvaluateTextToText(t) for t in post.tags]
            feedpage_template.posts[-1]['pub_date_str'] = post.date.strftime('%A, %d %B %Y 00:00:00 %Z')

        feedpage_template.posts.reverse()
        feedpage_text = str(feedpage_template)
        return output.File('application/xml', output.CrawlMode.CRAWLABLE, feedpage_text)

    def _GenerateHumansTxt(self):
        humans_txt_template_text = utils.QuickRead(self._config.template_humans_txt_path)
        humans_txt_template = template.Template(humans_txt_template_text)
        humans_txt_template.author = self._info.author
        humans_txt_template.email = self._info.email
        humans_txt_template.twitter = self._info.twitter
        humans_txt_template.location = self._info.location
        humans_txt_template.build_date_str = datetime.datetime.now().strftime('%A, %d %B %Y %S:%M:%H %Z')

        humans_txt_text = str(humans_txt_template)

        return output.File('text/plain', output.CrawlMode.CRAWLABLE, humans_txt_text)

    def _GenerateSitemapXml(self, robots_txt_path, out_dir):
        def LinearizeUnits(path, unit):
            if unit.crawl_mode is output.CrawlMode.NON_CRAWLABLE:
                return []

            if isinstance(unit, output.File):
                return [{'host': self._info.url,
                         'path': path,
                         'build_date_str': datetime.datetime.now().strftime('%Y-%M-%d')}]
            elif isinstance(unit, output.Copy):
                if unit.is_dir:
                    raise Error('Copy directory "%s" cannot be crawlable' % path)

                return [{'host': self._info.url,
                         'path': path,
                         'build_date_str': datetime.datetime.now().strftime('%Y-%M-%d')}]
            elif isinstance(unit, output.Dir):
                linearized_units = [LinearizeUnits(os.path.join(path, subpath), subunit)
                                    for (subpath, subunit) in unit.units.iteritems()]
                return [item for sublist in linearized_units for item in sublist]

        sitemap_xml_template_text = utils.QuickRead(self._config.template_sitemap_xml_path)
        sitemap_xml_template = template.Template(sitemap_xml_template_text)
        sitemap_xml_template.urls = LinearizeUnits('/', out_dir)
        sitemap_xml_template.robots_txt = {}
        sitemap_xml_template.robots_txt['host'] = self._info.url
        sitemap_xml_template.robots_txt['path'] = robots_txt_path
        sitemap_xml_template.robots_txt['build_date_str'] = datetime.datetime.now().strftime('%Y-%M-%d')

        sitemap_xml_text = str(sitemap_xml_template)

        return output.File('text/plain', output.CrawlMode.CRAWLABLE, sitemap_xml_text)

    def _GenerateRobotsTxt(self, sitemap_xml_path, out_dir):
        def LinearizeUnits(path, unit):
            if unit.crawl_mode is output.CrawlMode.CRAWLABLE:
                if isinstance(unit, (output.File, output.Copy)):
                    return []
                elif isinstance(unit, output.Dir):
                    linearized_units = [LinearizeUnits(os.path.join(path, subpath), subunit)
                                        for (subpath, subunit) in unit.units.iteritems()]
                    return [item for sublist in linearized_units for item in sublist]
            else:
                if isinstance(unit, output.File):
                    return [{'path': path}]
                elif isinstance(unit, output.Copy):
                    if unit.is_dir:
                        return [{'path': '%s/' % path}]
                    else:
                        return [{'path': path}]
                elif isinstance(unit, output.Dir):
                    return [{'path': '%s/' % path}]

        robots_txt_template_text = utils.QuickRead(self._config.template_robots_txt_path)
        robots_txt_template = template.Template(robots_txt_template_text)
        robots_txt_template.sitemap_xml_host = self._info.url
        robots_txt_template.sitemap_xml_path = sitemap_xml_path
        robots_txt_template.urls = LinearizeUnits('', out_dir)

        robots_txt_text = str(robots_txt_template)

        return output.File('text/plain', output.CrawlMode.CRAWLABLE, robots_txt_text)

    def Generate(self):
        out_dir = output.Dir(output.CrawlMode.CRAWLABLE)

        # generate home page
        homepage_unit = self._GenerateHomepage()
        out_dir.Add(self._info.output_homepage_path, homepage_unit)

        # generate one page for each article
        posts_dir = output.Dir(output.CrawlMode.CRAWLABLE)
        extra_image_units = []

        for post in self._post_db.post_map.itervalues():
            (postpage_unit, post_extra_image_units) = self._GeneratePostpage(post)
            posts_dir.Add(SiteBuilder._UniformPath(SiteBuilder._EvaluateTextToText(post.title)) + '.html', postpage_unit)
            extra_image_units.extend(post_extra_image_units)

        out_dir.Add(self._info.output_posts_dir, posts_dir)

        # generate rss feed
        feed_unit = self._GenerateFeed()
        out_dir.Add('feed.xml', feed_unit)

        # generate projects page (from projects description)

        # generate about page?

        # copy extra scripts

        foundation_unit = output.Copy(output.CrawlMode.NON_CRAWLABLE, self._config.template_foundation_dir)
        out_dir.Add('foundation', foundation_unit)

        # Copy CSS

        blogula_css_unit = output.Copy(output.CrawlMode.NON_CRAWLABLE, self._config.template_blogula_css_path)
        out_dir.Add('blogula.css', blogula_css_unit)

        # Generate CSS for CodeBlock cell code highliting.

        code_highlight_css = pygments.formatters.HtmlFormatter().get_style_defs('.code-block-highlight')
        code_highlight_css_unit = output.File('text/css', output.CrawlMode.NON_CRAWLABLE, code_highlight_css)
        out_dir.Add('code_highlight.css', code_highlight_css_unit)

        # Copy images image

        image_dir = output.Dir(output.CrawlMode.CRAWLABLE)

        ## Copy avatar image

        avatar_unit = output.Copy(output.CrawlMode.CRAWLABLE,
            os.path.join(os.path.dirname(self._info_path), self._info.avatar_path))
        image_dir.Add('avatar.jpg', avatar_unit)

        ## Copy post extra images

        for (basename, unit) in extra_image_units:
            image_dir.Add(basename, unit)

        out_dir.Add('img', image_dir)

        # Generate humans.txt file.
        humans_txt_unit = self._GenerateHumansTxt()
        out_dir.Add('humans.txt', humans_txt_unit)

        # Generate sitemap.xml file.
        sitemap_xml_unit = self._GenerateSitemapXml('/robots.txt', out_dir)
        out_dir.Add('sitemap.xml', sitemap_xml_unit)

        # Generate robots.txt file.
        robots_txt_unit = self._GenerateRobotsTxt('/sitemap.xml', out_dir)
        out_dir.Add('robots.txt', robots_txt_unit)

        return out_dir

    @property
    def config(self):
        return self._config

    @property
    def info(self):
        return self._info

    @property
    def post_db(self):
        return self._post_db

    @staticmethod
    def _EvaluateTextToText(text):
        text_file = StringIO.StringIO()

        for atom in text.atoms:
            if isinstance(atom, model.Word):
                text_file.write(atom.text)
                text_file.write(' ')
            elif isinstance(atom, model.Function):
                if atom.name == 'slash':
                    text_file.write('\\')
                    text_file.write(' ')
                elif atom.name == 'brace-beg':
                    text_file.write('{')
                    text_file.write(' ')
                elif atom.name == 'brace-end':
                    text_file.write('}')
                    text_file.write(' ')
                elif atom.name == 'f':
                    text_file.write(atom.arg_list[0])
                    text_file.write(' ')
                elif atom.name == 'def':
                    text_file.write(atom.arg_list[0])
                    text_file.write(' ')
                elif atom.name == 'ref':
                    text_file.write(atom.arg_list[0])
                    text_file.write(' ')
                else:
                    print('Unknown function %s - skipping' % atom.name)
            else:
                raise errors.Error('Unknown atom')

        text_file.seek(-1, 2)
        text_str = text_file.getvalue()
        text_file.close()

        return text_str

    @staticmethod
    def _EvaluateTextToHTML(text):
        html_file = StringIO.StringIO()

        for atom in text.atoms:
            if isinstance(atom, model.Word):
                html_file.write(atom.text)
                html_file.write(' ')
            elif isinstance(atom, model.Function):
                if atom.name == 'slash':
                    html_file.write('\')
                    html_file.write(' ')
                elif atom.name == 'brace-beg':
                    html_file.write('{')
                    html_file.write(' ')
                elif atom.name == 'brace-end':
                    html_file.write('}')
                    html_file.write(' ')
                elif atom.name == 'f':
                    html_file.write('\(')
                    html_file.write(atom.arg_list[0])
                    html_file.write('\)')
                    html_file.write(' ')
                elif atom.name == 'def':
                    html_file.write('<strong>')
                    html_file.write(atom.arg_list[0])
                    html_file.write('</strong>')
                    html_file.write(' ')
                elif atom.name == 'ref':
                    html_file.write('<a href="#">')
                    html_file.write(atom.arg_list[0])
                    html_file.write('</a>')
                    html_file.write(' ')
                else:
                    print('Unknown function %s - skipping' % atom.name)
            else:
                raise errors.Error('Unknown atom')

        html_file.seek(-1, 2)
        html_str = html_file.getvalue()
        html_file.close()

        return html_str

    @staticmethod
    def _LinearizeSectionToLineUnits(info_path, config, section, level):
        line_units = []
        extra_image_units = []

        if level >= 1:
            line_units.append({})
            line_units[-1]['type'] = 'header'
            line_units[-1]['level'] = \
                min(config.presentation_article_subtitle_heading_level_min + level - 1,
                    config.presentation_article_subtitle_heading_level_max)
            line_units[-1]['text_html'] = SiteBuilder._EvaluateTextToHTML(section.title)

        for paragraph in section.paragraphs:
            line_units.append({})

            if isinstance(paragraph.cell, model.Textual):
                line_units[-1]['type'] = 'textual'
                line_units[-1]['text_html'] = SiteBuilder._EvaluateTextToHTML(paragraph.cell.text)
            elif isinstance(paragraph.cell, model.List):
                line_units[-1]['type'] = 'list'
                if paragraph.cell.header_text is not None:
                    line_units[-1]['has_header'] = True
                    line_units[-1]['header_html'] = SiteBuilder._EvaluateTextToHTML(paragraph.cell.header_text)
                else:
                    line_units[-1]['has_header'] = False
                line_units[-1]['items'] = [SiteBuilder._EvaluateTextToHTML(l) for l in paragraph.cell.items]
            elif isinstance(paragraph.cell, model.Formula):
                line_units[-1]['type'] = 'formula'
                if paragraph.cell.header_text is not None:
                    line_units[-1]['has_header'] = True
                    line_units[-1]['header_html'] = SiteBuilder._EvaluateTextToHTML(paragraph.cell.header_text)
                else:
                    line_units[-1]['has_header'] = False
                line_units[-1]['formula'] = paragraph.cell.formula
            elif isinstance(paragraph.cell, model.CodeBlock):
                line_units[-1]['type'] = 'code-block'
                if paragraph.cell.header_text is not None:
                    line_units[-1]['has_header'] = True
                    line_units[-1]['header_html'] = SiteBuilder._EvaluateTextToHTML(paragraph.cell.header_text)
                else:
                    line_units[-1]['has_header'] = False

                try:
                    lexer = pygments.lexers.get_lexer_by_name(paragraph.cell.language)
                except pygments.util.ClassNotFound as e:
                    lexer = pygments.lexers.guess_lexer(paragraph.cell.code)

                formatter = pygments.formatters.HtmlFormatter(linenos=True, cssclass='code-block-highlight', cssstyles='font-size:0.75em;')

                line_units[-1]['code_html'] = pygments.highlight(paragraph.cell.code, lexer, formatter)
            elif isinstance(paragraph.cell, model.Image):
                line_units[-1]['type'] = 'image'
                if paragraph.cell.header_text is not None:
                    line_units[-1]['has_header'] = True
                    line_units[-1]['header_html'] = SiteBuilder._EvaluateTextToHTML(paragraph.cell.header_text)
                    line_units[-1]['alt_text'] = SiteBuilder._EvaluateTextToText(paragraph.cell.header_text)
                else:
                    line_units[-1]['has_header'] = False
                    line_units[-1]['alt_text'] = ''

                split_path = urlparse.urlparse(paragraph.cell.path)

                if split_path.scheme == 'http' or split_path.scheme == 'https':
                    line_units[-1]['path'] = paragraph.cell.path
                elif split_path.scheme == '':
                    image_basename = os.path.normpath(paragraph.cell.path).replace('/', '_')
                    line_units[-1]['path'] = '/img/%s' % image_basename

                    if os.path.isabs(paragraph.cell.path):
                        extra_image_path = paragraph.cell.path
                    else:
                        extra_image_path = os.path.join(os.path.dirname(info_path), paragraph.cell.path)

                    extra_image_units.append((image_basename, output.Copy(output.CrawlMode.CRAWLABLE, extra_image_path)))
                else:
                    raise errors.Error('Unsupported path format')
            else:
                raise errors.Error('Q')

        for subsection in section.subsections:
            (sub_line_units, sub_extra_image_units) = SiteBuilder._LinearizeSectionToLineUnits(info_path, config, subsection, level+1)
            line_units.extend(sub_line_units)
            extra_image_units.extend(sub_extra_image_units)

        return (line_units, extra_image_units)

HELP_DESCRIPTION = 'Blogula - a blog generator'
HELP_INFO = 'Path to blog information file'

def main(argv):
    arg_parser = argparse.ArgumentParser(description=HELP_DESCRIPTION)
    arg_parser.add_argument('-i', '--info_path', metavar='PATH', type=str, help=HELP_INFO, required=True)
    args = arg_parser.parse_args(argv[1:])

    config = _ParseConfig('config')
    info = model_parser.ParseInfo(args.info_path)
    post_db = model_parser.ParsePostDB(info)

    site_generator = SiteBuilder(args.info_path, config, info, post_db)
    out_dir = site_generator.Generate()

    output.WriteLocalOutput(info.output_dir, out_dir)

if __name__ == '__main__':
    main(sys.argv)


text:
package uk.org.ulcompsoc.ld32.systems;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import uk.org.ulcompsoc.ld32.components.Doomed;
import uk.org.ulcompsoc.ld32.components.Fade;
import uk.org.ulcompsoc.ld32.components.Killable;
import uk.org.ulcompsoc.ld32.components.Position;
import uk.org.ulcompsoc.ld32.components.Renderable;
import uk.org.ulcompsoc.ld32.components.Rotatable;
import uk.org.ulcompsoc.ld32.components.Tower;
import uk.org.ulcompsoc.ld32.util.LDUtil;
import uk.org.ulcompsoc.ld32.util.Mappers;
import uk.org.ulcompsoc.ld32.util.TextureManager;
import uk.org.ulcompsoc.ld32.util.TextureName;

import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntityListener;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;

public class RenderSystem extends IteratingSystem implements EntityListener, Comparator<Entity> {
	private final List<Entity> entities = new ArrayList<Entity>();

	@SuppressWarnings("unchecked")
	private final Family family = Family.all(Renderable.class).get();

	private ImmutableArray<Entity> entitiesImmu = null;

	private final Batch batch;
	private final ShapeRenderer renderer;
	private final OrthographicCamera camera;
	private final Color POSITIVE_HEALTH_COLOR = Color.GREEN;
	private final Color NEGATIVE_HEALTH_COLOR = Color.RED;
	private final float HEALTH_HEIGHT_POSITION_MODIFIER = 4.0f;

	private TextureManager textureManager;

	private boolean reorder = false;

	@SuppressWarnings("unchecked")
	public RenderSystem(int priority, final Batch batch, final ShapeRenderer renderer, final OrthographicCamera camera) {
		super(Family.all(Position.class, Renderable.class).get(), priority);

		this.batch = batch;
		this.renderer = renderer;
		this.camera = camera;
		this.textureManager = new TextureManager();
		this.textureManager.load();
		initPongTextureRegions();
	}

	@Override
	public void addedToEngine(Engine engine) {
		super.addedToEngine(engine);
		engine.addEntityListener(this);
		this.entitiesImmu = engine.getEntitiesFor(family);
	}

	@Override
	public void removedFromEngine(Engine engine) {
		super.removedFromEngine(engine);
		engine.removeEntityListener(this);
		this.entitiesImmu = null;
	}

	@Override
	public void update(float deltaTime) {
		if (reorder) {
			doReorder();
		}

		batch.setProjectionMatrix(camera.combined);
		batch.enableBlending();

		for (int i = 0; i < entities.size(); ++i) {
			processEntity(entities.get(i), deltaTime);
		}
	}

	public void doReorder() {
		entities.clear();

		for (int i = 0; i < entitiesImmu.size(); i++) {
			entities.add(entitiesImmu.get(i));
		}

		Collections.sort(entities, this);
		reorder = false;
	}

	@Override
	protected void processEntity(Entity entity, float deltaTime) {
		final Position p = Mappers.positionMapper.get(entity);
		final Renderable r = Mappers.renderableMapper.get(entity);
		final Killable k = Mappers.killableMapper.get(entity);

		final float scalingFactor = calculateScalingFactor(r);
		if (handleFade(entity, r, deltaTime)) {
			// if true, the entity is invisible and we can ignore it.
			return;
		}

		switch (r.type) {
		case SHAPE: {
			renderer.setProjectionMatrix(camera.combined);
			renderer.begin(ShapeType.Filled);
			renderer.setColor(r.color);
			renderer.rect(p.getX() - (r.size / 2.0f), p.getY() - (r.size / 2.0f), r.size, r.size);
			renderer.end();

			if (k != null) {
				drawHealthBar(p.getX() - (r.size / 2.0f), p.getY() - (r.size / 2.0f), k, r);
			}

			break;
		}

		case STATIC_TEXTURE: {
			drawFrame(entity, p, r, k, scalingFactor, r.region, deltaTime);
			// It's a tower
			if (Mappers.towerMapper.has(entity)) {
				this.drawTowerPongProgress(entity, scalingFactor, deltaTime);
			}
			break;
		}

		case ANIMATED_TEXTURE: {
			r.animTime += deltaTime;
			drawFrame(entity, p, r, k, scalingFactor, r.animation.getKeyFrame(r.animTime), deltaTime);
			break;
		}

		case SPRITE: {
			drawFrame(entity, p, r, k, scalingFactor, r.sprite, deltaTime);

			break;
		}

		default:
			break;
		}

	}

	private void drawFrame(final Entity entity, final Position p, final Renderable r, final Killable k,
	        float scalingFactor, final TextureRegion region, float deltaTime) {
		final float rotationDeg = handleRotation(entity, p, deltaTime);

		final float xOffset = region.getRegionWidth() / 2.0f;
		final float yOffset = region.getRegionHeight() / 2.0f;

		final float x = p.getX() - xOffset;
		final float y = p.getY() - yOffset;

		batch.begin();
		batch.setProjectionMatrix(camera.combined);

		final Color colorTemp = batch.getColor();
		if (r.color != null) {
			batch.setColor(r.color);
		}

		batch.draw(region, x, y, region.getRegionWidth() / 2.0f, region.getRegionHeight() / 2.0f,
		        region.getRegionWidth(), region.getRegionHeight(), scalingFactor, scalingFactor, rotationDeg);

		batch.setColor(colorTemp);

		batch.end();

		if (k != null) {
			drawHealthBar(p.getX(), p.getY(), k, r);
		}

	}

	/**
	 * Draws a health bar above a position based on the radius of the entity.
	 * Drawing appropriate regions for remaining health.
	 *
	 * @param k
	 *            for health information
	 * @param entityRenderable
	 *            the renderable of the entity whose healthbar this is
	 */
	private void drawHealthBar(float x, float y, Killable k, Renderable entityRenderable) {
		renderer.begin(ShapeType.Filled);

		x -= entityRenderable.getWidth() / 2.0f;
		y += entityRenderable.getHeight() / 1.9f;

		// Draw the positive health
		renderer.setColor(POSITIVE_HEALTH_COLOR);
		renderer.rect(x, y, entityRenderable.getWidth(), entityRenderable.getHeight() / HEALTH_HEIGHT_POSITION_MODIFIER);

		// Draw the negative health
		renderer.setColor(NEGATIVE_HEALTH_COLOR);

		float remaningHealth = k.getHealth() / k.getOrigHealth();
		// If there's no difference, default to 0
		if (remaningHealth >= 1.0f) {
			remaningHealth = 0.0f;
		} else {
			remaningHealth = 1.0f - remaningHealth;
		}

		renderer.rect(x, y, entityRenderable.getWidth() * remaningHealth, entityRenderable.getHeight()
		        / HEALTH_HEIGHT_POSITION_MODIFIER);

		renderer.end();
	}

	private float calculateScalingFactor(final Renderable sc) {
		if (sc != null) {
			if (sc.scaleAnimTimeElapsed <= 0.0f) {
				sc.scale = sc.baseScale;
			} else {
				final float scalePctage = 1.0f + LDUtil.normalCurve(0.0f, sc.totalScaleAnimTime,
				        sc.scaleAnimTimeElapsed, true);
				sc.scale = sc.baseScale * (scalePctage >= sc.maxScale ? sc.maxScale : scalePctage);
				// System.out.format("Scale = %f, tE = %f, tAT = %f\n",
				// sc.scale, sc.timeElapsed, sc.totalAnimTime);
			}

			return sc.scale;
		} else {
			return 1.0f;
		}
	}

	private TextureRegion pong0, pong1, pong2, pong3, pong4, pong5, pong6, pong7, pong8, pong9, pong10;

	private void initPongTextureRegions() {
		this.pong0 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_ZERO));
		this.pong1 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_ONE));
		this.pong2 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_TWO));
		this.pong3 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_THREE));
		this.pong4 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_FOUR));
		this.pong5 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_FIVE));
		this.pong6 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_SIX));
		this.pong7 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_SEVEN));
		this.pong8 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_EIGHT));
		this.pong9 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_NINE));
		this.pong10 = new TextureRegion(textureManager.nameMap.get(TextureName.PONG_TEN));
	}

	private void drawTowerPongProgress(Entity entity, float scalingFactor, float deltaTime) {
		Tower tower = Mappers.towerMapper.get(entity);
		tower.upgradeElapsedTime += deltaTime;
		Position p = Mappers.positionMapper.get(entity);

		TextureRegion region = null;

		switch (tower.pongBonusCounter) {
		case 0:
			region = pong0;
			break;
		case 1:
			region = pong1;
			break;
		case 2:
			region = pong2;
			break;
		case 3:
			region = pong3;
			break;
		case 4:
			region = pong4;
			break;
		case 5:
			region = pong5;
			break;
		case 6:
			region = pong6;
			break;
		case 7:
			region = pong7;
			break;
		case 8:
			region = pong8;
			break;
		case 9:
			region = pong9;
			break;
		case 10:
			region = pong10;
			break;
		}

		final float xOffset = region.getRegionWidth() / 2.0f;
		final float yOffset = region.getRegionHeight() / 2.0f;

		final float x = p.getX() - xOffset;
		final float y = p.getY() - yOffset;

		batch.begin();
		batch.setProjectionMatrix(camera.combined);
		batch.draw(region, x, y, region.getRegionWidth() / 2.0f, region.getRegionHeight() / 2.0f,
		        region.getRegionWidth(), region.getRegionHeight(), scalingFactor, scalingFactor, 0.0f);
		batch.end();
	}

	private boolean handleFade(final Entity entity, final Renderable r, float deltaTime) {
		final Fade fade = Mappers.fadeMapper.get(entity);

		if (fade != null) {
			fade.timeElapsed += deltaTime;

			final float fadePctage = LDUtil.smoothStep(0.0f, fade.fadeTime, fade.timeElapsed);
			final float mutatedAlpha = (1.0f - fadePctage * 1.0f);

			if (mutatedAlpha <= fade.targetAlpha) {
				r.color.a = fade.targetAlpha;

				if (fade.doomAfterFade) {
					entity.add(new Doomed());
				}

				if (fade.targetAlpha <= 0.01f) {
					// basically invisible, not much point rendering
					return true;
				}
			} else {
				r.color.a = mutatedAlpha;
			}
		}

		return false;
	}

	/**
	 *
	 * @param entity
	 * @param p
	 * @param deltaTime
	 * @return rotation IN DEGREES
	 */
	private float handleRotation(final Entity entity, final Position p, float deltaTime) {
		final Rotatable rot = Mappers.rotatableMapper.get(entity);

		final float rotation;
		if (rot != null) {
			if (rot.matchPhi) {
				rotation = p.getPhi();
			} else {
				if (rot.animatedRotation) {
					rot.animTime += deltaTime;
					if (rot.animTime >= rot.timeToRotate) {
						rot.animTime -= rot.timeToRotate;
					}

					rot.rotation = Math.min(2 * LDUtil.PI * rot.animTime / rot.timeToRotate, LDUtil.PI * 2);
				}

				rotation = rot.rotation;
			}
		} else {
			rotation = 0.0f;
		}

		return (float) Math.toDegrees(rotation);
	}

	@Override
	public void entityAdded(Entity entity) {
		final Renderable r = Mappers.renderableMapper.get(entity);
		if (r != null) {
			reorder = true;
		}
	}

	@Override
	public void entityRemoved(Entity entity) {
		entities.remove(entity);
	}

	@Override
	public int compare(Entity o1, Entity o2) {
		final Renderable r1 = Mappers.renderableMapper.get(o1);
		final Renderable r2 = Mappers.renderableMapper.get(o2);

		return r1.compareTo(r2);
	}

}

redpajama_set_name: RedPajamaGithub


text:
@implementation NCXNavigationDefinition

@synthesize  navigationPoints = navigationPoints_;
@synthesize HTML_NamesArray;
@synthesize bookName;

- (id) init
{
	if ((self = [super init]) != nil) {
		navigationPoints_ = [NSMutableArray new];
		HTML_NamesArray = [NSMutableArray new];

	}
	return self;
}

- (void) dealloc
{
	[navigationPoints_ release];
	[super dealloc];
}

- (void) addNavigationPoint: (NCXNavigationPoint*) navigationPoint
{
	[navigationPoints_ addObject: navigationPoint ];
}

@end


text:
//#define LOG_NDEBUG 0
#define LOG_TAG "VideoEditorSRC"

#include <stdlib.h>
#include <utils/Log.h>
#include <audio_utils/primitives.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/MetaData.h>
#include <media/stagefright/MediaBuffer.h>
#include <media/stagefright/MediaDefs.h>
#include "VideoEditorSRC.h"

namespace android {

VideoEditorSRC::VideoEditorSRC(const sp<MediaSource> &source) {
    ALOGV("VideoEditorSRC %p(%p)", this, source.get());
    static const int32_t kDefaultSamplingFreqencyHz = kFreq32000Hz;
    mSource = source;
    mResampler = NULL;
    mChannelCnt = 0;
    mSampleRate = 0;
    mOutputSampleRate = kDefaultSamplingFreqencyHz;
    mStarted = false;
    mInitialTimeStampUs = -1;
    mAccuOutBufferSize  = 0;
    mSeekTimeUs = -1;
    mBuffer = NULL;
    mLeftover = 0;
    mFormatChanged = false;
    mStopPending = false;
    mSeekMode = ReadOptions::SEEK_PREVIOUS_SYNC;

    // Input Source validation
    sp<MetaData> format = mSource->getFormat();
    const char *mime;
    CHECK(format->findCString(kKeyMIMEType, &mime));
    CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW));

    // Set the metadata of the output after resampling.
    mOutputFormat = new MetaData;
    mOutputFormat->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
    mOutputFormat->setInt32(kKeySampleRate, kDefaultSamplingFreqencyHz);
    mOutputFormat->setInt32(kKeyChannelCount, 2);  // always stereo
}

VideoEditorSRC::~VideoEditorSRC() {
    ALOGV("~VideoEditorSRC %p(%p)", this, mSource.get());
    stop();
}

status_t VideoEditorSRC::start(MetaData *params) {
    ALOGV("start %p(%p)", this, mSource.get());
    CHECK(!mStarted);

    // Set resampler if required
    checkAndSetResampler();

    mSeekTimeUs = -1;
    mSeekMode = ReadOptions::SEEK_PREVIOUS_SYNC;
    mStarted = true;
    mSource->start();

    return OK;
}

status_t VideoEditorSRC::stop() {
    ALOGV("stop %p(%p)", this, mSource.get());
    if (!mStarted) {
        return OK;
    }

    if (mBuffer) {
        mBuffer->release();
        mBuffer = NULL;
    }
    mSource->stop();
    if (mResampler != NULL) {
        delete mResampler;
        mResampler = NULL;
    }

    mStarted = false;
    mInitialTimeStampUs = -1;
    mAccuOutBufferSize = 0;
    mLeftover = 0;

    return OK;
}

sp<MetaData> VideoEditorSRC::getFormat() {
    ALOGV("getFormat");
    return mOutputFormat;
}

status_t VideoEditorSRC::read(
        MediaBuffer **buffer_out, const ReadOptions *options) {
    ALOGV("read %p(%p)", this, mSource.get());
    *buffer_out = NULL;

    if (!mStarted) {
        return ERROR_END_OF_STREAM;
    }

    if (mResampler) {
        // Store the seek parameters
        int64_t seekTimeUs;
        ReadOptions::SeekMode mode = ReadOptions::SEEK_PREVIOUS_SYNC;
        if (options && options->getSeekTo(&seekTimeUs, &mode)) {
            ALOGV("read Seek %lld", seekTimeUs);
            mSeekTimeUs = seekTimeUs;
            mSeekMode = mode;
        }

        // We ask for 1024 frames in output
        // resampler output is always 2 channels and 32 bits
        const size_t kOutputFrameCount = 1024;
        const size_t kBytes = kOutputFrameCount * 2 * sizeof(int32_t);
        int32_t *pTmpBuffer = (int32_t *)calloc(1, kBytes);
        if (!pTmpBuffer) {
            ALOGE("calloc failed to allocate memory: %d bytes", kBytes);
            return NO_MEMORY;
        }

        // Resample to target quality
        mResampler->resample(pTmpBuffer, kOutputFrameCount, this);

        if (mStopPending) {
            stop();
            mStopPending = false;
        }

        // Change resampler and retry if format change happened
        if (mFormatChanged) {
            mFormatChanged = false;
            checkAndSetResampler();
            free(pTmpBuffer);
            return read(buffer_out, NULL);
        }

        // Create a new MediaBuffer
        int32_t outBufferSize = kOutputFrameCount * 2 * sizeof(int16_t);
        MediaBuffer* outBuffer = new MediaBuffer(outBufferSize);

        // Convert back to 2 channels and 16 bits
        ditherAndClamp(
                (int32_t *)((uint8_t*)outBuffer->data() + outBuffer->range_offset()),
                pTmpBuffer, kOutputFrameCount);
        free(pTmpBuffer);

        // Compute and set the new timestamp
        sp<MetaData> to = outBuffer->meta_data();
        int64_t totalOutDurationUs = (mAccuOutBufferSize * 1000000) / (mOutputSampleRate * 2 * 2);
        int64_t timeUs = mInitialTimeStampUs + totalOutDurationUs;
        to->setInt64(kKeyTime, timeUs);

        // update the accumulate size
        mAccuOutBufferSize += outBufferSize;
        *buffer_out = outBuffer;
    } else {
        // Resampling not required. Read and pass-through.
        MediaBuffer *aBuffer;
        status_t err = mSource->read(&aBuffer, options);
        if (err != OK) {
            ALOGV("read returns err = %d", err);
        }

        if (err == INFO_FORMAT_CHANGED) {
            checkAndSetResampler();
            return read(buffer_out, NULL);
        }

        // EOS or some other error
        if(err != OK) {
            stop();
            *buffer_out = NULL;
            return err;
        }
        *buffer_out = aBuffer;
    }

    return OK;
}

status_t VideoEditorSRC::getNextBuffer(AudioBufferProvider::Buffer *pBuffer, int64_t pts) {
    ALOGV("getNextBuffer %d, chan = %d", pBuffer->frameCount, mChannelCnt);
    uint32_t done = 0;
    uint32_t want = pBuffer->frameCount * mChannelCnt * 2;
    pBuffer->raw = malloc(want);

    while (mStarted && want > 0) {
        // If we don't have any data left, read a new buffer.
        if (!mBuffer) {
            // if we seek, reset the initial time stamp and accumulated time
            ReadOptions options;
            if (mSeekTimeUs >= 0) {
                ALOGV("%p cacheMore_l Seek requested = %lld", this, mSeekTimeUs);
                ReadOptions::SeekMode mode = mSeekMode;
                options.setSeekTo(mSeekTimeUs, mode);
                mSeekTimeUs = -1;
                mInitialTimeStampUs = -1;
                mAccuOutBufferSize = 0;
            }

            status_t err = mSource->read(&mBuffer, &options);

            if (err != OK) {
                free(pBuffer->raw);
                pBuffer->raw = NULL;
                pBuffer->frameCount = 0;
            }

            if (err == INFO_FORMAT_CHANGED) {
                ALOGV("getNextBuffer: source read returned INFO_FORMAT_CHANGED");
                // At this point we cannot switch to a new AudioResampler because
                // we are in a callback called by the AudioResampler itself. So
                // just remember the fact that the format has changed, and let
                // read() handles this.
                mFormatChanged = true;
                return err;
            }

            // EOS or some other error
            if (err != OK) {
                ALOGV("EOS or some err: %d", err);
                // We cannot call stop() here because stop() will release the
                // AudioResampler, and we are in a callback of the AudioResampler.
                // So just remember the fact and let read() call stop().
                mStopPending = true;
                return err;
            }

            CHECK(mBuffer);
            mLeftover = mBuffer->range_length();
            if (mInitialTimeStampUs == -1) {
                int64_t curTS;
                sp<MetaData> from = mBuffer->meta_data();
                from->findInt64(kKeyTime, &curTS);
                ALOGV("setting mInitialTimeStampUs to %lld", mInitialTimeStampUs);
                mInitialTimeStampUs = curTS;
            }
        }

        // Now copy data to the destination
        uint32_t todo = mLeftover;
        if (todo > want) {
            todo = want;
        }

        uint8_t* end = (uint8_t*)mBuffer->data() + mBuffer->range_offset()
                + mBuffer->range_length();
        memcpy((uint8_t*)pBuffer->raw + done, end - mLeftover, todo);
        done += todo;
        want -= todo;
        mLeftover -= todo;

        // Release MediaBuffer as soon as possible.
        if (mLeftover == 0) {
            mBuffer->release();
            mBuffer = NULL;
        }
    }

    pBuffer->frameCount = done / (mChannelCnt * 2);
    ALOGV("getNextBuffer done %d", pBuffer->frameCount);
    return OK;
}

void VideoEditorSRC::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) {
    ALOGV("releaseBuffer: %p", pBuffers);
    free(pBuffer->raw);
    pBuffer->raw = NULL;
    pBuffer->frameCount = 0;
}

void VideoEditorSRC::checkAndSetResampler() {
    ALOGV("checkAndSetResampler");

    static const uint16_t kUnityGain = 0x1000;
    sp<MetaData> format = mSource->getFormat();
    const char *mime;
    CHECK(format->findCString(kKeyMIMEType, &mime));
    CHECK(!strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW));

    CHECK(format->findInt32(kKeySampleRate, &mSampleRate));
    CHECK(format->findInt32(kKeyChannelCount, &mChannelCnt));

    // If a resampler exists, delete it first
    if (mResampler != NULL) {
        delete mResampler;
        mResampler = NULL;
    }

    // Clear previous buffer
    if (mBuffer) {
        mBuffer->release();
        mBuffer = NULL;
    }

    if (mSampleRate != mOutputSampleRate || mChannelCnt != 2) {
        ALOGV("Resampling required (%d => %d Hz, # channels = %d)",
            mSampleRate, mOutputSampleRate, mChannelCnt);

        mResampler = AudioResampler::create(
                        16 /* bit depth */,
                        mChannelCnt,
                        mOutputSampleRate);
        CHECK(mResampler);
        mResampler->setSampleRate(mSampleRate);
        mResampler->setVolume(kUnityGain, kUnityGain);
    } else {
        ALOGV("Resampling not required (%d => %d Hz, # channels = %d)",
            mSampleRate, mOutputSampleRate, mChannelCnt);
    }
}

} //namespce android


text:
import mysql.connector
import configparser
import os

import scrapy
from scrapy.pipelines.images import ImagesPipeline
from scrapy.exceptions import DropItem

class MeiziImagesPipeline(ImagesPipeline):
    def connect_to_db(self):
        self.cfg = configparser.ConfigParser()
        self.cfg.read(os.path.abspath('./meizi/db.cfg'))
        self.cnt = mysql.connector.connect(
            user=self.cfg.get('connect', 'user'),
            password=self.cfg.get('connect', 'password'),
            host=self.cfg.get('connect', 'host'),
            database=self.cfg.get('connect', 'database')
        )
        self.cursor = self.cnt.cursor()

        self.GET_EXIST_ITEM = (
            "SELECT downloaded FROM meizi_spider "
            "WHERE img_md5 = %s "
        )

        self.UPDATE_IMG_ITEM = (
            "UPDATE meizi_spider "
            "SET downloaded=%(downloaded)s "
            "WHERE img_md5=%(img_md5)s"
        )

    def exist_item_images_downloaded(self, item):
        self.cursor.execute(
            self.GET_EXIST_ITEM,
            (item['img_md5'],)
        )
        row = self.cursor.fetchone()
        return row

    def update_img_item(self, item):
        try:
            tmp_dict = {
                'img_md5': item['img_md5'],
                'downloaded': True,
            }
            self.cursor.execute(
                self.UPDATE_IMG_ITEM,
                tmp_dict
            )
            self.cnt.commit()
        except mysql.connector.Error as err:
            print('Update img item err {0}'.format(err))

        # close db
        self.cursor.close()
        self.cnt.close()

    def get_media_requests(self, item, info):
        self.connect_to_db()
        if not self.exist_item_images_downloaded(item):
            for image_url in item['image_urls']:
                yield scrapy.Request(image_url)

    def item_completed(self, results, item, info):
        self.update_img_item(item)
        image_paths = [x['path'] for ok, x in results if ok]
        if not image_paths:
            raise DropItem('Item contains no images')
        item['image_paths'] = image_paths
        return item

class MeiziPipeline(object):
    def process_item(self, item, spider):
        return item

class MeiziMysqlStorePipeline(object):
    def __init__(self):

        self.cfg = configparser.ConfigParser()
        self.cfg.read(os.path.abspath('./meizi/db.cfg'))
        self.cnt = mysql.connector.connect(
            user=self.cfg.get('connect', 'user'),
            password=self.cfg.get('connect', 'password'),
            host=self.cfg.get('connect', 'host'),
            database=self.cfg.get('connect', 'database')
        )
        self.cursor = self.cnt.cursor()

        self.INSERT_IMG_ITEM = (
            "INSERT INTO meizi_spider "
            "(page_num, img_url, img_md5, up_vote, down_vote) "
            "VALUES (%(page_num)s, %(img_url)s, %(img_md5)s, %(up_vote)s, %(down_vote)s)"
        )

        self.CHECK_EXISTED_ITEM = (
            "SELECT * FROM meizi_spider "
            "WHERE img_md5 = %s "
        )

        self.UPDATE_IMG_ITEM = (
            "UPDATE meizi_spider "
            "SET up_vote=%(up_vote)s, down_vote=%(down_vote)s "
            "WHERE img_md5=%(img_md5)s"
        )

    def insert_img_item(self, item):
        try:
            self.cursor.execute(
                self.INSERT_IMG_ITEM,
                dict(item)
            )
            self.cnt.commit()
        except mysql.connector.Error as err:
            print('Insert img item err {0}'.format(err))

    def update_img_item(self, item):
        try:
            self.cursor.execute(
                self.UPDATE_IMG_ITEM,
                dict(item)
            )
            self.cnt.commit()
        except mysql.connector.Error as err:
            print('Update img item err {0}'.format(err))

    def not_existed_item(self, item):
        self.cursor.execute(
            self.CHECK_EXISTED_ITEM,
            (item['img_md5'],)
        )
        row = self.cursor.fetchone()
        return row is None

    def process_item(self, item, spider):
        if self.not_existed_item(item):
            self.insert_img_item(item)
        else:
            self.update_img_item(item)
        return item

    def close_spider(self, spider):
        self.cursor.close()
        self.cnt.close()


text:
package io.github.tomykaira.uchronie.ui

import org.eclipse.jgit.diff.DiffEntry
import io.github.tomykaira.uchronie.GitRepository

sealed trait DiffDecorator {
  val name: String

  def fullDiff(repository: GitRepository): String
}

object DiffDecorator {
  case class Each(diff: DiffEntry) extends DiffDecorator {
    val name = diff.getChangeType match {
      case DiffEntry.ChangeType.ADD | DiffEntry.ChangeType.MODIFY => diff.getNewPath
      case DiffEntry.ChangeType.DELETE => diff.getOldPath
      case DiffEntry.ChangeType.COPY | DiffEntry.ChangeType.RENAME =>
        diff.getOldPath + " -> " + diff.getNewPath
    }

    def fullDiff(repository: GitRepository): String = {
      repository.formatDiff(diff)
    }
  }

  case class All(prefix: String, diffs: List[DiffEntry]) extends DiffDecorator {
    val name = s"ALL of $prefix"

    def fullDiff(repository: GitRepository): String = {
      diffs.map(DiffDecorator.Each(_).fullDiff(repository)).mkString("\n\n")
    }
  }
}


text:
package networking

import (
	"context"
	"fmt"
	g "github.com/onsi/ginkgo"
	t "github.com/onsi/ginkgo/extensions/table"
	o "github.com/onsi/gomega"
	exutil "github.com/openshift/origin/test/extended/util"
	kapiv1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/util/uuid"
	clientset "k8s.io/client-go/kubernetes"
	"k8s.io/client-go/rest"
	frameworkpod "k8s.io/kubernetes/test/e2e/framework/pod"
	"strings"
	"time"
)

const (
	tuningNADName = "tuningnad"
	baseNAD       = "basenad"
	podName       = "testpod"

	sysctlKey  = "net.ipv4.conf.IFNAME.send_redirects"
	sysctlPath = "/proc/sys/net/ipv4/conf/%s/send_redirects"
)

type SysctlVariant struct {
	Sysctl string
	Value  string
	Path   string
}

var whitelistedSysctls = []SysctlVariant{
	{Sysctl: "net.ipv4.conf.IFNAME.send_redirects", Value: "1", Path: "/proc/sys/net/ipv4/conf/net1/send_redirects"},
	{Sysctl: "net.ipv4.conf.IFNAME.accept_source_route", Value: "1", Path: "/proc/sys/net/ipv4/conf/net1/accept_source_route"},
	{Sysctl: "net.ipv6.conf.IFNAME.accept_source_route", Value: "1", Path: "/proc/sys/net/ipv6/conf/net1/accept_source_route"},
	{Sysctl: "net.ipv4.conf.IFNAME.accept_redirects", Value: "1", Path: "/proc/sys/net/ipv4/conf/net1/accept_redirects"},
	{Sysctl: "net.ipv6.conf.IFNAME.accept_redirects", Value: "1", Path: "/proc/sys/net/ipv6/conf/net1/accept_redirects"},
	{Sysctl: "net.ipv4.conf.IFNAME.secure_redirects", Value: "1", Path: "/proc/sys/net/ipv4/conf/net1/secure_redirects"},
	// Bumping the net.ipv6 values for Multiarch until https://bugzilla.redhat.com/show_bug.cgi?id=2082239 is fixed in RHEL
	// uncomment the following two lines once the bug is fixed.
	// {Sysctl: "net.ipv6.neigh.IFNAME.base_reachable_time_ms", Value: "30010", Path: "/proc/sys/net/ipv6/neigh/net1/base_reachable_time_ms"},
	// {Sysctl: "net.ipv6.neigh.IFNAME.retrans_time_ms", Value: "1010", Path: "/proc/sys/net/ipv6/neigh/net1/retrans_time_ms"},
}

// getPodNodeName returns the name of the node the pod is scheduled on
func getPodNodeName(client clientset.Interface, namespace, name string) string {
	pod, err := client.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{})
	o.Expect(err).NotTo(o.HaveOccurred(), "unable get running pod")
	o.Expect(pod.Spec.NodeName).NotTo(o.BeEmpty(), "expected scheduled pod but found empty Spec.NodeName")
	return pod.Spec.NodeName
}

var _ = g.Describe("[sig-network][Feature:tuning]", func() {
	oc := exutil.NewCLI("tuning")
	f := oc.KubeFramework()

	g.It("pod should start with all sysctl on whitelist", func() {
		namespace := f.Namespace.Name
		sysctls := map[string]string{}
		for _, sysctl := range whitelistedSysctls {
			sysctls[sysctl.Sysctl] = sysctl.Value
		}
		err := createTuningNAD(oc.AdminConfig(), namespace, tuningNADName, sysctls)
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to create network-attachment-definition")

		exutil.CreateExecPodOrFail(f.ClientSet, namespace, podName, func(pod *kapiv1.Pod) {
			pod.ObjectMeta.Annotations = map[string]string{"k8s.v1.cni.cncf.io/networks": fmt.Sprintf("%s/%s", namespace, tuningNADName)}
		})
		for _, sysctl := range whitelistedSysctls {
			result, err := oc.AsAdmin().Run("exec").Args(podName, "-n", namespace, "--", "cat", sysctl.Path).Output()
			o.Expect(err).NotTo(o.HaveOccurred(), "error checking pod sysctl")
			o.Expect(result).To(o.Equal(sysctl.Value), "incorrect sysctl value")
		}
	})
	t.DescribeTable("pod should not start for sysctls not on whitelist", func(sysctl, value string) {
		namespace := f.Namespace.Name
		tuningNADName := "tuningnadwithdisallowedsysctls"
		err := createTuningNAD(oc.AdminConfig(), namespace, tuningNADName, map[string]string{sysctl: value})
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to create network-attachment-definition")

		podDefinition := frameworkpod.NewAgnhostPod(namespace, podName, nil, nil, nil)
		podDefinition.ObjectMeta.Annotations = map[string]string{"k8s.v1.cni.cncf.io/networks": fmt.Sprintf("%s/%s", namespace, tuningNADName)}
		pod := f.PodClient().Create(podDefinition)
		err = frameworkpod.WaitForPodCondition(f.ClientSet, namespace, pod.Name, "Failed", 30*time.Second, func(pod *kapiv1.Pod) (bool, error) {
			if pod.Status.Phase == kapiv1.PodPending {
				return true, nil
			}
			return false, nil
		})
		o.Expect(err).NotTo(o.HaveOccurred(), "incorrect pod status")
		o.Consistently(func() kapiv1.PodPhase {
			pod, err := f.ClientSet.CoreV1().Pods(namespace).Get(context.Background(), podName, metav1.GetOptions{})
			o.Expect(err).ToNot(o.HaveOccurred())
			return pod.Status.Phase
		}, 15*time.Second, 3*time.Second).Should(o.Equal(kapiv1.PodPending))
	},
		t.Entry("net.ipv4.conf.all.send_redirects", "net.ipv4.conf.all.send_redirects", "1"),
		t.Entry("net.ipv4.conf.IFNAME.arp_filter", "net.ipv4.conf.IFNAME.arp_filter", "1"),
	)

	g.It("pod sysctls should not affect node", func() {
		namespace := f.Namespace.Name
		g.By("creating a preexisting pod to check host sysctl")
		nodePod := frameworkpod.CreateExecPodOrFail(f.ClientSet, f.Namespace.Name, "nodeaccess-pod-", func(pod *kapiv1.Pod) {
			pod.Spec.Volumes = []kapiv1.Volume{
				{Name: "sysvolume", VolumeSource: kapiv1.VolumeSource{HostPath: &kapiv1.HostPathVolumeSource{Path: "/sys/class/net"}}},
				{Name: "procvolume", VolumeSource: kapiv1.VolumeSource{HostPath: &kapiv1.HostPathVolumeSource{Path: "/proc"}}},
			}
			pod.Spec.Containers[0].VolumeMounts = []kapiv1.VolumeMount{{Name: "sysvolume", MountPath: "/host/net"}, {Name: "procvolume", MountPath: "/host/proc"}}
			pod.Spec.HostNetwork = true
		})
		testNodeName := getPodNodeName(f.ClientSet, nodePod.Namespace, nodePod.Name)

		const baseNADName string = "basenad"
		const basePodName string = "basepod"
		hostIfName := strings.ReplaceAll(string(uuid.NewUUID()), "-", "")[:14]

		g.By("creating a first network-attachment-definition with a unique host interface name")
		err := createTuningNADWithBridgeName(oc.AdminConfig(), namespace, baseNADName, hostIfName, nil)
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to create first network-attachment-definition")

		g.By("creating a pod using the first network-attachment-definition to ensure the host interface exists")
		exutil.CreateExecPodOrFail(f.ClientSet, namespace, basePodName, func(pod *kapiv1.Pod) {
			pod.ObjectMeta.Annotations = map[string]string{"k8s.v1.cni.cncf.io/networks": fmt.Sprintf("%s/%s", namespace, baseNADName)}
			pod.Spec.NodeName = testNodeName
		})

		g.By("getting the value of the host interface sysctl")
		hostSysctlValue, err := oc.AsAdmin().Run("exec").Args(nodePod.Name, "--", "cat", "/host"+fmt.Sprintf(sysctlPath, hostIfName)).Output()
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to check sysctl")
		sysctlValue := "1"
		if sysctlValue == hostSysctlValue {
			sysctlValue = "0"
		}

		g.By("creating a second network-attachment-definition with sysctl of value other than the host sysctl value")
		testIfName := strings.ReplaceAll(string(uuid.NewUUID()), "-", "")[:14]
		err = createTuningNADWithBridgeName(oc.AdminConfig(), namespace, tuningNADName, testIfName, map[string]string{sysctlKey: sysctlValue})
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to create second network-attachment-definition")

		g.By("creating a pod with the same interface name as the host with a sysctl set")
		exutil.CreateExecPodOrFail(f.ClientSet, namespace, podName, func(pod *kapiv1.Pod) {
			pod.ObjectMeta.Annotations = map[string]string{"k8s.v1.cni.cncf.io/networks": fmt.Sprintf("%s/%s@%s", namespace, tuningNADName, hostIfName)}
			pod.Spec.NodeName = testNodeName
		})

		g.By("checking the value of the sysctl on the pod is that specified in the network-attachment-defintion")
		result, err := oc.AsAdmin().Run("exec").Args(podName, "-n", namespace, "--", "cat", fmt.Sprintf(sysctlPath, hostIfName)).Output()
		o.Expect(err).NotTo(o.HaveOccurred(), "error checking pod sysctls")
		o.Expect(result).To(o.Equal(sysctlValue), "incorrect sysctl value")

		g.By("checking that the value of the node sysctl did not change")
		hostSysctlValue2, err := oc.AsAdmin().Run("exec").Args(nodePod.Name, "--", "cat", "/host"+fmt.Sprintf(sysctlPath, hostIfName)).Output()
		o.Expect(err).NotTo(o.HaveOccurred(), "error checking pod sysctls")
		o.Expect(hostSysctlValue).Should(o.Equal(hostSysctlValue2))
	})

	g.It("pod sysctl should not affect existing pods", func() {
		namespace := f.Namespace.Name
		path := fmt.Sprintf(sysctlPath, "net1")
		err := createNAD(oc.AdminConfig(), namespace, baseNAD)
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to create network-attachment-definition")

		previousPod := frameworkpod.CreateExecPodOrFail(f.ClientSet, f.Namespace.Name, "previous-pod-", func(pod *kapiv1.Pod) {
			pod.ObjectMeta.Annotations = map[string]string{"k8s.v1.cni.cncf.io/networks": fmt.Sprintf("%s/%s", namespace, baseNAD)}
		})
		testNodeName := getPodNodeName(f.ClientSet, previousPod.Namespace, previousPod.Name)

		podOutputBeforeSysctlAplied, err := oc.AsAdmin().Run("exec").Args(previousPod.Name, "--", "cat", path).Output()
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to check sysctl value")
		sysctlValue := "1"
		if sysctlValue == podOutputBeforeSysctlAplied {
			sysctlValue = "0"
		}
		err = createTuningNAD(oc.AdminConfig(), namespace, tuningNADName, map[string]string{sysctlKey: sysctlValue})
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to create network-attachment-definition")

		exutil.CreateExecPodOrFail(f.ClientSet, namespace, podName, func(pod *kapiv1.Pod) {
			pod.ObjectMeta.Annotations = map[string]string{"k8s.v1.cni.cncf.io/networks": fmt.Sprintf("%s/%s", namespace, tuningNADName)}
			pod.Spec.NodeName = testNodeName
		})
		podOutputAfterSysctlAplied, err := oc.AsAdmin().Run("exec").Args(previousPod.Name, "--", "cat", path).Output()
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to check sysctl value")
		o.Expect(podOutputBeforeSysctlAplied).Should(o.Equal(podOutputAfterSysctlAplied))
	})

	g.It("pod sysctl should not affect newly created pods", func() {
		namespace := f.Namespace.Name
		path := fmt.Sprintf(sysctlPath, "net1")

		err := createNAD(oc.AdminConfig(), namespace, baseNAD)
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to create network-attachment-definition")

		previousPod := frameworkpod.CreateExecPodOrFail(f.ClientSet, f.Namespace.Name, "sysctl-pod-", func(pod *kapiv1.Pod) {
			pod.ObjectMeta.Annotations = map[string]string{"k8s.v1.cni.cncf.io/networks": fmt.Sprintf("%s/%s", namespace, baseNAD)}
		})
		testNodeName := getPodNodeName(f.ClientSet, previousPod.Namespace, previousPod.Name)

		podOutputBeforeSysctlAplied, err := oc.AsAdmin().Run("exec").Args(previousPod.Name, "--", "cat", path).Output()
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to check sysctl value")
		sysctlValue := "1"
		if sysctlValue == podOutputBeforeSysctlAplied {
			sysctlValue = "0"
		}
		err = createTuningNAD(oc.AdminConfig(), namespace, tuningNADName, map[string]string{sysctlKey: sysctlValue})
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to create network-attachment-definition")

		exutil.CreateExecPodOrFail(f.ClientSet, namespace, podName, func(pod *kapiv1.Pod) {
			pod.ObjectMeta.Annotations = map[string]string{"k8s.v1.cni.cncf.io/networks": fmt.Sprintf("%s/%s", namespace, tuningNADName)}
			pod.Spec.NodeName = testNodeName

		})
		podOutputAfterSysctlAplied, err := oc.AsAdmin().Run("exec").Args(previousPod.Name, "--", "cat", path).Output()
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to check sysctl value")
		o.Expect(podOutputBeforeSysctlAplied).Should(o.Equal(podOutputAfterSysctlAplied))

		nextPod := frameworkpod.CreateExecPodOrFail(f.ClientSet, f.Namespace.Name, "sysctl-pod-", func(pod *kapiv1.Pod) {
			pod.ObjectMeta.Annotations = map[string]string{"k8s.v1.cni.cncf.io/networks": fmt.Sprintf("%s/%s", namespace, baseNAD)}
			pod.Spec.NodeName = testNodeName
		})
		podOutput, err := oc.AsAdmin().Run("exec").Args(nextPod.Name, "--", "cat", path).Output()
		o.Expect(err).NotTo(o.HaveOccurred(), "unable to check sysctl value")
		o.Expect(podOutput).Should(o.Equal(podOutputBeforeSysctlAplied))
	})

})

func createNAD(config *rest.Config, namespace string, nadName string) error {
	return createTuningNAD(config, namespace, nadName, nil)
}

func createTuningNAD(config *rest.Config, namespace, nadName string, sysctls map[string]string) error {
	return createTuningNADWithBridgeName(config, namespace, nadName, "tunbr", sysctls)
}

func createTuningNADWithBridgeName(config *rest.Config, namespace, nadName, bridgeName string, sysctls map[string]string) error {
	sysctlString := ""
	for sysctl, value := range sysctls {
		if len(sysctlString) > 0 {
			sysctlString = sysctlString + ","
		}
		sysctlString = sysctlString + fmt.Sprintf("\"%s\":\"%s\"", sysctl, value)
	}
	if len(sysctlString) > 0 {
		sysctlString = fmt.Sprintf(`,{"type":"tuning","sysctl":{%s}}`, sysctlString)
	}
	nadConfig := fmt.Sprintf(`{"cniVersion":"0.4.0","name":"%s","plugins":[{"type":"bridge","bridge":"%s","ipam":{"type":"static","addresses":[{"address":"10.10.0.1/24"}]}}%s]}`, nadName, bridgeName, sysctlString)
	return createNetworkAttachmentDefinition(config, namespace, nadName, nadConfig)
}


text:
package com.soen341.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.soen341.dao.ScheduleDao;
import com.soen341.model.Constraints;
import com.soen341.model.Course;
import com.soen341.model.Prerequisite;
import com.soen341.model.Schedule;
import com.soen341.model.ScheduleMember;
import com.soen341.model.Section;
import com.soen341.model.TimeConstraint;
import com.soen341.model.TimeSlot;
import com.soen341.model.enums.DayOfWeekEnum;
import com.soen341.service.RegHistoryService;
import com.soen341.service.ScheduleService;

//--------------------------------------------------------------------------------------------------------------------------------
/**
* Provides implementation for services around the {@link Schedule} entity.
*/
//--------------------------------------------------------------------------------------------------------------------------------
@Service("scheduleService")
public class ScheduleServiceImpl implements ScheduleService
{
	@Autowired
	private RegHistoryService regHistoryService;

	@Autowired
	private ScheduleDao scheduleDao;

	//--------------------------------------------------------------------------------------------------------------------------------
	/**
	 * Saves Schedule object to the database.
	 */
	//--------------------------------------------------------------------------------------------------------------------------------
	public Boolean saveSchedule(Schedule schedule)
	{
		return scheduleDao.saveSchedule(schedule);
	}

	//--------------------------------------------------------------------------------------------------------------------------------
	/**
	 * Retrieves Schedule object from the temporary table (ScheduleTemp) by specified scheduleId
	 * and permanently saves it to the database.
	 */
	//--------------------------------------------------------------------------------------------------------------------------------
	public Boolean saveScheduleById(String scheduleId)
	{
		// TODO Auto-generated method stub
		return null;
	}

	//--------------------------------------------------------------------------------------------------------------------------------
	/**
	 * Returns Schedule object by specified scheduleId.
	 */
	//--------------------------------------------------------------------------------------------------------------------------------
	public Schedule getScheduleById(String scheduleId)
	{
		// TODO Auto-generated method stub
		return scheduleDao.getById(scheduleId);
	}

	//--------------------------------------------------------------------------------------------------------------------------------
	/**
	 * 	Returns a list of generated schedules for specified time ranges and credit limits.
	 */
	//--------------------------------------------------------------------------------------------------------------------------------

	 /* DONE:
	 * 	Create empty schedule and assign it an ID.
	 * 	Create an empty set of schedule members (member = course name + section)
	 * 	Get the list of courses available for registration
	 * 	For each course in the list do:
	 * 		if prerequisites completed and total # of credits < limit (default=18) and less than 6 courses taken)
	 * 			for each course section do:
	 * 				for each section TimeSlot do:
	 * 					if section time slots (lecture, tutorial and lab) match constraint times
	 * 						- add course to the Schedule
	 * 						- modify constraint times accordingly
	 * Save Schedule in the database
	 * Return Schedule object
	 *
	 * TODO: Generate multiple schedules
	 */

	public Set<Schedule> generateSchedule(Constraints userConstraints)
	{
		/*
		* schedule generator logic is located here
		*/

		Set<Schedule> schedules = new HashSet<Schedule>();

		//create Schedule
		Schedule schedule = new Schedule();
		schedule.setId("Sch01Sem01");		// should be revised if multiple schedules are generated
		schedule.setStudentId(userConstraints.getStudentId());

		// create new set of schedule members
		List<ScheduleMember> scheduleMembers = new ArrayList<ScheduleMember>();

		// create a copy of constraints object (constraintsClone)
		Constraints constraints = new Constraints(userConstraints);

		// get a list of available courses from the sequence
		List<Course> availableCourses = regHistoryService.getAvailable(constraints.getStudentId());

		Double credits = 0.00;			// amount of accumulated credits (specified by the user)
		Integer numberOfCourses = 0;	// course counter (6 courses limit)

		// for each course in the list of available courses do:
		for (Course availableCourse : availableCourses)
		{
			if (isPrerequsitesCompleted(availableCourse, availableCourses) &&
				(credits < constraints.getMaxCredits()) &&
				(numberOfCourses < 6))
			{
				// get a set of sections for the course
				Set<Section> courseSections = availableCourse.getSections();

				// for each section in the set of sections do:
				for (Section section : courseSections)
				{
					// verify if section time slots match constrains times
					List<TimeSlot> timeSlots = matchTimeSlots(section, constraints);

					// if matched, add course to the schedule and update constraints
					if (timeSlots != null)
					{
						credits =+ availableCourse.getCredits();
						numberOfCourses++;

						// create schedule member
						ScheduleMember scheduleMember = new ScheduleMember();
						scheduleMember.setScheduleMemberId(availableCourse.getCourseId().toString() + "Mem");
						scheduleMember.setCourseCode(availableCourse.getCourseId());
						scheduleMember.setSection(section);
						scheduleMember.setSchedule(schedule);

						// add schedule member to the set
						scheduleMembers.add(scheduleMember);

						// add set of schedule members to the schedule
						schedule.setScheduleMembers(scheduleMembers);

						updateTimeConstraints(timeSlots, constraints);

						// leave the loop and proceed with the next course
						break;
					}
				}
			}
		}

		// add schedule to the list of schedules
		schedules.add(schedule);

		// save schedule
		saveSchedule(schedule);

		return schedules;
	}

	// --------------------------------------------------------------------------------------------------------------------------------
	// Helper methods.
	// --------------------------------------------------------------------------------------------------------------------------------

	// -------------------------------------------------------------------------------
	/**
	 * 	Returns true if all prerequisites for the specified course were completed.
	 */
	// -------------------------------------------------------------------------------
	private Boolean isPrerequsitesCompleted(Course availableCourse, List<Course> avalableCourses)
	{
		// get prerequisites for the given course
		Set<Prerequisite> prerequisites = availableCourse.getPrerequisites();

		// verify if prerequisite is not in the list of available courses
		for (Prerequisite prerequisite : prerequisites)
		{
			String prerequisiteCode = prerequisite.getPrerequisiteId();
			for (Course course : avalableCourses)
				if (prerequisiteCode.equals(course.getCourseId()))
					return false;
		}

		return true;
	}

	// -------------------------------------------------------------------------------
	/**
	 * 	Returns a list of TimeSlots (lecture, tutorial, lab)
	 *	if they match time constraints. Return null otherwise.
	 */
	// -------------------------------------------------------------------------------
	private List<TimeSlot> matchTimeSlots(Section section, Constraints constraints)
	{
		List<TimeSlot> timeSlots = section.getTimeSlots();
		List<TimeConstraint> timeConstraints = constraints.getTimeConstraints();

		// for each TimeSlot in the section verify if it matches at least one time constraint
		for (TimeSlot timeSlot : timeSlots)
		{
			// match flag
			boolean timeSlotMatch = false;

			for (TimeConstraint timeConstraint : timeConstraints)
			{
				// get constraint date parameters

				DayOfWeekEnum constraintWeekDay = timeConstraint.getDayOfWeek();
				Date constraintStartTime = stringToDate(timeConstraint.getStartTime());
				Date constraintEndTime = stringToDate(timeConstraint.getEndTime());

				// get TimeSlot date parameters

				DayOfWeekEnum timeslotWeekDay = timeSlot.getDayOfWeek();
				Date timeslotStartTime = timeSlot.getStartTime();
				Date timeslotEndTime = timeSlot.getEndTime();

				// compare TimeSlot and constraint weekdays and times

				if (timeslotWeekDay.equals(constraintWeekDay) &&
					(timeslotStartTime.compareTo(constraintStartTime) >= 0) &&
					(timeslotEndTime.compareTo(constraintEndTime) <= 0))
				{
					// upon match update status flag and break the loop to verify next timeSlot;
					timeSlotMatch = true;
					break;
				}
			}

			// bail out if no match is found for at least one TimeSlot

			if (!timeSlotMatch)
				return null;
		}

		return timeSlots;
	}

	// -------------------------------------------------------------------------------
	/**
	 * Updates constraint time ranges according to reserved TimeSlot.
	 */
	// -------------------------------------------------------------------------------
	private void updateTimeConstraints(List<TimeSlot> timeSlots, Constraints constraints)
	{
		// get a list of time constraints
		List<TimeConstraint> timeConstraints = constraints.getTimeConstraints();

		for (TimeSlot timeSlot : timeSlots)
		{
			// create new list for updated time constraints to avoid modifying collection that is being iterated
			List<TimeConstraint> updatedTimeConstraints = new ArrayList<TimeConstraint>();

			DayOfWeekEnum slotDay = timeSlot.getDayOfWeek();
			Date slotStart = timeSlot.getStartTime();
			Date slotEnd = timeSlot.getEndTime();

			for (TimeConstraint timeConstraint : timeConstraints)
			{
				DayOfWeekEnum constraintDay = timeConstraint.getDayOfWeek();
				Date constraintStart = stringToDate(timeConstraint.getStartTime());
				Date constraintEnd = stringToDate(timeConstraint.getEndTime());

				if (slotDay.equals(constraintDay))
				{
					if (slotStart.equals(constraintStart) && slotEnd.equals(constraintEnd))
						timeConstraints.remove(timeConstraint);

					else if (slotStart.equals(constraintStart) && slotEnd.before(constraintEnd))
					{
						timeConstraint.setStartTime(dateToString(slotEnd));
						updatedTimeConstraints.add(timeConstraint);
					}

					else if (slotStart.after(constraintStart) && slotEnd.equals(constraintEnd))
					{
						timeConstraint.setEndTime(dateToString(slotStart));
						updatedTimeConstraints.add(timeConstraint);
					}

					else
					{
						timeConstraint.setEndTime(dateToString(slotStart));
						updatedTimeConstraints.add(timeConstraint);

						TimeConstraint splitConstraint = new TimeConstraint();
						splitConstraint.setDayOfWeek(constraintDay);
						splitConstraint.setStartTime(dateToString(slotEnd));
						splitConstraint.setEndTime(dateToString(constraintEnd));

						updatedTimeConstraints.add(splitConstraint);
					}
				}
				else
					updatedTimeConstraints.add(timeConstraint);
			}

			timeConstraints = updatedTimeConstraints;
		}
	}

	// -------------------------------------------------------------------------------
	/**
	 * Converts time String to the Date object
	 */
	// -------------------------------------------------------------------------------
	public static Date stringToDate(String time)
	{
		DateFormat timeConverter = new SimpleDateFormat("HH:mm:ss");

		Date date = null;
		try
		{
			date = timeConverter.parse(time);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return date;
	}

	// -------------------------------------------------------------------------------
	/**
	 * Converts Date to String
	 */
	// -------------------------------------------------------------------------------
	public static String dateToString(Date time)
	{
		SimpleDateFormat form = new SimpleDateFormat("HH:mm:ss");
		return form.format(time);
	}
}


text:
package org.apache.camel.dataformat.bindy.csv;

import java.util.List;

import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration
public class BindyComplexCsvUnmarshallTest extends AbstractJUnit4SpringContextTests {

    private static final Class<?> TYPE = org.apache.camel.dataformat.bindy.model.complex.twoclassesandonelink.Order.class;

    @Produce("direct:start")
    protected ProducerTemplate template;

    private String record = "01,,Albert,Cartier,ISIN,BE12345678,SELL,,1500,EUR,08-01-2009\r\n" + "02,A1,,Preud'Homme,ISIN,XD12345678,BUY,,2500,USD,08-01-2009\r\n"
                            + "03,A2,Jacques,,,BE12345678,SELL,,1500,EUR,08-01-2009\r\n" + "04,A3,Michel,Dupond,,,BUY,,2500,USD,08-01-2009\r\n"
                            + "05,A4,Annie,Dutronc,ISIN,BE12345678,,,1500,EUR,08-01-2009\r\n" + "06,A5,Andr" + "\uc3a9" + ",Rieux,ISIN,XD12345678,SELL,Share,,USD,08-01-2009\r\n"
                            + "07,A6,Myl" + "\uc3a8" + "ne,Farmer,ISIN,BE12345678,BUY,1500,,,08-01-2009\r\n" + "08,A7,Eva,Longoria,ISIN,XD12345678,SELL,Share,2500,USD,\r\n"
                            + ",,,D,,BE12345678,SELL,,,,08-01-2009\r\n" + ",,,D,ISIN,BE12345678,,,,,08-01-2009\r\n" + ",,,D,ISIN,LU123456789,,,,,\r\n"
                            + "10,A8,Pauline,M,ISIN,XD12345678,SELL,Share,2500,USD,08-01-2009\r\n" + "10,A9,Pauline,M,ISIN,XD12345678,BUY,Share,2500.45,USD,08-01-2009";

    private String singleRecord = "01,,Albert,Cartier,ISIN,BE12345678,SELL,,1500,EUR,08-01-2009";

    @EndpointInject("mock:result")
    private MockEndpoint resultEndpoint;

    @Test
    public void testUnMarshallMessage() throws Exception {
        resultEndpoint.expectedMessageCount(1);
        resultEndpoint.message(0).body().isInstanceOf(List.class);

        template.sendBody(record);

        resultEndpoint.assertIsSatisfied();

        // there should be 13 element in the list
        List list = resultEndpoint.getReceivedExchanges().get(0).getIn().getBody(List.class);
        Assert.assertEquals(13, list.size());

        resultEndpoint.reset();

        // now single test
        resultEndpoint.expectedMessageCount(1);
        resultEndpoint.message(0).body().isInstanceOf(TYPE);

        template.sendBody(singleRecord);

        resultEndpoint.assertIsSatisfied();
    }

    public static class ContextConfig extends RouteBuilder {
        @Override
        public void configure() {
            from("direct:start")
                .unmarshal(new BindyCsvDataFormat(TYPE))
                .to("mock:result");
        }
    }
}


text:
<!DOCTYPE html>

    <meta charset="UTF-8">
    <meta name="dpl-info" content="status=plan">
    <title>悬停效果</title>

            A


text: DBMS project - Furnitures store - fully functional


text:
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsDownloader.h"
#include "nsICachingChannel.h"
#include "nsIInputStream.h"
#include "nsDirectoryServiceUtils.h"
#include "nsDirectoryServiceDefs.h"
#include "nsNetUtil.h"
#include "nsCRTGlue.h"

nsDownloader::~nsDownloader()
{
    if (mLocation && mLocationIsTemp) {
        // release the sink first since it may still hold an open file
        // descriptor to mLocation.  this needs to happen before the
        // file can be removed otherwise the Remove call will fail.
        if (mSink) {
            mSink->Close();
            mSink = nullptr;
        }

        nsresult rv = mLocation->Remove(false);
        if (NS_FAILED(rv))
            NS_ERROR("unable to remove temp file");
    }
}

NS_IMPL_ISUPPORTS3(nsDownloader,
                   nsIDownloader,
                   nsIStreamListener,
                   nsIRequestObserver)

NS_IMETHODIMP
nsDownloader::Init(nsIDownloadObserver *observer, nsIFile *location)
{
    mObserver = observer;
    mLocation = location;
    return NS_OK;
}

NS_IMETHODIMP
nsDownloader::OnStartRequest(nsIRequest *request, nsISupports *ctxt)
{
    nsresult rv = NS_ERROR_FAILURE;
    if (!mLocation) {
        nsCOMPtr<nsICachingChannel> caching = do_QueryInterface(request, &rv);
        if (NS_SUCCEEDED(rv))
            rv = caching->SetCacheAsFile(true);
    }
    if (NS_FAILED(rv)) {
        // OK, we will need to stream the data to disk ourselves.  Make
        // sure mLocation exists.
        if (!mLocation) {
            rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mLocation));
            if (NS_FAILED(rv)) return rv;

            char buf[13];
            NS_MakeRandomString(buf, 8);
            memcpy(buf+8, ".tmp", 5);
            rv = mLocation->AppendNative(nsDependentCString(buf, 12));
            if (NS_FAILED(rv)) return rv;

            rv = mLocation->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
            if (NS_FAILED(rv)) return rv;

            mLocationIsTemp = true;
        }

        rv = NS_NewLocalFileOutputStream(getter_AddRefs(mSink), mLocation);
        if (NS_FAILED(rv)) return rv;

        // we could wrap this output stream with a buffered output stream,
        // but it shouldn't be necessary since we will be writing large
        // chunks given to us via OnDataAvailable.
    }
    return rv;
}

NS_IMETHODIMP
nsDownloader::OnStopRequest(nsIRequest  *request,
                            nsISupports *ctxt,
                            nsresult     status)
{
    if (!mSink && NS_SUCCEEDED(status)) {
        nsCOMPtr<nsICachingChannel> caching = do_QueryInterface(request, &status);
        if (NS_SUCCEEDED(status)) {
            status = caching->GetCacheFile(getter_AddRefs(mLocation));
            if (NS_SUCCEEDED(status)) {
                NS_ASSERTION(mLocation, "success without a cache file");
                // ok, then we need to hold a reference to the cache token in
                // order to ensure that the cache file remains valid until we
                // get destroyed.
                caching->GetCacheToken(getter_AddRefs(mCacheToken));
            }
        }
    }
    else if (mSink) {
        mSink->Close();
        mSink = nullptr;
    }

    mObserver->OnDownloadComplete(this, request, ctxt, status, mLocation);
    mObserver = nullptr;

    return NS_OK;
}

NS_METHOD
nsDownloader::ConsumeData(nsIInputStream* in,
                          void* closure,
                          const char* fromRawSegment,
                          uint32_t toOffset,
                          uint32_t count,
                          uint32_t *writeCount)
{
    nsDownloader *self = (nsDownloader *) closure;
    if (self->mSink)
        return self->mSink->Write(fromRawSegment, count, writeCount);

    *writeCount = count;
    return NS_OK;
}

NS_IMETHODIMP
nsDownloader::OnDataAvailable(nsIRequest *request, nsISupports *ctxt,
                              nsIInputStream *inStr,
                              uint64_t sourceOffset, uint32_t count)
{
    uint32_t n;
    return inStr->ReadSegments(ConsumeData, this, count, &n);
}


text:
import GraphQL from 'graphql';
import { Node } from './node.js';
import { Entity } from './entity.js';
import {
  id,
  mbid,
  title,
  disambiguation,
  fieldWithID,
  connectionWithExtras,
  linkedQuery,
} from './helpers.js';
import { aliases } from './alias.js';
import { artists } from './artist.js';
import { collections } from './collection.js';
import { rating } from './rating.js';
import { relationships } from './relationship.js';
import { tags } from './tag.js';

const { GraphQLObjectType, GraphQLString, GraphQLList } = GraphQL;

export const Work = new GraphQLObjectType({
  name: 'Work',
  description: `A [work](https://musicbrainz.org/doc/Work) is a distinct
intellectual or artistic creation, which can be expressed in the form of one or
more audio recordings.`,
  interfaces: () => [Node, Entity],
  fields: () => ({
    id,
    mbid,
    title,
    disambiguation,
    aliases,
    iswcs: {
      type: new GraphQLList(GraphQLString),
      description: `A list of [ISWCs](https://musicbrainz.org/doc/ISWC) assigned
to the work by copyright collecting agencies.`,
    },
    language: {
      type: GraphQLString,
      description: 'The language in which the work was originally written.',
    },
    ...fieldWithID('type', {
      description: 'The type of work.',
    }),
    artists,
    relationships,
    collections,
    rating,
    tags,
  }),
});

export const WorkConnection = connectionWithExtras(Work);

export const works = linkedQuery(WorkConnection);


text:
package org.apache.archiva.metadata.repository.stats.model;

import org.apache.archiva.metadata.model.MetadataFacet;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * Provides statistics data of metadata repositories.
 *
 * @since 2.3
 */
public interface RepositoryStatistics extends MetadataFacet
{
    String FACET_ID = "org.apache.archiva.metadata.repository.stats";

    String getRepositoryId( );

    Date getScanEndTime( );

    Date getScanStartTime( );

    long getTotalArtifactCount( );

    void setTotalArtifactCount( long totalArtifactCount );

    long getTotalArtifactFileSize( );

    void setTotalArtifactFileSize( long totalArtifactFileSize );

    long getTotalFileCount( );

    void setTotalFileCount( long totalFileCount );

    long getTotalGroupCount( );

    void setTotalGroupCount( long totalGroupCount );

    long getTotalProjectCount( );

    void setTotalProjectCount( long totalProjectCount );

    void setNewFileCount( long newFileCount );

    long getNewFileCount( );

    long getDuration( );

    /**
     * Statistics data by artifact type.
     *
     * @return A list of data keys and values
     */
    Map<String, Long> getTotalCountForType( );

    /**
     * Returns the value for the given artifact type.
     *
     * @param type The artifact type
     * @return The count value.
     */
    long getTotalCountForType( String type );

    /**
     * Sets the value for the given artifact type.
     * @param type The artifact type.
     * @param count The count value.
     */
    void setTotalCountForType( String type, long count );

    /**
     * Reads custom statistic values that are store implementation
     * specific.
     *
     * @param fieldName A unique field name.
     */
    long getCustomValue(String fieldName);

    /**
     * Saves custom statistic values that are store implementation
     * specific. The field name should be unique (e.g. prefixed by the
     * package name of the data provider).
     *
     * @param fieldName A unique field name.
     * @param count The statistic counter value
     */
    void setCustomValue(String fieldName, long count);

    /**
     * Returns the list of field names of custom values stored in this instance.
     * @return the list of stored custom values
     */
    List<String> getAvailableCustomValues();

}


text:
class CASW_Queen;

class CASW_Objective_Kill_Queen : public CASW_Objective
{
public:
	DECLARE_CLASS( CASW_Objective_Kill_Queen, CASW_Objective );
	DECLARE_SERVERCLASS();
	DECLARE_DATADESC();

	CASW_Objective_Kill_Queen();
	virtual ~CASW_Objective_Kill_Queen();

	virtual void Spawn();

	CNetworkHandle(CASW_Queen, m_hQueen);
	string_t	m_QueenName;
};

#endif // _INCLUDED_ASW_OBJECTIVE_KILL_QUEEN_H


text:
package org.sat4j.minisat.orders;

/**
 * @author leberre TODO To change the template for this generated type comment
 *         go to Window - Preferences - Java - Code Style - Code Templates
 */
public final class PureOrder extends VarOrderHeap {

    /**
     * Comment for serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    private int period;

    private int cpt;

    public PureOrder() {
        this(20);
    }

    public PureOrder(int p) {
        setPeriod(p);
    }

    public void setPeriod(int p) {
        this.period = p;
        this.cpt = this.period;
    }

    public int getPeriod() {
        return this.period;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.sat4j.minisat.core.VarOrder#select()
     */
    @Override
    public int select() {
        // wait period branching
        if (this.cpt < this.period) {
            this.cpt++;
        } else {
            // try to find a pure literal
            this.cpt = 0;
            int nblits = 2 * this.lits.nVars();
            for (int i = 2; i <= nblits; i++) {
                if (this.lits.isUnassigned(i)
                        && this.lits.watches(i).size() > 0
                        && this.lits.watches(i ^ 1).size() == 0) {
                    return i;
                }
            }
        }
        // not found: using normal order
        return super.select();
    }

    @Override
    public String toString() {
        return "tries to first branch on a single phase watched unassigned variable (pure literal if using a CB data structure) else VSIDS from MiniSAT"; //$NON-NLS-1$
    }
}


text:
using System.Collections.Generic;
using FileTape.ReadCursors;

namespace FileTape.PartitionsEnumeration
{
    public interface IPartitionsEnumerator
    {
        IEnumerable<IReadPartitionCursorProvider> GetCursors();
        string CreateFileNameForNextPartition();
    }
}


text:
module WalletsHelper
  def get_badge_details wallet
    days_since_updated = (Time.current.to_date - wallet.last_changed.to_date).to_i
    case days_since_updated
      when 0..1 then { icon: 'success', text: 'Fresh' }
      when 1..3 then { icon: 'warning', text: 'Aging' }
      else { icon: 'danger', text: 'Old' }
    end
  end
end


text:
Klau and Chris

the wedding of the year

and we are under construction

          Juan Carlos Rea - Copyright © 2016


text:
bool IsQuickTimeInstalled();

//---------------------------------------------------------------------

//******************************************************************************
//    TLevelWriterMov Proxy - delegates to a background 32-bit process
//******************************************************************************

class TLevelWriterMov : public TLevelWriter
{
	unsigned int m_id;

public:
	TLevelWriterMov(const TFilePath &path, TPropertyGroup *winfo);
	~TLevelWriterMov();

	void setFrameRate(double fps);

	TImageWriterP getFrameWriter(TFrameId fid);
	void save(const TImageP &img, int frameIndex);

	void saveSoundTrack(TSoundTrack *st);

public:
	static TLevelWriter *create(const TFilePath &f, TPropertyGroup *winfo)
	{
		return new TLevelWriterMov(f, winfo);
	};

	QLocalSocket *socket();
};

//******************************************************************************
//    TLevelReaderMov Proxy
//******************************************************************************

class TLevelReaderMov : public TLevelReader
{
	unsigned int m_id;
	int m_lx, m_ly;

public:
	TLevelReaderMov(const TFilePath &path);
	~TLevelReaderMov();
	TImageReaderP getFrameReader(TFrameId fid);
	TLevelP loadInfo();

	const TImageInfo *getImageInfo(TFrameId fid) { return m_info; }
	const TImageInfo *getImageInfo() { return m_info; }

	void enableRandomAccessRead(bool enable);
	void load(const TRasterP &rasP, int frameIndex, const TPoint &pos, int shrinkX = 1, int shrinkY = 1);
	TDimension getSize() const { return TDimension(m_lx, m_ly); }
	TRect getBBox() const { return TRect(0, 0, m_lx - 1, m_ly - 1); }

public:
	static TLevelReader *create(const TFilePath &f)
	{
		return new TLevelReaderMov(f);
	}
};

//===========================================================================

namespace Tiio
{
class MovWriterProperties : public TPropertyGroup
{
public:
	MovWriterProperties();
};
}

//===========================================================================

#endif //x64

#endif //TIIO_MOV_PROXY_H


text:
//
// This source code implements specifications defined by the Java
// Community Process. In order to remain compliant with the specification
// DO NOT add / change / or delete method signatures!
//

package javax.resource.spi;

import javax.resource.ResourceException;

/**
 * @since 1.5
 * @version $Rev$ $Date$
 */
public interface LazyEnlistableConnectionManager {
    void lazyEnlist(ManagedConnection mc) throws ResourceException;
}