2929
3030import copy
3131import os
32- from sys import path as PYTHONPATH , stderr
32+ from sys import path as PYTHONPATH
33+ from sys import stderr
3334
34- from natsort import natsorted
3535from pies import *
3636
3737from . import settings
38+ from natsort import natsorted
3839
3940
4041class Sections (object ):
@@ -60,9 +61,11 @@ def __init__(self, file_path=None, file_contents=None, **setting_overrides):
6061 file_name = file_name [file_name .rfind ('/' ) + 1 :]
6162 if file_name in self .config ['skip' ]:
6263 print ("WARNING: {0} was skipped as it's listed in 'skip' setting" .format (file_path ), file = stderr )
63- self .file_path = file_path
64- with open (file_path ) as file :
65- file_contents = file .read ()
64+ file_contents = None
65+ else :
66+ self .file_path = file_path
67+ with open (file_path ) as file :
68+ file_contents = file .read ()
6669
6770 if file_contents is None :
6871 return
@@ -222,21 +225,22 @@ def _add_formatted_imports(self):
222225 if straight_modules or from_modules :
223226 output .append ("" )
224227
225- while output [- 1 :] == ["" ]:
228+ while map ( unicode . strip , output [- 1 :]) == ["" ]:
226229 output .pop ()
227230
228- while self .import_index + 2 < len (self .out_lines ) and self .out_lines [self .import_index + 1 ] == "" :
229- self .out_lines .pop (self .import_index + 1 )
231+ self .out_lines [self .import_index :1 ] = output
230232
231- if len (self .out_lines ) > self .import_index + 1 :
232- next_construct = self .out_lines [self .import_index + 1 ]
233+ imports_tail = self .import_index + len (output )
234+ while map (unicode .strip , self .out_lines [imports_tail : imports_tail + 1 ]) == ["" ]:
235+ self .out_lines .pop (imports_tail )
236+
237+ if len (self .out_lines ) > imports_tail :
238+ next_construct = self .out_lines [imports_tail ]
233239 if next_construct .startswith ("def" ) or next_construct .startswith ("class" ) or \
234240 next_construct .startswith ("@" ):
235- output + = ["" , "" ]
241+ self . out_lines [ imports_tail : 1 ] = ["" , "" ]
236242 else :
237- output += ["" ]
238-
239- self .out_lines [self .import_index :1 ] = output
243+ self .out_lines [imports_tail :1 ] = ["" ]
240244
241245 @staticmethod
242246 def _strip_comments (line ):
@@ -280,29 +284,14 @@ def _parse(self):
280284 import_string = import_string .replace ("[[i]]" , "_import" )
281285
282286 imports = import_string .split ()
283- if "as" in imports and import_type != 'from' :
284- while True :
285- try :
286- index = imports .index ('as' )
287- except :
288- break
289- self .as_map [imports [index - 1 ]] = imports [index + 1 ]
290- from_import = imports [index - 1 ]
291- module_placment = self .place_module (from_import )
292- self .imports [module_placment ][import_type ].update ([from_import ])
293- del imports [index - 1 :index + 2 ]
294- elif import_type == 'from' and "as" in imports :
295- while True :
296- try :
297- index = imports .index ('as' )
298- except :
299- break
300- from_import = imports [0 ]
301- self .as_map [from_import ] = imports [index + 1 ]
302- module_placment = self .place_module (from_import )
303- imports = ["{0} as {1}" .format (imports [index - 1 ], imports [index + 1 ])]
304- self .imports [module_placment ][import_type ].setdefault (from_import , set ()).update (imports )
305- del imports [index - 1 :index + 1 ]
287+ if "as" in imports :
288+ while "as" in imports :
289+ index = imports .index ('as' )
290+ if import_type == "from" :
291+ self .as_map [imports [0 ] + "." + imports [index - 1 ]] = imports [index + 1 ]
292+ else :
293+ self .as_map [imports [index - 1 ]] = imports [index + 1 ]
294+ del imports [index :index + 2 ]
306295 if import_type == "from" :
307296 impot_from = imports .pop (0 )
308297 root = self .imports [self .place_module (impot_from )][import_type ]
0 commit comments