Advertisement
1400_SpaceCat

Untitled

Jun 23rd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. # import settings
  2.  
  3. class NpcMeta(type):
  4.     def __new__(cls, name, base, attrs, **kwargs):
  5.         super_new = super().__new__
  6.  
  7.         parents = [b for b in base if isinstance(b, NpcMeta)]
  8.         if not parents:
  9.             return super_new(cls, name, base, attrs)
  10.  
  11.         module = attrs.pop('__module__')
  12.         new_attrs = {'__module__': module}
  13.         new_class = super_new(cls, name, base, new_attrs, **kwargs)
  14.         attr_meta = attrs.pop('Meta', None)
  15.         meta = attr_meta or getattr(new_class, 'Meta', None)
  16.         base_meta = getattr(new_class, '_meta', None)
  17.  
  18.         for i in meta.__dict__.keys():
  19.             if i.find("__"):
  20.                 if getattr(meta, i) in (None, []):
  21.                     setattr(meta, i, "%s.%s" % (name, i))
  22.                     print("*** Empty '%s' parameter in '%s'. Setting to '%s'" % (i, name, getattr(meta, i)))
  23.  
  24.         meta_args = {k : v for k, v in meta.__dict__.items() if k.find("__")}
  25.         new_class._write(meta_args)
  26.         return new_class
  27.  
  28.     def _write(cls, data):
  29.         with open("npc.txt", "r+", encoding="utf-8") as f:
  30.             tree = f"[{cls.__name__}]\n"
  31.             for i, j in data.items():
  32.                 tree += f"{i} = {j}\n"
  33.  
  34.             f.write(tree)
  35.  
  36. class NpcBase(metaclass=NpcMeta):
  37.     def __init__(self, *args, **kwargs):
  38.         cls = self.__class__
  39.         # opts = self._meta
  40.         super().__init__()
  41.  
  42.  
  43. class TestNpc(NpcBase):
  44.     class Meta:
  45.         npc_name = "Joe0"
  46.         npc_health = None
  47.         npc_fraction = "Cool fraction"
  48.         npc_armor_name = "Cool Armor"
  49.         npc_armor_health = 30
  50.         npc_phrases = ["1", "2", "3"]
  51.         npc_inventory = []
  52.  
  53. class TestNpc1(NpcBase):
  54.     class Meta:
  55.         npc_name = "Joe1"
  56.         npc_health = 100
  57.         npc_fraction = "Cool fraction"
  58.         npc_armor_name = "Cool Armor"
  59.         npc_armor_health = 30
  60.         npc_phrases = ["1", "2", "3"]
  61.         npc_inventory = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement