File packages/category.py changed (mode: 100644) (index e6bc0ec..a0c460f) |
1 |
|
from packages.item_upload import checkEncoding |
|
2 |
|
from packages.config import get_path |
|
|
1 |
|
""" |
|
2 |
|
written by: Sebastian Fricke |
|
3 |
|
2019-01-15 |
|
4 |
|
|
|
5 |
|
class and functions for handling the category config |
|
6 |
|
|
|
7 |
|
CategoryConfig class: |
|
8 |
|
contains the final dictionary id_list |
|
9 |
|
the raw_data list for the initial read |
|
10 |
|
and the path object with path string and encoding |
|
11 |
|
""" |
|
12 |
|
|
3 |
13 |
import os |
import os |
4 |
|
from pathlib import Path |
|
|
14 |
|
import sys |
|
15 |
|
from packages.item_upload import checkEncoding, errorPrint, warnPrint |
|
16 |
|
from packages.config import get_path |
5 |
17 |
|
|
6 |
|
class CategoryConfig: |
|
|
18 |
|
class CategoryConfig(object): |
|
19 |
|
""" |
|
20 |
|
id_list: dictionary of category name with it's id |
|
21 |
|
raw_data: read lines from the file |
|
22 |
|
path: path string + encoding |
|
23 |
|
|
|
24 |
|
methods: |
|
25 |
|
findConfig |
|
26 |
|
readConfig |
|
27 |
|
rawConfig |
|
28 |
|
""" |
7 |
29 |
def __init__(self): |
def __init__(self): |
8 |
30 |
self.id_list = dict() |
self.id_list = dict() |
9 |
31 |
self.raw_data = [] |
self.raw_data = [] |
10 |
32 |
self.path = {'path':'', 'encoding':''} |
self.path = {'path':'', 'encoding':''} |
11 |
33 |
|
|
12 |
34 |
def findConfig(self, root): |
def findConfig(self, root): |
|
35 |
|
""" |
|
36 |
|
findConfig: |
|
37 |
|
search for the category config in 2 ways |
|
38 |
|
1. search in the root of the project |
|
39 |
|
2. ask the user to identify the file |
|
40 |
|
The location of the category config can also |
|
41 |
|
be saved into the config file |
|
42 |
|
""" |
|
43 |
|
|
13 |
44 |
try: |
try: |
14 |
45 |
self.path['path'] = os.path.join(root, 'category') |
self.path['path'] = os.path.join(root, 'category') |
15 |
|
except Exception as err: |
|
16 |
|
print("ERROR @ findCategoryConfig: Building path failed: {0}" |
|
17 |
|
.format(err)) |
|
|
46 |
|
except (ValueError, Exception) as err: |
|
47 |
|
errorPrint("Building path failed", |
|
48 |
|
err, sys.exc_info()[2].tb_lineno) |
18 |
49 |
return False |
return False |
19 |
50 |
|
|
20 |
|
if(os.path.isfile(self.path['path'])): |
|
21 |
|
self.path = check_encoding(self.path) |
|
|
51 |
|
if os.path.isfile(self.path['path']): |
|
52 |
|
self.path = checkEncoding(self.path) |
22 |
53 |
return True |
return True |
23 |
|
else: |
|
24 |
|
try: |
|
25 |
|
self.path['path'] = get_path(message='category config path', |
|
26 |
|
path_type='file', |
|
27 |
|
initialdir=root) |
|
28 |
|
self.path = check_encoding(self.path) |
|
29 |
|
except Exception as err: |
|
30 |
|
print("ERROR @ findCategoryConfig: searching file: {0}" |
|
31 |
|
.format(err)) |
|
32 |
|
if(not(self.path['path'])): |
|
33 |
|
return False |
|
34 |
|
else: |
|
35 |
|
return True |
|
|
54 |
|
|
|
55 |
|
try: |
|
56 |
|
self.path['path'] = get_path(message='category config path', |
|
57 |
|
path_type='file', |
|
58 |
|
initialdir=root) |
|
59 |
|
self.path = checkEncoding(self.path) |
|
60 |
|
except (ValueError, Exception) as err: |
|
61 |
|
errorPrint("searching file failed", |
|
62 |
|
err, sys.exc_info()[2].tb_lineno) |
|
63 |
|
|
|
64 |
|
if not self.path['path']: |
|
65 |
|
return False |
|
66 |
|
|
|
67 |
|
return True |
36 |
68 |
|
|
37 |
69 |
|
|
38 |
70 |
def rawConfig(self): |
def rawConfig(self): |
|
71 |
|
""" |
|
72 |
|
rawConfig: |
|
73 |
|
read the lines of the categoryConfig |
|
74 |
|
without parsing or assing any specific values |
|
75 |
|
""" |
39 |
76 |
with open(self.path['path'], |
with open(self.path['path'], |
40 |
|
mode='r', encoding=self.path['encoding']) as config: |
|
|
77 |
|
mode='r', encoding=self.path['encoding']) as config: |
41 |
78 |
try: |
try: |
42 |
79 |
self.raw_data =\ |
self.raw_data =\ |
43 |
80 |
[row.strip(' ').strip('\n').split(';') for row in config] |
[row.strip(' ').strip('\n').split(';') for row in config] |
44 |
|
except Exception as err: |
|
45 |
|
print("ERROR @ findCategoryConfig: Read data failed: {0}" |
|
46 |
|
.format(err)) |
|
|
81 |
|
except (ValueError, Exception) as err: |
|
82 |
|
errorPrint("raw data read failed", |
|
83 |
|
err, sys.exc_info()[2].tb_lineno) |
47 |
84 |
return len(self.raw_data) > 0 |
return len(self.raw_data) > 0 |
48 |
85 |
|
|
49 |
86 |
|
|
50 |
87 |
def readConfig(self): |
def readConfig(self): |
51 |
|
if(not(self.raw_data)): |
|
52 |
|
if(not(self.rawConfig())): |
|
|
88 |
|
""" |
|
89 |
|
readConfig: |
|
90 |
|
assign the values found in raw_data to the id_list |
|
91 |
|
""" |
|
92 |
|
if not self.raw_data: |
|
93 |
|
if not self.rawConfig(): |
53 |
94 |
return False |
return False |
54 |
95 |
for row in self.raw_data: |
for row in self.raw_data: |
55 |
96 |
option = "".join(row).split('=') |
option = "".join(row).split('=') |
56 |
97 |
if(len(option[0]) > 1 and len(option[1]) > 1): |
if(len(option[0]) > 1 and len(option[1]) > 1): |
57 |
98 |
try: |
try: |
58 |
99 |
self.id_list[option[0]] = int(option[1]) |
self.id_list[option[0]] = int(option[1]) |
59 |
|
except Exception as err: |
|
60 |
|
print("ERROR @ readCategoryConfig: convert to int: {0}" |
|
61 |
|
.format(err)) |
|
|
100 |
|
except ValueError as err: |
|
101 |
|
warnPrint("Integer conversion failed", |
|
102 |
|
sys.exc_info()[2].tb_lineno, err) |
62 |
103 |
else: |
else: |
63 |
104 |
return False |
return False |
64 |
105 |
|
|
65 |
106 |
return True |
return True |
66 |
|
|
|