|
5 | 5 | # =============================================================================
|
6 | 6 | # >> IMPORTS
|
7 | 7 | # =============================================================================
|
8 |
| -# Python Imports |
9 |
| -# Collections |
10 |
| -from collections import OrderedDict |
| 8 | +# Source.Python Imports |
| 9 | +# Cvars |
| 10 | +from cvars.public import PublicConVar |
11 | 11 |
|
12 | 12 |
|
13 | 13 | # =============================================================================
|
|
20 | 20 | # =============================================================================
|
21 | 21 | # >> CLASSES
|
22 | 22 | # =============================================================================
|
23 |
| -class PluginInfo(OrderedDict): |
24 |
| - """Stores information for a plugin.""" |
| 23 | +class PluginInfo(dict): |
| 24 | + """Store information for a plugin.""" |
25 | 25 |
|
26 |
| - def __getattr__(self, attribute): |
27 |
| - """Redirect to __getitem__.""" |
28 |
| - # Is the attribute private? |
29 |
| - if attribute.startswith('_'): |
| 26 | + def __init__(self, name, verbose_name=None, author=None, description=None, |
| 27 | + version=None, url=None, permissions=None, public_convar=True, |
| 28 | + display_in_listing=None, **kwargs): |
| 29 | + """Initialize the instance. |
30 | 30 |
|
31 |
| - # Raise an error |
32 |
| - # This is done to fix an issue with OrderedDict.__init__ |
33 |
| - raise AttributeError('Private attributes not allowed') |
| 31 | + :param str name: |
| 32 | + Name of the plugin on the file system. |
| 33 | + :param str verbose_name: |
| 34 | + A verbose name for the plugin (e.g. GunGame). |
| 35 | + :param str author: |
| 36 | + Name of the author. |
| 37 | + :param str description: |
| 38 | + A short description of what the plugin does. |
| 39 | + :param str version: |
| 40 | + Current version of the plugin. |
| 41 | + :param str url: |
| 42 | + A link to a thread in the 'Plugin Releases' forum section or the |
| 43 | + plugin's SPPM link. |
| 44 | + :param list permissions: |
| 45 | + A list of permissions defined or used by the plugin. The list |
| 46 | + should contain tuples that define the permission and a short |
| 47 | + description of the permission. |
| 48 | + :param public_convar: |
| 49 | + If set to ``True``, a public convar will be generated based on the |
| 50 | + plugin name, verbose name and version. Set it to ``False`` if you |
| 51 | + don't want a public convar or set it to a dictionary containing |
| 52 | + the parameters to create a :class:`cvars.public.PublicConvar` |
| 53 | + instance. |
| 54 | + :param list display_in_listing: |
| 55 | + A list that contains custom attributes that should appear in the |
| 56 | + plugin listing (e.g. sp plugin list). |
| 57 | + :param kwargs: |
| 58 | + Any additional attributes you want to set. If you want those |
| 59 | + attributes to appear in the plugin listing, update |
| 60 | + :attr:`display_in_listing`. |
| 61 | + """ |
| 62 | + super().__init__(**kwargs) |
| 63 | + self.name = name |
| 64 | + self._verbose_name = verbose_name |
| 65 | + self.author = author |
| 66 | + self.description = description |
| 67 | + self._version = version |
| 68 | + self.url = url |
34 | 69 |
|
35 |
| - # Redirect to __getitem__ |
36 |
| - return self[attribute] |
| 70 | + # All permissions defined by this plugin |
| 71 | + # A list that contains tuples: |
| 72 | + # Example: |
| 73 | + # [('test1.kick', 'Permission to kick players.'), |
| 74 | + # ('test1.ban', 'Permission to ban players.'), |
| 75 | + # ('test1.start_vote', 'Permission to start a vote.')] |
| 76 | + self.permissions = [] if permissions is None else permissions |
| 77 | + self.public_convar = public_convar |
37 | 78 |
|
38 |
| - def __setattr__(self, attribute, value): |
39 |
| - """Redirect to __setitem__.""" |
40 |
| - # Is the attribute private? |
41 |
| - if attribute.startswith('_'): |
| 79 | + self.display_in_listing = [] if display_in_listing is None else display_in_listing |
42 | 80 |
|
43 |
| - # Re-call __setattr__ |
44 |
| - # This is done to fix an issue with OrderedDict.__init__ |
45 |
| - super().__setattr__(attribute, value) |
| 81 | + def _create_public_convar(self): |
| 82 | + """Create a public convar if :attr:`public_convar` is set to True.""" |
| 83 | + name = '{}_version'.format(self.name) |
| 84 | + description = '{} version.'.format(self.verbose_name) |
| 85 | + if self.public_convar is True: |
| 86 | + self.public_convar = PublicConVar( |
| 87 | + name, |
| 88 | + self.version, |
| 89 | + description |
| 90 | + ) |
| 91 | + elif isinstance(self.public_convar, dict): |
| 92 | + self.public_convar = PublicConVar( |
| 93 | + self.public_convar.pop('name', name), |
| 94 | + self.public_convar.pop('value', self.version), |
| 95 | + self.public_convar.pop('description', description), |
| 96 | + **self.public_convar) |
46 | 97 |
|
47 |
| - # No need to go further |
48 |
| - return |
| 98 | + def get_verbose_name(self): |
| 99 | + """Return the verbose name of the plugin. |
49 | 100 |
|
50 |
| - # Redirect to __setitem__ |
51 |
| - self[attribute] = value |
| 101 | + If no verbose name has been set, the plugin name will be titled. |
| 102 | +
|
| 103 | + :rtype: str |
| 104 | + """ |
| 105 | + if self._verbose_name is None: |
| 106 | + return self.name.replace('_', ' ').title() |
| 107 | + |
| 108 | + return self._verbose_name |
| 109 | + |
| 110 | + def set_verbose_name(self, value): |
| 111 | + """Set the verbose name of the plugin.""" |
| 112 | + self._verbose_name = value |
| 113 | + |
| 114 | + verbose_name = property(get_verbose_name, set_verbose_name) |
| 115 | + |
| 116 | + def get_version(self): |
| 117 | + """Return the plugin's version. |
| 118 | +
|
| 119 | + :rtype: str |
| 120 | + """ |
| 121 | + if self._version is None: |
| 122 | + return 'unversioned' |
| 123 | + |
| 124 | + return self._version |
| 125 | + |
| 126 | + def set_version(self, value): |
| 127 | + """Set the plugin's version.""" |
| 128 | + self._version = value |
| 129 | + |
| 130 | + version = property(get_version, set_version) |
| 131 | + |
| 132 | + # Redirect __getitem__ and __setitem__ to __getattr__ and __setattr__ |
| 133 | + __getattr__ = dict.__getitem__ |
| 134 | + __setattr__ = dict.__setitem__ |
0 commit comments