Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0e94e51efb
Fetching contributors…

Cannot retrieve contributors at this time

file 96 lines (80 sloc) 1.968 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
'''
@author Sergey Chikuyonok (serge.che@gmail.com)
@link http://chikuyonok.ru
'''
import os.path

def read(path):
"""
Read file content and return it
@param path: File's relative or absolute path
@type path: str
@return: str
"""
content = None
try:
fp = open(path, 'rb')
content = fp.read()
fp.close()
except:
pass

return content

def locate_file(editor_file, file_name): # (FM) infinite loop fix
"""
Locate <code>file_name</code> file that relates to <code>editor_file</code>.
File name may be absolute or relative path
@type editor_file: str
@type file_name: str
@return String or None if <code>file_name</code> cannot be located
"""
result = ''

previous_parent = ''
parent = os.path.dirname(editor_file)
while parent and os.path.exists(parent) and parent != previous_parent:
tmp = create_path(parent, file_name)
if os.path.exists(tmp):
result = tmp
break
previous_parent = parent
parent = os.path.dirname(parent)

return result

def create_path(parent, file_name):
"""
Creates absolute path by concatenating <code>parent</code> and <code>file_name</code>.
If <code>parent</code> points to file, its parent directory is used
@type parent: str
@type file_name: str
@return: str
"""
result = ''

if os.path.exists(parent):
if os.path.isfile(parent):
parent = os.path.dirname(parent)

result = os.path.normpath(os.path.join(parent, file_name))

return result

def save(file, content):
"""
Saves <code>content</code> as <code>file</code>
@param file: File's asolute path
@type file: str
@param content: File content
@type content: str
"""
try:
fp = open(file, 'wb')
except:
fdirs, fname = os.path.split(file)
if fdirs:
os.makedirs(fdirs)
fp = open(file, 'wb')

fp.write(content)
fp.close()

def get_ext(file):
"""
Returns file extention in lower case
@type file: str
@return: str
"""
ext = os.path.splitext(file)[1]
if ext:
ext = ext[1:]

return ext.lower()
Something went wrong with that request. Please try again.