Browse Source

release configparser.py

pikastech 3 years ago
parent
commit
2da2f124cf

+ 12 - 0
examples/configparser/test2.py

@@ -0,0 +1,12 @@
+import pika_configparser
+
+fd = open('test/assets/widget_config.ini', 'r')
+s = fd.read(-1)
+print(s)
+config = pika_configparser.ConfigParser()
+config.read_string(s)
+print(config)
+font_color = config.get('文本控件_20030101', 'font_color')
+type(font_color)
+print(font_color)
+fd.close()

+ 112 - 0
package/configparser/configparser.py

@@ -0,0 +1,112 @@
+from PikaStdData import String
+
+class ConfigParser():
+    content = ''
+    config_dict = {}
+
+    def _parse(self):
+        self.content = String(self.content).replace('\r', '')
+        content = String(self.content)
+        lines = content.split('\n')
+        for line in lines:
+            line = String(line)
+            if line.startwith('#'):
+                continue
+            if line.startwith(';'):
+                continue
+            if line.startwith('['):
+                section = String(line.replace('[', ''))
+                section = section.replace(']', '')
+                self.config_dict[section] = {}
+                continue
+
+            if line.strip() == '':
+                continue
+
+            stmt = line.split('=')
+            key = String(stmt[0]).strip()
+            value = String(_getvalue(stmt)).strip()
+            section_dict = self.config_dict[section]
+            section_dict[key] = value
+
+    def sections(self):
+        section_keys = self.config_dict.keys()
+        sections = []
+        for section_item in section_keys:
+            sections.append(section_item)
+        return sections
+
+    def options(self, section):
+        section_dict = self.config_dict[section]
+        option_keys = section_dict.keys()
+        options = []
+        for option_item in option_keys:
+            options.append(option_item)
+        return options
+
+    def get(self, section, option):
+        section_dict = self.config_dict[section]
+        return section_dict[option]
+
+    def set(self, section, option, value):
+        section_dict = self.config_dict[section]
+        section_dict[option] = value
+
+    # support config[key] = val
+    def __set__(self, __key, __val):
+        self.config_dict[__key] = __val
+
+    # support val = config[key]
+    def __get__(self, __key):
+        return self.config_dict[__key]
+
+    def items(self, section):
+        section_dict = self.config_dict[section]
+        section_keys = section_dict.keys()
+        items = []
+        for key in section_keys:
+            val = section_dict[key]
+            items.append([key, val])
+        return items
+
+    def __str__(self):
+        content = ''
+        section_keys = self.config_dict.keys()
+        for section_item in section_keys:
+            content += '[' + section_item + ']\n'
+            section_dict = self.config_dict[section_item]
+            section_keys = section_dict.keys()
+            for key in section_keys:
+                val = section_dict[key]
+                content += key + ' = ' + val + '\n'
+            content += '\n'
+        return content
+
+    def write(self, file_name):
+        print('Error: write() method not implemented')
+        raise
+        self.content = self.__str__(self)
+        print(self.content)
+
+    def read_string(self, content):
+        self.content = content
+        self._parse()
+
+    def read(self, file_name):
+        print('Error: read() method not implemented')
+        raise
+        content = ''
+        self.content = content
+        self._parse()
+
+def _getvalue(stmt):
+    index = 0
+    val = ''
+    for item in stmt:
+        if index > 0:
+            if val != '':
+                val += ('=' + item)
+            else:
+                val += item
+        index = index + 1
+    return val

+ 112 - 0
port/linux/package/pikascript/pikascript-lib/configparser/configparser.py

@@ -0,0 +1,112 @@
+from PikaStdData import String
+
+class ConfigParser():
+    content = ''
+    config_dict = {}
+
+    def _parse(self):
+        self.content = String(self.content).replace('\r', '')
+        content = String(self.content)
+        lines = content.split('\n')
+        for line in lines:
+            line = String(line)
+            if line.startwith('#'):
+                continue
+            if line.startwith(';'):
+                continue
+            if line.startwith('['):
+                section = String(line.replace('[', ''))
+                section = section.replace(']', '')
+                self.config_dict[section] = {}
+                continue
+
+            if line.strip() == '':
+                continue
+
+            stmt = line.split('=')
+            key = String(stmt[0]).strip()
+            value = String(_getvalue(stmt)).strip()
+            section_dict = self.config_dict[section]
+            section_dict[key] = value
+
+    def sections(self):
+        section_keys = self.config_dict.keys()
+        sections = []
+        for section_item in section_keys:
+            sections.append(section_item)
+        return sections
+
+    def options(self, section):
+        section_dict = self.config_dict[section]
+        option_keys = section_dict.keys()
+        options = []
+        for option_item in option_keys:
+            options.append(option_item)
+        return options
+
+    def get(self, section, option):
+        section_dict = self.config_dict[section]
+        return section_dict[option]
+
+    def set(self, section, option, value):
+        section_dict = self.config_dict[section]
+        section_dict[option] = value
+
+    # support config[key] = val
+    def __set__(self, __key, __val):
+        self.config_dict[__key] = __val
+
+    # support val = config[key]
+    def __get__(self, __key):
+        return self.config_dict[__key]
+
+    def items(self, section):
+        section_dict = self.config_dict[section]
+        section_keys = section_dict.keys()
+        items = []
+        for key in section_keys:
+            val = section_dict[key]
+            items.append([key, val])
+        return items
+
+    def __str__(self):
+        content = ''
+        section_keys = self.config_dict.keys()
+        for section_item in section_keys:
+            content += '[' + section_item + ']\n'
+            section_dict = self.config_dict[section_item]
+            section_keys = section_dict.keys()
+            for key in section_keys:
+                val = section_dict[key]
+                content += key + ' = ' + val + '\n'
+            content += '\n'
+        return content
+
+    def write(self, file_name):
+        print('Error: write() method not implemented')
+        raise
+        self.content = self.__str__(self)
+        print(self.content)
+
+    def read_string(self, content):
+        self.content = content
+        self._parse()
+
+    def read(self, file_name):
+        print('Error: read() method not implemented')
+        raise
+        content = ''
+        self.content = content
+        self._parse()
+
+def _getvalue(stmt):
+    index = 0
+    val = ''
+    for item in stmt:
+        if index > 0:
+            if val != '':
+                val += ('=' + item)
+            else:
+                val += item
+        index = index + 1
+    return val

+ 10 - 1
port/linux/pkg-push.sh

@@ -1,13 +1,22 @@
 FLAG_OK="\033[32m[ OK ]\033[0m"
 FLAG_INFO="\033[32m[Info]\033[0m"
 FLAG_NOTE="\033[35m[Note]\033[0m"
+FLAG_ERROR="\033[31m[Error]\033[0m"
 
 if [ $# != 1 ] ; then
 echo "USAGE: $0 [pkg name]"
 echo " e.g.: $0 ctypes"
 exit 1;
 fi
+
 pkg=$1
+
+# Check if the package exists
+if [ ! -d package/pikascript/pikascript-lib/$pkg ] ; then
+echo -e "$FLAG_ERROR Package $pkg does not exist"
+exit 1;
+fi
+
 cp package/pikascript/pikascript-lib/$pkg ../../package/ -r
 git add package/pikascript/pikascript-lib/$pkg
 
@@ -36,7 +45,7 @@ ls ../../examples/$pkg
 fi
 
 git add ../../package/$pkg
-echo -e "$FLAG_INFO C files added:"
+echo -e "$FLAG_INFO lib files added:"
 # list files name in package/pikascript/pikascript-lib/$pkg
 ls  package/pikascript/pikascript-lib/$pkg
 echo -e "$FLAG_OK Push \033[32m$pkg\033[0m to ../../package/$pkg successfully!"