File: printing.py

package info (click to toggle)
python-seedir 0.5.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 3,392 kB
  • sloc: python: 2,758; makefile: 3
file content (172 lines) | stat: -rw-r--r-- 4,881 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
"""
General module of resources and helpers for printing and making folder trees
in seedir.

"""

__pdoc__ = {'is_match': False,
            'format_indent': False,
            'words': False}



import copy
import os
import re


STYLE_DICT = {
    'lines': {'split':'├─',
              'extend':'│ ',
              'space':'  ',
              'final':'└─',
              'folderstart':'',
              'filestart':'',
              'folderend': '/',
              'fileend': ''},
    'dash':  {'split':'|-',
              'extend':'| ',
              'space':'  ',
              'final':'|-',
              'folderstart':'',
              'filestart':'',
              'folderend': '/',
              'fileend': ''},
    'spaces':{'split':'  ',
              'extend':'  ',
              'space':'  ',
              'final':'  ',
              'folderstart':'',
              'filestart':'',
              'folderend': '/',
              'fileend': ''},
    'plus':  {'split':'+-',
              'extend':'| ',
              'space':'  ',
              'final':'+-',
              'folderstart':'',
              'filestart':'',
              'folderend': '/',
              'fileend': ''},
    'arrow': {'split':'  ',
              'extend':'  ',
              'space':'  ',
              'final':'  ',
              'folderstart':'>',
              'filestart':'>',
              'folderend': '/',
              'fileend': ''}
    }
'''"Tokens" used to create folder trees in different styles'''

try:
    import emoji
    STYLE_DICT["emoji"] = {
        'split':'├─',
        'extend':'│ ',
        'space':'  ',
        'final':'└─',
        'folderstart':emoji.emojize(':file_folder:' + ' '),
        'filestart':emoji.emojize(':page_facing_up:' + ' '),
        'folderend': '/',
        'fileend': ''
    }
except ImportError:
    pass


filepath = os.path.dirname(os.path.abspath(__file__))
wordpath = os.path.join(filepath, 'words.txt')
with open(wordpath, 'r') as wordfile:
    words = [line.strip() for line in wordfile.readlines()]
"""List of dictionary words for seedir.fakedir.randomdir()"""

# functions

def is_match(pattern, string, regex=True):
    '''Function for matching strings using either regular expression
    or literal interpretation.'''
    if regex:
        return bool(re.search(pattern, string))
    else:
        return pattern == string

def get_styleargs(style):
    '''
    Return the string tokens associated with different styles for printing
    folder trees with `seedir.realdir.seedir()`.

    Parameters
    ----------
    style : str
        Style name.  Current options are `'lines'`, `'spaces'`, `'arrow'`,
        `'plus'`, `'dash'`, or `'emoji'`.

    Raises
    ------
    ValueError
        Style not recognized.

    ImportError
        'emoji' style requested but emoji package not installed.

    Returns
    -------
    dict
        Dictionary of tokens for the given style.

    '''
    if style not in STYLE_DICT and style == 'emoji':
        error_text = 'style "emoji" requires "emoji" to be installed'
        error_text += ' (pip install emoji) '
        raise ImportError(error_text)
    elif style not in STYLE_DICT:
        error_text = 'style "{}" not recognized, must be '.format(style)
        error_text += 'lines, spaces, arrow, plus, dash, or emoji'
        raise ValueError(error_text)
    else:
        return copy.deepcopy(STYLE_DICT[style])

def format_indent(style_dict, indent=2):
    '''
    Format the indent of style tokens, from seedir.STYLE_DICT or returned
    by seedir.get_styleargs().

    Note that as of v0.3.1, the dictionary is modified in place,
    rather than a new copy being created.

    Parameters
    ----------
    style_dict : dict
        Dictionary of style tokens.
    indent : int, optional
        Number of spaces to indent. The default is 2.  With 0, all tokens
        become the null string.  With 1, all tokens are only the first
        character.  With 2, the style tokens are returned unedited.  When >2,
        the final character of each token (excep the file/folder start/end tokens)
        are extened n - indent times, to give a string whose
        length is equal to indent.

    Returns
    -------
    output : dict
        New dictionary of edited tokens.

    '''
    indentable = ['split', 'extend', 'space', 'final']
    if indent < 0 or not isinstance(indent, int):
        raise ValueError('indent must be a non-negative integer')
    elif indent == 0:
        for key in indentable:
            style_dict[key] = ''
    elif indent == 1:
        for key in indentable:
            style_dict[key] = style_dict[key][0]
    elif indent > 2:
        extension = indent - 2
        for key in indentable:
            val = style_dict[key]
            style_dict[key] = val + val[-1] * extension

    return None