/Skype4Py/conversion.py

https://github.com/mcahriman/Skype4Py
Python | 526 lines | 324 code | 29 blank | 173 comment | 15 complexity | 52358c2f239e3832523565c80ff0f9ba MD5 | raw file
  1. """Conversion between constants and text.
  2. """
  3. __docformat__ = 'restructuredtext en'
  4. import os
  5. import enums
  6. # Following code is needed when building executable files using py2exe.
  7. # Together with the lang.__init__ it makes sure that all languages
  8. # are included in the package built by py2exe. The tool looks just at
  9. # the imports, it ignores the 'if' statement.
  10. #
  11. # More about py2exe: http://www.py2exe.org/
  12. if False:
  13. import lang
  14. class Conversion(object):
  15. """Allows conversion between constants and text. Access using `skype.Skype.Convert`.
  16. """
  17. def __init__(self, Skype):
  18. """__init__.
  19. :Parameters:
  20. Skype : `Skype`
  21. Skype object.
  22. """
  23. self._Language = ''
  24. self._Module = None
  25. self._SetLanguage('en')
  26. def _TextTo(self, Prefix, Value):
  27. enum = [z for z in [(y, getattr(enums, y)) for y in [x for x in dir(enums) if x.startswith(Prefix)]] if z[1] == Value]
  28. if enum:
  29. return str(Value)
  30. raise ValueError('Bad text')
  31. def _ToText(self, Prefix, Value):
  32. enum = [z for z in [(y, getattr(enums, y)) for y in [x for x in dir(enums) if x.startswith(Prefix)]] if z[1] == Value]
  33. if enum:
  34. try:
  35. return unicode(getattr(self._Module, enum[0][0]))
  36. except AttributeError:
  37. pass
  38. raise ValueError('Bad identifier')
  39. def AttachmentStatusToText(self, Status):
  40. """Returns attachment status as text.
  41. :Parameters:
  42. Status : `enums`.apiAttach*
  43. Attachment status.
  44. :return: Text describing the attachment status.
  45. :rtype: unicode
  46. """
  47. return self._ToText('api', Status)
  48. def BuddyStatusToText(self, Status):
  49. """Returns buddy status as text.
  50. :Parameters:
  51. Status : `enums`.bud*
  52. Buddy status.
  53. :return: Text describing the buddy status.
  54. :rtype: unicode
  55. """
  56. return self._ToText('bud', Status)
  57. def CallFailureReasonToText(self, Reason):
  58. """Returns failure reason as text.
  59. :Parameters:
  60. Reason : `enums`.cfr*
  61. Call failure reason.
  62. :return: Text describing the call failure reason.
  63. :rtype: unicode
  64. """
  65. return self._ToText('cfr', Reason)
  66. def CallStatusToText(self, Status):
  67. """Returns call status as text.
  68. :Parameters:
  69. Status : `enums`.cls*
  70. Call status.
  71. :return: Text describing the call status.
  72. :rtype: unicode
  73. """
  74. return self._ToText('cls', Status)
  75. def CallTypeToText(self, Type):
  76. """Returns call type as text.
  77. :Parameters:
  78. Type : `enums`.clt*
  79. Call type.
  80. :return: Text describing the call type.
  81. :rtype: unicode
  82. """
  83. return self._ToText('clt', Type)
  84. def CallVideoSendStatusToText(self, Status):
  85. """Returns call video send status as text.
  86. :Parameters:
  87. Status : `enums`.vss*
  88. Call video send status.
  89. :return: Text describing the call video send status.
  90. :rtype: unicode
  91. """
  92. return self._ToText('vss', Status)
  93. def CallVideoStatusToText(self, Status):
  94. """Returns call video status as text.
  95. :Parameters:
  96. Status : `enums`.cvs*
  97. Call video status.
  98. :return: Text describing the call video status.
  99. :rtype: unicode
  100. """
  101. return self._ToText('cvs', Status)
  102. def ChatLeaveReasonToText(self, Reason):
  103. """Returns leave reason as text.
  104. :Parameters:
  105. Reason : `enums`.lea*
  106. Chat leave reason.
  107. :return: Text describing the chat leave reason.
  108. :rtype: unicode
  109. """
  110. return self._ToText('lea', Reason)
  111. def ChatMessageStatusToText(self, Status):
  112. """Returns message status as text.
  113. :Parameters:
  114. Status : `enums`.cms*
  115. Chat message status.
  116. :return: Text describing the chat message status.
  117. :rtype: unicode
  118. """
  119. return self._ToText('cms', Status)
  120. def ChatMessageTypeToText(self, Type):
  121. """Returns message type as text.
  122. :Parameters:
  123. Type : `enums`.cme*
  124. Chat message type.
  125. :return: Text describing the chat message type.
  126. :rtype: unicode
  127. """
  128. return self._ToText('cme', Type)
  129. def ChatStatusToText(self, Status):
  130. """Returns chatr status as text.
  131. :Parameters:
  132. Status : `enums`.chs*
  133. Chat status.
  134. :return: Text describing the chat status.
  135. :rtype: unicode
  136. """
  137. return self._ToText('chs', Status)
  138. def ConnectionStatusToText(self, Status):
  139. """Returns connection status as text.
  140. :Parameters:
  141. Status : `enums`.con*
  142. Connection status.
  143. :return: Text describing the connection status.
  144. :rtype: unicode
  145. """
  146. return self._ToText('con', Status)
  147. def GroupTypeToText(self, Type):
  148. """Returns group type as text.
  149. :Parameters:
  150. Type : `enums`.grp*
  151. Group type.
  152. :return: Text describing the group type.
  153. :rtype: unicode
  154. """
  155. return self._ToText('grp', Type)
  156. def OnlineStatusToText(self, Status):
  157. """Returns online status as text.
  158. :Parameters:
  159. Status : `enums`.ols*
  160. Online status.
  161. :return: Text describing the online status.
  162. :rtype: unicode
  163. """
  164. return self._ToText('ols', Status)
  165. def SmsMessageStatusToText(self, Status):
  166. """Returns SMS message status as text.
  167. :Parameters:
  168. Status : `enums`.smsMessageStatus*
  169. SMS message status.
  170. :return: Text describing the SMS message status.
  171. :rtype: unicode
  172. """
  173. return self._ToText('smsMessageStatus', Status)
  174. def SmsMessageTypeToText(self, Type):
  175. """Returns SMS message type as text.
  176. :Parameters:
  177. Type : `enums`.smsMessageType*
  178. SMS message type.
  179. :return: Text describing the SMS message type.
  180. :rtype: unicode
  181. """
  182. return self._ToText('smsMessageType', Type)
  183. def SmsTargetStatusToText(self, Status):
  184. """Returns SMS target status as text.
  185. :Parameters:
  186. Status : `enums`.smsTargetStatus*
  187. SMS target status.
  188. :return: Text describing the SMS target status.
  189. :rtype: unicode
  190. """
  191. return self._ToText('smsTargetStatus', Status)
  192. def TextToAttachmentStatus(self, Text):
  193. """Returns attachment status code.
  194. :Parameters:
  195. Text : unicode
  196. Text, one of 'UNKNOWN', 'SUCCESS', 'PENDING_AUTHORIZATION', 'REFUSED', 'NOT_AVAILABLE',
  197. 'AVAILABLE'.
  198. :return: Attachment status.
  199. :rtype: `enums`.apiAttach*
  200. """
  201. conv = {'UNKNOWN': enums.apiAttachUnknown,
  202. 'SUCCESS': enums.apiAttachSuccess,
  203. 'PENDING_AUTHORIZATION': enums.apiAttachPendingAuthorization,
  204. 'REFUSED': enums.apiAttachRefused,
  205. 'NOT_AVAILABLE': enums.apiAttachNotAvailable,
  206. 'AVAILABLE': enums.apiAttachAvailable}
  207. try:
  208. return self._TextTo('api', conv[Text.upper()])
  209. except KeyError:
  210. raise ValueError('Bad text')
  211. def TextToBuddyStatus(self, Text):
  212. """Returns buddy status code.
  213. :Parameters:
  214. Text : unicode
  215. Text, one of 'UNKNOWN', 'NEVER_BEEN_FRIEND', 'DELETED_FRIEND', 'PENDING_AUTHORIZATION',
  216. 'FRIEND'.
  217. :return: Buddy status.
  218. :rtype: `enums`.bud*
  219. """
  220. conv = {'UNKNOWN': enums.budUnknown,
  221. 'NEVER_BEEN_FRIEND': enums.budNeverBeenFriend,
  222. 'DELETED_FRIEND': enums.budDeletedFriend,
  223. 'PENDING_AUTHORIZATION': enums.budPendingAuthorization,
  224. 'FRIEND': enums.budFriend}
  225. try:
  226. return self._TextTo('bud', conv[Text.upper()])
  227. except KeyError:
  228. raise ValueError('Bad text')
  229. def TextToCallStatus(self, Text):
  230. """Returns call status code.
  231. :Parameters:
  232. Text : unicode
  233. Text, one of `enums`.cls*.
  234. :return: Call status.
  235. :rtype: `enums`.cls*
  236. :note: Currently, this method only checks if the given string is one of the allowed ones and
  237. returns it or raises a ``ValueError``.
  238. """
  239. return self._TextTo('cls', Text)
  240. def TextToCallType(self, Text):
  241. """Returns call type code.
  242. :Parameters:
  243. Text : unicode
  244. Text, one of `enums`.clt*.
  245. :return: Call type.
  246. :rtype: `enums`.clt*
  247. :note: Currently, this method only checks if the given string is one of the allowed ones and
  248. returns it or raises a ``ValueError``.
  249. """
  250. return self._TextTo('clt', Text)
  251. def TextToChatMessageStatus(self, Text):
  252. """Returns message status code.
  253. :Parameters:
  254. Text : unicode
  255. Text, one of `enums`.cms*.
  256. :return: Chat message status.
  257. :rtype: `enums`.cms*
  258. :note: Currently, this method only checks if the given string is one of the allowed ones and
  259. returns it or raises a ``ValueError``.
  260. """
  261. return self._TextTo('cms', Text)
  262. def TextToChatMessageType(self, Text):
  263. """Returns message type code.
  264. :Parameters:
  265. Text : unicode
  266. Text, one of `enums`.cme*.
  267. :return: Chat message type.
  268. :rtype: `enums`.cme*
  269. :note: Currently, this method only checks if the given string is one of the allowed ones and
  270. returns it or raises a ``ValueError``.
  271. """
  272. return self._TextTo('cme', Text)
  273. def TextToConnectionStatus(self, Text):
  274. """Retunes connection status code.
  275. :Parameters:
  276. Text : unicode
  277. Text, one of `enums`.con*.
  278. :return: Connection status.
  279. :rtype: `enums`.con*
  280. :note: Currently, this method only checks if the given string is one of the allowed ones and
  281. returns it or raises a ``ValueError``.
  282. """
  283. return self._TextTo('con', Text)
  284. def TextToGroupType(self, Text):
  285. """Returns group type code.
  286. :Parameters:
  287. Text : unicode
  288. Text, one of `enums`.grp*.
  289. :return: Group type.
  290. :rtype: `enums`.grp*
  291. :note: Currently, this method only checks if the given string is one of the allowed ones and
  292. returns it or raises a ``ValueError``.
  293. """
  294. return self._TextTo('grp', Text)
  295. def TextToOnlineStatus(self, Text):
  296. """Returns online status code.
  297. :Parameters:
  298. Text : unicode
  299. Text, one of `enums`.ols*.
  300. :return: Online status.
  301. :rtype: `enums`.ols*
  302. :note: Currently, this method only checks if the given string is one of the allowed ones and
  303. returns it or raises a ``ValueError``.
  304. """
  305. return self._TextTo('ols', Text)
  306. def TextToUserSex(self, Text):
  307. """Returns user sex code.
  308. :Parameters:
  309. Text : unicode
  310. Text, one of `enums`.usex*.
  311. :return: User sex.
  312. :rtype: `enums`.usex*
  313. :note: Currently, this method only checks if the given string is one of the allowed ones and
  314. returns it or raises a ``ValueError``.
  315. """
  316. return self._TextTo('usex', Text)
  317. def TextToUserStatus(self, Text):
  318. """Returns user status code.
  319. :Parameters:
  320. Text : unicode
  321. Text, one of `enums`.cus*.
  322. :return: User status.
  323. :rtype: `enums`.cus*
  324. :note: Currently, this method only checks if the given string is one of the allowed ones and
  325. returns it or raises a ``ValueError``.
  326. """
  327. return self._TextTo('cus', Text)
  328. def TextToVoicemailStatus(self, Text):
  329. """Returns voicemail status code.
  330. :Parameters:
  331. Text : unicode
  332. Text, one of `enums`.vms*.
  333. :return: Voicemail status.
  334. :rtype: `enums`.vms*
  335. :note: Currently, this method only checks if the given string is one of the allowed ones and
  336. returns it or raises a ``ValueError``.
  337. """
  338. return self._TextTo('vms', Text)
  339. def UserSexToText(self, Sex):
  340. """Returns user sex as text.
  341. :Parameters:
  342. Sex : `enums`.usex*
  343. User sex.
  344. :return: Text describing the user sex.
  345. :rtype: unicode
  346. """
  347. return self._ToText('usex', Sex)
  348. def UserStatusToText(self, Status):
  349. """Returns user status as text.
  350. :Parameters:
  351. Status : `enums`.cus*
  352. User status.
  353. :return: Text describing the user status.
  354. :rtype: unicode
  355. """
  356. return self._ToText('cus', Status)
  357. def VoicemailFailureReasonToText(self, Reason):
  358. """Returns voicemail failure reason as text.
  359. :Parameters:
  360. Reason : `enums`.vmr*
  361. Voicemail failure reason.
  362. :return: Text describing the voicemail failure reason.
  363. :rtype: unicode
  364. """
  365. return self._ToText('vmr', Reason)
  366. def VoicemailStatusToText(self, Status):
  367. """Returns voicemail status as text.
  368. :Parameters:
  369. Status : `enums`.vms*
  370. Voicemail status.
  371. :return: Text describing the voicemail status.
  372. :rtype: unicode
  373. """
  374. return self._ToText('vms', Status)
  375. def VoicemailTypeToText(self, Type):
  376. """Returns voicemail type as text.
  377. :Parameters:
  378. Type : `enums`.vmt*
  379. Voicemail type.
  380. :return: Text describing the voicemail type.
  381. :rtype: unicode
  382. """
  383. return self._ToText('vmt', Type)
  384. def _GetLanguage(self):
  385. return self._Language
  386. def _SetLanguage(self, Language):
  387. try:
  388. self._Module = __import__('lang.%s' % Language, globals(), locals(), ['lang'])
  389. self._Language = str(Language)
  390. except ImportError:
  391. raise ValueError('Unknown language: %s' % Language)
  392. Language = property(_GetLanguage, _SetLanguage,
  393. doc="""Language used for all "ToText" conversions.
  394. Currently supported languages: ar, bg, cs, cz, da, de, el, en, es, et, fi, fr, he, hu, it, ja, ko,
  395. lt, lv, nl, no, pl, pp, pt, ro, ru, sv, tr, x1.
  396. :type: str
  397. """)