time cockpit, the world's greatest time tracking software, offers the possibility to access it's data using scripting (IronPython). The following code sample shows how to fill an entity names "Country" by downloading all countries from www.geonames.org.
Country can be created like this:
# Country
countryType = ModelEntity({ "Name": "Country", "ElementGuid": Guid("BE9FF5DC-F70B-4D17-9022-F6DD8498564A"), "InvariantFriendlyName": "Land" })
countryType.Properties.Add(TextProperty({ "Name": "IsoCode", "InvariantFriendlyName": "ISO-Code (2stellig)", "MaxStorageSize": 2, "DefaultValueExpression": "''", "IsNullable": False }))
countryType.Properties.Add(TextProperty({ "Name": "Name", "InvariantFriendlyName": "Ländername", "MaxStorageSize": 50, "IsDefaultDisplayProperty": True, "DefaultValueExpression": "''", "IsNullable": False }))
countryType.Properties.Add(TextProperty({ "Name": "GermanName", "InvariantFriendlyName": "Ländername", "MaxStorageSize": 50 }))
countryType.Properties.Add(TextProperty({ "Name": "Currency", "InvariantFriendlyName": "Währung", "MaxStorageSize": 3 }))
Context.Model.Entities.Add(countryType)
Here is the script for filling it:
clr.AddReference("System")
from System.Net import WebRequest
from System.IO import StreamReader
from TimeCockpit.Data.QueryLanguage import QueryParameter
def download_text(url):
req = WebRequest.Create(url)
resp = req.GetResponse()
try:
respStream = resp.GetResponseStream()
try:
streamReader = StreamReader(respStream)
return streamReader.ReadToEnd()
finally:
streamReader.Close()
finally:
respStream.Close()
countries = {}
content = download_text("http://download.geonames.org/export/dump/countryInfo.txt")
for line in content.splitlines():
if not line.startswith("#"):
columns = line.split("\t")
countries[columns[0]] = {"IsoCode": columns[0], "EnglishName": columns[4], "Currency": columns[10], "Continent": columns[8], "GermanName": columns[4]}
for country in countries:
germanCountryInfo = download_text("http://ws.geonames.org/countryInfoCSV?lang=de&country=%(countryIsoCode)s" % { "countryIsoCode": country })
if len(germanCountryInfo) > 0:
germanLines = germanCountryInfo.splitlines()
if len(germanLines) >= 2:
countries[country]["GermanName"] = germanLines[1].split("\t")[4]
dbCountry = Context.SelectSingleWithParams({"Query": "From C In Country Where C.IsoCode = @IsoCode Select C", "@IsoCode": country})
if dbCountry == None:
dbCountry = Context.CreateCountry()
dbCountry.IsoCode = country
dbCountry.Name = countries[country]["EnglishName"]
dbCountry.Currency = countries[country]["Currency"]
dbCountry.GermanName = countries[country]["GermanName"]
Context.SaveObject(dbCountry)
print "Done!"