Skip to content

Commit 6b45cf5

Browse files
authored
Merge pull request #377 from nexB/376-about-from-dict
Add a `from_dict` constructor on the `About` class
2 parents b3e4d3b + e4ad747 commit 6b45cf5

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

configure

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ fi
2121

2222
CONFIGURE_ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2323

24-
python "$CONFIGURE_ROOT_DIR/etc/configure.py" $CFG_CMD_LINE_ARGS
25-
if [ -f "$CONFIGURE_ROOT_DIR/bin/activate" ]; then
26-
source $CONFIGURE_ROOT_DIR/bin/activate
24+
if [[ "$PYTHON_EXE" == "" ]]; then
25+
PYTHON_EXE=python
2726
fi
27+
28+
$PYTHON_EXE "$CONFIGURE_ROOT_DIR/etc/configure.py" $CFG_CMD_LINE_ARGS

src/attributecode/model.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,15 @@ def load_dict(self, fields_dict, base_dir, running_inventory=False, reference_di
987987
self.errors = errors
988988
return errors
989989

990+
@classmethod
991+
def from_dict(cls, about_data, base_dir=''):
992+
"""
993+
Return an About object loaded from a python dict.
994+
"""
995+
about = cls()
996+
about.load_dict(about_data, base_dir=base_dir)
997+
return about
998+
990999
def dumps(self):
9911000
"""
9921001
Return self as a formatted ABOUT string.

tests/test_model.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,31 @@ def test_load_dict_as_dict_is_idempotent_ignoring_special(self):
849849

850850
assert expected == dict(as_dict)
851851

852+
def test_about_model_class_from_dict_constructor(self):
853+
about_data = {
854+
'about_resource': ['.'],
855+
'attribute': 'yes',
856+
'author': ['Jillian Daguil, Chin Yeung Li, Philippe Ombredanne, Thomas Druez'],
857+
'copyright': 'Copyright (c) 2013-2014 nexB Inc.',
858+
'description': 'AboutCode is a tool to process ABOUT files. An ABOUT file is a file.',
859+
'homepage_url': 'http://dejacode.org',
860+
'license_expression': 'apache-2.0',
861+
'name': 'AboutCode',
862+
'owner': 'nexB Inc.',
863+
'vcs_repository': 'https://github.com/dejacode/about-code-tool.git',
864+
'vcs_tool': 'git',
865+
'version': '0.11.0',
866+
}
867+
868+
about = model.About.from_dict(about_data)
869+
assert isinstance(about, model.About)
870+
871+
about_data.update({
872+
'about_file_path': None,
873+
'about_resource': OrderedDict([('.', None)]),
874+
})
875+
assert about_data == about.as_dict()
876+
852877
def test_write_output_csv(self):
853878
path = 'test_model/this.ABOUT'
854879
test_file = get_test_loc(path)

0 commit comments

Comments
 (0)