Python Forum
Python3x, from_import(name, from) function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3x, from_import(name, from) function
#6
my full code

# __init__.py
import os

__directory__ = os.path.dirname(__file__)

__langpacksdir__ = __directory__ + "/" + "langpacks"

# this dict is not a langpack only contains system language packs so this dict musn't be translated !
LANGPACKS = {
  "_val": "This PACK Contains Language Packs",
  "_info": "LANGPACKS of language module of RcD 1.0 Created by Lorderon",

  "_lang_count": 0, # this
  "_langpacks": [] 
}

LANGMODULES = {}

ignoredfolders = ("__pycache__",)

for name in os.listdir(__langpacksdir__):
  if os.path.isfile(__directory__ + "/" + name) or name in ignoredfolders:
    continue

  try:
    exec("from .langpacks import {}\n"
         "langmodule = {}\n"
         "del {}".format(name, name, name) )
    
    langpack = langmodule.PACK
    langid = langpack["_lang_id"]
    if langpack["_pack_type"] != "langpack":
      raise Exception
  except:
    # from traceback import print_exc; print_exc()
    continue
    

  if langid in LANGMODULES:
    continue

  LANGMODULES[langid] = langmodule
  LANGPACKS[langid] = langpack
  LANGPACKS["_langpacks"].append(langid)
  LANGPACKS["_lang_count"] += 1

del langpack, langmodule, langid, name
i can't find another way than exec func to do this. because load_soruce func from imp module is deprecated in python3x and i didn't do that with antoher modules' func (importlib) ): i love load_source <3
Reply


Messages In This Thread
RE: Python3x, from_import(name, from) function - by harun2525 - May-12-2017, 10:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3x running only with one instance (how can I prevent too many running instance) harun2525 5 21,772 Jul-21-2017, 07:36 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020