Passed
Pull Request — master (#443)
by Jaisen
05:39
created

sqlite.SQLite.before()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 3
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
"""
2
SQLite plugin object.
3
This plugin stores metadata about all media in an sqlite database.
4
5
You'll need to create a SQLite database using the schema.sql file and 
6
    reference it in your configuration.
7
8
```
9
[PluginSQLite]
10
database_file=/path/to/database.db
11
12
.. moduleauthor:: Jaisen Mathai <[email protected]>
13
"""
14
from __future__ import print_function
15
16
import os
17
import sqlite3
18
#import json
19
20
#from google_auth_oauthlib.flow import InstalledAppFlow
21
#from google.auth.transport.requests import AuthorizedSession
22
#from google.oauth2.credentials import Credentials
23
24
from elodie.media.photo import Photo
25
from elodie.media.video import Video
26
from elodie.plugins.plugins import PluginBase
27
28
class SQLite(PluginBase):
29
    """A class to execute plugin actions.
30
       
31
       Requires a config file with the following configurations set.
32
       database_file:
33
            The full path to the SQLite database (.db).
34
    
35
    """
36
37
    __name__ = 'SQLite'
38
39
    def __init__(self):
40
        super(SQLite, self).__init__()
41
        
42
        self.database_schema = '{}{}{}'.format(os.path.dirname(os.path.realpath(__file__)), os.sep, 'schema.sql')
43
        self.database_file = None
44
        if('database_file' in self.config_for_plugin):
45
            self.database_file = self.config_for_plugin['database_file']
46
47
        self.con = sqlite3.connect(self.database_file)
48
        self.con.row_factory = sqlite3.Row
49
        self.cursor = self.con.cursor()
50
51
    def after(self, file_path, destination_folder, final_file_path, metadata):
52
53
        # We check if the source path exists in the database already.
54
        # If it does then we assume that this is an update operation.
55
        full_destination_path = '{}{}'.format(destination_folder, final_file_path)
56
        self.cursor.execute("SELECT `pathOriginal` FROM `metadata` WHERE `pathOriginal`=:pathOriginal", {'pathOriginal': file_path})
57
        if(self.cursor.fetchone() is None):
58
            self.log(u'SQLite plugin inserting {}'.format(file_path))
59
            sql_statement, sql_values = self._insert_row_sql(file_path, full_destination_path, metadata)
60
        else:
61
            self.log(u'SQLite plugin updating {}'.format(file_path))
62
            sql_statement, sql_values = self._update_row_sql(file_path, full_destination_path, metadata)
63
64
        self.cursor.execute(sql_statement, sql_values)
65
66
    def batch(self):
67
        pass
68
69
    def before(self, file_path, destination_folder):
70
        pass
71
72
    def create_schema(self):
73
        with open(self.database_schema, 'r') as fp_schema:
74
            sql_statement = fp_schema.read()
75
76
        with self.con:
77
            self.cursor.executescript(sql_statement)
78
79
    def run_query(self, sql, values):
80
        self.cursor.execute(sql, values)
81
        return self.cursor.fetchall()
82
83
    def _insert_row_sql(self, current_path, final_path, metadata):
84
        return (
85
            "INSERT INTO `metadata` (`pathOriginal`) VALUES(:pathOriginal)",
86
            {'pathOriginal': final_path}
87
        )
88
89
    def _update_row_sql(self, current_path, final_path, metadata):
90
        return (
91
            "UPDATE `metadata` SET `pathOriginal`=:pathOriginal WHERE `pathOriginal`=:currentPathOriginal",
92
            {'currentPathOriginal': current_path, 'pathOriginal': final_path}
93
        )
94