Beispiel #1
0
 def test_create_class(self):
     class_metadata = {'module': 'collections', 'name': 'Counter'}
     cls = BW2Package._create_class(class_metadata, False)
     import collections
     self.assertEqual(cls, collections.Counter)
     class_metadata = {'module': 'bw2data.method', 'name': 'Method'}
     cls = BW2Package._create_class(class_metadata, False)
     self.assertEqual(cls, Method)
Beispiel #2
0
 def test_create_class_whitelist(self):
     bad_class_metadata = {
         'module': 'some.package',
         'name': 'Foo',
     }
     with self.assertRaises(UnsafeData):
         BW2Package._create_class(bad_class_metadata)
     with self.assertRaises(ImportError):
         BW2Package._create_class(bad_class_metadata, False)
Beispiel #3
0
    def test_create_class(self):
        class_metadata = {"module": "collections", "name": "Counter"}
        cls = BW2Package._create_class(class_metadata, False)
        import collections

        self.assertEqual(cls, collections.Counter)
        class_metadata = {"module": "bw2data.method", "name": "Method"}
        cls = BW2Package._create_class(class_metadata, False)
        self.assertEqual(cls, Method)
Beispiel #4
0
 def test_create_class_whitelist(self):
     bad_class_metadata = {
         "module": "some.package",
         "name": "Foo",
     }
     with self.assertRaises(UnsafeData):
         BW2Package._create_class(bad_class_metadata)
     with self.assertRaises(ImportError):
         BW2Package._create_class(bad_class_metadata, False)
Beispiel #5
0
 def test_whitelist(self):
     good_class_metadata = {
         "module": "bw2io.tests",
         "name": "MockDS",
     }
     bad_class_metadata = {
         "module": "some.package",
         "name": "Foo",
     }
     self.assertTrue(BW2Package._is_whitelisted(good_class_metadata))
     self.assertFalse(BW2Package._is_whitelisted(bad_class_metadata))
Beispiel #6
0
 def test_whitelist(self):
     good_class_metadata = {
         'module': 'bw2io.tests',
         'name': 'MockDS',
     }
     bad_class_metadata = {
         'module': 'some.package',
         'name': 'Foo',
     }
     self.assertTrue(BW2Package._is_whitelisted(good_class_metadata))
     self.assertFalse(BW2Package._is_whitelisted(bad_class_metadata))
Beispiel #7
0
 def test_class_metadata(self):
     class_metadata = {
         'module': 'bw2io.tests',
         'name': 'MockDS',
     }
     self.assertEqual(BW2Package._get_class_metadata(MockDS('foo')),
                      class_metadata)
Beispiel #8
0
 def test_class_metadata(self):
     class_metadata = {
         "module": "bw2io.tests",
         "name": "MockDS",
     }
     self.assertEqual(BW2Package._get_class_metadata(MockDS("foo")),
                      class_metadata)
Beispiel #9
0
 def test_roundtrip_obj(self):
     obj = MockDS("Slick Al")
     obj.register()
     obj.write(["a boring string", {'foo': 'bar'}, (1, 2, 3)])
     fp = BW2Package.export_obj(obj)
     obj.deregister()
     del obj
     self.assertFalse('Slick Al' in mocks)
     obj_list = BW2Package.import_file(fp)
     self.assertEqual(len(obj_list), 1)
     obj = obj_list[0]
     self.assertTrue('Slick Al' in mocks)
     self.assertTrue(isinstance(obj, MockDS))
     self.assertEqual(obj.load(),
                      ["a boring string", {
                          'foo': 'bar'
                      }, (1, 2, 3)])
Beispiel #10
0
 def test_load_obj(self):
     test_data = {
         "metadata": {
             "foo": "bar"
         },
         "name": ["Johnny", "B", "Good"],
         "class": {
             "module": "fractions",
             "name": "Fraction"
         },
         "data": {},
     }
     after = BW2Package._load_obj(copy.deepcopy(test_data), False)
     for key in test_data:
         self.assertTrue(key in after)
     with self.assertRaises(InvalidPackage):
         BW2Package._load_obj({})
     self.assertEqual(after["class"], fractions.Fraction)
Beispiel #11
0
 def test_load_obj(self):
     test_data = {
         'metadata': {
             'foo': 'bar'
         },
         'name': ['Johnny', 'B', 'Good'],
         'class': {
             'module': 'fractions',
             'name': 'Fraction'
         },
         'data': {}
     }
     after = BW2Package._load_obj(copy.deepcopy(test_data), False)
     for key in test_data:
         self.assertTrue(key in after)
     with self.assertRaises(InvalidPackage):
         BW2Package._load_obj({})
     self.assertEqual(after['class'], fractions.Fraction)
Beispiel #12
0
def export_db(db_name, filename) :
    """Export Db and linked parameters"""
    db = bw.Database(db_name)
    db_params = DatabaseParameter.select().where(DatabaseParameter.database == db_name)

    # Export Db params
    db.metadata["database_parameters"] = [param_data(param) for param in db_params]

    # List of all project params used in this dataset
    used_project_params = list(param.name for param in _listParams(db_name) if param.dbname is None)

    if len(used_project_params) > 0 :
        error('Warning : this DB uses project parameters that are exported as well and might override project params at import time : ', used_project_params)

        proj_params = list(ProjectParameter.get(ProjectParameter.name==name) for name in used_project_params)

        db.metadata["project_parameters"] = [param_data(param) for param in proj_params]

    BW2Package._write_file(filename, [BW2Package._prepare_obj(db, False)])
Beispiel #13
0
 def test_validation(self):
     good_dict = {
         'metadata': {
             'foo': 'bar'
         },
         'name': 'Johnny',
         'class': {
             'module': 'some',
             'name': 'thing'
         },
         'data': {}
     }
     self.assertTrue(BW2Package._is_valid_package(good_dict))
     d = copy.deepcopy(good_dict)
     d['name'] = ()
     self.assertTrue(BW2Package._is_valid_package(d))
     for key in ['metadata', 'name', 'data']:
         d = copy.deepcopy(good_dict)
         del d[key]
         self.assertFalse(BW2Package._is_valid_package(d))
Beispiel #14
0
 def test_validation(self):
     good_dict = {
         "metadata": {
             "foo": "bar"
         },
         "name": "Johnny",
         "class": {
             "module": "some",
             "name": "thing"
         },
         "data": {},
     }
     self.assertTrue(BW2Package._is_valid_package(good_dict))
     d = copy.deepcopy(good_dict)
     d["name"] = ()
     self.assertTrue(BW2Package._is_valid_package(d))
     for key in ["metadata", "name", "data"]:
         d = copy.deepcopy(good_dict)
         del d[key]
         self.assertFalse(BW2Package._is_valid_package(d))
Beispiel #15
0
 def test_create_obj(self):
     mock_data = {
         'class': {
             'module': 'bw2io.tests',
             'name': 'MockDS'
         },
         'metadata': {
             'circle': 'square'
         },
         'data': [],
         'name': 'Wilhelm'
     }
     data = BW2Package._load_obj(mock_data)
     obj = BW2Package._create_obj(data)
     self.assertTrue(isinstance(obj, MockDS))
     self.assertTrue("Wilhelm" in mocks)
     self.assertEqual(mocks['Wilhelm'], {'circle': 'square'})
     mocks.__init__()
     self.assertEqual(mocks['Wilhelm'], {'circle': 'square'})
     self.assertEqual(MockDS("Wilhelm").load(), [])
Beispiel #16
0
 def test_create_obj(self):
     mock_data = {
         "class": {
             "module": "bw2io.tests",
             "name": "MockDS"
         },
         "metadata": {
             "circle": "square"
         },
         "data": [],
         "name": "Wilhelm",
     }
     data = BW2Package._load_obj(mock_data)
     obj = BW2Package._create_obj(data)
     self.assertTrue(isinstance(obj, MockDS))
     self.assertTrue("Wilhelm" in mocks)
     self.assertEqual(mocks["Wilhelm"], {"circle": "square"})
     mocks.__init__()
     self.assertEqual(mocks["Wilhelm"], {"circle": "square"})
     self.assertEqual(MockDS("Wilhelm").load(), [])
Beispiel #17
0
    def backup(self):
        """Save a backup to ``backups`` folder.

        Returns:
            File path of backup.

        """
        try:
            from bw2io import BW2Package
            return BW2Package.export_obj(self)
        except ImportError:
            print("bw2io not installed")
Beispiel #18
0
def import_db(filename) :
    """Export Db and linked parameters"""

    db = BW2Package.import_file(filename)[0]
    if "database_parameters" in db.metadata :
        params = db.metadata["database_parameters"]
        bw.parameters.new_database_parameters(params, db.name)

    if "project_parameters" in db.metadata:
        params = db.metadata["project_parameters"]
        bw.parameters.new_project_parameters(params)

    # Reload the parameters
    loadParams()

    return db
Beispiel #19
0
    def extract_BW2Package(self, packagefilepath):

        imported_file = BW2Package.load_file(packagefilepath)
        for i in range(len(imported_file)):

            data = [v for k, v in imported_file[i]['data'].items()]

            for ds in data:
                if 'parameters' in ds.keys():
                    parameters, parameters_full = convert_parameters_to_wurst_style(
                        ds['parameters'])
                    ds['parameters'] = parameters
                    ds['parameters full'] = parameters_full

            name = imported_file[i]['name']

            add_input_info_for_indigenous_exchanges(data, [name])

            #add_input_info_for_indigenous_exchanges(data, [name])

            self.db.extend(data)
            self.database_names.append(name)