Ver código fonte

Ajouté une gestion de paramètres en ligne de commande

nom.py -h pour voir l'aide
pull/1/head
Figg 3 anos atrás
pai
commit
fddef4ae4c
1 arquivos alterados com 37 adições e 12 exclusões
  1. 37
    12
      export_data-v2.py

+ 37
- 12
export_data-v2.py Ver arquivo

@@ -6,14 +6,14 @@ from matplotlib import pyplot as plt
6 6
 import os
7 7
 import json
8 8
 import re
9
-
9
+import sys, getopt
10 10
 
11 11
 
12 12
 # ------------------------------------------------
13
-#                   Constants
13
+#                   Globals
14 14
 # ------------------------------------------------
15 15
 
16
-DATA_PATH = 'D:/Files/Data/Messenger/'
16
+DATA_PATH = './data/'
17 17
 
18 18
 OTHER_LABEL = 'Les Autres'
19 19
 
@@ -25,11 +25,30 @@ CONTENT = 'content'
25 25
 TIMESTAMP = 'timestamp_ms'
26 26
 SENDER = 'sender_name'
27 27
 
28
+HELP = """General options :
29
+    -h, --help          Consulter l'aide
30
+    --path=<path>       Redéfinir le chemin d'accès aux données (par défaut ./data)
31
+    """
28 32
 
29 33
 # ------------------------------------------------
30 34
 #                   Functions
31 35
 # ------------------------------------------------
32 36
 
37
+def handleArguments(argv):
38
+    try:
39
+        opts, args = getopt.getopt(argv, 'h',['help','path='])
40
+    except getopt.GetoptError:
41
+        print('Usage:\n '+os.path.basename(__file__)+' <command> [option]\n')
42
+        print(HELP)
43
+        sys.exit(2)
44
+
45
+    for opt, arg in opts:
46
+        if opt in ('-h', '--help'):
47
+            print(HELP)
48
+            sys.exit()
49
+        elif opt in ('--path'):
50
+            DATA_PATH = arg
51
+
33 52
 def readBrokenFbJson(datafile_path):
34 53
     # ntm facebook
35 54
     # https://stackoverflow.com/questions/50008296/facebook-json-badly-encoded
@@ -42,11 +61,12 @@ def readBrokenFbJson(datafile_path):
42 61
         return json.loads(repaired.decode('utf8'))
43 62
 
44 63
 def computeData():
45
-    # Tous les fichiers du dossier sont traités sans validation
46
-    datafiles_path = [DATA_PATH + filename for filename in os.listdir(DATA_PATH) if filename [-4:] == "json"]
47
-    print(datafiles_path)
64
+    # Tous les fichiers du dossier sont traités sans distinction
65
+    datafiles_path = [DATA_PATH + filename for filename in os.listdir(DATA_PATH)]
48 66
     messages, participants = [], []
49 67
 
68
+    print(datafiles_path)
69
+    
50 70
     for datafile_path in datafiles_path:
51 71
         datacontent = readBrokenFbJson(datafile_path)
52 72
         if datacontent is None : continue
@@ -81,8 +101,8 @@ def computeParticipation(messages):
81 101
     return sorted(result.items(), key = lambda x: x[1])
82 102
 
83 103
 def mergeSmallParticipation(rawParticipation, threshold = 1):
84
-    values = [e[1] for e in participation]
85
-    labels = [e[0] for e in participation]
104
+    values = [e[1] for e in rawParticipation]
105
+    labels = [e[0] for e in rawParticipation]
86 106
 
87 107
     totalValues = sum(values)
88 108
 
@@ -107,9 +127,14 @@ def displayParticipation(participation):
107 127
 #                   Main Code
108 128
 # ------------------------------------------------
109 129
 
110
-participants, messages = computeData()
111
-messages = filterMessages(messages)
130
+def main(argv):
131
+    handleArguments(argv)
132
+
133
+    participants, messages = computeData()
134
+    messages = filterMessages(messages)
112 135
 
113
-participation = computeParticipation(messages)
114
-displayParticipation(participation)
136
+    participation = computeParticipation(messages)
137
+    displayParticipation(participation)
115 138
 
139
+if __name__ == "__main__":
140
+    main(sys.argv[1:])

Carregando…
Cancelar
Salvar