Coverage for licenzy\management.py: 15%

48 statements  

« prev     ^ index     » next       coverage.py v7.10.4, created at 2025-08-17 11:06 -0400

1""" 

2🔧 Licenzy Management - License activation and status management 

3 

4This module provides functions for managing licenses: activating, 

5deactivating, and checking status. Perfect for CLI integration. 

6""" 

7 

8from pathlib import Path 

9from typing import Optional 

10from .core import LicenseManager, get_license_manager 

11 

12 

13def activate_license(license_key: str) -> bool: 

14 """ 

15 🔓 Activate a license key 

16  

17 Validates and stores the license key for future use. 

18  

19 Args: 

20 license_key: The license key to activate 

21  

22 Returns: 

23 bool: True if activation successful, False otherwise 

24 """ 

25 # Validate the license first 

26 manager = LicenseManager(license_key) 

27 valid, message = manager.validate_license() 

28 

29 if valid: 

30 # Save to user's home directory 

31 license_dir = Path.home() / ".licenzy" 

32 license_dir.mkdir(exist_ok=True) 

33 

34 license_file = license_dir / "license.key" 

35 license_file.write_text(license_key) 

36 

37 print("✅ License activated successfully!") 

38 print(f"📍 Saved to: {license_file}") 

39 print(f"📋 {message}") 

40 return True 

41 else: 

42 print(f"❌ License activation failed: {message}") 

43 return False 

44 

45 

46def deactivate_license(): 

47 """ 

48 🔒 Deactivate the current license 

49  

50 Removes the stored license key from the system. 

51 """ 

52 license_file = Path.home() / ".licenzy" / "license.key" 

53 

54 if license_file.exists(): 

55 license_file.unlink() 

56 print("✅ License deactivated successfully") 

57 print(f"🗑️ Removed: {license_file}") 

58 else: 

59 print("ℹ️ No active license to deactivate") 

60 

61 

62def show_license_status(): 

63 """ 

64 📊 Show current license status 

65  

66 Displays detailed information about the current license state. 

67 """ 

68 manager = get_license_manager() 

69 valid, message = manager.validate_license() 

70 

71 print("🔑" + "=" * 59) 

72 print("🔑 LICENZY LICENSE STATUS") 

73 print("🔑" + "=" * 59) 

74 

75 if valid: 

76 print("📋 License Status: 🟢 ACTIVE") 

77 print(f"📋 {message}") 

78 

79 if manager.license_info: 

80 info = manager.license_info 

81 print(f"👤 User ID: {info['user_id']}") 

82 print(f"📦 Plan: {info['plan']}") 

83 print(f"📅 Expires: {info['expires'].strftime('%Y-%m-%d %H:%M:%S')}") 

84 print(f"⏰ Days Remaining: {info['days_remaining']}") 

85 else: 

86 print("📋 License Status: 🔴 INVALID") 

87 print(f"{message}") 

88 print() 

89 print("💡 To activate a license:") 

90 print(" licenzy activate your-license-key") 

91 

92 print("🔑" + "=" * 59) 

93 

94 

95def get_license_key_location() -> Optional[Path]: 

96 """ 

97 📍 Get the location of the stored license key 

98  

99 Returns: 

100 Path to license file if it exists, None otherwise 

101 """ 

102 license_file = Path.home() / ".licenzy" / "license.key" 

103 return license_file if license_file.exists() else None