<s> import re <EOL> import os <EOL> import sys <EOL> import codecs <EOL> import xml . etree . ElementTree as ET <EOL> class Node ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , level , text ) : <EOL> self . level = level <EOL> self . text = text <EOL> self . children = [ ] <EOL> def add_child ( self , node ) : <EOL> """<STR_LIT>""" <EOL> self . children . append ( node ) <EOL> class OrgParser ( object ) : <EOL> NODE_RE = re . compile ( '<STR_LIT>' ) <EOL> TITLE_RE = re . compile ( '<STR_LIT>' ) <EOL> AUTHOR_RE = re . compile ( '<STR_LIT>' ) <EOL> ROOT_RE = re . compile ( '<STR_LIT>' ) <EOL> def __init__ ( self , org_file ) : <EOL> self . org_file = org_file <EOL> self . title = '<STR_LIT>' <EOL> self . author = '<STR_LIT>' <EOL> self . root_name = '<STR_LIT>' <EOL> self . nodes = [ ] <EOL> self . prev_node = None <EOL> with codecs . open ( org_file , '<STR_LIT:r>' , encoding = '<STR_LIT>' ) as f : <EOL> self . content = f . readlines ( ) <EOL> def parse ( self ) : <EOL> """<STR_LIT>""" <EOL> for line in self . content : <EOL> line = line . strip ( ) <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> self . handle_meta ( line [ <NUM_LIT:2> : ] ) <EOL> elif line . startswith ( '<STR_LIT:*>' ) : <EOL> self . add_node ( line ) <EOL> def handle_meta ( self , line ) : <EOL> """<STR_LIT>""" <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> match = self . TITLE_RE . search ( line ) <EOL> if match : <EOL> self . title = match . group ( '<STR_LIT:title>' ) <EOL> elif line . startswith ( '<STR_LIT>' ) : <EOL> match = self . AUTHOR_RE . search ( line ) <EOL> if match : <EOL> self . author = match . group ( '<STR_LIT>' ) <EOL> elif line . startswith ( '<STR_LIT>' ) : <EOL> match = self . ROOT_RE . search ( line ) <EOL> if match : <EOL> self . root_name = match . group ( '<STR_LIT:root>' ) <EOL> def add_node ( self , line ) : <EOL> """<STR_LIT>""" <EOL> match = self . NODE_RE . match ( line ) <EOL> if match : <EOL> level = match . group ( '<STR_LIT>' ) . count ( '<STR_LIT:*>' ) <EOL> text = match . group ( '<STR_LIT:text>' ) <EOL> newnode = Node ( level = level , text = text ) <EOL> if level == <NUM_LIT:1> : <EOL> try : <EOL> self . nodes [ level - <NUM_LIT:1> ] . append ( newnode ) <EOL> except IndexError : <EOL> self . nodes . append ( [ newnode ] ) <EOL> else : <EOL> parent = self . nodes [ level - <NUM_LIT:2> ] [ - <NUM_LIT:1> ] <EOL> parent . add_child ( newnode ) <EOL> try : <EOL> self . nodes [ level - <NUM_LIT:1> ] . append ( newnode ) <EOL> except IndexError : <EOL> self . nodes . append ( [ newnode ] ) <EOL> def to_opml ( self ) : <EOL> """<STR_LIT>""" <EOL> skip_root = False <EOL> if len ( self . nodes ) == <NUM_LIT:1> : <EOL> self . root_name = self . nodes [ <NUM_LIT:0> ] . text <EOL> skip_root = True <EOL> root = ET . Element ( '<STR_LIT>' , attrib = { '<STR_LIT:version>' : '<STR_LIT:1.0>' } ) <EOL> head = ET . SubElement ( root , '<STR_LIT>' ) <EOL> title = ET . SubElement ( head , '<STR_LIT:title>' ) <EOL> title . text = self . title <EOL> author = ET . SubElement ( head , '<STR_LIT>' ) <EOL> author . text = self . author <EOL> body = ET . SubElement ( root , '<STR_LIT:body>' ) <EOL> outline = ET . SubElement ( body , '<STR_LIT>' , attrib = { <EOL> '<STR_LIT:text>' : self . root_name } ) <EOL> def iterate_children ( node , ol ) : <EOL> for child in node . children : <EOL> element = ET . SubElement ( <EOL> ol , '<STR_LIT>' , attrib = { '<STR_LIT:text>' : child . text } ) <EOL> iterate_children ( child , element ) <EOL> for root_node in self . nodes [ <NUM_LIT:0> ] : <EOL> if not skip_root : <EOL> ol = ET . SubElement ( outline , '<STR_LIT>' , attrib = { <EOL> '<STR_LIT:text>' : root_node . text } ) <EOL> iterate_children ( root_node , ol ) <EOL> else : <EOL> iterate_children ( root_node , outline ) <EOL> opml_file = os . path . splitext ( self . org_file ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> tree = ET . ElementTree ( root ) <EOL> tree . write ( opml_file , encoding = '<STR_LIT>' , xml_declaration = True ) <EOL> return opml_file <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> print '<STR_LIT>' <EOL> sys . exit ( - <NUM_LIT:2> ) <EOL> p = OrgParser ( sys . argv [ <NUM_LIT:1> ] ) <EOL> p . parse ( ) <EOL> print '<STR_LIT>' % p . to_opml ( ) </s>
