Debian Codegraph @

seedir/printing.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 General module of resources and helpers for printing and making folder trees
4 in seedir.
5
6 """
7
8 __pdoc__ = {'is_match': False,
9 'format_indent': False,
10 'words': False}
11
12
13
14 import copy
15 import os
16 import re
17
18
19 STYLE_DICT = {
20 'lines': {'split':'├─',
21 'extend':'│ ',
22 'space':' ',
23 'final':'└─',
24 'folderstart':'',
25 'filestart':'',
26 'folderend': '/',
27 'fileend': ''},
28 'dash': {'split':'|-',
29 'extend':'| ',
30 'space':' ',
31 'final':'|-',
32 'folderstart':'',
33 'filestart':'',
34 'folderend': '/',
35 'fileend': ''},
36 'spaces':{'split':' ',
37 'extend':' ',
38 'space':' ',
39 'final':' ',
40 'folderstart':'',
41 'filestart':'',
42 'folderend': '/',
43 'fileend': ''},
44 'plus': {'split':'+-',
45 'extend':'| ',
46 'space':' ',
47 'final':'+-',
48 'folderstart':'',
49 'filestart':'',
50 'folderend': '/',
51 'fileend': ''},
52 'arrow': {'split':' ',
53 'extend':' ',
54 'space':' ',
55 'final':' ',
56 'folderstart':'>',
57 'filestart':'>',
58 'folderend': '/',
59 'fileend': ''}
60 }
61 '''"Tokens" used to create folder trees in different styles'''
62
63 try:
64 import emoji
65 STYLE_DICT["emoji"] = {
66 'split':'├─',
67 'extend':'│ ',
68 'space':' ',
69 'final':'└─',
70 'folderstart':emoji.emojize(':file_folder:' + ' '),
71 'filestart':emoji.emojize(':page_facing_up:' + ' '),
72 'folderend': '/',
73 'fileend': ''
74 }
75 except ImportError:
76 pass
77
78
79 filepath = os.path.dirname(os.path.abspath(__file__))
80 wordpath = os.path.join(filepath, 'words.txt')
81 with open(wordpath, 'r') as wordfile:
82 words = [line.strip() for line in wordfile.readlines()]
83 """List of dictionary words for seedir.fakedir.randomdir()"""
84
85 # functions
86
87 def is_match(pattern, string, regex=True):
88 '''Function for matching strings using either regular expression
89 or literal interpretation.'''
90 if regex:
91 return bool(re.search(pattern, string))
92 else:
93 return pattern == string
94
95 def get_styleargs(style):
96 '''
97 Return the string tokens associated with different styles for printing
98 folder trees with `seedir.realdir.seedir()`.
99
100 Parameters
101 ----------
102 style : str
103 Style name. Current options are `'lines'`, `'spaces'`, `'arrow'`,
104 `'plus'`, `'dash'`, or `'emoji'`.
105
106 Raises
107 ------
108 ValueError
109 Style not recognized.
110
111 ImportError
112 'emoji' style requested but emoji package not installed.
113
114 Returns
115 -------
116 dict
117 Dictionary of tokens for the given style.
118
119 '''
120 if style not in STYLE_DICT and style == 'emoji':
121 error_text = 'style "emoji" requires "emoji" to be installed'
122 error_text += ' (pip install emoji) '
123 raise ImportError(error_text)
124 elif style not in STYLE_DICT:
125 error_text = 'style "{}" not recognized, must be '.format(style)
126 error_text += 'lines, spaces, arrow, plus, dash, or emoji'
127 raise ValueError(error_text)
128 else:
129 return copy.deepcopy(STYLE_DICT[style])
130
131 def format_indent(style_dict, indent=2):
132 '''
133 Format the indent of style tokens, from seedir.STYLE_DICT or returned
134 by seedir.get_styleargs().
135
136 Note that as of v0.3.1, the dictionary is modified in place,
137 rather than a new copy being created.
138
139 Parameters
140 ----------
141 style_dict : dict
142 Dictionary of style tokens.
143 indent : int, optional
144 Number of spaces to indent. The default is 2. With 0, all tokens
145 become the null string. With 1, all tokens are only the first
146 character. With 2, the style tokens are returned unedited. When >2,
147 the final character of each token (excep the file/folder start/end tokens)
148 are extened n - indent times, to give a string whose
149 length is equal to indent.
150
151 Returns
152 -------
153 output : dict
154 New dictionary of edited tokens.
155
156 '''
157 indentable = ['split', 'extend', 'space', 'final']
158 if indent < 0 or not isinstance(indent, int):
159 raise ValueError('indent must be a non-negative integer')
160 elif indent == 0:
161 for key in indentable:
162 style_dict[key] = ''
163 elif indent == 1:
164 for key in indentable:
165 style_dict[key] = style_dict[key][0]
166 elif indent > 2:
167 extension = indent - 2
168 for key in indentable:
169 val = style_dict[key]
170 style_dict[key] = val + val[-1] * extension
171
172 return None