15 from .model.utils import WithSession |
15 from .model.utils import WithSession |
16 |
16 |
17 from . import config |
17 from . import config |
18 |
18 |
19 def checkConfig(): |
19 def checkConfig(): |
|
20 """check configuration file syntax. |
|
21 |
|
22 :return: boolena value. |
|
23 """ |
|
24 |
20 try: |
25 try: |
21 l = configParser.read(confFiles) |
26 l = configParser.read(confFiles) |
22 if len(l) > 0: |
27 if len(l) > 0: |
23 return True |
28 return True |
24 return False |
29 return False |
25 except (NeededOption,ValidateException) as e: |
30 except (NeededOption,ValidateException) as e: |
26 log.msg("Error while processing config file: %s"%e,logLevel=logging.ERROR) |
31 log.msg("Error while processing config file: %s"%e,logLevel=logging.ERROR) |
27 return False |
32 return False |
28 |
33 |
29 def checkDatabase(): |
34 def checkDatabase(): |
|
35 """Checks, if all tables are created. |
|
36 |
|
37 :return: boolean value |
|
38 """ |
30 engine = create_engine(config.main.dburl) |
39 engine = create_engine(config.main.dburl) |
31 for t in Base.metadata.sorted_tables: |
40 for t in Base.metadata.sorted_tables: |
32 if not t.exists(engine): |
41 if not t.exists(engine): |
33 return False |
42 return False |
34 return True |
43 return True |
35 |
44 |
36 def checkDatabaseConnection(): |
45 def checkDatabaseConnection(): |
|
46 """Checks, if database can be connected. |
|
47 |
|
48 :return: boolean value |
|
49 """ |
37 try: |
50 try: |
38 engine = create_engine(config.main.dburl) |
51 engine = create_engine(config.main.dburl) |
39 con = engine.connect() |
52 con = engine.connect() |
40 con.close() |
53 con.close() |
41 return True |
54 return True |
42 except DatabaseError as e: |
55 except DatabaseError as e: |
43 log.msg("Error while trying to connect to database\n%s"%e,logLevel=logging.ERROR) |
56 log.msg("Error while trying to connect to database\n%s"%e,logLevel=logging.ERROR) |
44 return False |
57 return False |
45 |
58 |
46 def createDatabase(): |
59 def createDatabase(): |
|
60 """Create all database tables or only missing.""" |
47 engine = create_engine(config.main.dburl) |
61 engine = create_engine(config.main.dburl) |
48 Base.metadata.create_all(engine) |
62 Base.metadata.create_all(engine) |
49 |
63 |
50 def createSampleConfig(): |
64 def createSampleConfig(): |
|
65 """create a sample configuration file 'iro.conf' with all possible provider sections.""" |
51 if not os.path.exists("iro.conf"): |
66 if not os.path.exists("iro.conf"): |
52 with open("iro.conf",'w') as fp: |
67 with open("iro.conf",'w') as fp: |
53 fp.write("\n".join(main.sampleConf())) |
68 fp.write("\n".join(main.sampleConf())) |
54 fp.write("\n") |
69 fp.write("\n") |
55 k = providers.keys() |
70 k = providers.keys() |
59 fp.write("\n") |
74 fp.write("\n") |
60 else: |
75 else: |
61 log.msg("iro.conf exists and will not be overwritten.") |
76 log.msg("iro.conf exists and will not be overwritten.") |
62 |
77 |
63 def getAllRoutes(providers,write=False): |
78 def getAllRoutes(providers,write=False): |
|
79 """Checks and update offer list. |
|
80 |
|
81 :param boolean write: check or update list |
|
82 :return dict: |
|
83 - **"orphand"** (Set) -- a set of orphand offers |
|
84 - **"added"** (Set) -- a set of new offers. The new name have a schema provider_typ_route |
|
85 |
|
86 """ |
64 engine = create_engine(config.main.dburl) |
87 engine = create_engine(config.main.dburl) |
65 ret={"orphand":Set(),"added":Set()} |
88 ret={"orphand":Set(),"added":Set()} |
66 with WithSession(engine,write) as session: |
89 with WithSession(engine,write) as session: |
67 ret["orphand"]=Set([i[0] for i in session.query(Offer.name).all()]) |
90 ret["orphand"]=Set([i[0] for i in session.query(Offer.name).all()]) |
68 for provider in providers: |
91 for provider in providers: |