def copy_files_in_dir_if_changed(src, dst): if not os.path.isdir(src): return if not os.path.isdir(dst): os.makedirs(dst) for item in os.listdir(src): path = os.path.join(src, item) if os.path.isfile(path): copy_dst = dst dstpath = os.path.join(dst, item) md5 = FileUtils.get_file_md5(path) dstmd5 = 0 if os.path.exists(dstpath): dstmd5 = FileUtils.get_file_md5(dstpath) if md5 != dstmd5: shutil.copy(path, copy_dst) Log.i("{0} done".format(path)) else: Log.i("{0} don't need copy".format(path)) if os.path.isdir(path): new_dst = os.path.join(dst, item) FileUtils.copy_files_in_dir_if_changed(path, new_dst)
def remove_files_in_dir(src, ext): for item in os.listdir(src): path = os.path.join(src, item) if os.path.isfile(path): if os.path.splitext(path)[1] == ext: os.remove(path) if os.path.isfile(path.replace(".json", ".skel")) is False: Log.i(path) if os.path.isdir(path): new_dst = os.path.join(src, item) FileUtils.remove_files_in_dir(new_dst, ext)
def resize(path, size): try: img = Image.open(img_path) (width, height) = img.size new_width = 200 new_height = height * new_width / width out = img.resize((new_width, new_height)) ext = os.path.splitext(img_path)[1] new_file_name = '%s%s' % ('small', ext) out.save(new_file_name) except Exception: Log.w("resize error")
def encrypt_file(path, key, sign): file = open(path, "rb+") buff = file.read() pre_sign = str(buff[0:len(sign)]) if pre_sign == sign: Log.i('have entrypt :' + path) else: Log.i('entrypt file :' + path) bytes = Crypto.encrypt_by_xxtea(buff, key) bytes = sign + bytes file.seek(0) file.write(bytes) file.close()
def get_operation(self, operation_name): operation = None try: operation_full_path = 'operations.%s' % operation_name module_t = __import__(operation_full_path) opt_module = getattr(module_t, operation_name) c = getattr(opt_module, operation_name.title()) operation = object.__new__(c) operation.__init__() except Exception as e: Log.e(e) operation = None return operation
def parse_value_by_type(self, value, type): if type == "int": return int(value) elif type == "string": return str(value) elif type == "vector<int>": values = value.split("|") int_values = [] for var in values: int_values.append(int(var)) return int_values elif type == "vector<string>": values = value.split("|") return values else: Log.w("unknow type: " + type) return value
def parse(self, path): if os.path.splitext(path)[1] != '.xlsx': Log.w('unavailable file type!') return self._datas = {} xlsx = xlrd.open_workbook(path) sheets = xlsx.sheets() table = sheets[0] rowcount = table.nrows for i in range(rowcount): if i == 0: pass elif i == 1: self._fields = table.row_values(i) elif i == 2: self._types = table.row_values(i) else: line = table.row_values(i) line_value = self.parse_line(line) line_id = line_value[self._fields[0]] if line_id is None: Log.e('The id field must be nonil expected') continue if line_id in self._datas: Log.w('The table contains multipe line in a same id') continue self._datas[line_id] = line_value return self._datas
def get_plugin(self, plugin_name): plugin = None try: plugin_full_path = 'plugins.%s.%s' % (plugin_name, plugin_name) module_t = __import__(plugin_full_path) opt_module = getattr(module_t, plugin_name) opt_module = getattr(opt_module, plugin_name) classname = "" names = plugin_name.split("_") for name in names: classname += name.title() c = getattr(opt_module, classname) plugin = object.__new__(c) plugin.__init__() except Exception as e: Log.e(e) plugin = None return plugin
def show_version_info(self): Log.i("print something introduce operation.")
def modify_image_colors(path): Log.e(path) img = Image.open(path) Log.e(img) (w, h) = img.size count_map = {} type_map = {} def gen_key(pixel): if type(pixel) == int: return "%s" % pixel elif len(pixel) == 1: return "%s" % pixel[0] elif len(pixel) == 3: return "%s_%s_%s" % (pixel[0], pixel[1], pixel[2]) elif len(pixel) == 4: return "%s_%s_%s_%s" % (pixel[0], pixel[1], pixel[2], pixel[3]) for x in range(0, w): for y in range(0, h): # Log.e("%s %s = %s" % (x, y, img.getpixel((x, y)))) pixel = img.getpixel((x, y)) key = gen_key(pixel) if key not in count_map: count_map[key] = 0 count_map[key] += 1 def modify(pixel): if type(pixel) == int: return pixel elif len(pixel) == 3: r = random.randint(0, 2) if r == 0: return (min(pixel[0] + 1, 255), pixel[1], pixel[2]) if r == 1: return (pixel[0], min(pixel[1] + 1, 255), pixel[2]) if r == 2: return (pixel[0], pixel[1], min(pixel[2] + 1, 255)) elif len(pixel) == 4: r = random.randint(0, 2) if r == 0: return (min(pixel[0] + 1, 255), pixel[1], pixel[2], pixel[3]) if r == 1: return (pixel[0], min(pixel[1] + 1, 255), pixel[2], pixel[3]) if r == 2: return (pixel[0], pixel[1], min(pixel[2] + 1, 255), pixel[3]) else: return pixel keys = [] for var in count_map.keys(): keys.append(var) for i in range(0, 3): k = random.choice(keys) if k not in type_map: colors = [] for c in k.split("_"): colors.append(int(c)) type_map[k] = tuple(modify(colors)) for x in range(0, w): for y in range(0, h): # print x, y pixel = img.getpixel((x, y)) key = gen_key(pixel) if key in type_map: img.putpixel((x, y), type_map[key]) img.save(path)
def gen_codes(dst_dir, fold_num=5, class_num_per_dir=10, delt_num=3): mkdir_and_romove_old(dst_dir) Log.i('output path : ' + dst_dir) fold_name_arr = get_elements_by_random(COMMON_WORDS, fold_num) all_class_info = {} for fold_name in fold_name_arr: Log.i('-----generate element <' + fold_name + '>-----') element_name = fold_name fold_name = fold_name.lower() + 's' dir_path = os.path.join(dst_dir, fold_name) mkdir_and_romove_old(dir_path) # base class base_variables = get_elements_by_random(VARIABLE_WORDS, random.uniform(5, 10)) base_classname = gen_class_name(_pre_name, 'Base', element_name) gen_class(dir_path, base_classname, '', base_variables) # files class_num = int(class_num_per_dir + random.uniform(-3, 3)) sub_name_arr = get_elements_by_random(FUNCTION_MIDDLE_WORDS, class_num) import_class_names = [] for y in range(0, class_num): classname = gen_class_name(_pre_name, sub_name_arr[y], element_name) import_class_names.append(classname) variables = get_elements_by_random(VARIABLE_WORDS, random.uniform(3, 10)) for index in range(0, len(variables)): var = variables[index] variables[index] = (var[0], element_name.lower() + upper_first_char(var[1])) gen_class(dir_path, classname, base_classname, variables) all_class_info[element_name] = import_class_names # manager class manager_classname = gen_class_name(_pre_name, element_name, 'Manager') gen_manager_class(dir_path, manager_classname, element_name, base_variables, import_class_names) # readme file readme_file_path = os.path.join(dst_dir, "readme.txt") readme_file = open(readme_file_path, 'w') for x in range(1, 10): readme_file.write( '-------------------- code %d ----------------------\n' % x) elements1 = get_elements_by_random(fold_name_arr, random.uniform(1, fold_num)) import_content = '' operate_content = '' for name in elements1: manager_name = gen_class_name(_pre_name, name, 'Manager') import_content = import_content + '#import "%s.h"\n' % manager_name for y in range(1, int(random.uniform(2, fold_num))): operate_content = operate_content + '[[%s sharedInstance] create%sByName:@"%s"];\n' % ( manager_name, name, random.choice(all_class_info[name])) readme_file.write(import_content) readme_file.write('\n') readme_file.write(operate_content) readme_file.write('\n\n') readme_file.close()