fromIcal.py 36.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
#!/usr/bin/python
"""fromIcal.py -- interpret iCalendar data as RDF

USAGE:
  python fromIcal.py  [options]  foo.ics > foo.rdf

  options:
    --base uri
    --noprotocol    Supress SEQUENCE and DTSTAMP
    --noalarm       Supress VALARMs
    --x             include X- properties

REFERENCES

 Internet Calendaring and Scheduling Core
 Object Specification (iCalendar)
 November 1998
 http://www.ietf.org/rfc/rfc2445.txt
 http://www.w3.org/2002/12/cal/rfc2445
 http://www.w3.org/2002/12/cal/rfc2445.html


  NOTE: We don't claim to implement the whole spec, nor to even have
   read all of it. We're taking a data-driven, test-driven approach
   to RFC2445 coverage/conformance. We start with a .ics file that we
   understand (because it came from a tool that acts as we expect in
   response to it or some such) and we implement the parts of the
   spec necessary to grok the data in that file. As we work on more
   test files, we cover (and carefully read) more parts of the spec.

 Building an RDF model: A quick look at iCalendar
 http://www.w3.org/2000/01/foo
 TimBL 2000/10/02


  Python Style Guide
  Author: Guido van Rossum
  http://www.python.org/doc/essays/styleguide.html

TODO
  - RDF API in place of SAX?
  - rename fromIcal.py?
  
LICENSE

RDF Calendar Workspace: http://www.w3.org/2002/12/cal/

Copyright 2002-2003 World Wide Web Consortium, (Massachusetts
Institute of Technology, European Research Consortium for
Informatics and Mathematics, Keio University). All Rights
Reserved. This work is distributed under the W3C(R) Software License
  http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.


"""

__version__ = "$Id: fromIcal.py,v 2.33 2007/02/20 14:58:45 timbl Exp $"


from warnings import warn
import codecs, quopri


import XMLWriter
from icslex import unbreak, parseLine, unesc, recurlex


def main():
    import sys
    
    sx = XMLWriter.T(codecs.getwriter('utf-8')(sys.stdout))

    base = None
    suppressed = ['X-']
    while len(sys.argv) > 1:
	if sys.argv[1] == '--base':
	    base = sys.argv[2]
	    del sys.argv[1:3]
	elif sys.argv[1] == '--noprotocol':
	    suppressed = suppressed + [ 'SEQUENCE', 'DTSTAMP']
	    del sys.argv[1:2]
	elif sys.argv[1] == '--x':
	    del suppressed[0]
	elif sys.argv[1] == '--noalarm':
	    suppressed = suppressed + [ 'Valarm']
	    del sys.argv[1:2]
	elif sys.argv[1] == '--notimezone':
	    suppressed = suppressed + [ 'Vtimezone']
	    del sys.argv[1:2]
	elif sys.argv[1] == '--help':
	    print __doc__
	    return
	else:
	    break

    interpret(sx, codecs.open(sys.argv[1], 'r', 'utf-8'), base, suppressed)

    
class Namespace:
    def __init__(self, nsURI, names=()):
        self._n = nsURI
        self._names = names


    def bindAttr(self, pfx, attrs):
        if pfx:
            attrs['xmlns:%s' % pfx] = self._n
        else:
            attrs['xmlns'] = self._n

    def __getattr__(self, lname):
        if lname in self._names:
            return self._n+lname
        else:
            raise AttributeError, lname

    def sym(self, lname):
	return self._n + lname
        
RDF = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
iCalendar = Namespace('http://www.w3.org/2002/12/cal/icaltzd#',
                      ('dateTime', ))
XMLSchema = Namespace('http://www.w3.org/2001/XMLSchema#',
                      ('dateTime', 'date', 'double', 'integer', 'duration'))


def interpret(sx, fp, base=None, suppressed =[]):
    lines = unbreak(fp)
    n, p, v = parseLine(lines.next())

    if v != 'VCALENDAR':
	raise SyntaxError('Expected CALENDAR but found: %s' % (v))
    calendars = []
    findComponents(lines, v, calendars)
    
    attrs = {}
    RDF.bindAttr('rdf', attrs)
    iCalendar.bindAttr('', attrs)
    if base:
        attrs['xml:base'] = base


    sx.startElement('rdf:RDF', attrs)

    doComponents(sx, calendars, iCalendarDefs, suppressed = suppressed)

    sx.endElement('rdf:RDF')


#########
#
# Property Declarations:
# ICALTOKEN -> ('rdfname', 'DEFAULT-TYPE', minCardinality, maxCardinality)
#
# In theory, these could be derived from the rfc2445-formal schema, but
# they're copied by hand, so far. Reading from rfc2445-formal rather
# than from rfc2445.txt ensures that we have the relevant information
# formalized in machine-readable form, but also provides careful
# review where a mechanized translation might mask some bugs.
#
# The default types are keyed in on-demand as we encounter
# the properties in test data. Those that we haven't tested
# produce a RuntimeError, telling us to add the default type
# (and a test case for it!).
#
# Mappings to rdfname and DEFAULT-TYPE could be flattened,
# but mappings to cardinalities depend on the containing component.
# Cardinalities aren't used yet. (oops; forgot YouArentGonnaNeedIt)
#
#
# We represent symbolic values as strings, i.e.
#     :transp "OPAQUE";
# where the calendar test suite currently uses URIs, i.e.
#     :transp :opaque;
# Relevant code is marked with @@symbol.
#
# See also "How should I implement controlled vocabularies?"
# in http://esw.w3.org/topic/PropertiesForNaming
#  as of 2003-04-18 16:10:57
#

tzprop = {
    "DTSTART": ('dtstart', 'DATE-TIME', 1, 1),
    "TZOFFSETTO": ('tzoffsetto', 'TEXT', 1, 1),
    "TZOFFSETFROM": ('tzoffsetfrom', 'TEXT', 1, 1),
    
    
    "COMMENT": ('comment', 'TEXT', 0, None),
    "RDATE": ('rdate', 'DATE-TIME', 0, None),
    "RRULE": ('rrule', 'RECUR', 0, None),
    'EXDATE': (u'exdate', 'DATE-TIME', None, None),
    'RECURRENCE-ID': (u'recurrenceId', 'DATE-TIME', None, None),
    "TZNAME": ('tzname', 'TEXT', 0, None),
    }

ValarmDefs = ('Valarm',
              {"ACTION": ('action', 'TEXT', 0, None), #@@symbol
               "ATTACH": ('attach', 'URI', 0, None),
               "ATTENDEE": ('attendee', 'CAL-ADDRESS', 0, None),
               "DESCRIPTION": ('description', 'TEXT', 0, 1),
               "DURATION": ('duration', 'DURATION', 0, None),
               "REPEAT": ('repeat', 0, None),
               "SUMMARY": ('summary', "TEXT", 0, None),
               "TRIGGER": ('trigger', "DURATION", 0, None),
               },
              {})

iCalendarDefs = {'VCALENDAR':
                 ('Vcalendar',
                  {'CALSCALE': ('calscale', 'TEXT', 0, 1),
                   'METHOD': ('method', 'TEXT', 0, 1),
                   'VERSION': ('version', 'TEXT', 1, 1),
                   'PRODID': ('prodid', 'TEXT', 1, 1)
                   },
                  
                  {'VTIMEZONE':
                   ('Vtimezone',
                    {"TZID": ('tzid', 'TEXT', 1, 1), #hmm... fragid?
                     "LAST-MODIFIED": ('lastModified', "DATE-TIME", 0, 1),
                     "TZURL": ('tzurl', 'URI', 0, 1),
                     },
                    {"STANDARD": ('standard', tzprop, {}),
                     "DAYLIGHT": ('daylight', tzprop, {})
                     }
                    ),
                   
                   'VEVENT':
                   ('Vevent',
                    {"ATTACH": ('attach', 'URI', 0, None),
                     "CATEGORIES": ('categories', "TEXT", 0, None), #@@list
                     "SUMMARY": ('summary', "TEXT", 0, None),
                     "DTEND": ('dtend', 'DATE-TIME', 0, None),
                     "DTSTART": ('dtstart', 'DATE-TIME', 0, None),
                     "DURATION": ('duration', 'DURATION', 0, None),
                     "TRANSP": ('transp', 'TEXT', 0, None), #@@symbol
                     "ATTENDEE": ('attendee', 'CAL-ADDRESS', 0, None),
                     "CONTACT": ('contact', 0, None),
                     "ORGANIZER": ('organizer', "CAL-ADDRESS", 0, None),
                     "RELATED-TO": ('relatedTo', 0, None),
        # notes on rfc2445#sec4.8.4.6 Uniform Resource Locator
        # 
        # This is very muddled modelling; url makes sense as
        # a value type, but not as a property name. It's a grab-bag
        # for concepts like foaf:homePage, dc:related (which
        # is another grab bag) etc.
                     "URL": ('url', 'URI', 0, None),
                     "UID": ('uid', "TEXT", 0, None),
                     "EXRULE": ('exrule', 0, None),
                     "CLASS": ('class', 'TEXT', 0, None), #@@symbol
                     "RDATE": ('rdate', 'DATE-TIME', 0, None),
                     "RRULE": ('rrule', 'RECUR', 0, None),
                     'EXDATE': (u'exdate', 'DATE-TIME', None, None),
                     'RECURRENCE-ID': (u'recurrenceId', 'DATE-TIME', None, None),
                     "TRIGGER": ('trigger', "DURATION", 0, None),
                     "CREATED": ('created', "DATE-TIME", 0, None),
                     "DTSTAMP": ('dtstamp', 'DATE-TIME', 1, 1),
                     "LAST-MODIFIED": ('lastModified', "DATE-TIME", 0, 1),
                     "SEQUENCE": ('sequence', "INTEGER", 0, None),
                     "REQUEST-STATUS": ('requestStatus', 0, None),
                     "COMMENT": ('comment', 'TEXT', 0, None),
                     "DESCRIPTION": ('description', "TEXT", 0, 1),
                     'GEO': ('geo', ('FLOAT',), None, None),
                     "LOCATION": ('location', 'TEXT', 0, None),
                     'PRIORITY': ('priority', 'INTEGER', None, None),
                     "RESOURCES": ('resources', 0, None),
                     "STATUS": ('status', 'TEXT', 0, 1),
                     },
                    {"VALARM": ValarmDefs }
                    ),
                   'VTODO':
                   (u'Vtodo',
                    {'ATTACH': (u'attach', 'URI', None, None),
                     'ATTENDEE': (u'attendee', 'CAL-ADDRESS', None, None),
                     'CATEGORIES'	: (u'categories', 'TEXT', None, None),
                     'CLASS': (u'class', 'TEXT', None, None),
                     'COMMENT': (u'comment', 'TEXT', None, None),
                     'COMPLETED': (u'completed', 'DATE-TIME', None, None),
                     'CONTACT': (u'contact', 'TEXT', None, None),
                     'CREATED': (u'created', 'DATE-TIME', None, None),
                     'DESCRIPTION': (u'description', 'TEXT', None, None),
                     'DTSTAMP': (u'dtstamp', 'DATE-TIME', None, None),
                     'DTSTART': (u'dtstart', 'DATE-TIME', None, None),
                     'DUE': (u'due', 'DATE-TIME', None, None),
                     'DURATION': (u'duration', 'DURATION', None, None),
                     'EXRULE': (u'exrule', 'RECUR', None, None),
                     'GEO': (u'geo', '@@', None, None),
                     'LAST-MODIFIED': (u'lastModified', 'DATE-TIME', None, None),
                     'LOCATION': (u'location', 'TEXT', None, None),
                     'ORGANIZER': (u'organizer', 'CAL-ADDRESS', None, None),
                     'PERCENT-COMPLETE': (u'percentComplete', 'INTEGER', None, None),
                     'PRIORITY': (u'priority', 'INTEGER', None, None),
                     'RDATE': (u'rdate', 'DATE-TIME', None, None),
                     'RELATED-TO': (u'relatedTo', 'TEXT', None, None),
                     'REQUEST-STATUS': (u'requestStatus', 'TEXT', None, None),
                     'RESOURCES': (u'resources', 'TEXT', None, None),
                     'RRULE': (u'rrule', 'RECUR', None, None),
                     'EXDATE': (u'exdate', 'DATE-TIME', None, None),
                     'RECURRENCE-ID': (u'recurrenceId', 'DATE-TIME', None, None),
                     'SEQUENCE': (u'sequence', 'INTEGER', None, None),
                     'STATUS': (u'status', 'TEXT', None, None),
                     'SUMMARY': (u'summary', 'TEXT', None, None),
                     'TRIGGER': (u'trigger', 'DURATION', None, None),
                     'UID': (u'uid', 'TEXT', None, None),
                     'URL': (u'url', 'URI', None, None)},
                    {"VALARM": ValarmDefs },
                    )
                   
                   #@@others
                   }
                  )
                 }


def doComponents(sx, components, compDecls, stripe=None, suppressed =[]):
    """interpret components

    stripe says whether we need a <component> element
    or a parseType="Resource" attribute to fix up the striping.
    or None, in which case we need to declare X- namespaces
    
    raises KeyError for unknown component name. @@test this
    """

    for name, props, subs in components:
        elt, propDecls, subDecls = compDecls[name]
	if elt in suppressed: 
	    continue
        attrs = {}
        if stripe == 'component':
            sx.startElement('component', {})
        elif stripe == 'Resource':
            attrs['rdf:parseType'] = stripe
        else:
            bindX(attrs, props, components)

        try:
            i = lookup(props, 'UID')
            if not('#' in i): i = "#" + i
            attrs['rdf:about'] = i
        except KeyError:
            try:
                tzid = lookup(props, 'TZID')
                attrs['rdf:about'] = timeZoneNameSpace(tzid) + 'tz'
            except KeyError:
                pass

        sx.startElement(elt, attrs)
        doProperties(sx, '', props, propDecls, suppressed = suppressed)

        if elt == 'Vtimezone':
            doComponents(sx, subs, subDecls, 'Resource', suppressed = suppressed)
        else:
            doComponents(sx, subs, subDecls, 'component', suppressed = suppressed)

        sx.endElement(elt)
        if stripe == 'component':
            sx.endElement('component')


def bindX(attrs, props, components):
    """ bind x: namespace per prodid

    hmm... use vendorid as prefix?
    """

    try:
        prodid = lookup(props, 'PRODID') #@@ call unesc to parse \,
    except KeyError:
        warn("RFC2445 requires a prodid. none found")
        return

    def findVendorids(results, components):
        for name, props, subs in components:
            for n, p, v in props:
                if n[:2] == "X-":
                    vendorid, lname = n[2:].split('-', 1)
                    results.append(vendorid.lower())
            findVendorids(results, subs)

    vendorids = []
    findVendorids(vendorids, components)
    puri = prodURI(prodid)
    for vid in vendorids:
        attrs['xmlns:x-' + vid] = puri

            

def lookup(props, k):
    for n, p, v in props:
        if n == k:
            return v
    raise KeyError, k

def prodURI(prodid):
    r"""turn prodid into a URI

    cf discussion starting with
    x-properties and namespaces
    posted by DanC at 2003-02-26 17:17 (+)
    http://rdfig.xmlhack.com/2003/02/26/2003-02-26.html#1046279854.884486
    and continuing thru
      RDF calendar agenda item C: prodid support to ical2rdf.pl
      posted by libby at 2003-07-09 14:59 (+)
      http://rdfig.xmlhack.com/2003/07/09/2003-07-09.html#1057762764.179078


    >>> prodURI("-//Apple Computer\, Inc//iCal 1.0//EN")
    'http://www.w3.org/2002/12/cal/prod/Apple_Comp_628d9d8459c556fa#'

    """

    import sha
    
    if prodid[:3] == "-//": prodid = prodid[3:]
    prodid = prodid.replace(' ', '_').replace("//", "_")
    digest = sha.new(prodid.lower()).hexdigest()
    return 'http://www.w3.org/2002/12/cal/prod/' + \
           prodid[:10] + '_' + digest[:16] + '#'





def doProperties(sx, pfx, props, schema, suppressed =[]):
    """write each property as XML
    
    raises KeyError for properties that are neither X-
    properties nor in the schema
    """
    
    for n, params, val in props:
	if n in suppressed:
	    continue

        if n[:2] == "X-":
            if 'X-' in suppressed: continue
            vendorid, lname = n[2:].split('-', 1)
            id = 'x-' + vendorid.lower() + ':' + camelCase(lname)
            vtype, minc, maxc = 'TEXT', 0, None
        else:
	    try:
                id, vtype, minc, maxc = schema[n]
            except ValueError:
                raise RuntimeError, "missing default type: %s -> %s" %\
                      (n, schema[n])


        elt = '%s%s' % (pfx, id)

        for pn, pv in params:
            if pn == 'VALUE':
                vtype = pv.upper()
        if vtype == 'TEXT':
            doText(sx, elt, params, val)
        elif vtype == 'INTEGER':
            doInteger(sx, elt, params, val)
        elif vtype == 'DURATION':
            doDuration(sx, elt, params, val)
        elif vtype == 'DATE-TIME':
            doDateTime(sx, elt, params, val)
        elif vtype == 'DATE':
            doDate(sx, elt, params, val)
        elif vtype == 'RECUR':
            doRecur(sx, elt, params, val)
        elif vtype == 'CAL-ADDRESS':
            doCalAddress(sx, elt, params, val)
        elif vtype == 'URI':
            doURI(sx, elt, params, val)
        elif vtype == ('FLOAT',):
            doListOfFLOAT(sx, elt, params, val)
        else:
            warn("@@value type %s not implemented (%s: %s)" % (vtype, n, val))

def doInteger(sx, elt, params, val):
    sx.startElement(elt, {'rdf:datatype': XMLSchema.integer})
    sx.characters(val, 0, len(val))
    for pn, pv in params:
        if pn=='VALUE': pass
        else:
            raise ValueError, "unexpected param %s=%s" % (pn, pv)
    sx.endElement(elt)

def doURI(sx, elt, params, val):
    sx.startElement(elt, {'rdf:resource': val})
    for pn, pv in params:
        if pn=='VALUE': pass
        else:
            raise ValueError, "unexpected param %s=%s" % (pn, pv)
    sx.endElement(elt)

def doText(sx, elt, params, val):
    attrs = {}

    val = unesc(val)   # @@ or only if not QUOTED-PRINTABLE ?

    for pn, pv in params:
        if pn=='VALUE': pass
	elif (pn, pv) == ('ENCODING', 'QUOTED-PRINTABLE'):
	    val = quopri.decodestring(val)
	elif pn == "LANGUAGE":
	    attrs[ 'xml:lang'] = pv
        else:
            warn("unexpected param %s=%s on elt '%s'" % (pn, pv, elt))

    sx.startElement(elt, attrs)
    sx.characters(val, 0, len(val))
    sx.endElement(elt)


# Hmm... we can't use dt:dateTime nor dt:duration as the property
# here, because datatype properties are inverse functional, but
# iCalendar DATE-TIME values have other properties, i.e. tzid.
#
# In a way, it's a good thing anyway, since using datatype properties
# would take us out of OWL DL.

def doDateTime(sx, elt, params, val):
    val = datePunc(val)

    tzid = None
    
    for pn, pv in params:
        if pn == 'VALUE': pass # delete this in doParams?
        elif pn == 'TZID':
            tzid = pv
        else:
            raise ValueError, "unexpected param %s=%s" % (pn, pv)

    if val.endswith('Z'):
        sx.startElement(elt, {'rdf:datatype': XMLSchema.dateTime})
        sx.characters(val, 0, len(val))
        sx.endElement(elt)
    elif tzid:
        sx.startElement(elt, {'rdf:datatype': timeZoneNameSpace(tzid)+'tz'})
        sx.characters(val, 0, len(val))
        sx.endElement(elt)
    else:
        sx.startElement(elt, {'rdf:datatype': iCalendar.dateTime})
        sx.characters(val, 0, len(val))
        sx.endElement(elt)



OlsonPfxs=('/softwarestudio.org/Olson_20011030_5/',
           '/softwarestudio.org/Olson_20010831_3/',
           '/softwarestudio.org/Olson_20011030_4/',
           '/softwarestudio.org/Olson_20020614_6/',
           '/softwarestudio.org/Olson_20011030_2/')
TzdPfx='http://www.w3.org/2002/12/cal/tzd/'

def timeZoneNameSpace(tzid):
    """map tzid into URI space rooted at TzdPfx

    rfc2445#sec4.8.3.1 says:
    "The presence of the SOLIDUS character (US-ASCII
    decimal 47) as a prefix, indicates that this TZID represents an
    unique ID in a globally defined time zone registry (when such
    registry is defined)."

    >>> timeZoneNameSpace('/softwarestudio.org/Olson_20011030_5/Europe/London')
    'http://www.w3.org/2002/12/cal/tzd/Europe/London#'

    If an unknown registry is uses...
    
    >>> timeZoneNameSpace('/foo')
    Traceback (most recent call last):
      raise ValueError, "unknown global tzid:" + tzid
    ValueError: unknown global tzid:/foo


    We do some namespace squatting: in theory,
    the name 'Europe/London' could be used as a local reference
    for Chicago time. But in practice, this works:
    
    >>> timeZoneNameSpace('Europe/London')
    'http://www.w3.org/2002/12/cal/tzd/Europe/London#'

    A bit more squatting:
    >>> timeZoneNameSpace('US/Eastern')
    'http://www.w3.org/2002/12/cal/tzd/America/New_York#'

    
    @@raises RuntimeError for unrecognized local refs.
    
    """
    
    if tzid.startswith('/'):
        for pfx in OlsonPfxs:
            if tzid.startswith(pfx):
                tzns = TzdPfx + tzid[len(pfx):] + '#'
                break
        else:
            raise ValueError, "unknown global tzid:" + tzid
    else:
        tzid = {'US/Eastern': 'America/New_York',
                'US/Central': 'America/Chicago',
                # mountain? denver?
                'US/Pacific': 'America/Los_Angeles',
                }.get(tzid, tzid)
        
        for area in ('Africa', 'Antarctica', 'Asia', 'Australia',
                     'Europe', 'Pacific', 'America', 'Arctic', 'Atlantic',
                     'Indian'):
            if tzid.startswith(area + '/'):
                tzns = TzdPfx + tzid + '#'
                break
        else:
            raise RuntimeError, "unsupported local timezone: " + tzid
    return tzns


def doDate(sx, elt, params, val):
    sx.startElement(elt, {'rdf:datatype': XMLSchema.date})

    val = "%s-%s-%s" % (val[:4], val[4:6], val[6:8])
    sx.characters(val, 0, len(val))

    for pn, pv in params:
        if pn == 'VALUE': pass # delete this in doParams?
        else:
            raise ValueError, "unexpected param %s=%s" % (pn, pv)
    sx.endElement(elt)

def doCalAddress(sx, elt, params, val):
    sx.startElement(elt, {'rdf:parseType': "Resource"})

    val = val.replace("MAILTO:", 'mailto:') # rfc2445#sec4.3.3 weirdness
    # hmm... use or mention of the address?
    sx.startElement('calAddress', {'rdf:resource': val})
    sx.endElement('calAddress')

    for pn, pv in params:
        if pn == 'VALUE': pass # delete this in doParams?
        elif pn == 'CN':
            sx.startElement('cn', {}) #@@ add to ical schema
            sx.characters(pv, 0, len(pv))
            sx.endElement('cn')
        elif pn == 'DIR':
            sx.startElement('dir', {'rdf:resource': pv})
            sx.endElement('cn')
        elif pn in ('CUTYPE', 'ROLE', 'RSVP', 'PARTSTAT', 'LANGUAGE'):
            pn = pn.lower() # lower is sufficient to camelCase
            pv = pv.upper() # @@symbol
            sx.startElement(pn, {})
            sx.characters(pv, 0, len(pv))
            sx.endElement(pn)
	elif pn == 'X-UID':
	    pn = pn.lower()    # @@@@ should be namespaced from the client software mfr
            sx.startElement(pn, {})
            sx.characters(pv, 0, len(pv))
            sx.endElement(pn)
	    
        else:
            raise ValueError, "unexpected param %s=%s" % (pn, pv)
    sx.endElement(elt)

def doRecur(sx, elt, params, val):
    sx.startElement(elt, {'rdf:parseType': "Resource"})

    for n, v in recurlex(val, downcase=False).iteritems():
        sx.startElement(n, type(v) is type(1) and \
                        {'rdf:datatype': XMLSchema.integer} or {})
        sx.characters(str(v), 0, len(str(v)))
        sx.endElement(n)
    for pn, pv in params:
        if pn == 'VALUE': pass # delete this in doParams?
        else:
            raise ValueError, "unexpected param %s=%s" % (pn, pv)

    sx.endElement(elt)

def doDuration(sx, elt, params, val):
    """duration is an odd beast in iCalendar. There is a duration
    property as well as a duration value type. We'll use cal:duration
    for the property.

    The DURATION value type is actually more than just a
    XMLSchema.duration; it also has a RELATED parameter.
    So for
      TRIGGER;VALUE=DURATION;RELATED=START:-PT15M
    we'll write
      { ?E cal:trigger [ rdf:value "-PT15M"^^xsdt:duration;
                         cal:related "START"] }
    """
    sx.startElement(elt, {'rdf:parseType': "Resource"})
    sx.startElement('rdf:value', {'rdf:datatype': XMLSchema.duration})
    sx.characters(val, 0, len(val))
    sx.endElement('duration')
    for pn, pv in params:
        if pn == 'VALUE': pass # delete this in doParams?
        elif pn == 'RELATED':
            pv = pv.upper() # rfc2445#sec2 names
            sx.startElement('related', {})
            sx.characters(pv, 0, len(pv))
            sx.endElement('related')
        else:
            raise ValueError, "unexpected param %s=%s" % (pn, pv)
    sx.endElement(elt)


# We considered generalizing this to lists of
# other sorts of values but declined on the
# YouArentGonnaNeedIt principle.
def doListOfFLOAT(sx, elt, params, val):
    x, y = val.split(';')

    for pn, pv in params:
        if pn=='VALUE': pass
        else:
            raise ValueError, "unexpected param %s=%s" % (pn, pv)

    #ugh... can't use parseType="Collection" with literal values
    #http://www.w3.org/2000/03/rdf-tracking/#rdfxml-literals-in-collections
    sx.startElement(elt, {'rdf:parseType': "Resource"})

    sx.startElement('rdf:first', {'rdf:datatype': XMLSchema.double})
    sx.characters(x, 0, len(x))
    sx.endElement('rdf:first')

    sx.startElement('rdf:rest', {'rdf:parseType': "Resource"})

    sx.startElement('rdf:first', {'rdf:datatype': XMLSchema.double})
    sx.characters(y, 0, len(y))
    sx.endElement('rdf:first')

    sx.startElement('rdf:rest',
                    {'rdf:resource':
                     'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'})
    sx.endElement('rdf:rest')
    sx.endElement('rdf:rest')

    sx.endElement(elt)


def datePunc(val):
    """convert RFC2445 date to ISO/W3C/XML date form

    >>> datePunc('19701025T020000')
    '1970-10-25T02:00:00'
    >>> datePunc('20020630T230353Z')
    '2002-06-30T23:03:53Z'
    """
    return "%s-%s-%sT%s:%s:%s%s" % (val[:4], val[4:6], val[6:8],
                                    val[9:11], val[11:13], val[13:15],
                                    val[15:])


def camelCase(n, initialCap=0):
    """
    >>> camelCase("VEVENT", 1)
    'Vevent'
    
    >>> camelCase("LAST-MODIFIED")
    'lastModified'

    >>> camelCase("LOCATION")
    'location'
    """
    
    words = map(lambda w: w.lower(), n.split('-'))

    def ucfirst(w):
        return w[0].upper() + w[1:]
    
    if initialCap:
        return ''.join(map(ucfirst, words))
    else:
        return words[0] + ''.join(map(ucfirst, words[1:]))


def findComponents(lines, container, components=[]):
    """return a list of (name, props, subcomponents) and remaining lines

    # @@TODO: make this a generator

    """

    props = []
    subs = []

    while 1:
        try:
            n, p, v = parseLine(lines.next(), downcase=False)
        except StopIteration:
            break
        #print "finding...", n, p, v

        #print >>sys.stderr, "began", container, n, p, v
        if n == 'END':
            # @@hmm... found an extra space after END:DAYLIGHT
            # in test/20030410querymtg.ics
            # where did that come from? allow it, or fix test data?
            v = v.rstrip().upper()
            if v != container:
                raise ValueError, 'expected "%s" but found "%s"' % \
                      (container, v)
            components.append((container, props, subs))
            return
        elif n == 'BEGIN':
            findComponents(lines, v, subs)
        else:
            props.append((n, p, v))
            
    
def _test():
    import sys
    from pprint import pprint
    import doctest, fromIcal
    doctest.testmod(fromIcal)

    lines = unbreak(open(sys.argv[1]))
    n, p, v = parseLine(lines.next())
    c = []
    findComponents(lines, v, c)
    pprint(c)
    #unittest.main()

if __name__ == '__main__':
    import sys
    if sys.argv[1] == '--test':
        del sys.argv[1]
        _test()
    else:
        main()

# $Log: fromIcal.py,v $
# Revision 2.33  2007/02/20 14:58:45  timbl
# explain what we found if not CALENDAR
#
# Revision 2.32  2006/04/21 16:46:46  connolly
# demote unexpected param on text to warning, to deal with x2v's charset hack
#
# Revision 2.31  2006/04/11 20:29:00  connolly
# finished factoring out icslex stuff: unbreak, parseLine
# findComponents is now more straightforwardly recursive
#
# Revision 2.30  2006/04/09 06:02:39  connolly
# changeset:   7:5f8c551b2de38fb115789dfe7cbca0288a978f61
# tag:         tip
# user:        Dan Connolly <connolly@w3.org>
# date:        Sun Apr  9 01:01:32 2006 -0500
# files:       icslex.py
# description:
# add bymonthday to recurlex
#
#
# changeset:   6:32c567b22753c64f71c8de298adb87bad91ef567
# user:        Dan Connolly <connolly@w3.org>
# date:        Sun Apr  9 00:54:59 2006 -0500
# files:       icsxml.py
# description:
# use utf-8 to read files; kludge a couple more fields that the template assumes
#
#
# changeset:   5:12370cd5ad97cd5cea04e7ed4d5f6b55c0ac39ff
# user:        Dan Connolly <connolly@w3.org>
# date:        Sun Apr  9 00:54:13 2006 -0500
# files:       icslex.py
# description:
# make interval explict; use utf-8 to read files
#
#
# changeset:   4:0f319182ea4d6ee8a8b7f2ef042683323b75658d
# user:        Dan Connolly <connolly@w3.org>
# date:        Sun Apr  9 00:37:07 2006 -0500
# files:       icsxml.py
# description:
# works in one case, with a couple kludges
#
#
# changeset:   3:3e542292c8040d0dab310748ef07ffbce0a15b4a
# user:        Dan Connolly <connolly@w3.org>
# date:        Sun Apr  9 00:36:43 2006 -0500
# files:       icslex.py
# description:
# date, recur lex details
#
#
# changeset:   2:c2881393d0156b9263d760e98953ece6ba7591a6
# user:        Dan Connolly <connolly@w3.org>
# date:        Sun Apr  9 00:01:33 2006 -0500
# files:       icslex.py
# description:
# - parsing collections of properties as a dict/JSON object works
# - names are downcased by default
# - formatted docs per rst/epydoc
#
#
# changeset:   1:ecc1ad118fc61abb55e9634d15921483134f3328
# user:        Dan Connolly <connolly@w3.org>
# date:        Sat Apr  8 22:06:28 2006 -0500
# files:       icslex.py
# description:
# unbreak works
#
#
# changeset:   0:ec6eb270779b1ae046b9dd04be92034375392722
# user:        Dan Connolly <connolly@w3.org>
# date:        Sat Apr  8 21:50:45 2006 -0500
# files:       icslex.py
# description:
# parseLine tests pass
#
# Revision 2.29  2005/11/09 23:10:48  connolly
# - changed the way duration values are modelled
#     The iCalendar DURATION value type is actually more than just a
#     XMLSchema.duration; it also has a RELATED parameter.
#     So for
#       TRIGGER;VALUE=DURATION;RELATED=START:-PT15M
#     we'll write
#       { ?E cal:trigger [ rdf:value "-PT15M"^^xsdt:duration;
#                          cal:related "START"] }
#
# - fixed test data to have rdf:datatype on integer
#   values, to match the schema (which matches the RFC)
#
# - fixed schema to show DATE-TIME properties (dtstart, ...)
#   as DatatypeProperties
#   (there are little/no tests for PERIOD; beware)
#
# - scraped more details about property parameters (e.g. partstat, cn,
#   cutype, ...) and rrule parts (freq, interval, ...) from the RFC so
#   that they show up as links in the hypertext version and as RDF
#   properties in the schema.  likewise timezone components (standard,
#   daylight)
#  - side effect: added some whitespace in rfc2445.html
#
# - demoted x- properties
#  - removed x- properties from .rdf versions of test data
#    this allows the round-trip tests to pass
#  - fromIcal.py doesn't output them unless you give the --x option
#
# - added Makefile support for consistency checking with pellet
#
# - demoted blank line diagnostic in fromIcal.py to a comment
#
# - silenced some left-over debug diagnostics in slurpIcalSpec.py
#
# - fixed test/test-created.rdf; added it to fromIcalTest.py list
#
# Revision 2.28  2005/09/08 00:43:49  connolly
# avoid double hashes in ID
#
# Revision 2.27  2005/04/22 14:16:56  connolly
# fix problems found when converting all the timezone files
# in evolution-data-server_1.0.4-1_i386.deb:
# - handle RDATE
# - handle multiple OlsonPfxs
#
# Revision 2.26  2005/04/04 21:17:14  connolly
# fix initialization of iCalendar namespace
#
# Revision 2.25  2005/03/30 15:35:21  connolly
# new namespace for timezones-as-datatypes design: icaltzd
#
# Revision 2.24  2005/02/26 03:20:47  connolly
# fromIcal.py
# - revert the uid: trick; back to uids as fragids
# - timezones as datatypes in dates, dateTimes
# - Valarm supported in Vtodo as well as Vevent
#   (@@need test smaller than MozMulipleVcalendars.ics)
# - re-indented Vtodo decls while I was at it
# - case-fold END:xyz
#
# fromIcalTest.py
# - base in http space
# - new tag-bug case
#
# test/*.rdf
# - base in http space
# - timezones as datatypes
#
# test/cal-regression.n3
# - moved tests that don't use X- first
# - got rid of initRDF
#
# test/cal-retest.py
# - replace ical2rdf.pl with fromIcal.py
# - base in http space
#
# test/cal-spec-examples.n3 new
#
# test/graphCompare.n3 oops; extra debug crud
#
# Revision 2.23  2005/02/10 21:39:00  timbl
# COUNT, LANGUAGE, X-UID, QUOTED-PRINTABLE under DanC's supervision
#
# Revision 2.22  2005/02/02 21:54:45  timbl
# added --noalarm option - kindofa hack - take 2
#
# Revision 2.21  2005/02/02 21:51:46  timbl
# added --noalarm option - kindofa hack
#
# Revision 2.20  2005/02/02 21:39:20  timbl
# sync
#
# Revision 2.19  2005/02/01 15:29:43  timbl
# hack to CREATED to add default type DATE-TIME.
#
# Revision 2.18  2005/02/01 15:26:53  timbl
# pre hack to CREATED  default type.
#
# Revision 2.17  2005/01/28 04:07:49  timbl
# Event URIs now absolute. Added --noprotocol and --help options
#
# Revision 2.16  2004/09/30 14:16:01  connolly
# parseLine was buggy in the case of ; in values
#
# Revision 2.15  2004/04/14 21:31:26  connolly
# added --base support so we can test with fragids
#
# Revision 2.14  2004/04/14 21:12:13  connolly
#
# - revamped doDateTime: use datatypes for dateTime values
#   - added __getattr__ to Namespace class
# - make well-known tzids into URIs in 2002/12/cal space
# - make UID into fragid
# - make local tzid into fragid
#
# Revision 2.13  2004/04/08 14:09:11  connolly
# priority on VEVENT fixed
#
# Revision 2.12  2004/04/07 18:27:17  connolly
# use real datatypes for list of floats, i.e. geo
#
# Revision 2.11  2004/04/07 18:10:22  connolly
# convert list of float, as in GEO
#
# Revision 2.10  2004/03/25 04:00:59  connolly
# allow recurrenceId wherever rrule can go
# handle WKST in recur values
#
# Revision 2.9  2004/03/25 03:45:09  connolly
# handle UNTIL in rrule
# added EXDATE to compDecls wherever RRULE occurs
#
# Revision 2.7  2004/03/23 14:59:28  connolly
# allow missing PRODID
#
# Revision 2.6  2004/03/10 21:59:31  connolly
# calendar schema is now generated from the RFC
#
# Revision 2.5  2004/02/29 14:52:11  connolly
# todo support in fromIcal; value type label in schema
#
# Revision 2.4  2004/02/12 07:17:05  connolly
# - handle URI value type
# - a few more default value type declarations
#
# Revision 2.3  2004/02/12 06:30:48  connolly
# - doText unescapes text values per rfc2445#sec4.3.11
# - LAST-MODIFIED applies to VEVENT (fixed typo in RFC)
# - default type added for COMMENT
# - disabled UID->fragid conversion cuz it interferes with graph comparison
# - handle DIR parameter on CAL-ADDRESS value type
#
# Revision 2.2  2004/02/11 22:04:10  connolly
# slightly nicer XML writer
#
# Revision 2.1  2004/02/11 16:40:23  connolly
# finish renaming icalWebize.py to fromIcal.py
#
# Revision 2.0  2004/02/11 16:37:48  connolly
# copied from icalWebize.py
#
# Revision 1.11  2004/02/09 23:09:11  connolly
# - more refined X- property handling
# - generate fragids for uid, TZID
# - fixed utf-8 reading (thx MK for move test data!)
# - noted RDF API, name change TODOs per discussion with timbl
# - allow blank lines, space after names, (warn about possible confict with RFC)
# - pass over VALUE parameter in doText
#
# Revision 1.10  2004/02/08 19:14:39  connolly
# handles all of cal01.ics, though not quite the same way
# that ical2rdf.pl does
# - elaborated comment about declarations and rfc2445-formal
# - elaborated @@symbol comment
# - fixed tzoffsetfrom decl bug
# - handle X- properties
# - handle DATE value type
# - changed calAddress to use, rather than mention, ala ical2rdf.pl
# - handle (and test) Z on dateTimes
# - more uniform handling of case insensitivity (still not tested)
#
# Revision 1.9  2004/02/08 05:17:56  connolly
# working except X- props
# symbols are still text; not (yet?) interpreted as URIs
# treating Valarm as a class; leading toward changing it this way
#
# Revision 1.8  2004/02/08 04:28:43  connolly
# removed i: prefix
# enough property declarations with value types for cal01.ics
# added <component> elements and parseType to fix striping
# factored out text property code
#
# Revision 1.7  2004/02/08 00:10:00  connolly
# data-driven processing of components
#
# Revision 1.6  2004/02/07 06:28:35  connolly
# note a bug with unescaping TEXT values
#
# Revision 1.5  2004/02/07 03:40:17  connolly
# note intention to use generators
# rearrange module docstring
#
# Revision 1.4  2004/02/06 07:41:41  connolly
# syntax bug in one of the "not implemented" warnings
#
# Revision 1.3  2004/02/06 07:13:24  connolly
# started support for VEVENT
#   - TODO: reformat dates
#   - non-text properties; e.g. organizer
# moved main() before Namespace class
#
# Revision 1.2  2004/02/06 06:56:38  connolly
# lines are arranged into components
# calendar object and its properties are written as RDF
#
# Revision 1.1  2004/02/06 05:22:11  connolly
# parses lines into name, params, value
#