<s> from __future__ import absolute_import , division , print_function <EOL> import gzip <EOL> from datashape import discover , dshape <EOL> from collections import Iterator <EOL> from toolz import partial , concat <EOL> import uuid <EOL> import os <EOL> from . . compatibility import unicode <EOL> from . . chunks import chunks <EOL> from . . drop import drop <EOL> from . . temp import Temp <EOL> from . . append import append <EOL> from . . convert import convert <EOL> from . . resource import resource <EOL> class TextFile ( object ) : <EOL> canonical_extension = '<STR_LIT>' <EOL> def __init__ ( self , path , ** kwargs ) : <EOL> self . path = path <EOL> @ property <EOL> def open ( self ) : <EOL> if self . path . split ( os . path . extsep ) [ - <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> return gzip . open <EOL> else : <EOL> return open <EOL> @ convert . register ( Iterator , ( TextFile , Temp ( TextFile ) ) , cost = <NUM_LIT:0.1> ) <EOL> def textfile_to_iterator ( data , ** kwargs ) : <EOL> with data . open ( data . path ) as f : <EOL> for line in f : <EOL> yield line <EOL> @ convert . register ( Iterator , chunks ( TextFile ) , cost = <NUM_LIT:0.1> ) <EOL> def chunks_textfile_to_iterator ( data , ** kwargs ) : <EOL> return concat ( map ( partial ( convert , Iterator ) , data ) ) <EOL> @ discover . register ( ( TextFile , Temp ( TextFile ) ) ) <EOL> def discover_textfile ( data , ** kwargs ) : <EOL> return dshape ( '<STR_LIT>' ) <EOL> @ append . register ( ( Temp ( TextFile ) , TextFile ) , Iterator ) <EOL> def append_iterator_to_textfile ( target , source , ** kwargs ) : <EOL> with target . open ( target . path , '<STR_LIT:a>' ) as f : <EOL> for item in source : <EOL> f . write ( unicode ( item ) ) <EOL> f . write ( '<STR_LIT:\n>' ) <EOL> return target <EOL> @ append . register ( TextFile , object ) <EOL> def append_anything_to_textfile ( target , source , ** kwargs ) : <EOL> return append ( target , convert ( Iterator , source , ** kwargs ) , ** kwargs ) <EOL> @ convert . register ( Temp ( TextFile ) , Iterator ) <EOL> def iterator_to_temp_textfile ( seq , ** kwargs ) : <EOL> fn = str ( uuid . uuid1 ( ) ) <EOL> txt = Temp ( TextFile ) ( fn ) <EOL> return append ( txt , seq , ** kwargs ) <EOL> @ resource . register ( '<STR_LIT>' ) <EOL> def resource_sas ( uri , ** kwargs ) : <EOL> return TextFile ( uri ) <EOL> @ drop . register ( TextFile ) <EOL> def drop_textfile ( data , ** kwargs ) : <EOL> os . remove ( data . path ) </s>
