Skip to content

Commit 49f1ebd

Browse files
committed
Apply black formatting
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 1496d5a commit 49f1ebd

2 files changed

Lines changed: 54 additions & 50 deletions

File tree

gemfileparser/__init__.py

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class Dependency(object):
2121
"""
2222

2323
def __init__(self):
24-
self.name = ''
24+
self.name = ""
2525
self.requirement = []
26-
self.autorequire = ''
27-
self.source = ''
26+
self.autorequire = ""
27+
self.source = ""
2828
self.parent = []
29-
self.group = ''
29+
self.group = ""
3030

3131
def to_dict(self):
3232
return dict(
@@ -43,41 +43,44 @@ class GemfileParser(object):
4343
"""
4444
Create a GemfileParser object to perform operations.
4545
"""
46+
4647
gemfile_regexes = collections.OrderedDict()
47-
gemfile_regexes['source'] = re.compile(r"source:[ ]?(?P<source>[a-zA-Z:\/\.-]+)")
48-
gemfile_regexes['git'] = re.compile(r"git:[ ]?(?P<git>[a-zA-Z:\/\.-]+)")
49-
gemfile_regexes['platform'] = re.compile(r"platform:[ ]?(?P<platform>[a-zA-Z:\/\.-]+)")
50-
gemfile_regexes['path'] = re.compile(r"path:[ ]?(?P<path>[a-zA-Z:\/\.-]+)")
51-
gemfile_regexes['branch'] = re.compile(r"branch:[ ]?(?P<branch>[a-zA-Z:\/\.-]+)")
52-
gemfile_regexes['autorequire'] = re.compile(r"require:[ ]?(?P<autorequire>[a-zA-Z:\/\.-]+)")
53-
gemfile_regexes['group'] = re.compile(r"group:[ ]?(?P<group>[a-zA-Z:\/\.-]+)")
54-
gemfile_regexes['name'] = re.compile(r"(?P<name>[a-zA-Z]+[\.0-9a-zA-Z _-]*)")
55-
gemfile_regexes['requirement'] = re.compile(r"(?P<requirement>([>|<|=|~>|\d]+[ ]*[0-9\.\w]+[ ,]*)+)")
48+
gemfile_regexes["source"] = re.compile(r"source:[ ]?(?P<source>[a-zA-Z:\/\.-]+)")
49+
gemfile_regexes["git"] = re.compile(r"git:[ ]?(?P<git>[a-zA-Z:\/\.-]+)")
50+
gemfile_regexes["platform"] = re.compile(r"platform:[ ]?(?P<platform>[a-zA-Z:\/\.-]+)")
51+
gemfile_regexes["path"] = re.compile(r"path:[ ]?(?P<path>[a-zA-Z:\/\.-]+)")
52+
gemfile_regexes["branch"] = re.compile(r"branch:[ ]?(?P<branch>[a-zA-Z:\/\.-]+)")
53+
gemfile_regexes["autorequire"] = re.compile(r"require:[ ]?(?P<autorequire>[a-zA-Z:\/\.-]+)")
54+
gemfile_regexes["group"] = re.compile(r"group:[ ]?(?P<group>[a-zA-Z:\/\.-]+)")
55+
gemfile_regexes["name"] = re.compile(r"(?P<name>[a-zA-Z]+[\.0-9a-zA-Z _-]*)")
56+
gemfile_regexes["requirement"] = re.compile(
57+
r"(?P<requirement>([>|<|=|~>|\d]+[ ]*[0-9\.\w]+[ ,]*)+)"
58+
)
5659

5760
group_block_regex = re.compile(r"group[ ]?:[ ]?(?P<groupblock>.*?) do")
5861

5962
gemspec_add_dvtdep_regex = re.compile(r".*add_development_dependency(?P<line>.*)")
6063
gemspec_add_rundep_regex = re.compile(r".*add_runtime_dependency(?P<line>.*)")
6164
gemspec_add_dep_regex = re.compile(r".*add_dependency(?P<line>.*)")
6265

63-
def __init__(self, filepath, appname=''):
66+
def __init__(self, filepath, appname=""):
6467
self.filepath = filepath
6568

66-
self.current_group = 'runtime'
69+
self.current_group = "runtime"
6770

6871
self.appname = appname
6972
self.dependencies = {
70-
'development': [],
71-
'runtime': [],
72-
'dependency': [],
73-
'test': [],
74-
'production': [],
75-
'metrics': [],
73+
"development": [],
74+
"runtime": [],
75+
"dependency": [],
76+
"test": [],
77+
"production": [],
78+
"metrics": [],
7679
}
7780
with open(filepath) as gf:
7881
self.contents = gf.readlines()
7982

80-
self.gemspec = filepath.endswith(('.gemspec', '.podspec'))
83+
self.gemspec = filepath.endswith((".gemspec", ".podspec"))
8184

8285
@staticmethod
8386
def preprocess(line):
@@ -86,7 +89,7 @@ def preprocess(line):
8689
"""
8790

8891
if "#" in line:
89-
line = line[:line.index('#')]
92+
line = line[: line.index("#")]
9093
line = line.strip()
9194
return line
9295

@@ -97,7 +100,7 @@ def parse_line(self, line):
97100

98101
# csv requires a file-like object
99102
linefile = io.StringIO(line)
100-
for line in csv.reader(linefile, delimiter=','):
103+
for line in csv.reader(linefile, delimiter=","):
101104
column_list = []
102105
for column in line:
103106
stripped_column = (
@@ -122,7 +125,7 @@ def parse_line(self, line):
122125
for criteria, criteria_regex in GemfileParser.gemfile_regexes.items():
123126
match = criteria_regex.match(column)
124127
if match:
125-
if criteria == 'requirement':
128+
if criteria == "requirement":
126129
dep.requirement.append(match.group(criteria))
127130
else:
128131
setattr(dep, criteria, match.group(criteria))
@@ -139,18 +142,18 @@ def parse_gemfile(self):
139142

140143
for line in self.contents:
141144
line = self.preprocess(line)
142-
if line == '' or line.startswith('source'):
145+
if line == "" or line.startswith("source"):
143146
continue
144147

145-
elif line.startswith('group'):
148+
elif line.startswith("group"):
146149
match = self.group_block_regex.match(line)
147150
if match:
148-
self.current_group = match.group('groupblock')
151+
self.current_group = match.group("groupblock")
149152

150-
elif line.startswith('end'):
151-
self.current_group = 'runtime'
153+
elif line.startswith("end"):
154+
self.current_group = "runtime"
152155

153-
elif line.startswith('gemspec'):
156+
elif line.startswith("gemspec"):
154157
# Gemfile contains a call to gemspec
155158
gemfiledir = os.path.dirname(self.filepath)
156159
gemspec_list = glob.glob(os.path.join(gemfiledir, "*.gemspec"))
@@ -160,7 +163,7 @@ def parse_gemfile(self):
160163
gemspec_file = gemspec_list[0]
161164
self.parse_gemspec(path=os.path.join(gemfiledir, gemspec_file))
162165

163-
elif line.startswith('gem '):
166+
elif line.startswith("gem "):
164167
line = line[3:]
165168
self.parse_line(line)
166169

@@ -176,17 +179,17 @@ def parse_gemspec(self, path=None):
176179
line = self.preprocess(line)
177180
match = self.gemspec_add_dvtdep_regex.match(line)
178181
if match:
179-
self.current_group = 'development'
182+
self.current_group = "development"
180183
else:
181184
match = self.gemspec_add_rundep_regex.match(line)
182185
if match:
183-
self.current_group = 'runtime'
186+
self.current_group = "runtime"
184187
else:
185188
match = self.gemspec_add_dep_regex.match(line)
186189
if match:
187-
self.current_group = 'dependency'
190+
self.current_group = "dependency"
188191
if match:
189-
line = match.group('line')
192+
line = match.group("line")
190193
self.parse_line(line)
191194
return self.dependencies
192195

@@ -205,6 +208,7 @@ def command_line():
205208
A minimal command line entry point.
206209
"""
207210
import sys
211+
208212
if len(sys.argv) < 2:
209213
print("Usage : parsegemfile <input file>")
210214
sys.exit(0)

tests/test_gemfileparser.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ def check_gemparser_results(test_file, regen=False):
1616

1717
gemparser = GemfileParser(test_file)
1818
dependencies = {
19-
group: [dep.to_dict() for dep in deps]
20-
for group, deps in gemparser.parse().items()
19+
group: [dep.to_dict() for dep in deps] for group, deps in gemparser.parse().items()
2120
}
2221

23-
expected_file = test_file + '-expected.json'
22+
expected_file = test_file + "-expected.json"
2423
if regen:
25-
with open(expected_file, 'w') as o:
24+
with open(expected_file, "w") as o:
2625
json.dump(dependencies, o, indent=2)
2726

2827
with open(expected_file) as o:
@@ -32,39 +31,40 @@ def check_gemparser_results(test_file, regen=False):
3231

3332

3433
def test_source_only_gemfile():
35-
check_gemparser_results('tests/Gemfile')
34+
check_gemparser_results("tests/Gemfile")
3635

3736

3837
def test_gemfile_1():
39-
check_gemparser_results('tests/Gemfile_1')
38+
check_gemparser_results("tests/Gemfile_1")
4039

4140

4241
def test_gemfile_2():
43-
check_gemparser_results('tests/Gemfile_2')
42+
check_gemparser_results("tests/Gemfile_2")
4443

4544

4645
def test_gemfile_3():
47-
check_gemparser_results('tests/Gemfile_3')
46+
check_gemparser_results("tests/Gemfile_3")
4847

4948

5049
def test_gemfile_4():
51-
check_gemparser_results('tests/Gemfile_4')
50+
check_gemparser_results("tests/Gemfile_4")
5251

5352

5453
def test_gemfile_platforms():
55-
check_gemparser_results('tests/Gemfile_5')
54+
check_gemparser_results("tests/Gemfile_5")
5655

5756

5857
def test_gemspec_1():
59-
check_gemparser_results('tests/sample.gemspec')
58+
check_gemparser_results("tests/sample.gemspec")
6059

6160

6261
def test_gemspec_2():
63-
check_gemparser_results('tests/address_standardization.gemspec')
62+
check_gemparser_results("tests/address_standardization.gemspec")
6463

6564

6665
def test_gemspec_3():
67-
check_gemparser_results('tests/arel.gemspec')
66+
check_gemparser_results("tests/arel.gemspec")
67+
6868

6969
def test_gemspec_4():
70-
check_gemparser_results('tests/logstash-mixin-ecs_compatibility_support.gemspec')
70+
check_gemparser_results("tests/logstash-mixin-ecs_compatibility_support.gemspec")

0 commit comments

Comments
 (0)