File size: 8,784 Bytes
8a79f2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
from future.utils import raise_from, string_types
from builtins import (bytes, str, open, super, range,
                      zip, round, input, int, pow, object)

import os
import re
import io
import sys
import glob
import yaml
import codecs
import filecmp
import traceback

import gslab_make.private.messages as messages
from gslab_make.private.exceptionclasses import CritError


def decode(string):
    """Decode string."""

    if (sys.version_info < (3, 0)) and isinstance(string, string_types):
        string = codecs.decode(string, 'latin1')

    return(string)


def encode(string):
    """Clean string for encoding."""

    if (sys.version_info < (3, 0)) and isinstance(string, unicode):
        string = codecs.encode(string, 'utf-8') 

    return(string)


def convert_to_list(obj, warning_type):
    """Convert object to list."""
    
    obj = [obj] if isinstance(obj, string_types) else obj
    
    if type(obj) is not list:
        if (warning_type == 'dir'):
            raise_from(TypeError(messages.type_error_dir_list % obj), None)
        elif (warning_type == 'file'):
            raise_from(TypeError(messages.type_error_file_list % obj), None)

    return(obj)


def norm_path(path):
    """Normalize path to be OS-compatible."""

    if path:
        path = re.split('[/\\\\]+', path)
        path = os.path.sep.join(path)
        path = os.path.expanduser(path)
        path = os.path.abspath(path)

    return(path)


def get_path(paths_dict, key, throw_error = True):
    """Get path for key.

    Parameters
    ----------
    path_dict : dict
        Dictionary of paths.
    key : str
        Path to get from dictionary.
    throw_error : bool
        Return error instead of ``None``. Defaults to ``True``. 

    Returns
    -------
    path : str
        Path requested.
    """
    
    try:
        path = paths_dict[key]
        if isinstance(path, string_types): 
            path = norm_path(path) 
        elif isinstance(path, list): 
            path = [norm_path(p) for p in path]
    except KeyError:
        if throw_error:
            raise_from(CritError(messages.crit_error_no_key % (key, key)), None)
        else:
            path = None

    return(path)


def glob_recursive(path, depth, max_depth = 20, quiet = True):
    """Walks through path. 
    
    Notes
    -----
    Takes glob-style wildcards.

    Parameters
    ----------
    path : str
        Path to walk through.
    depth : int
        Level of depth when walking through path.
    max_depth : int
        Maximum level of depth allowed. Defaults to 20.
    quiet : bool, optional
        Suppress warning if no files globbed. Defaults to ``True``. 

    Returns
    -------
    path_files : list
        List of files contained in path.
    """

    depth = max_depth if depth > max_depth else depth
    path_walk = norm_path(path)
    path_files = glob.glob(path_walk)

    i = 0     
    while i <= depth:          
        path_walk = os.path.join(path_walk, "*")
        glob_files = glob.glob(path_walk)
        if glob_files:
            path_files.extend(glob_files) 
            i += 1
        else:
            break

    path_files = [p for p in path_files if os.path.isfile(p)]
    if not path_files and not quiet:
        print(messages.warning_glob % (path, depth))

    return(path_files)

 
def file_to_array(file_name):
    """Read file and extract lines to list. 

    Parameters
    ----------
    file_name : str
        Path of file to read.

    Returns
    -------
    array : list
        List of lines contained in file.
    """
       
    with io.open(file_name, encoding = 'utf-8') as f:
        array = [line.strip() for line in f]
        array = [line for line in array if line]
        array = [line for line in array if not re.match('\#',line)]

    return(array)


def format_traceback(trace = ''):
    """Format traceback message.

    Parameters
    ----------
    trace : str
        Traceback to format. Defaults to ``traceback.format_exc``.

    Notes
    -----
    Format trackback for readability to pass into user messages. 

    Returns
    -------
    formatted : str
        Formatted traceback.
    """
    
    if not trace:
        trace = traceback.format_exc()

    trace = trace.strip()
    trace = '\n' + decode(trace)
    formatted = re.sub('\n', '\n  > ', trace)

    return(formatted)


def format_message(message):
    """Format message."""

    message = message.strip()
    star_line = '*' * (len(message) + 4)
    formatted = star_line + '\n* %s *\n' + star_line
    formatted = formatted % message

    return(formatted)


def format_list(list):
    """Format list. 

    Parameters
    ----------
    list : list
        List to format.

    Notes
    -----
    Format list for readability to pass into user messages.

    Returns
    -------
    formatted : str
        Formatted list.
    """

    formatted = ['`' + str(item) + '`' for item in list]
    formatted = ", ".join(formatted)
    
    return(formatted)


def open_yaml(path):
    """Safely loads YAML file."""

    path = norm_path(path)

    with io.open(path, 'r') as f:
        stream = yaml.safe_load(f)

    return(stream)


# ~~~~~~~~~~ #
# DEPRECATED #
# ~~~~~~~~~~ #
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
def check_duplicate(original, copy): 
    """Check duplicate.

    Parameters
    ----------
    original : str
        Original path.
    copy : str 
        Path to check if duplicate.

    Returns
    -------
    duplicate : bool
        Destination is duplicate.
    """

    duplicate = os.path.exists(copy)
    
    if duplicate: 
        if os.path.isfile(original):
            duplicate = filecmp.cmp(original, copy)            
        elif os.path.isdir(copy):
            dircmp = filecmp.dircmp(original, copy, ignore = ['.DS_Store'])
            duplicate = parse_dircmp(dircmp)
        else:
            duplicate = False
            
    return(duplicate)
    

def parse_dircmp(dircmp):
    """Parse dircmp to see if directories duplicate. 

    Parameters
    ----------
    dircmp : ``filecmp.dircmp``
        dircmp to parse if directories duplicate.

    Returns
    -------
    duplicate : bool
        Directories are duplicates.
    """

    # Check directory
    if dircmp.left_only:
        return False
    if dircmp.right_only:
        return False
    if dircmp.diff_files:
        return False
    if dircmp.funny_files:
        return False
    if dircmp.common_funny:
        return False

    # Check subdirectories
    duplicate = True
    
    for subdir in dircmp.subdirs.itervalues():
        if duplicate:
            duplicate = check_duplicate(subdir)
        else:
            break
        
    return(duplicate)