nsarchive 0.1a0__py3-none-any.whl → 0.2a0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
nsarchive/__init__.py CHANGED
@@ -3,16 +3,19 @@ import time
3
3
  import deta
4
4
 
5
5
  from .cls.entities import *
6
+ from .cls.votes import *
7
+
6
8
  from .cls.exceptions import *
7
9
 
8
10
  class EntityInstance:
9
11
  def __init__(self, token: str) -> None:
10
12
  self.db = deta.Deta(token)
11
13
  self.base = self.db.Base('entities')
14
+ self.electors = self.db.Base('electors')
12
15
 
13
16
  def get_entity(self, id: str) -> User | Organization | Entity:
14
- _base = self.base
15
- _data = _base.get(id)
17
+ id = id.upper()
18
+ _data = self.base.get(id)
16
19
 
17
20
  if _data is None:
18
21
  return Entity("0")
@@ -30,7 +33,7 @@ class EntityInstance:
30
33
  entity.xp = _data['xp']
31
34
 
32
35
  if type(entity) == Organization:
33
- entity.owner = self.get_entity(_data['owner_id'])
36
+ entity.owner = self.get_entity(_data['owner_id'].upper())
34
37
  entity.members = [
35
38
  self.get_entity(_id) for _id in _data['members']
36
39
  ]
@@ -40,7 +43,7 @@ class EntityInstance:
40
43
  entity.boosts = _data['boosts']
41
44
 
42
45
  return entity
43
-
46
+
44
47
  def save_entity(self, entity: Entity) -> None:
45
48
  _base = self.base
46
49
  _data = {
@@ -52,13 +55,32 @@ class EntityInstance:
52
55
  }
53
56
 
54
57
  if type(entity) == Organization:
55
- _data['owner_id'] = entity.owner.id if entity.owner else Entity(0)
56
- _data['members'] = [ member.id for member in entity.members ] if entity.members else []
58
+ _data['owner_id'] = entity.owner.id.upper() if entity.owner else Entity("0")
59
+ _data['members'] = [ member.id.upper() for member in entity.members ] if entity.members else []
57
60
  _data['certifications'] = entity.certifications
58
61
  elif type(entity) == User:
59
62
  _data['boosts'] = entity.boosts
60
63
 
61
- _base.put(_data, entity.id, expire_in = 3 * 31536000) # Données supprimées tous les trois ans
64
+ _base.put(_data, entity.id.upper(), expire_in = 3 * 31536000) # Données supprimées tous les trois ans
65
+
66
+ def get_elector(self, id: str) -> Elector:
67
+ id = id.upper()
68
+ _data = self.electors.get(id)
69
+
70
+ if _data is None:
71
+ return Elector('0')
72
+
73
+ elector = Elector(id)
74
+ elector.votes = _data['votes']
75
+
76
+ return elector
77
+
78
+ def save_elector(self, elector: Elector):
79
+ _data = {
80
+ "votes": elector.votes
81
+ }
82
+
83
+ self.electors.put(_data, elector.id.upper())
62
84
 
63
85
  def fetch(self, query = None, listquery: dict | None = None) -> list:
64
86
  _res = self.base.fetch(query).items
@@ -70,8 +92,47 @@ class EntityInstance:
70
92
  _res.remove(item)
71
93
 
72
94
  return _res
73
-
95
+
74
96
  def get_entity_groups(self, id: int) -> list[Organization]:
75
97
  groups = self.fetch({'_type': 'organization'}, {'members': str(id)})
76
98
 
77
- return [ self.get_entity(int(group['id'], 16)) for group in groups ]
99
+ return [ self.get_entity(int(group['id'], 16)) for group in groups ]
100
+
101
+ class RepublicInstance:
102
+ def __init__(self, token: str) -> None:
103
+ self.db = deta.Deta(token)
104
+ self.votes = self.db.Base('votes')
105
+
106
+ def get_vote(self, id: str) -> Vote | ClosedVote:
107
+ id = id.upper()
108
+ _data = self.votes.get(id)
109
+
110
+ if _data is None:
111
+ return None
112
+
113
+ if _data['_type'] == 'open':
114
+ vote = Vote(id, _data['title'], tuple(_data['choices'].keys()))
115
+ elif _data['_type'] == 'closed':
116
+ vote = ClosedVote(id, _data['title'])
117
+ else:
118
+ vote = Vote('0', 'Unknown Vote', ())
119
+
120
+ vote.author = _data['author']
121
+ vote.startDate = _data['startDate']
122
+ vote.endDate = _data['endDate']
123
+ vote.choices = _data['choices']
124
+
125
+ return vote
126
+
127
+ def save_vote(self, vote: Vote | ClosedVote):
128
+ _base = self.base
129
+ _data = {
130
+ '_type': 'open' if type(vote) == Vote else 'closed' if type(vote) == ClosedVote else 'unknown',
131
+ 'title': vote.title,
132
+ 'author': vote.author,
133
+ 'startDate': vote.startDate,
134
+ 'endDate': vote.endDate,
135
+ 'choices': vote.choices
136
+ }
137
+
138
+ _base.put(_data, vote.id.upper())
nsarchive/cls/entities.py CHANGED
@@ -62,4 +62,29 @@ class Organization(Entity):
62
62
  self.members.remove(member)
63
63
 
64
64
  def set_owner(self, member: User) -> None:
65
- self.owner = member
65
+ self.owner = member
66
+
67
+ class Elector(User):
68
+ def __init__(self, id: str) -> None:
69
+ self.id: str = id
70
+ self.votes: list[str] = []
71
+
72
+ class FunctionalUser(User):
73
+ def __init__(self, id: str):
74
+ super().__init__(id)
75
+
76
+ self.permissions: dict = {
77
+ 'approve_project': False,
78
+ 'create_org': False,
79
+ 'destroy_gov': False,
80
+ 'destroy_org': False,
81
+ 'propose_projects': False
82
+ }
83
+
84
+ self.mandates: int = 0
85
+ self.contribs: dict = {
86
+ 'projects': 0,
87
+ 'approved_projects': 0,
88
+ 'admin_actions': 0,
89
+ 'law_votes': 0
90
+ }
nsarchive/cls/votes.py CHANGED
@@ -1,7 +1,16 @@
1
1
  import time
2
2
 
3
+ from .entities import FunctionalUser
4
+
3
5
  class Vote:
4
- def __init__(self, id: int, title: str, choices: list[str]) -> None:
5
- self.id: int = id
6
+ def __init__(self, id: str, title: str, choices: tuple[str]) -> None:
7
+ self.id: str = id
6
8
  self.title: str = title
7
- self.choices = { choice : 0 for choice in choices }
9
+ self.choices = { choice : 0 for choice in choices }
10
+ self.author: FunctionalUser
11
+ self.startDate: int = 0
12
+ self.endDate: int = 0
13
+
14
+ class ClosedVote(Vote):
15
+ def __init__(self, id: str, title: str) -> None:
16
+ super().__init__(id, title, ('yes', 'no', 'blank'))
@@ -0,0 +1,175 @@
1
+ Metadata-Version: 2.1
2
+ Name: nsarchive
3
+ Version: 0.2a0
4
+ Summary:
5
+ License: GPL-3.0
6
+ Author: happex
7
+ Author-email: 110610727+okayhappex@users.noreply.github.com
8
+ Requires-Python: >=3.10,<4.0
9
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Description-Content-Type: text/markdown
15
+
16
+ # nsarchive
17
+
18
+ `nsarchive` est un module Python pour la gestion des entités (utilisateurs et organisations) à l'aide de la base de données Deta. Ce module permet de créer, récupérer, sauvegarder et gérer des entités et leurs attributs spécifiques.
19
+
20
+ ## Pré-requis
21
+
22
+ Listes des choses à avoir afin de pouvoir utiliser le module correctement:
23
+ - [Python 3.10](https://www.python.org/downloads/) ou supérieur
24
+ - Un token [Deta](https://deta.space), donné par un administrateur ou pour votre collection personnelle
25
+ - Un bon capuccino, pour commencer la programmation en bons termes
26
+
27
+ > **Note:** Il vous faudra un token différent pour accéder aux différentes parties de la base de données. Vos tokens n'expireront pas à moins que l'ordre en aura été donné
28
+
29
+ ## Installation
30
+
31
+ Vous pouvez installer ce module via pip :
32
+
33
+ ```sh
34
+ pip install nsarchive
35
+ ```
36
+
37
+ ## Utilisation
38
+
39
+ ### Importation et Initialisation
40
+
41
+ Pour utiliser `nsarchive`, commencez par importer le module et initialiser une instance d'`EntityInstance` avec votre token Deta.
42
+
43
+ ```python
44
+ from nsarchive import EntityInstance
45
+
46
+ # Remplacez 'your_deta_token' par votre véritable token Deta
47
+ entity_instance = EntityInstance(token = 'your_deta_token')
48
+ ```
49
+
50
+ ### Récupérer une Entité
51
+
52
+ Vous pouvez récupérer une entité (Utilisateur ou Organisation) à l'aide de son ID.
53
+
54
+ ```python
55
+ entity = entity_instance.get_entity(id = 'entity_id')
56
+ print(entity.name)
57
+ ```
58
+
59
+ **ATTENTION: Les entités sont identifiées sous une forme hexadécimale. Pour les avoir, vous devez convertir leur ID Discord en hexadécimale puis enlever le préfixe `0x`.**
60
+
61
+ Pour les organisations, l'ID Discord correspondra à la formule suivante: `ID fondateur // 100000`.
62
+
63
+ N'oubliez pas de toujours utiliser un `str` dans les ID pour interagir avec la base de données.
64
+
65
+ ### Sauvegarder une Entité
66
+
67
+ Après avoir modifié une entité, vous pouvez la sauvegarder dans la base de données.
68
+
69
+ ```python
70
+ entity.rename("Nouveau Nom")
71
+ entity_instance.save_entity(entity)
72
+ ```
73
+
74
+ ### Rechercher des Entités
75
+
76
+ Vous pouvez rechercher des entités avec des critères spécifiques.
77
+
78
+ ```python
79
+ entities = entity_instance.fetch(query = {'name': 'Alice'})
80
+ for entity in entities:
81
+ print(entity['name'])
82
+ ```
83
+
84
+ ### Gérer les Organisations
85
+
86
+ Les organisations peuvent avoir des membres et des certifications. Voici comment ajouter un membre ou une certification.
87
+
88
+ ```python
89
+ organization = entity_instance.get_entity(id = 'org_id')
90
+ user = entity_instance.get_entity(id = 'user_id')
91
+
92
+ # Ajouter un membre
93
+ organization.add_member(user)
94
+ entity_instance.save_entity(organization)
95
+
96
+ # Ajouter une certification
97
+ organization.add_certification('Certification Example')
98
+ entity_instance.save_entity(organization)
99
+ ```
100
+
101
+ Les certifications pourront être utilisées pour vérifier l'officialité d'une organisation, mais également pour déterminer si l'on peut accorder (ou non) des permissions à ses membres.
102
+
103
+ ### Exemples de Classes
104
+
105
+ #### `Entity`
106
+
107
+ Classe parente des classes `User` et `Organization`, elle est utilisée lorsque le module ne peut pas déterminer l'appartenance d'une identité à l'une de ces deux classes ou à l'autre.
108
+
109
+ ```python
110
+ from nsarchive.cls.entities import Entity
111
+
112
+ entity = Entity(id='entity_id')
113
+ entity.rename('New Name')
114
+ entity.add_xp(100)
115
+ print(entity.get_level())
116
+ ```
117
+
118
+ #### `User`
119
+
120
+ ```python
121
+ from nsarchive.cls.entities import User
122
+
123
+ user = User(id = 'user_id')
124
+ user.edit_boost(name = 'admin', multiplier = 5) # Négliger le paramètre <multiplier> ou le fixer à un nombre négatif reviendrait à supprimer le boost.
125
+ print(user.boosts)
126
+ ```
127
+
128
+ > **Note:** Lorsqu'on ajoute de l'expérience à un utilisateur via la méthode `add_xp`, le nombre de points ajoutés est automatiquement multiplié par le bonus le plus important dont l'utilisateur bénéficie.
129
+
130
+ #### `Organization`
131
+
132
+ ```python
133
+ from nsarchive.cls.entities import Organization
134
+
135
+ organization = Organization(id = 'org_id')
136
+ organization.set_owner(user)
137
+ organization.add_member(user)
138
+ print(organization.members)
139
+ ```
140
+
141
+ > **Note:** Les attributs `owner` et `members` sont indépendants. L'owner peut être n'importe quelle personne faisant ou non partie des `members`.
142
+
143
+ ## Gestion des Exceptions
144
+
145
+ `nsarchive` fournit des exceptions spécifiques pour gérer les erreurs courantes.
146
+
147
+ #### `NameTooLongError`
148
+
149
+ Lancé lorsque le nom d'une entité dépasse la longueur maximale autorisée (32 caractères).
150
+
151
+ ```python
152
+ from nsarchive.cls.exceptions import NameTooLongError
153
+
154
+ try:
155
+ entity.rename('Ce nom est long, voire même très long, je dirais même extrêmement long')
156
+ except NameTooLongError as e:
157
+ print(e)
158
+ ```
159
+
160
+ #### `EntityTypeError`
161
+
162
+ Lancé lorsque le type d'entité est incorrect. Vous ne devriez normalement pas la rencontrer en utilisant le module, mais elle pourrait vous être utile.
163
+
164
+ ```python
165
+ from nsarchive.cls.exceptions import EntityTypeError
166
+
167
+ try:
168
+ # Code qui peut lancer une EntityTypeError
169
+ except EntityTypeError as e:
170
+ print(e)
171
+ ```
172
+
173
+ ## License
174
+
175
+ Ce projet est sous licence GNU GPL-3.0 - Voir le fichier [LICENSE](LICENSE) pour plus de détails.
@@ -0,0 +1,8 @@
1
+ nsarchive/__init__.py,sha256=0eu5Tf7ZuUmMQeeZ001ePNWBisoz1njUA2__Ruy6wjU,4600
2
+ nsarchive/cls/entities.py,sha256=mhBHZBtYK1E2dfTDoFHNdw1uu5QsnOc-Utuu5EjtV7c,2556
3
+ nsarchive/cls/exceptions.py,sha256=TrH9PvHhVZi7wap9ZfBLGRWJY3OBCYgWAMnco5uadYY,420
4
+ nsarchive/cls/votes.py,sha256=vASgf9ies8YPPlKBoaQdB5jB_Sp4GS_Onl8hv3_7HJU,507
5
+ nsarchive-0.2a0.dist-info/LICENSE,sha256=aFLFZg6LEJFpTlNQ8su3__jw4GfV-xWBmC1cePkKZVw,35802
6
+ nsarchive-0.2a0.dist-info/METADATA,sha256=xlZA4CPlK_RrfnzpnPApuAKhrTZgPBef9TI-8VxplIw,5552
7
+ nsarchive-0.2a0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
8
+ nsarchive-0.2a0.dist-info/RECORD,,
@@ -1,17 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: nsarchive
3
- Version: 0.1a0
4
- Summary:
5
- License: GPL-3.0
6
- Author: happex
7
- Author-email: 110610727+okayhappex@users.noreply.github.com
8
- Requires-Python: >=3.10,<4.0
9
- Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
10
- Classifier: Programming Language :: Python :: 3
11
- Classifier: Programming Language :: Python :: 3.10
12
- Classifier: Programming Language :: Python :: 3.11
13
- Classifier: Programming Language :: Python :: 3.12
14
- Description-Content-Type: text/markdown
15
-
16
- # 1ns-archive
17
-
@@ -1,8 +0,0 @@
1
- nsarchive/__init__.py,sha256=cWlY3WvOeAmzwU0TC6VH0V2NfZeL4dbi_7ocneQ5els,2767
2
- nsarchive/cls/entities.py,sha256=IrQSciTyb1fS-AumcGxyD35ONfvNa49CwfTQNDrFEkA,1895
3
- nsarchive/cls/exceptions.py,sha256=TrH9PvHhVZi7wap9ZfBLGRWJY3OBCYgWAMnco5uadYY,420
4
- nsarchive/cls/votes.py,sha256=p5QnvQQTaKkZDjfZa2qDgGrpCmu4vQV8-9drPayZ0oE,221
5
- nsarchive-0.1a0.dist-info/LICENSE,sha256=aFLFZg6LEJFpTlNQ8su3__jw4GfV-xWBmC1cePkKZVw,35802
6
- nsarchive-0.1a0.dist-info/METADATA,sha256=Kn6vtRhpgpk091l-IlmMpfUGVf8bbnfig0eA63c93cw,518
7
- nsarchive-0.1a0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
8
- nsarchive-0.1a0.dist-info/RECORD,,