<commit_before># -*- encoding: utf-8 -*-
'''
Requires http://pypi.python.org/pypi/icalendar/
easy_install icalendar
'''

import urllib, urllib2
from itertools import *

import simplejson
from icalendar import Calendar, Event

#from amara.tools.atomtools import feed
from akara.services import simple_service, response

SERVICE_ID = 'http://purl.org/akara/services/builtin/ical.json'
@simple_service('post', SERVICE_ID, 'ical.json', 'application/json')
def ical2json(body, ctype):
    '''
    Convert iCalendar info to Exhibit JSON
    (see: http://www.ibm.com/developerworks/web/library/wa-realweb6/ )

    Sample request:
    * curl --request POST --data-binary "@foo.ics" --header "Content-Type: text/calendar" "http://localhost:8880/ical.json"
    '''
    ids = set()
    entries = []
    cal = Calendar.from_string(body)
    #[ c['UID'] for c in  cal.subcomponents if c.name == 'VEVENT' ]
    for count, component in enumerate(cal.walk()):
        #if count > MAXRECORDS: break
        if component.name != 'VEVENT': continue
        entry = {}
        entry['summary'] = unicode(component['SUMMARY'])
        entry['label'] = entry['summary'] + '_' + str(count)
        entry['description'] = unicode(component['DESCRIPTION'])
        entry['start'] = component['DTSTART'].dt.isoformat()
        entry['end'] = component['DTEND'].dt.isoformat()
        entry['timestamp'] = component['DTSTAMP'].dt.isoformat()
        entry['url'] = component['URL']
        entry['id'] = unicode(component['UID'])
        entries.append(entry)
    return simplejson.dumps({'items': entries}, indent=4)


<commit_msg>Work around trac #6 - infinite loop problems with ImportError in top-level modules<commit_after># -*- encoding: utf-8 -*-
'''
Requires http://pypi.python.org/pypi/icalendar/
easy_install icalendar
'''

import urllib, urllib2
from itertools import *

import simplejson
# Top-level import errors cause an infinite loop problem (see trac #6)
# If this third-party package doesn't exist, report the problem but
# keep on going.
try:
    from icalendar import Calendar, Event
except ImportError, err:
    import warnings
    warnings.warn("Cannot import 'icalendar': %s" % (err,))
    Calendar = Event = None

from akara.services import simple_service, response

SERVICE_ID = 'http://purl.org/akara/services/builtin/ical.json'
@simple_service('post', SERVICE_ID, 'ical.json', 'application/json')
def ical2json(body, ctype):
    '''
    Convert iCalendar info to Exhibit JSON
    (see: http://www.ibm.com/developerworks/web/library/wa-realweb6/ )

    Sample request:
    * curl --request POST --data-binary "@foo.ics" --header "Content-Type: text/calendar" "http://localhost:8880/ical.json"
    '''
    ids = set()
    entries = []
    cal = Calendar.from_string(body)
    #[ c['UID'] for c in  cal.subcomponents if c.name == 'VEVENT' ]
    for count, component in enumerate(cal.walk()):
        #if count > MAXRECORDS: break
        if component.name != 'VEVENT': continue
        entry = {}
        entry['summary'] = unicode(component['SUMMARY'])
        entry['label'] = entry['summary'] + '_' + str(count)
        entry['description'] = unicode(component['DESCRIPTION'])
        entry['start'] = component['DTSTART'].dt.isoformat()
        entry['end'] = component['DTEND'].dt.isoformat()
        entry['timestamp'] = component['DTSTAMP'].dt.isoformat()
        entry['url'] = component['URL']
        entry['id'] = unicode(component['UID'])
        entries.append(entry)
    return simplejson.dumps({'items': entries}, indent=4)



{'fraction_non_alphanumeric': 0.1557817359344077, 'fraction_numerical': 0.005654509471303365, 'mean_word_length': 3.5534105534105533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1327484', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1074, 'n_words': 257}