PK!OZslackperson.py"""Defines a class for storing a person's slack profile information.""" import attr from nameparser import HumanName def _makenames(user): fname = '' lname = '' if 'first_name' in user['profile']: fname = user['profile']['first_name'] if 'last_name' in user['profile']: lname = user['profile']['last_name'] if not fname: if 'real_name' in user['profile']: name = HumanName(user['profile']['real_name']) fname = name.first lname = name.last elif 'display_name' in user['profile']: name = HumanName(user['profile']['display_name']) fname = name.first lname = name.last else: fname = '' lname = '' return fname, lname @attr.s class SlackPerson(object): """Class for storing/getting slack profile information.""" userid = attr.ib() email = attr.ib() username = attr.ib() fname = attr.ib() lname = attr.ib() team = attr.ib() @classmethod def from_userlist(cls, team_user_list, username=None, userid=None): """Initializes SlackPerson. Arguments: username: the handle with or without @ used to mention someone userid: the internal userid from slack. team_user_list: json object returned by the slack api `users.list` method. """ if username: if username[0] == '@': username = username[1:] test_value = username key = 'name' elif userid: test_value = userid key = 'id' else: raise AttributeError("username or user_id is required") try: for user in team_user_list['members']: if test_value == user[key]: fname, lname = _makenames(user) # parse the json to get the user's info return cls(username=user['name'], userid=user['id'], email=user['profile']['email'], fname=fname, lname=lname, team=user['profile']['team'] ) except (KeyError, TypeError) as err: raise SlackDataError( "team_user_list is not formed correctly: {}".format(err)) raise SlackDataError("{} was not found in the user list".format( test_value)) @classmethod def from_userinfo(cls, userinfo): """Initializes SlackPerson. Arguments: userinfo: json object returned by slackapi 'user.info' """ # parse the json to get the user's info if not isinstance(userinfo, dict): raise TypeError('userinfo is not a dict') try: user = userinfo['user'] except KeyError: try: _ = userinfo['name'] user = userinfo except KeyError as err: raise SlackDataError(("userinfo is not formed correctly: " " {}".format(err))) try: fname, lname = _makenames(user) return cls(username=user['name'], userid=user['id'], email=user['profile']['email'], fname=fname, lname=lname, team=user['profile']['team'] ) except (KeyError, TypeError) as err: raise SlackDataError(("userinfo is not formed correctly: " " {}".format(err))) class SlackDataError(Exception): """Exception raised if input data is invalid in some way.""" pass PK!Z=//!slackperson-0.5.dist-info/LICENSEMIT License Copyright (c) 2017 Frederic Henry Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HnHTUslackperson-0.5.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!H%_ Z"slackperson-0.5.dist-info/METADATAKo0{l%b%<P! lMکĿ]h= K/$˥F[tk؏۵ZK: )E-oyKxACTTi6FVas_sTZ$`€9:Tp }]U^XͶ*Bl=CXO :ֻ̚P |"I`)MՆ?mAW;'FW׿ 'Ivqv 䐾@}PK!HFUQt slackperson-0.5.dist-info/RECORD}Kr0нg6tE 2|ba@"p'4v]t *Ս{Ayiqe܅I"lZ){&:%ҍgpa,N/tk/sAJ=1.ba&w6D` 'G̃g0PK!OZslackperson.pyPK!Z=//!slackperson-0.5.dist-info/LICENSEPK!HnHTUslackperson-0.5.dist-info/WHEELPK!H%_ Z"slackperson-0.5.dist-info/METADATAPK!HFUQt |slackperson-0.5.dist-info/RECORDPKv