Skip to content

Commit

Permalink
Merge pull request #15 from lyckade/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
lyckade committed Aug 7, 2014
2 parents cb5be34 + 9d44542 commit a2f8e87
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 229 deletions.
Binary file modified xpstart/ExclusionScenery.zip
Binary file not shown.
51 changes: 51 additions & 0 deletions xpstart/exclusion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import xpstart


class Exclusion(xpstart.Base):
def __init__(self, xp_path, gui=None):
xpstart.Base.__init__(self, gui)

self.gui = gui

if xp_path.endswith("/") or xp_path.endswith("\\"):
xp_path = xp_path[:-1]
self.xp_path = xp_path

self.scenery_path = "%s/Custom Scenery" % (self.xp_path)

self.exclusions_archive_file = "ExclusionScenery.zip"
self.exclusions_archive = False


def close_archive(self):
if self.exclusions_archive:
self.exclusions_archive.close()

def extract_scenery(self, icao):
import os.path

if not self.exclusions_archive:
return False
icao_exists = False
path = "Exclusion_%s/" % (icao)
if os.path.exists("%s/%s" % (self.scenery_path, path)):
self.echo("Exclusion scenery for %s already exists." % (icao))
return True
nl = self.exclusions_archive.namelist()
for f in nl:
if f.startswith(path):
icao_exists = True
self.exclusions_archive.extract(f, self.scenery_path)
self.echo("Copying %s to scenery folder." % (f))
if not icao_exists:
self.echo("No exclusion scenery availiable for %s" % (icao))
return icao_exists

def open_archive(self, filename=""):
import zipfile

if filename == "":
filename = self.exclusions_archive_file
self.exclusions_archive = zipfile.ZipFile(filename, "r")


1 change: 1 addition & 0 deletions xpstart/layer_definitions.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Layergroup:Default:layers:new add-ons;Airport;Airport pack;Exclusion;Default scenery;OSM scenery;Phototextures;Ground mesh;Library;
Layergroup:Default:checkOrder:4;8;7;6;5;3;2;1;0;
Layergroup:Default:defaultSceneryLayer:Default scenery;
Layer:Library:defaultRules:sum->min->10;aptdat->is->0;library->is->1;dsf->is->0;
Layer:Airport:defaultRules:icaos->is->1;dsf->min->1;
Layer:Airport pack:defaultRules:icaos->min->2;dsf->min->1;title->not_in->Global Airports;
Expand Down
146 changes: 72 additions & 74 deletions xpstart/layergroup.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import os

import scenery
import xpstart
import os


class Layer(xpstart.Base):
"""
The model for a scenery layer
"""
def __init__(self,title,gui=None):
xpstart.Base.__init__(self,gui)

def __init__(self, title, gui=None):
xpstart.Base.__init__(self, gui)

self.gui = gui

self.title = title
#=======================================================================
# =======================================================================
# Description will displayed in the gui to help the user
#=======================================================================
# =======================================================================
self.description = ""

self.dataFile = "xpstart/layer_definitions.txt"
#self.writeData("dataname", "Test")
#=======================================================================
Expand All @@ -26,13 +28,11 @@ def __init__(self,title,gui=None):
# defaultRules['obj']['min'] = 1
#=======================================================================
self.defaultRules = {}

self.loadDefaultRules()




def addDefaultRule(self,cmd):

def addDefaultRule(self, cmd):
"""
Adds one comand to the defaultRules. Every command is in the logik
"Entity->Rule->Value;"
Expand All @@ -43,8 +43,8 @@ def addDefaultRule(self,cmd):
if not c[1] in self.defaultRules[c[0]]:
self.defaultRules[c[0]][c[1]] = {}
self.defaultRules[c[0]][c[1]] = c[2]
def checkEntities(self,entities):

def checkEntities(self, entities):
"""
Checks the entities with the defaultRules
@param entities: entityName:Value
Expand All @@ -53,10 +53,10 @@ def checkEntities(self,entities):
"""
if len(self.defaultRules) == 0:
return False
for entity,rules in self.defaultRules.items():
for entity, rules in self.defaultRules.items():
if entity not in entities:
return False
for rule,val in rules.items():
for rule, val in rules.items():
if rule == "min" and int(val) > int(entities[entity]):
return False
elif rule == "max" and int(val) < int(entities[entity]):
Expand All @@ -74,12 +74,10 @@ def checkEntities(self,entities):
return False
elif rule == "not_in" and str(entities[entity]) in l:
return False

return True






def loadDefaultRules(self):
"""
Loads the rules
Expand All @@ -93,87 +91,86 @@ def loadDefaultRules(self):
for cmd in cmds:
if cmd is not "":
self.addDefaultRule(cmd)







class Layergroup(xpstart.Base):
"""
All sceneries of an x-plane installation can be grouped into layers.
That helps to sort the sceneries.
"""
def __init__(self,xpPath,gui=None):
xpstart.Base.__init__(self,gui)

def __init__(self, xpPath, gui=None):
xpstart.Base.__init__(self, gui)
self.gui = gui
if xpPath.endswith("/") or xpPath.endswith("\\"):
xpPath = xpPath[:-1]
self.xpPath = xpPath


self.title = "Default"
"""
Title is important for the read and write methods and specify the
instance. If not changed it will be the Default instance
"""

self.defaultLayer = "new add-ons"

self.layers = {}
"""
Dict to store the layers object and other data
The object is always found at self.layers[title]['object']
"""

self.order = []
"""
List to get the Layers in the right order
"""

self.layerDefFile = "xpstart/layer_definitions.txt"
"""File were all the definitions to the layers are stored """


self.sceneryFolder = "Custom Scenery" #: The name of the sceneryFolder !Don't change!


self.sceneryFolder = "Custom Scenery" # : The name of the sceneryFolder !Don't change!

self.defaultSceneryLayer = self.readData("defaultSceneryLayer", self.layerDefFile)[:-1]
"""The layer which contains the default scenery"""

self.loadLayers()
"""All the Layers needs to be loaded"""

self.sceneries = self.loadXpSceneries()
"""All active sceneries of the XP installation are loaded as scenery objects"""


def checkIcaos(self):
"""
Walks over all layers and returns a dict with the layername icao an scenerynames
"""
warnings = {} #: Dictionary where all the warnings are collected
warnings = {} # : Dictionary where all the warnings are collected
icaos = {}
for layer in self.layers:
for layer in self.order:
# for layer in self.layers:
if layer not in self.layers:
continue
for icao in self.layers[layer]['icaos']:
if icao not in icaos:
icaos[icao] = []
for sc in self.layers[layer]['icaos'][icao]:

icaos[icao].append({"title":sc,"layer":layer})
#icaos[icao] = icaos[icao] + self.layers[layer]['icaos'][icao]
icaos[icao].append({"title": sc, "layer": layer})
# icaos[icao] = icaos[icao] + self.layers[layer]['icaos'][icao]
for icao in icaos:
if len(icaos[icao])>1:
if len(icaos[icao]) > 1:
warnings[icao] = icaos[icao]
return warnings

# for layer in self.layers:
#
# for icao in self.layers[layer]['icaos']:
# if len(self.layers[layer]['icaos'][icao])>1:
# if layer not in warnings:
# warnings[layer] = {}
# warnings[layer][icao] = self.layers[layer]['icaos'][icao]
# return warnings


# for layer in self.layers:

#
# for icao in self.layers[layer]['icaos']:
# if len(self.layers[layer]['icaos'][icao])>1:
# if layer not in warnings:
# warnings[layer] = {}
# warnings[layer][icao] = self.layers[layer]['icaos'][icao]
# return warnings

def loadLayers(self):
"""Loads the defined layers out of the dataFile and adds an Layer object
to the self.layers list.
Expand All @@ -187,25 +184,25 @@ def loadLayers(self):
continue
self.order.append(title)
self.layers[title] = {}
self.layers[title]['object'] = Layer(title,self.gui)
self.layers[title]['object'] = Layer(title, self.gui)
# Make instance here for easy append later
self.layers[title]['sceneries'] = []
self.layers[title]['icaos'] = {}


def loadXpSceneries(self):
"""
Loads all sceneries and orders it in the self.layers[title]['sceneries'] dict
"""
sceneryPath = "%s/%s" % (self.xpPath,self.sceneryFolder)
sceneryPath = "%s/%s" % (self.xpPath, self.sceneryFolder)
sceneries = {}

for entry in os.listdir(sceneryPath):
# Path to the entry
abspath = "%s/%s" % (sceneryPath,entry)
abspath = "%s/%s" % (sceneryPath, entry)
# A scenery has to be a directory
if os.path.isdir(abspath):
sceneryObj = scenery.Scenery(abspath,self.gui)
sceneryObj = scenery.Scenery(abspath, self.gui)
sceneries[sceneryObj.title] = sceneryObj
# First the userLayer than authLayergoup than default
if sceneryObj.userLayer in self.layers:
Expand All @@ -219,7 +216,7 @@ def loadXpSceneries(self):
self.layers[layerTitle]['sceneries'].append(sceneryObj.title)
# Icao Codes for a layer are collected
# All is put into a dictionary
if len(sceneryObj.icaoCodes)>0:
if len(sceneryObj.icaoCodes) > 0:
for icao in sceneryObj.icaoCodes:
if icao == "":
continue
Expand All @@ -228,12 +225,14 @@ def loadXpSceneries(self):
self.layers[layerTitle]['icaos'][icao] = []
else:
# Give out a warning!
self.log("Warning: %s in scenery %s is not the first apearance of that icao in that layer!" % (icao,sceneryObj.title))
self.log(
"Warning: %s in scenery %s is not the first apearance of that icao in that layer!" % (
icao, sceneryObj.title))
self.layers[layerTitle]['icaos'][icao].append(sceneryObj.title)
#self.layers[layerTitle]['icaos'].append(sceneryObj.icaoCodes)
# self.layers[layerTitle]['icaos'].append(sceneryObj.icaoCodes)
return sceneries
def makeSceneryEntities(self,scenery):

def makeSceneryEntities(self, scenery):
"""
This method is called by loadDefaultRules. On this way additional entities
can be added to the default layer definition.
Expand All @@ -253,15 +252,14 @@ def makeSceneryEntities(self,scenery):
entities['library'] = 1
else:
entities['library'] = 0



if scenery.aptDat:
entities['aptdat'] = 1
else:
entities['aptdat'] = 0
return entities
def searchDefaultLayer(self,sceneryObj):

def searchDefaultLayer(self, sceneryObj):
"""
Walks through the layers and make a check, if it is the defaultLayer to
the scenery object.
Expand Down
6 changes: 6 additions & 0 deletions xpstart/scenery.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os

import xpstart


class Scenery(xpstart.Base):
"""
The scenery class holds informations about a x-plane scenery. To create a scenery object
Expand Down Expand Up @@ -171,6 +173,10 @@ def searchIcaoCodes(self):
Returns a list with all the ICAO codes find in the apt.dat file of the
scenery. If there is no apt.dat file the list will be empty []
"""
if self.sceneryTxt:
sceneryTxtIcao = self.getTxtProperty("ICAO", self.pathSceneryTxt)
if sceneryTxtIcao is not "":
return [sceneryTxtIcao]
# If there is no apt.dat an empty list is returned
if not self.aptDat:
return []
Expand Down
15 changes: 15 additions & 0 deletions xpstart/test_exclusion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from unittest import TestCase

import exclusion

__author__ = 'lyckade'
xplanepath = "PathToXPlaneFolder"


class TestExclusion(TestCase):
def test_extract_scenery(self):
test = exclusion.Exclusion(xplanepath)
test.open_archive()
test.extract_scenery("EDDH")
test.close_archive()
# self.fail()
Loading

0 comments on commit a2f8e87

Please sign in to comment.