#!/usr/bin/env python# -*- coding: utf-8 -*-## This file is part of the `pypath` python module## Copyright 2014-2023# EMBL, EMBL-EBI, Uniklinik RWTH Aachen, Heidelberg University## Authors: see the file `README.rst`# Contact: Dénes Türei (turei.denes@gmail.com)## Distributed under the GPLv3 License.# See accompanying file LICENSE.txt or copy at# https://www.gnu.org/licenses/gpl-3.0.html## Website: https://pypath.omnipathdb.org/#fromfuture.utilsimportiteritemsimportosimportjsonimportwarningsimportpypath.share.commonascommon# these are all ordinal scales from the smallest to the greatest freedom_purpose_levels={'composite':25,'free':20,'commercial':15,'for_profit':15,'forprofit':15,'nonprofit':10,'non_profit':10,'academic':5,'ignore':0,}_sharing_levels={'composite':20,'free':25,'share':20,'deriv':20,'alike':15,'noderiv':10,'noshare':5,'ignore':0,}_attrib_levels={'composite':20,'free':10,'noattrib':10,'attrib':5,'ignore':0,}_int_purpose_levels=common.swap_dict(_purpose_levels)_int_sharing_levels=common.swap_dict(_sharing_levels)_int_attrib_levels=common.swap_dict(_attrib_levels)
[docs]@classmethoddeflevel_to_int(cls,level):""" Return the value for the key ``level`` as an integer on the ordinal scale of the levels. """ifcls.check_level(level):returncls.levels[level]return99
[docs]@classmethoddefint_to_level(cls,i):""" Returns a set of labels corresponding to the level nearest to the integer ``i`` on the ordinal scale of levels. """ifi>max(cls.int_levels.keyd()):i=max(cls.int_levels.keys())elifinotincls.int_levels:for_iinsorted(cls.int_levels.keys()):if_i>i:i=_ibreakreturncls.int_levels[i]
[docs]@classmethoddefcheck_level(cls,level):""" Checks of ``level`` is a valid key for the """iflevelnotincls.levels:cls.unknown_warning(level)returnFalsereturnTrue
[docs]defto_int(self):""" Returns the value of the current level of this license feature as an integer on the ordinal scale of levels. """returnself.level_to_int(self.level)
def__int__(self):returnself.to_int()@classmethoddefensure_int(cls,other):ifisinstance(other,int):returnotherelifisinstance(other,str):returncls.level_to_int(other)else:try:returnint(other)except(TypeError,ValueError):cls.unknown_warning(other)return99def__eq__(self,other):i_level=self.ensure_int(other)returnint(self)==i_leveldef__gt__(self,other):i_level=self.ensure_int(other)returnint(self)>i_leveldef__ge__(self,other):i_level=self.ensure_int(other)returnint(self)>=i_leveldef__lt__(self,other):i_level=self.ensure_int(other)returnint(self)<i_leveldef__le__(self,other):i_level=self.ensure_int(other)returnint(self)<=i_leveldefenables(self,other):returnself>=other@classmethoddefunknown_warning(cls,value):warnings.warn('Unknown `%s` level for licenses: `%s`. ''This will always disable the resource.'%(cls.name,str(value),))
[docs]defenables(self,purpose,sharing=None,attrib=None):""" Checks if the license enables a particular use, according to purpose, sharing and attribution. For example, to check if the license enables academic use with redistribution under a compatible license, call ``License.enables(purpose = 'academic', sharing = 'alike')``. """return((notattriborself.attrib.enables(attrib))and(notsharingorself.sharing.enables(sharing))and(self.purpose.enables(purpose)))
@propertydeffeatures(self):returndict((aspect,getattr(self,aspect).level)foraspectin('purpose','sharing','attrib'))@propertydeffeatures_str(self):returncommon.dict_str(self.features)@classmethoddeffrom_json(cls,path,**kwargs):withopen(path,'r')asfp:json_data=json.load(fp)json_data.update(kwargs)returncls(**json_data)# some shortcut methods@classmethoddef_generate_enables_methods(cls):defget_enables_method(aspect,level):@propertydefenables_method(self):returngetattr(self,aspect).enables(level)returnenables_methodforaspectin('attrib','sharing','purpose'):forlevelinglobals()['_%s_levels'%aspect]:iflevelin('composite','ignore','free','attrib'):continuemethod=get_enables_method(aspect,level)method_name=levelsetattr(cls,level,method)