1 | #!/usr/bin/python |
---|
2 | """ |
---|
3 | Provides a class to encapsulate MythTV Storage Groups. |
---|
4 | """ |
---|
5 | try: |
---|
6 | from MythTV.MythLog import * |
---|
7 | from MythTV.MythDB import * |
---|
8 | except: |
---|
9 | print "MythStorage module cannot be initialized." |
---|
10 | |
---|
11 | import os, string |
---|
12 | |
---|
13 | # create logging object |
---|
14 | log = MythLog(CRITICAL, '#%(levelname)s - %(message)s', 'MythStorage') |
---|
15 | |
---|
16 | class MythStorage(MythDB): |
---|
17 | def __init__(self): |
---|
18 | self.db = MythDB() |
---|
19 | def GetStorageDirs(self, host = 'localhost'): |
---|
20 | """ |
---|
21 | Get the list of storage directories. |
---|
22 | """ |
---|
23 | |
---|
24 | query = """SELECT DISTINCT dirname FROM storagegroup |
---|
25 | WHERE hostname=%s |
---|
26 | """ |
---|
27 | cursor = self.db.cursor() |
---|
28 | cursor.execute(query, (host)) |
---|
29 | rows = cursor.fetchall() |
---|
30 | if rows: |
---|
31 | return rows |
---|
32 | else: |
---|
33 | return None |
---|
34 | |
---|
35 | cursor.close() |
---|
36 | |
---|
37 | def findRecordingDir(self, basename, host): |
---|
38 | """ |
---|
39 | Find the directory which contains the given file. |
---|
40 | """ |
---|
41 | rows = self.GetStorageDirs(host) |
---|
42 | for row in rows: |
---|
43 | if os.path.exists(row[0] + basename): |
---|
44 | return row[0] |
---|
45 | break |
---|
46 | return None |
---|
47 | |
---|
48 | def findRecordingFile(self, basename, host): |
---|
49 | """ |
---|
50 | Find the full pathname of the given file. |
---|
51 | """ |
---|
52 | rows = self.GetStorageDirs(host) |
---|
53 | for row in rows: |
---|
54 | if os.path.exists(row[0] + basename): |
---|
55 | return row[0] + basename |
---|
56 | break |
---|
57 | return None |
---|
58 | |
---|
59 | |
---|
60 | |
---|
61 | if __name__ == '__main__': |
---|
62 | banner = "'mst' is a MythStorage instance." |
---|
63 | try: |
---|
64 | import readline, rlcompleter |
---|
65 | except: |
---|
66 | pass |
---|
67 | else: |
---|
68 | readline.parse_and_bind("tab: complete") |
---|
69 | banner = banner + " TAB completion is available." |
---|
70 | mst = MythStorage(sys.argv[1:]) |
---|
71 | namespace = globals().copy() |
---|
72 | namespace.update(locals()) |
---|
73 | code.InteractiveConsole(namespace).interact(banner) |
---|
74 | |
---|