-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformatter.py
89 lines (69 loc) · 2.91 KB
/
formatter.py
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
#!/usr/bin/env python
import re
import sys
'''
Format of input string will be changed according to pytest requirements.
The 'directory' variable defines the root directory of the repository.
Examples:
input: 'repo.core.tests.test_account.TestCase'
output: ['django-sites/repo/core/tests/test_account.py::TestCase']
'''
directory = "django-sites"
def handling_references(user_references):
""" handles multiple or one reference and returns a list of strings to match the manage.py requirements """
result = ''
for reference in user_references:
adjusted_reference = rearrange_string(reference)
result += ' ' + adjusted_reference
return result.strip().split()
def rearrange_string(user_input):
""" rearranging string according to pytest standard format """
user_input = complete_string(user_input)
user_input = unify_input(user_input)
user_input_as_list = user_input.split('.')
user_input_as_list = format_string_for_pytest(user_input_as_list)
return "".join(user_input_as_list)
def complete_string(user_input):
""" adds the directory name with a leading slash and .py to the string """
user_input = user_input.replace('.py', '', 1)
user_input.lstrip("/")
if directory not in user_input:
user_input = directory + '/' + user_input
return user_input
def unify_input(user_input):
""" changes all separators (except underscore and dash) to dots """
pattern = re.compile(r'[^A-Za-z0-9_\-\.]+')
if re.search(pattern, user_input):
return pattern.sub(".", user_input)
return user_input
def format_string_for_pytest(user_input_as_list):
""" rebuilds the user input as needed for pytest """
before_module = True
for counter, element in enumerate(user_input_as_list):
if 'test_' in element and before_module:
user_input_as_list[counter] += ".py"
before_module = False
if len(user_input_as_list) is not counter + 1: # counter starts with 0 so +1 is needed
user_input_as_list[counter] = extend_string(user_input_as_list[counter], before_module)
return user_input_as_list
def extend_string(extend_element, before_module):
""" translates the separating dots to slashes before filename or to quad points after the filename """
if before_module:
extend_element += "/"
else:
extend_element += "::"
return extend_element
def format_sys_args(sys_args):
""" This comes handy when you integrate it into a python script or django's manage.py.
For example:
```python
if __name__ == "__main__":
if 'test' in sys.argv and 'test-group' not in str(sys.argv):
execute_from_command_line(format_sys_args(sys.argv))
else:
execute_from_command_line(sys.argv)
```
"""
return sys_args[:2] + handling_references(sys_args[2:])
if __name__ == '__main__':
print(handling_references(sys.argv[1:]))