<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import re <EOL> import shlex <EOL> import platform <EOL> from collections import OrderedDict <EOL> _IDENTIFIER_PATTERN = '<STR_LIT>' <EOL> _SQUOTE_RE = re . compile ( r"<STR_LIT>" ) <EOL> _DQUOTE_RE = re . compile ( r'<STR_LIT>' ) <EOL> _HEADING_RE = re . compile ( <EOL> r'<STR_LIT>' + _IDENTIFIER_PATTERN + r'<STR_LIT>' ) <EOL> _VAR_RE = re . compile ( <EOL> r'<STR_LIT>' + _IDENTIFIER_PATTERN + r'<STR_LIT>' ) <EOL> if sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:3> ) : <EOL> FileNotFoundError = IOError <EOL> class InvalidConfigError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , errors ) : <EOL> Exception . __init__ ( self ) <EOL> self . filename = filename <EOL> self . errors = errors <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> self . filename , <EOL> list ( tup [ <NUM_LIT:0> ] for tup in self . errors ) <EOL> ) <EOL> class Vexrc ( object ) : <EOL> """<STR_LIT>""" <EOL> default_heading = "<STR_LIT:root>" <EOL> default_encoding = "<STR_LIT:utf-8>" <EOL> def __init__ ( self ) : <EOL> self . encoding = self . default_encoding <EOL> self . headings = OrderedDict ( ) <EOL> self . headings [ self . default_heading ] = OrderedDict ( ) <EOL> self . headings [ '<STR_LIT>' ] = OrderedDict ( ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . headings . get ( key ) <EOL> @ classmethod <EOL> def from_file ( cls , path , environ ) : <EOL> """<STR_LIT>""" <EOL> instance = cls ( ) <EOL> instance . read ( path , environ ) <EOL> return instance <EOL> def read ( self , path , environ ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> inp = open ( path , '<STR_LIT:rb>' ) <EOL> except FileNotFoundError as error : <EOL> if error . errno != <NUM_LIT:2> : <EOL> raise <EOL> return None <EOL> parsing = parse_vexrc ( inp , environ ) <EOL> for heading , key , value in parsing : <EOL> heading = self . default_heading if heading is None else heading <EOL> if heading not in self . headings : <EOL> self . headings [ heading ] = OrderedDict ( ) <EOL> self . headings [ heading ] [ key ] = value <EOL> parsing . close ( ) <EOL> def get_ve_base ( self , environ ) : <EOL> """<STR_LIT>""" <EOL> ve_base_value = self . headings [ self . default_heading ] . get ( '<STR_LIT>' ) <EOL> if ve_base_value : <EOL> ve_base = os . path . expanduser ( ve_base_value ) <EOL> else : <EOL> ve_base = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not ve_base : <EOL> if platform . system ( ) == '<STR_LIT>' and os . name == '<STR_LIT>' : <EOL> _win_drive = environ . get ( '<STR_LIT>' ) <EOL> home = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if home : <EOL> home = os . path . join ( _win_drive , home ) <EOL> else : <EOL> home = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not home : <EOL> home = os . path . expanduser ( '<STR_LIT>' ) <EOL> if not home : <EOL> return '<STR_LIT>' <EOL> ve_base = os . path . join ( home , '<STR_LIT>' ) <EOL> return ve_base or '<STR_LIT>' <EOL> def get_shell ( self , environ ) : <EOL> """<STR_LIT>""" <EOL> command = self . headings [ self . default_heading ] . get ( '<STR_LIT>' ) <EOL> if not command and os . name != '<STR_LIT>' : <EOL> command = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> command = shlex . split ( command ) if command else None <EOL> return command <EOL> def extract_heading ( line ) : <EOL> """<STR_LIT>""" <EOL> match = _HEADING_RE . match ( line ) <EOL> if match : <EOL> return match . group ( <NUM_LIT:1> ) <EOL> return None <EOL> def extract_key_value ( line , environ ) : <EOL> """<STR_LIT>""" <EOL> segments = line . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> if len ( segments ) < <NUM_LIT:2> : <EOL> return None <EOL> key , value = segments <EOL> value = value . strip ( ) <EOL> if value [ <NUM_LIT:0> ] == "<STR_LIT:'>" and _SQUOTE_RE . match ( value ) : <EOL> value = value [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> elif value [ <NUM_LIT:0> ] == '<STR_LIT:">' and _DQUOTE_RE . match ( value ) : <EOL> template = value [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> value = template . format ( ** environ ) <EOL> key = key . strip ( ) <EOL> value = value . strip ( ) <EOL> return key , value <EOL> def parse_vexrc ( inp , environ ) : <EOL> """<STR_LIT>""" <EOL> heading = None <EOL> errors = [ ] <EOL> with inp : <EOL> for line_number , line in enumerate ( inp ) : <EOL> line = line . decode ( "<STR_LIT:utf-8>" ) <EOL> if not line . strip ( ) : <EOL> continue <EOL> extracted_heading = extract_heading ( line ) <EOL> if extracted_heading is not None : <EOL> heading = extracted_heading <EOL> continue <EOL> kv_tuple = extract_key_value ( line , environ ) <EOL> if kv_tuple is None : <EOL> errors . append ( ( line_number , line ) ) <EOL> continue <EOL> try : <EOL> yield heading , kv_tuple [ <NUM_LIT:0> ] , kv_tuple [ <NUM_LIT:1> ] <EOL> except GeneratorExit : <EOL> break <EOL> if errors : <EOL> raise InvalidConfigError ( inp . name , errors ) </s>
