summaryrefslogtreecommitdiff
path: root/panfry/util.py
blob: c4bab4236611f7616782d6e15866e4671f06979f (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import distutils.dir_util as dir_util
import shutil


def copy(src, dst, ignore=None):
    if os.path.isdir(src):
        dir_util.copy_tree(src, dst)
    else:
        shutil.copy(src, dst)


def open_file(path, mode='r'):
    if mode == 'w' or os.path.isfile(path):
        return open(path, mode)
    else:
        return None


def write_file(path, content):
    fd = open_file(path, mode='w')
    if fd:
        fd.write(content.encode('utf-8'))
        fd.close()
        return path
    else:
        return None


def read_file(path):
    content = ''
    fd = open_file(path)
    if fd:
        content = fd.read()
        fd.close()
        return content
    else:
        print("Error: could not open %s" % path)
        return content


def read_file_lines(path):
    content = []
    fd = open_file(path)
    if fd:
        content = fd.readlines()
        fd.close()
    return content


def get_lines(content):
    '''
    return list of content split by line.
    Leading/trailing blank lines are not reserved.
    '''
    return content.strip().split('\n')