added a check for keywords in non-parts

Sat, 22 Jun 2019 23:52:08 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 22 Jun 2019 23:52:08 +0300
changeset 69
a24c4490d9f2
parent 68
c19dd0d3248e
child 70
2453681c6a69

added a check for keywords in non-parts

header.py file | annotate | diff | comparison | revisions
parse.py file | annotate | diff | comparison | revisions
tests/misc.py file | annotate | diff | comparison | revisions
--- a/header.py	Sat Jun 22 23:38:51 2019 +0300
+++ b/header.py	Sat Jun 22 23:52:08 2019 +0300
@@ -21,6 +21,12 @@
     @property
     def valid(self):
         return True
+    @property
+    def effective_filetype(self):
+        if self.filetype.startswith('Unofficial_'):
+            return self.filetype.rsplit('Unofficial_')[1]
+        else:
+            return self.filetype
 
 class BadHeader:
     def __init__(self, index, reason):
--- a/parse.py	Sat Jun 22 23:38:51 2019 +0300
+++ b/parse.py	Sat Jun 22 23:52:08 2019 +0300
@@ -34,6 +34,8 @@
     @property
     def has_header(self):
         return self.header and not isinstance(self.header, header.BadHeader)
+    def find_first_header_object(self, object_type):
+        return self.body[self.header.first_occurrence[object_type]]
 
 def model_vertices(
     model,
--- a/tests/misc.py	Sat Jun 22 23:38:51 2019 +0300
+++ b/tests/misc.py	Sat Jun 22 23:52:08 2019 +0300
@@ -64,7 +64,7 @@
     if model.header.valid and model.header.bfc == 'NOCERTIFY':
         yield report_problem(
             'bfc-nocertify',
-            bad_object = model.body[model.header.first_occurrence['bfc']],
+            bad_object = model.find_first_header_object('bfc'),
         )
 
 @problem_type('physical-colour-part',
@@ -75,7 +75,7 @@
     if model.header.valid and 'Physical_Colour' in model.header.qualifiers:
         yield report_problem(
             'physical-colour-part',
-            bad_object = model.body[model.header.first_occurrence['part type']],
+            bad_object = model.find_first_header_object('part type'),
         )
 
 @problem_type('unofficial-part',
@@ -86,7 +86,8 @@
     if model.header.valid and not model.header.filetype.startswith('Unofficial_'):
         yield report_problem(
             'unofficial-part',
-            bad_object = model.body[model.header.first_occurrence['part type']])
+            bad_object = model.find_first_header_object('part type')
+        )
 
 @problem_type('primitive-ccw',
     severity = 'hold',
@@ -97,8 +98,6 @@
     message = 'BFC declaration is missing',
 )
 def header_bfc_test(model):
-    def find_the_bfc_object(model):
-        return model.body[model.header.first_occurrence['bfc']],
     if model.header.valid and not model.header.bfc:
         yield report_problem(
             'no-bfc-line',
@@ -109,9 +108,26 @@
     and model.header.bfc != 'CERTIFY CCW':
         yield report_problem(
             'primitive-bfc-ccw',
-            bad_object = find_the_bfc_object(model),
+            bad_object = model.find_first_header_object('bfc'),
         )
 
+@problem_type('keywords-for-nonparts',
+    severity = 'warning',
+    message = lambda type: str.format(
+        'Keywords are not allowed for {type} files',
+        type = type,
+    ),
+)
+def keywords_tests(model):
+    if model.header.valid:
+        if model.header.keywords \
+        and model.header.effective_filetype != 'Part':
+            yield report_problem(
+                'keywords-for-nonparts',
+                bad_object = model.find_first_header_object('keywords'),
+                type = model.header.effective_filetype,
+            )
+
 manifest = {
     'tests': [
         colours_test,
@@ -121,5 +137,6 @@
         physical_colours_test,
         unofficiality_test,
         header_bfc_test,
+        keywords_tests,
     ],
 }

mercurial