news.py :  » Development » PyObjC » trunk » pyobjc » pyobjc-website » lib » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyObjC 
PyObjC » trunk » pyobjc » pyobjc website » lib » news.py
"""
Support for news items
"""
import datetime, os

from docutils.core import publish_parts

def newsSelection(listing, maxAge=90, minitems=5):
    """
    Returns the news selection for the home page, defaults to the last
    quarter and at least 5 news items.
    """
    result = []
    mindate = datetime.date.today() - datetime.timedelta(maxAge)
    mindate = mindate.strftime('%Y-%m-%d')
    for item in listing:
        if len(result) < minitems:
            result.append(item)

        elif item['date'] >= mindate:
            result.append(item)

        else:
            break

    return result

def parseNews(sourcedir):
    """
    Returns an array of news items, sorted by date (newest item first)

    Very item is a dictionary with these keys:

    * date: the date for this item (YYYY-mm-dd)

    * headline: the headline

    * body: text of the message (in HTML format)

    * basename: basefile name for the news item
    """
    result = []
    for fn in os.listdir(sourcedir):
        if not fn[0].isdigit():
            continue

        basename = os.path.splitext(fn)[0]

        if '_' in basename:
            date = fn.split('_')[0]

        else:
            date = fn

        assert date.isdigit() and len(date) == 8

        date = date[0:4] + '-' + date[4:6] + '-' + date[6:8]

        content = open(os.path.join(sourcedir, fn), 'r').read()


        parts = publish_parts(
            source=content,
            source_path=fn,
            writer_name='html',
            settings_overrides=dict(
                input_encoding='utf-8',
                initial_header_level=2,
            ))

        result.append(dict(
            basename = basename,
            date = date,
            headline = parts['title'],
            body = parts['body'],
        ))

    result.sort(key=lambda item: item['date'])
    result.reverse()
    return result


if __name__ == "__main__":
    print parseNews('../news')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.