Skip to content

Commit 4b01bd1

Browse files
committed
Add major language support, update boolean_attributes, and add dynamic attribute tests
1 parent 68cb3ca commit 4b01bd1

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/typecode/contenttype.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ class Type(object):
205205
"is_filesystem",
206206
"is_java_class",
207207
"is_java_source",
208+
"is_julia_source",
209+
"is_python_source",
210+
"is_javascript_source",
211+
"is_cpp_source",
212+
"is_rust_source",
213+
"is_go_source",
208214
"is_media",
209215
"is_media_with_meta",
210216
"is_office_doc",
@@ -743,7 +749,17 @@ def is_source(self):
743749
return False
744750

745751
# Recognize "known-by-extension" source types
746-
elif self.is_java_source or self.is_c_source or self.is_julia_source:
752+
elif (
753+
self.is_java_source or
754+
self.is_c_source or
755+
self.is_julia_source or
756+
self.is_python_source or
757+
self.is_javascript_source or
758+
self.is_cpp_source or
759+
self.is_rust_source or
760+
self.is_go_source
761+
762+
):
747763
return True
748764

749765
elif self.filetype_pygment or self.is_script is True:
@@ -772,6 +788,26 @@ def is_julia_source(self):
772788
"""
773789
return self.is_file and self.location.lower().endswith(".jl")
774790

791+
@property
792+
def is_python_source(self):
793+
return self.is_file and self.file_name.lower().endswith(('.py', '.pyw'))
794+
795+
@property
796+
def is_javascript_source(self):
797+
return self.is_file and self.file_name.lower().endswith(('.js', '.mjs'))
798+
799+
@property
800+
def is_cpp_source(self):
801+
return self.is_file and self.file_name.lower().endswith(('.cpp', '.cc', '.cxx', '.hpp'))
802+
803+
@property
804+
def is_rust_source(self):
805+
return self.is_file and self.file_name.lower().endswith('.rs')
806+
807+
@property
808+
def is_go_source(self):
809+
return self.is_file and self.file_name.lower().endswith('.go')
810+
775811
@property
776812
def is_c_source(self):
777813
C_EXTENSIONS = set(

tests/test_contenttype.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,23 @@ def test_size(self):
418418
test_dir = self.get_test_loc("contenttype/size")
419419
result = size(test_dir)
420420
assert result == 18
421+
422+
def test_contenttype_language_attributes_exist(self):
423+
from typecode.contenttype import get_type
424+
425+
# Get a Type object to test against using a dummy file extension
426+
test_type = get_type('dummy.txt')
427+
428+
expected_language_attributes = [
429+
'is_c_source',
430+
'is_java_source',
431+
'is_julia_source',
432+
'is_python_source',
433+
'is_javascript_source',
434+
'is_cpp_source',
435+
'is_rust_source',
436+
'is_go_source'
437+
]
438+
439+
for attr_name in expected_language_attributes:
440+
assert hasattr(test_type, attr_name), f"ContentType is missing the expected attribute: {attr_name}"

0 commit comments

Comments
 (0)