示例#1
0
class spacebook(MDApp):
    title = 'spacebook'
    icon  = 'data/images/icon.png'
    nav_drawer = ObjectProperty()
    lang = StringProperty('en')

    def __init__(self, **kvargs):
        super(spacebook, self).__init__(**kvargs)
        Window.bind(on_keyboard=self.events_program)
        Window.soft_input_mode = 'below_target'

        self.list_previous_screens = ['base']
        self.window = Window
        self.config = ConfigParser()
        self.manager = None
        self.window_language = None
        self.exit_interval = False
        self.dict_language = literal_eval(
            open(
                os.path.join(self.directory, 'data', 'locales', 'locales.txt')).read()
        )
        self.translation = Translation(
            self.lang, 'Ttest', os.path.join(self.directory, 'data', 'locales')
        )

    def get_application_config(self):
        return super(spacebook, self).get_application_config(
                        '{}/%(appname)s.ini'.format(self.directory))

    def build_config(self, config):
        config.adddefaultsection('General')
        config.setdefault('General', 'language', 'en')

    def set_value_from_config(self):
        self.config.read(os.path.join(self.directory, 'spacebook.ini'))
        self.lang = self.config.get('General', 'language')

    def build(self):
        self.set_value_from_config()
        self.load_all_kv_files(os.path.join(self.directory, 'libs', 'uix', 'kv'))
        self.screen = StartScreen()
        self.manager = self.screen.ids.manager
        self.nav_drawer = self.screen.ids.nav_drawer

        return self.screen

    def load_all_kv_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            kv_file = os.path.join(directory_kv_files, kv_file)
            if os.path.isfile(kv_file):
                if not PY2:
                    with open(kv_file, encoding='utf-8') as kv:
                        Builder.load_string(kv.read())
                else:
                    Builder.load_file(kv_file)

    def events_program(self, instance, keyboard, keycode, text, modifiers):
        if keyboard in (1001, 27):
            if self.nav_drawer.state == 'open':
                self.nav_drawer.set_state("toggle")
            self.back_screen(event=keyboard)
        elif keyboard in (282, 319):
            pass

        return True

    def back_screen(self, event=None):
        if event in (1001, 27):
            if self.manager.current == 'base':
                self.dialog_exit()
                return
            try:
                self.manager.current = self.list_previous_screens.pop()
            except:
                self.manager.current = 'base'
            self.screen.ids.action_bar.title = self.title
            self.screen.ids.action_bar.left_action_items = \
                [['menu', lambda x: self.nav_drawer.set_state("toggle")]]

    def show_about(self, *args):
        self.nav_drawer.set_state("toggle")
        self.screen.ids.about.ids.label.text = \
            self.translation._(
                u'[size=20][b]spacebook[/b][/size]\n\n'
                u'[b]Version:[/b] {version}\n'
                u'[b]License:[/b] MIT\n\n'
                u'[size=20][b]Developer[/b][/size]\n\n'
                u'[ref=SITE_PROJECT]'
                u'[color={link_color}]NAME_AUTHOR[/color][/ref]\n\n'
                u'[b]Source code:[/b] '
                u'[ref=REPO_PROJECT]'
                u'[color={link_color}]GitHub[/color][/ref]').format(
                version=__version__,
                link_color=get_hex_from_color(self.theme_cls.primary_color)
            )
        self.manager.current = 'about'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def show_license(self, *args):
        self.nav_drawer.set_state("toggle")
        if not PY2:
            self.screen.ids.license.ids.text_license.text = \
                self.translation._('%s') % open(
                    os.path.join(self.directory, 'LICENSE'), encoding='utf-8').read()
        else:
            self.screen.ids.license.ids.text_license.text = \
                self.translation._('%s') % open(
                    os.path.join(self.directory, 'LICENSE')).read()
        self.manager.current = 'license'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]
        self.screen.ids.action_bar.title = \
            self.translation._('MIT LICENSE')

    def select_locale(self, *args):
        def select_locale(name_locale):
            for locale in self.dict_language.keys():
                if name_locale == self.dict_language[locale]:
                    self.lang = locale
                    self.config.set('General', 'language', self.lang)
                    self.config.write()

        dict_info_locales = {}
        for locale in self.dict_language.keys():
            dict_info_locales[self.dict_language[locale]] = \
                ['locale', locale == self.lang]

        if not self.window_language:
            self.window_language = card(
                Lists(
                    dict_items=dict_info_locales,
                    events_callback=select_locale, flag='one_select_check'
                ),
                size=(.85, .55)
            )
        self.window_language.open()

    def dialog_exit(self):
        def check_interval_press(interval):
            self.exit_interval += interval
            if self.exit_interval > 5:
                self.exit_interval = False
                Clock.unschedule(check_interval_press)

        if self.exit_interval:
            sys.exit(0)
            
        Clock.schedule_interval(check_interval_press, 1)
        toast(self.translation._('Press Back to Exit'))

    def on_lang(self, instance, lang):
        self.translation.switch_lang(lang)
示例#2
0
class Project_Test_APP(App):
    '''Функционал программы.'''

    title = 'Project_Test_APP'
    icon = 'icon.png'
    nav_drawer = ObjectProperty()
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'Blue'
    lang = StringProperty('en')

    def __init__(self, **kvargs):
        super(Project_Test_APP, self).__init__(**kvargs)
        Window.bind(on_keyboard=self.events_program)
        Window.soft_input_mode = 'below_target'

        self.list_previous_screens = ['base']
        self.window = Window
        self.plugin = ShowPlugins(self)
        self.config = ConfigParser()
        self.manager = None
        self.window_language = None
        self.exit_interval = False
        self.dict_language = literal_eval(
            open(os.path.join(self.directory, 'data', 'locales',
                              'locales.txt')).read())
        self.translation = Translation(
            self.lang, 'Ttest', os.path.join(self.directory, 'data',
                                             'locales'))

    def get_application_config(self):
        return super(Project_Test_APP, self).get_application_config(
            '{}/%(appname)s.ini'.format(self.directory))

    def build_config(self, config):
        '''Создаёт файл настроек приложения project_test_app.ini.'''

        config.adddefaultsection('General')
        config.setdefault('General', 'language', 'en')

    def set_value_from_config(self):
        '''Устанавливает значения переменных из файла настроек project_test_app.ini.'''

        self.config.read(os.path.join(self.directory, 'project_test_app.ini'))
        self.lang = self.config.get('General', 'language')

    def build(self):
        self.set_value_from_config()
        self.load_all_kv_files(
            os.path.join(self.directory, 'libs', 'uix', 'kv'))
        self.screen = StartScreen()  # главный экран программы
        self.manager = self.screen.ids.manager
        self.nav_drawer = self.screen.ids.nav_drawer

        return self.screen

    def load_all_kv_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            kv_file = os.path.join(directory_kv_files, kv_file)
            if os.path.isfile(kv_file):
                if not PY2:
                    with open(kv_file, encoding='utf-8') as kv:
                        Builder.load_string(kv.read())
                else:
                    Builder.load_file(kv_file)

    def events_program(self, instance, keyboard, keycode, text, modifiers):
        '''Вызывается при нажатии кнопки Меню или Back Key
        на мобильном устройстве.'''

        if keyboard in (1001, 27):
            if self.nav_drawer.state == 'open':
                self.nav_drawer.toggle_nav_drawer()
            self.back_screen(event=keyboard)
        elif keyboard in (282, 319):
            pass

        return True

    def back_screen(self, event=None):
        '''Менеджер экранов. Вызывается при нажатии Back Key
        и шеврона "Назад" в ToolBar.'''

        # Нажата BackKey.
        if event in (1001, 27):
            if self.manager.current == 'base':
                self.dialog_exit()
                return
            try:
                self.manager.current = self.list_previous_screens.pop()
            except:
                self.manager.current = 'base'
            self.screen.ids.action_bar.title = self.title
            self.screen.ids.action_bar.left_action_items = \
                [['menu', lambda x: self.nav_drawer._toggle()]]

    def show_plugins(self, *args):
        '''Выводит на экран список плагинов.'''

        self.plugin.show_plugins()

    def show_about(self, *args):
        self.nav_drawer.toggle_nav_drawer()
        self.screen.ids.about.ids.label.text = \
            self.translation._(
                u'[size=20][b]Project_Test_APP[/b][/size]\n\n'
                u'[b]Version:[/b] {version}\n'
                u'[b]License:[/b] MIT\n\n'
                u'[size=20][b]Developer[/b][/size]\n\n'
                u'[ref=SITE_PROJECT]'
                u'[color={link_color}]flytrue[/color][/ref]\n\n'
                u'[b]Source code:[/b] '
                u'[ref=https://github.com/flytrue/testapp]'
                u'[color={link_color}]GitHub[/color][/ref]').format(
                version=__version__,
                link_color=get_hex_from_color(self.theme_cls.primary_color)
            )
        self.manager.current = 'about'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def show_license(self, *args):
        if not PY2:
            self.screen.ids.license.ids.text_license.text = \
                self.translation._('%s') % open(
                    os.path.join(self.directory, 'LICENSE'), encoding='utf-8').read()
        else:
            self.screen.ids.license.ids.text_license.text = \
                self.translation._('%s') % open(
                    os.path.join(self.directory, 'LICENSE')).read()
        self.nav_drawer._toggle()
        self.manager.current = 'license'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen()]]
        self.screen.ids.action_bar.title = \
            self.translation._('MIT LICENSE')

    def select_locale(self, *args):
        '''Выводит окно со списком имеющихся языковых локализаций для
        установки языка приложения.'''
        def select_locale(name_locale):
            '''Устанавливает выбранную локализацию.'''

            for locale in self.dict_language.keys():
                if name_locale == self.dict_language[locale]:
                    self.lang = locale
                    self.config.set('General', 'language', self.lang)
                    self.config.write()

        dict_info_locales = {}
        for locale in self.dict_language.keys():
            dict_info_locales[self.dict_language[locale]] = \
                ['locale', locale == self.lang]

        if not self.window_language:
            self.window_language = card(Lists(dict_items=dict_info_locales,
                                              events_callback=select_locale,
                                              flag='one_select_check'),
                                        size=(.85, .55))
        self.window_language.open()

    def dialog_exit(self):
        def check_interval_press(interval):
            self.exit_interval += interval
            if self.exit_interval > 5:
                self.exit_interval = False
                Clock.unschedule(check_interval_press)

        if self.exit_interval:
            sys.exit(0)

        Clock.schedule_interval(check_interval_press, 1)
        toast(self.translation._('Press Back to Exit'))

    def on_lang(self, instance, lang):
        self.translation.switch_lang(lang)
示例#3
0
class keepupthepaceMD(MDApp):
    title = 'keepupthepaceMD'
    icon = 'icon.png'
    nav_drawer = properties.ObjectProperty()
    lang = properties.StringProperty('en')
    #load saved profiles and instantiate the default one
    listofprofiles, myProfile = persistence.getDefaultProfileFromShelf()

    # define the properties that will be updated in the user interface
    knbmi = properties.StringProperty('0')
    kbbmi = properties.StringProperty('0')
    krmr1918 = properties.StringProperty('0')
    krmr1984 = properties.StringProperty('0')
    krmr1990 = properties.StringProperty('0')
    krmrml1918 = properties.StringProperty('0')
    krmrml1984 = properties.StringProperty('0')
    krmrml1990 = properties.StringProperty('0')
    khbe1918 = properties.StringProperty('0')
    khbe1984 = properties.StringProperty('0')
    khbe1990 = properties.StringProperty('0')
    kqfp = properties.StringProperty('0')
    kefp = properties.StringProperty('0')

    # initialize a profile to test the app, if none loaded
    if not (isinstance(myProfile, profile.Profile)):
        myProfile = profile.Profile('not coming from shelf')
        myProfile.isdefault = True
        myProfile.age = 35
        myProfile.gender = enumandconst.Gender.FEMALE
        myProfile.weightIntegerPart = 60
        myProfile.weightDecimalPart = 0
        myProfile.heightIntegerPart = 1
        myProfile.heightDecimalPart = 68
        myProfile.metricChoice = enumandconst.MetricChoice.ISO
        myProfile.computeAll()
    else:
        myProfile.computeAll()

    # initialize all the MET tables
    bicycling = c01.Bicycling
    conditionningExercises = c02.ConditionningExercises
    dancing = c03.Dancing
    fishingHunting = c04.FishingHunting
    homeActivity = c05.HomeActivity
    homeRepair = c06.HomeRepair
    inactivity = c07.Inactivity
    lawnGarden = c08.LawnGarden
    miscellaneous = c09.Miscellaneous
    musicPlaying = c10.MusicPlaying
    occupation = c11.Occupation
    running = c12.Running
    selfcare = c13.SelfCare
    sexualActivity = c14.SexualActivity
    sports = c15.Sports
    transportation = c16.Transportation
    walking = c17.Walking
    waterActivities = c18.WaterActivities
    winterActivites = c19.WinterActivities
    religiousActivities = c20.ReligiousActivities
    volunteeractivities = c21.VolunteerActivities

    def __init__(self, **kvargs):
        super(keepupthepaceMD, self).__init__(**kvargs)
        Window.bind(on_keyboard=self.events_program)
        Window.soft_input_mode = 'below_target'

        self.list_previous_screens = ['base']
        self.window = Window
        self.config = ConfigParser()
        self.manager = None
        self.window_language = None
        self.exit_interval = False
        self.gender_menu = None
        self.dict_language = literal_eval(
            open(os.path.join(self.directory, 'data', 'locales',
                              'locales.txt')).read())
        self.translation = Translation(
            self.lang, 'Ttest', os.path.join(self.directory, 'data',
                                             'locales'))

        self.listOfMetTables = [
            self.translation._('01-Bicycling'),
            self.translation._('02-Conditionning Exercises'),
            self.translation._('03-Dancing'),
            self.translation._('04-Fishing & Hunting'),
            self.translation._('05-Home Activity'),
            self.translation._('06-Home Repair'),
            self.translation._('07-Inactivity'),
            self.translation._('08-Lawn Garden'),
            self.translation._('09-Miscellaneous'),
            self.translation._('10-Music Playing'),
            self.translation._('11-Occupation'),
            self.translation._('12-Runing'),
            self.translation._('13-Self Care'),
            self.translation._('14-Sexual Activity'),
            self.translation._('15-Sports'),
            self.translation._('16-Transportation'),
            self.translation._('17-Walking'),
            self.translation._('18-Water Activities'),
            self.translation._('19-Winter Activities'),
            self.translation._('20-Religious Activities'),
            self.translation._('21-Volunteer Activities')
        ]

    def get_application_config(self):
        return super(keepupthepaceMD, self).get_application_config(
            '{}/%(appname)s.ini'.format(self.directory))

    def build_config(self, config):
        config.adddefaultsection('General')
        config.setdefault('General', 'language', 'en')

    def set_value_from_config(self):
        self.config.read(os.path.join(self.directory, 'keepupthepacemd.ini'))
        self.lang = self.config.get('General', 'language')

    def build(self):
        LabelBase.register(name="Arial", fn_regular="Arial.ttf")

        theme_font_styles.append('Arial')
        self.theme_cls.font_styles["Arial"] = [
            "Arial",
            16,
            False,
            0.15,
        ]
        self.set_value_from_config()
        self.load_all_kv_files(
            os.path.join(self.directory, 'libs', 'uix', 'kv'))
        self.screen = StartScreen()
        self.manager = self.screen.ids.manager
        self.nav_drawer = self.screen.ids.nav_drawer

        return self.screen

    def refreshValuesToDisplayOnScreen(self):
        '''
            Refresh the values to display whenever it's needed
        '''
        self.kbbmi = self.myProfile.displaybBMI()
        self.knbmi = self.myProfile.displaynBMI()
        self.krmr1918, self.krmr1984, self.krmr1990 = self.myProfile.displayRMR(
        )
        self.krmrml1918, self.krmrml1984, self.krmrml1990 = self.myProfile.displayRMRml(
        )
        self.khbe1918, self.khbe1984, self.khbe1990 = self.myProfile.displayHBE(
        )
        self.kqfp, self.kefp = self.myProfile.displayFAT()

    def doThingsBetweenScreen(self):
        '''
            save profiles when leaving some selectted tabs
        '''
        self.myProfile.computeAll()
        persistence.saveprofiles()
        self.refreshValuesToDisplayOnScreen()

    def on_stop(self):
        persistence.saveprofiles()

    def on_pause(self):
        persistence.saveprofiles()

    def load_all_kv_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            kv_file = os.path.join(directory_kv_files, kv_file)
            if os.path.isfile(kv_file):
                with open(kv_file, encoding='utf-8') as kv:
                    Builder.load_string(kv.read())

    def events_program(self, instance, keyboard, keycode, text, modifiers):
        if keyboard == 27:
            if self.nav_drawer.state == 'open':
                self.nav_drawer.toggle_nav_drawer()
            self.back_screen(event=keyboard)
        return True

    def back_screen(self, event=None):
        if event == 27:
            if self.manager.current == 'base':
                self.dialog_exit()
                return
            try:
                self.doThingsBetweenScreen()
                self.manager.current = self.list_previous_screens.pop()
            except:
                self.manager.current = 'base'
            self.screen.ids.action_bar.title = self.title
            self.screen.ids.action_bar.left_action_items = \
                [['menu', lambda x: self.nav_drawer.toggle_nav_drawer()]]

    def show_metrics(self, *args):
        self.nav_drawer.toggle_nav_drawer()
        self.manager.current = 'metrics'
        self.screen.ids.action_bar.left_action_items = \
            [['menu', lambda x: self.nav_drawer.toggle_nav_drawer()]]
        self.screen.ids.action_bar.right_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def show_onelist(self, *args):
        self.list_previous_screens.append('compendiumlist')
        self.manager.current = 'onelist'
        self.screen.ids.action_bar.left_action_items = \
            [['menu', lambda x: self.nav_drawer.toggle_nav_drawer()]]
        self.screen.ids.action_bar.right_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def show_compendium(self, *args):
        self.nav_drawer.toggle_nav_drawer()
        self.manager.current = 'compendiumlist'
        self.screen.ids.action_bar.left_action_items = \
            [['menu', lambda x: self.nav_drawer.toggle_nav_drawer()]]
        self.screen.ids.action_bar.right_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def show_about(self, *args):
        self.nav_drawer.toggle_nav_drawer()
        self.screen.ids.about.ids.label.text = \
            self.translation._(
                u'[size=20][b]keepupthepaceMD[/b][/size]\n\n'
                u'[b]Version:[/b] {version}\n'
                u'[b]License:[/b] MIT\n\n'
                u'[size=20][b]Developer[/b][/size]\n\n'
                u'[ref=SITE_PROJECT]'
                u'[color={link_color}]NAME_AUTHOR[/color][/ref]\n\n'
                u'[b]Source code:[/b] '
                u'[ref=REPO_PROJECT]'
                u'[color={link_color}]GitHub[/color][/ref]').format(
                version=__version__,
                link_color=get_hex_from_color(self.theme_cls.primary_color)
            )
        self.manager.current = 'about'
        self.screen.ids.action_bar.left_action_items = \
            [['menu', lambda x: self.nav_drawer.toggle_nav_drawer()]]
        self.screen.ids.action_bar.right_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def show_license(self, *args):
        self.nav_drawer.toggle_nav_drawer()
        self.screen.ids.license.ids.text_license.text = \
            self.translation._('%s') % open(
                os.path.join(self.directory, 'LICENSE'), encoding='utf-8').read()
        self.manager.current = 'license'
        self.screen.ids.action_bar.left_action_items = \
            [['menu', lambda x: self.nav_drawer.toggle_nav_drawer()]]
        self.screen.ids.action_bar.right_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]
        self.screen.ids.action_bar.title = \
            self.translation._('MIT LICENSE')

    def select_locale(self, *args):
        def select_locale(name_locale):
            for locale in self.dict_language.keys():
                if name_locale == self.dict_language[locale]:
                    self.lang = locale
                    self.config.set('General', 'language', self.lang)
                    self.config.write()

        dict_info_locales = {}
        for locale in self.dict_language.keys():
            dict_info_locales[self.dict_language[locale]] = \
                ['locale', locale == self.lang]

        if not self.window_language:
            self.window_language = card(Lists(dict_items=dict_info_locales,
                                              events_callback=select_locale,
                                              flag='one_select_check'),
                                        size=(.85, .55))
        self.window_language.open()

    def dialog_exit(self):
        def check_interval_press(interval):
            self.exit_interval += interval
            if self.exit_interval > 5:
                self.exit_interval = False
                Clock.unschedule(check_interval_press)

        if self.exit_interval:
            sys.exit(0)

        Clock.schedule_interval(check_interval_press, 1)
        toast(self.translation._('Press Back to Exit'))

    def on_lang(self, instance, lang):
        self.translation.switch_lang(lang)
示例#4
0
class Gllearn(MDApp):
    title = 'Gllearn'
    icon = '.data/images/icon.png'
    nav_drawer = ObjectProperty()
    lang = StringProperty('ru')
    lang_games = StringProperty('en')
    __version__ = '0.6'

    def __init__(self, **kvargs):
        super(Gllearn, self).__init__(**kvargs)
        Window.bind(on_keyboard=self.events_program)
        Window.soft_input_mode = 'below_target'

        self.list_previous_screens = ['base']
        self.window = Window
        self.config = ConfigParser()
        self.manager = None
        self.window_language = None
        self.window_game_language = None
        self.exit_interval = False
        self.dict_language = literal_eval(
            open(os.path.join(self.directory, 'data', 'locales',
                              'locales.txt')).read())

        self.translation = Translation(
            self.lang, 'Gllearn',
            os.path.join(self.directory, 'data', 'locales'))
        self.translation_game = Translation(
            self.lang_games, 'Games',
            os.path.join(self.directory, 'data', 'locales'))

        self.snake_words_with_color = [{
            word: get_random_color(alpha=1.0)
            for word in words
        } for words in [
            s.split(' ')
            for s in self.translation_game._('snake_rounds').split(' | ')
        ]]
        self.current_round_snake = 0

    def set_value_from_config(self):
        self.config.read(os.path.join(self.directory, 'gllearn.ini'))
        self.lang = self.config.get('General', 'language')
        self.lang_games = self.config.get('Games', 'language')

    def build(self):
        self.theme_cls.primary_palette = "Indigo"
        self.set_value_from_config()
        self.load_all_kv_files(
            os.path.join(self.directory, 'libs', 'uix', 'kv'))
        self.screen = StartScreen()
        self.manager = self.screen.ids.manager
        self.nav_drawer = self.screen.ids.nav_drawer
        return self.screen

    def load_all_kv_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            kv_file = os.path.join(directory_kv_files, kv_file)
            if os.path.isfile(kv_file):
                with open(kv_file, encoding='utf-8') as kv:
                    Builder.load_string(kv.read())

    def events_program(self, instance, keyboard, keycode, text, modifiers):
        if keyboard in (1001, 27):
            if self.nav_drawer.state == 'open':
                self.nav_drawer.set_state()
            self.back_screen(event=keyboard)
        elif keyboard in (282, 319):
            pass
        return True

    def back_screen(self, event=None):
        if event in (1001, 27):
            if self.manager.current == 'base':
                self.dialog_exit()
                return
            try:
                self.manager.current = self.list_previous_screens.pop()
                self.manager.transition.direction = 'right'
            except Exception:
                self.manager.current = 'base'
                self.manager.transition.direction = 'right'
            self.screen.ids.action_bar.title = self.title
            self.screen.ids.action_bar.left_action_items = \
                [['menu', lambda x: self.nav_drawer.set_state()]]

    def back_screen_on_game(self, event=None):
        if event in (1001, 27):
            if self.manager.current == 'base':
                self.dialog_exit()
                return
            try:
                self.manager.current = self.list_previous_screens.pop()
                self.manager.transition.direction = 'down'
            except Exception:
                self.manager.current = 'base'
                self.manager.transition.direction = 'down'
            self.screen.ids.action_bar.title = self.title
            Clock.unschedule(self.show_snake)
            self.screen.ids.action_bar.left_action_items = \
                [['menu', lambda x: self.nav_drawer.set_state()]]

    def show_about(self, *args):
        self.nav_drawer.set_state()
        self.screen.ids.about.ids.label.text = \
            self.translation._(
                u'[size=20][b]Gllearn[/b][/size]\n\n'
                u'[b]Version:[/b] {version}\n'
                u'[b]License:[/b] MIT\n\n'
                u'[size=20][b]Developer[/b][/size]\n\n'
                u'[ref=SITE_PROJECT]'
                u'[color={link_color}]Minnakhmetov Musa Albertovich[/color][/ref]\n\n'
                u'[b]Source code:[/b] '
                u'[ref=https://github.com/katustrica/Gllearn]'
                u'[color={link_color}]GitHub[/color][/ref]').format(
                version=self.__version__,
                link_color=get_hex_from_color(self.theme_cls.primary_color)
            )
        self.manager.current = 'about'
        self.manager.transition.direction = 'left'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def show_words(self, *args):
        self.screen.ids.action_bar.title = f'Змейка полиглот {self.current_round_snake+1} lvl'
        self.manager.current = 'words'
        self.manager.transition.direction = 'up'
        self.screen.ids.action_bar.left_action_items = [[
            'chevron-left', lambda x: self.back_screen_on_game(27)
        ]]
        Clock.schedule_once(self.show_snake, 8)

    def next_snake_words(self, *args):
        Clock.unschedule(self.show_snake)
        self.screen.ids.action_bar.title = f'Змейка полиглот {self.current_round_snake+1} lvl'
        self.manager.current = 'words'
        self.manager.transition.direction = 'down'
        self.screen.ids.action_bar.left_action_items = [[
            'chevron-left', lambda x: self.back_screen_on_game(27)
        ]]
        Clock.schedule_once(self.show_snake, 8)

    def show_snake(self, *args):
        self.manager.current = 'snake'
        self.manager.transition.direction = 'up'
        self.screen.ids.action_bar.left_action_items = [[
            'chevron-left', lambda x: self.back_screen_on_game(27)
        ]]

    def show_translator(self, *args):
        self.manager.current = 'translator'
        self.screen.ids.action_bar.title = f'Переводчик'
        self.manager.transition.direction = 'up'
        self.screen.ids.action_bar.left_action_items = [[
            'chevron-left', lambda x: self.back_screen_on_game(27)
        ]]

    def show_license(self, *args):
        self.nav_drawer.set_state()
        self.screen.ids.license.ids.text_license.text = \
            self.translation._('%s') % open(
                os.path.join(self.directory, 'LICENSE'), encoding='utf-8').read()
        self.manager.current = 'license'
        self.manager.transition.direction = 'left'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]
        self.screen.ids.action_bar.title = \
            self.translation._('MIT LICENSE')

    def select_locale(self, *args):
        def select_locale(name_locale):
            for locale in self.dict_language.keys():
                if name_locale == self.dict_language[locale]:
                    self.lang = locale
                    self.config.set('General', 'language', self.lang)
                    self.config.write()

        dict_info_locales = {}
        for locale in self.dict_language.keys():
            dict_info_locales[self.dict_language[locale]] = \
                ['locale', locale == self.lang]

        if not self.window_language:
            self.window_language = card(Lists(dict_items=dict_info_locales,
                                              events_callback=select_locale,
                                              flag='one_select_check'),
                                        size=(.85, .55))
        self.window_language.open()

    def select_game_locale(self, *args):
        def select_game_locale(name_locale):
            for locale in self.dict_language.keys():
                if name_locale == self.dict_language[locale]:
                    self.lang_games = locale
                    self.config.set('Games', 'language', self.lang_games)
                    self.config.write()
                    self.translation_game.switch_lang(name_locale)
                    self.snake_words_with_color = [{
                        word: get_random_color(alpha=1.0)
                        for word in words
                    } for words in [
                        s.split(' ') for s in self.translation_game._(
                            'snake_rounds').split(' | ')
                    ]]

        dict_info_locales = {}
        for locale in self.dict_language.keys():
            dict_info_locales[self.dict_language[locale]] = \
                ['locale', locale == self.lang_games]
        if not self.window_game_language:
            self.window_game_language = card(Lists(
                dict_items=dict_info_locales,
                events_callback=select_game_locale,
                flag='one_select_check'),
                                             size=(.85, .55))
        self.window_game_language.open()

    def dialog_exit(self):
        def check_interval_press(interval):
            self.exit_interval += interval
            if self.exit_interval > 5:
                self.exit_interval = False
                Clock.unschedule(check_interval_press)

        if self.exit_interval:
            sys.exit(0)

        Clock.schedule_interval(check_interval_press, 1)
        toast(self.translation._('Press Back to Exit'))

    def on_lang(self, instance, lang):
        self.translation.switch_lang(lang)
        self.translation_game.switch_lang(lang)
示例#5
0
class name_project(App):
    title = 'TalantA'
    icon = 'icon.png'
    nav_drawer = ObjectProperty()
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'Brown'
    lang = StringProperty('en')

    def __init__(self, **kvargs):
        super(name_project, self).__init__(**kvargs)
        Window.bind(on_keyboard=self.events_program)
        Window.soft_input_mode = 'below_target'

        self.list_previous_screens = ['base']
        self.window = Window
        self.config = ConfigParser()
        self.manager = None
        self.window_language = None
        self.exit_interval = False
        self.dict_language = literal_eval(
            open(os.path.join(self.directory, 'data', 'locales',
                              'locales.txt')).read())
        self.translation = Translation(
            self.lang, 'Ttest', os.path.join(self.directory, 'data',
                                             'locales'))

    def get_application_config(self):
        return super(name_project, self).get_application_config(
            '{}/%(appname)s.ini'.format(self.directory))

    def build_config(self, config):

        config.adddefaultsection('General')
        config.setdefault('General', 'language', 'en')

    def set_value_from_config(self):

        self.config.read(os.path.join(self.directory, 'name_project.ini'))
        self.lang = self.config.get('General', 'language')

    def build(self):
        self.set_value_from_config()
        self.load_all_kv_files(
            os.path.join(self.directory, 'libs', 'uix', 'kv'))
        self.screen = StartScreen()
        self.manager = self.screen.ids.manager
        self.nav_drawer = self.screen.ids.nav_drawer

        return self.screen

    def load_all_kv_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            kv_file = os.path.join(directory_kv_files, kv_file)
            if os.path.isfile(kv_file):
                if not PY2:
                    with open(kv_file, encoding='utf-8') as kv:
                        Builder.load_string(kv.read())
                else:
                    Builder.load_file(kv_file)

    def events_program(self, instance, keyboard, keycode, text, modifiers):

        if keyboard in (1001, 27):
            if self.nav_drawer.state == 'open':
                self.nav_drawer.toggle_nav_drawer()
            self.back_screen(event=keyboard)
        elif keyboard in (282, 319):
            pass

        return True

    def back_screen(self, event=None):

        if event in (1001, 27):
            if self.manager.current == 'base':
                self.dialog_exit()
                return
            try:
                self.manager.current = self.list_previous_screens.pop()
            except:
                self.nav_drawer.toggle_nav_drawer()
                self.manager.current = 'base'
            self.screen.ids.action_bar.title = self.title
            self.screen.ids.action_bar.left_action_items = \
                [['menu', lambda x: self.nav_drawer._toggle()]]

    def show_about(self, *args):
        self.nav_drawer.toggle_nav_drawer()

        self.manager.current = 'about'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def show_help(self, *args):
        self.nav_drawer.toggle_nav_drawer()

        self.manager.current = 'helper'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def show_browse_art(self, *args):
        self.nav_drawer.toggle_nav_drawer()

        self.manager.current = 'browse'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def toggle_nav(self, *args):
        self.nav_drawer.toggle_nav_drawer()

    def show_account(self, *args):
        self.nav_drawer.toggle_nav_drawer()

        self.manager.current = 'account'
        self.screen.ids.action_bar.left_action_items = \
        [['chevron-left', lambda x: self.back_screen(27)]]

    def show_sign_up(self, *args):
        #self.nav_drawer.toggle_nav_drawer()

        self.manager.current = 'signup'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.show_login()]]

    def show_login(self, *args):
        # self.nav_drawer.toggle_nav_drawer()

        self.manager.current = 'login'
        self.screen.ids.action_bar.left_action_items = []

    def show_events(self, *args):
        self.manager.current = 'browse_gallery'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def select_locale(self, *args):
        def select_locale(name_locale):

            for locale in self.dict_language.keys():
                if name_locale == self.dict_language[locale]:
                    self.lang = locale
                    self.config.set('General', 'language', self.lang)
                    self.config.write()

        dict_info_locales = {}
        for locale in self.dict_language.keys():
            dict_info_locales[self.dict_language[locale]] = \
                ['locale', locale == self.lang]

        if not self.window_language:
            self.window_language = card(Lists(dict_items=dict_info_locales,
                                              events_callback=select_locale,
                                              flag='one_select_check'),
                                        size=(.85, .55))
        self.window_language.open()

    def dialog_exit(self):
        def check_interval_press(interval):
            self.exit_interval += interval
            if self.exit_interval > 5:
                self.exit_interval = False
                Clock.unschedule(check_interval_press)

        if self.exit_interval:
            sys.exit(0)

        Clock.schedule_interval(check_interval_press, 1)
        toast(self.translation._('Press Back to Exit'))

    def on_lang(self, instance, lang):
        self.translation.switch_lang(lang)
示例#6
0
class VKGroups(App, AuthorizationOnVK, GetAndSaveLoginPassword):
    '''Функционал программы.'''

    title = 'VKGroups'
    icon = 'icon.png'
    use_kivy_settings = False
    nav_drawer = ObjectProperty()
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'BlueGrey'
    lang = StringProperty('ru')

    def __init__(self, **kvargs):
        super(VKGroups, self).__init__(**kvargs)

        Window.bind(on_keyboard=self.events_program)

        self.POSSIBLE_FILES = \
            ['.png', '.jpg', '.jpeg', '.gif', '.zip', '.txt']
        self.DEVISE_ONLINE = {
            'mobile': 'desktop-mac',
            'computer': 'laptop',
            0: 'power'
        }

        self.PATTERN_WHOM_COMMENT = pattern_whom_comment
        self.PATTERN_REPLACE_LINK = pattern_replace_link

        self.window = Window
        self.config = ConfigParser()
        # Окно прогресса.
        self.load_dialog = ModalView(size_hint=(None, None),
                                     pos_hint={
                                         'x': 5.0 / Window.width,
                                         'y': 5.0 / Window.height
                                     },
                                     background_color=[0, 0, 0, .2],
                                     size=(dp(120), dp(50)),
                                     background=os.path.join(
                                         'data', 'images', 'decorator.png'),
                                     auto_dismiss=False)
        # Экземпляр для вывода списка плагинов пользователя.
        self.instance_list_user_plugins = ShowPlugins(self)
        # Файловый менеджер.
        self.window_file_manager = ModalView(
            size_hint=(1, 1),
            auto_dismiss=False,
            on_open=lambda x: self.load_dialog.dismiss())
        self.window_file_manager_open = False
        self.file_manager = FileManager(
            exit_manager=self.exit_manager,
            floating_button_color=self.theme_cls.primary_color)
        self.window_file_manager.add_widget(self.file_manager)

        self.current_screen_tab = None
        self.current_tab_manager = None
        self.name_press_tab = 'Home_page'
        self.file_manager_not_opening = True
        self.password_form = None
        self.box_posts = None
        self.attach_file = []
        self.attach_image = []
        self.box_for_attach_file = None
        self.box_for_attach_image = None
        self.group_info = None
        self.result_sending_post = None
        self.exit_interval = False
        self.flag_attach = ''
        self.window_user_groups = None
        self.window_language = None
        self.path_to_avatar = os.path.join(self.directory, 'data', 'images',
                                           'avatar.png')
        self.dict_language = ast.literal_eval(
            open(os.path.join(self.directory, 'data', 'locales',
                              'locales')).read())

    def get_application_config(self):
        return super(VKGroups, self).get_application_config(
            '{}/%(appname)s.ini'.format(self.directory))

    def build_config(self, config):
        '''Создаёт файл настроек приложения vkgroups.ini.'''

        config.adddefaultsection('General')
        config.setdefault('General', 'language', 'ru')
        config.setdefault('General', 'theme', 'default')
        config.setdefault('General', 'authorization', 0)
        config.setdefault('General', 'issues_in_group', 0)
        config.setdefault('General', 'count_issues', 20)
        config.setdefault('General', 'user_name', 'User')
        config.setdefault('General', 'last_group', '99411738')
        config.setdefault('General', 'regdata',
                          "{'login': None, 'password': None}")
        config.setdefault('General', 'last_screen', 'load screen')
        config.setdefault('General', 'last_path', '/')
        config.setdefault('General', 'show_dialog_on_download', 0)

    def set_value_from_config(self):
        '''Устанавливает значения переменных из файла настроек
        vkgroups.ini.'''

        self.config.read(os.path.join(self.directory, 'vkgroups.ini'))
        self.theme = self.config.get('General', 'theme')
        self.language = self.config.get('General', 'language')
        self.authorization = self.config.getint('General', 'authorization')
        self.last_path = self.config.get('General', 'last_path')
        self.last_screen = self.config.get('General', 'last_screen')
        self.regdata = \
            ast.literal_eval(self.config.get('General', 'regdata'))
        self.login = self.regdata['login']
        self.password = self.regdata['password']
        try:
            self.user_name = ast.literal_eval(
                self.config.get('General', 'user_name')).decode('utf-8')
        except ValueError:
            self.user_name = self.config.get('General', 'user_name')
        self.group_id = self.config.get('General', 'last_group')
        self.count_issues = self.config.getint('General', 'count_issues')
        self.issues_in_group = \
            self.config.getint('General', 'issues_in_group')
        self.show_dialog_on_download = \
            self.config.getint('General', 'show_dialog_on_download')

    def build(self):
        self.set_value_from_config()
        self.translation = Translation(self.language, 'kivyissues',
                                       '%s/data/locales' % self.directory)
        self.RELATION = {
            1: self.translation._('не женат/не замужем'),
            2: self.translation._('есть друг/подруга'),
            3: self.translation._('помолвлен'),
            4: self.translation._('женат/замужем'),
            5: self.translation._('всё сложно'),
            6: self.translation._('в активном поиске'),
            7: self.translation._('влюблён/влюблена'),
            8: self.translation._('в гражданском браке'),
            0: self.translation._('не указано')
        }
        self.message_about_files_mismatch = {
            'FILE': self.translation._('This file unsupported!'),
            'FOTO': self.translation._('This is not image!')
        }
        self.load_dialog.add_widget(
            MDLabel(text=self.translation._(' Загрузка...')))
        self.load_all_kv_files(self.directory + '/libs/uix/kv')
        self.screen = StartScreen()  # главный экран программы
        self.navigation_button = self.screen.ids.navigation_button
        self.previous = self.navigation_button.ids.previous
        self.manager = self.screen.ids.manager
        self.nav_drawer = self.screen.ids.nav_drawer
        self.current_screen_tab = self.navigation_button.ids.home_page

        if not self.login or not self.password:
            self.show_screen_registration()
        else:  # авторизация на сервере
            self._authorization_on_vk(self.login, self.password, self.group_id)

        Clock.schedule_interval(self.wait_info_for_home_page_group, 1)
        return self.screen

    def wait_info_for_home_page_group(self, interval):
        '''Ожидает получения данных от сервера после чего устанавливает
        значения переменных для экрана Previous.'''

        if self.group_info:
            self.previous.ids.group_title.source = \
                self.group_info['photo_200']
            self.previous.ids.group_name.text = \
                '[size=17][b]%s[/b][/size]\n[size=14]%s[/size]' % (
                    self.group_info['name'], self.group_info['status']
                )
            self.previous.ids.group_link.text = \
                '[ref={link}]{link}[/ref]'.format(
                    link='https://vk.com/%s' % self.group_info['screen_name']
                )
            self.previous.ids.group_people.text = \
                '%s %s' % (
                    self.translation._('Участники'),
                    self.group_info['members_count']
                )
            self.previous.ids.description.text = \
                self.group_info['description']
            self.nav_drawer.ids.user_name.text = \
                '[b]%s[/b]\n[size=12]online[/size]\n' % get_user_name()[0]

            Clock.unschedule(self.wait_info_for_home_page_group)
            Clock.schedule_once(
                lambda kwargs: self.show_form_for_messages(whom_name=''), 1)
            self.check_groups_user(name_group=self.group_info['name'],
                                   info_group=self.group_info['status'],
                                   logo_group=self.group_info['photo_200'])

        if os.path.exists('%s/data/state.txt' % self.directory):
            os.remove('%s/data/state.txt' % self.directory)

    def check_state_fields(self, id_instance, text_field):
        '''Сохраняет содержимое формы регистрации.'''

        if text_field == '':
            return

        if os.path.exists('%s/data/state.txt' % self.directory):
            data_state = ast.literal_eval(
                open('%s/data/state.txt' % self.directory).read())
        else:
            data_state = {}

        data_state[id_instance] = text_field

        # TODO: добавить шифрование данных.
        with open('%s/data/state.txt' % self.directory, 'w') as state_form:
            state_form.write(str(data_state))

    def check_groups_user(self, **kwargs):
        '''Проверяет и добавляет в список новые группы пользователя, которые он посещал.
        Данные имеют вид:
        {'Имя группы': ['Описание группы', 'ссылка на логотип группы', 'id группы,]}.'''

        file_groups_path = '%s/data/groups_user.ini' % self.directory
        if not os.path.exists(file_groups_path):
            with open(file_groups_path, 'w') as file_groups_user:
                file_groups_user.write('{}')

        groups_user = ast.literal_eval(
            open('%s/data/groups_user.ini' % self.directory,
                 encoding='utf-8').read())
        if not kwargs['name_group'] in groups_user:
            groups_user[kwargs['name_group']] = \
                [kwargs['info_group'], kwargs['logo_group'], self.group_id]
            with open(file_groups_path, 'w',
                      encoding='utf-8') as file_groups_user:
                file_groups_user.write(str(groups_user))

    def show_login_and_password(self, selection):
        '''
        Устанавливает свойства текстовых полей для ввода логина и пароля.

        :type selection: <class 'int'>

        '''

        if selection:
            self.password_form.ids.login.password = False
            self.password_form.ids.password.password = False
        else:
            self.password_form.ids.login.password = True
            self.password_form.ids.password.password = True

    def show_groups_user(self, *args):
        '''Выводит окно со списком групп пользователя.'''
        def callback_on_button_click(**kwargs):
            def on_press(text_button):
                # TODO: добавить группу в словарь groups.ini.
                field.dismiss()
                toast(str(text_button))

            field = input_dialog(
                title=kwargs['title'],
                hint_text='',
                text_button_ok=self.translation._('Добавить'),
                text_button_cancel=self.translation._('Отмена'),
                events_callback=on_press)

        def callback_on_item_click(name_item, mode):
            description_group, logo_group, id_group = groups_user[name_item]
            dialog(title=name_item,
                   text='%s\nID - %s' % (description_group, str(id_group)))

        if not self.window_user_groups:
            dict_groups_user = {}
            groups_user = ast.literal_eval(
                open('%s/data/groups_user.ini' % self.directory,
                     encoding='utf-8').read())

            for name_group in groups_user.keys():
                description_group, logo_group, id_group = groups_user[
                    name_group]
                dict_groups_user[name_group] = [description_group, logo_group]

            list_user_groups = ListUserGroups()
            _list = list_user_groups.ids.groups_list
            _list.events_callback = callback_on_item_click
            list_user_groups.ids.add_group.on_press = \
                lambda **kwargs: callback_on_button_click(
                    title=self.translation._('Введите ID группы:')
                )
            _list.two_list_custom_icon(dict_groups_user, IconItemAsync)

            self.window_user_groups = card(list_user_groups, size=(.85, .55))
        self.window_user_groups.open()

    def show_screen_registration(self, fail_registration=False):
        '''Окно с формой регистрации.'''

        if not self.password_form:
            self.password_form = \
                PasswordForm(callback=self.check_fields_login_password)
            self.password_form.ids.group_id.text = self.group_id

        self.screen.ids.load_screen.add_widget(self.password_form)

        # Если произошла ошибка регистрации, деактивируем спиннер и чистим
        # лейблы статуса авторизации.
        if fail_registration:
            self.screen.ids.load_screen.ids.spinner.active = False
            self.screen.ids.load_screen.ids.status.text = ''

    def show_screen_connection_failed(self, text_error):
        '''Выводит подпись о не активном соединении и кнопку для повторного
        подключения.'''

        self.screen.ids.load_screen.ids.spinner.active = False
        self.screen.ids.load_screen.ids.status.text = ''

        box = BoxLayout(orientation='vertical',
                        spacing=dp(10),
                        size_hint_y=None,
                        height=dp(100),
                        pos_hint={'center_y': .5})
        texts_errors = {
            'OAuth2 authorization error':
            self.translation._('Повторите попытку позже…'),
            'Failed to establish a new connection':
            self.translation._('Соединение Интернет отсутствует')
        }

        _text_error = self.translation._('Неизвестная ошибка…')
        for name_error in texts_errors.keys():
            if name_error in text_error:
                _text_error = texts_errors[name_error]

        box.add_widget(
            MDLabel(text=_text_error, halign='center', font_style='Subhead'))
        box.add_widget(
            MDFlatButton(text=self.translation._('Повторить попытку'),
                         theme_text_color='Custom',
                         pos_hint={'center_x': .5},
                         text_color=self.theme_cls.primary_color,
                         on_release=lambda x: self._authorization_on_vk(
                             self.login,
                             self.password,
                             self.group_id,
                             from_fail_screen=True)))
        self.screen.ids.load_screen.add_widget(box)

    @thread
    def send_post(self, text):
        self.result_sending_post, text_error = \
             create_issue(text, self.attach_file, self.attach_image)

    @thread
    def send_messages(self, text, user_id):
        self.result_sending_post, text_error = \
            send_message(
                user_id=user_id,
                files=self.attach_file,
                images=self.attach_image,
                text=text
            )

    @thread
    def send_comment(self, text, post_id, comment_id):
        self.result_sending_post, text_error = \
            create_comment(text, self.attach_file, self.attach_image, post_id, comment_id)

    def show_result_sending_posts(self, interval):
        def unschedule():
            Clock.unschedule(self.show_result_sending_posts)
            self.result_sending_post = None
            toast(message)
            self.clear_box_for_attach(remove_all=True)

        message = self.translation._('Sent!')
        if self.result_sending_post:
            # Обновляем списки постов/комментариев.
            if self.name_press_tab != 'Home_page':
                self.box_posts.update_posts()
            unschedule()
        elif self.result_sending_post is False:
            message = self.translation._('Error while sending!')
            unschedule()

    def callback_for_input_text(self, **kwargs):
        '''Вызывается при событиях из формы ввода текста.'''

        self.flag_attach = kwargs.get('flag')
        data = kwargs.get('kwargs')
        comment_id = int(data.get('comment_id', 0))
        post_id = int(data.get('post_id', 0))
        user_id = int(
            data.get('kwargs').get('user_id')) if 'kwargs' in data else 0
        whom_name = data.get('whom_name', '')
        text = whom_name + ', ' + self.form_for_messages.ids.text_form.text if whom_name != '' else self.form_for_messages.ids.text_form.text

        if self.flag_attach in ('FILE', 'FOTO'):
            self.show_manager(self.last_path, self.tap_on_file_in_filemanager)
        elif self.flag_attach == 'SEND':
            if self.manager.current == 'navigation button' and self.name_press_tab != 'Home_page' or self.manager.current == 'user info':
                self.form_for_messages.hide()

            if text.isspace() or text != '':
                if self.manager.current == 'navigation button' and self.name_press_tab == 'Wall_posts':
                    self.send_comment(text, post_id, comment_id)
                elif self.manager.current == 'user info':
                    self.send_messages(text, user_id)
                else:
                    self.send_post(text)

                self.form_for_messages.clear()
                Clock.schedule_interval(self.show_result_sending_posts, 0)

    def clear_box_for_attach(self, remove_all=False):
        self.attach_file = []
        self.attach_image = []

        if remove_all:
            if self.box_for_attach_image:
                self.form_for_messages.remove_widget(self.box_for_attach_image)
            if self.box_for_attach_file:
                self.form_for_messages.remove_widget(self.box_for_attach_file)

        self.box_for_attach_image = None
        self.box_for_attach_file = None

    def remove_attach(self, select_instance_attach):
        '''Удаляет превью файлов и изображений из формы для отправки сообщений.'''
        def _remove_attach(interval):
            parent_widget.remove_widget(instance_attach)

        if self.box_for_attach_file and select_instance_attach in self.box_for_attach_file.children:
            parent_widget = self.box_for_attach_file
        else:
            parent_widget = self.box_for_attach_image

        for instance_attach in parent_widget.children:
            if instance_attach == select_instance_attach:
                Clock.schedule_once(_remove_attach, .25)
                break

    def add_preview_attached_image(self, path_to_attach):
        '''Добавляет превью файлов в форму для отправки сообщений.'''

        if os.path.splitext(path_to_attach)[1] not in self.POSSIBLE_FILES[-2:]:
            if not self.box_for_attach_image:
                self.box_for_attach_image = BoxAttach(spacing=dp(5))
                self.form_for_messages.add_widget(self.box_for_attach_image)
            self.box_for_attach_image.add_widget(
                Icon(source=path_to_attach,
                     size_hint=(None, None),
                     size=(dp(60), dp(120)),
                     on_release=self.remove_attach))

    def add_preview_attached_file(self, path_to_attach):
        if not self.box_for_attach_file:
            self.box_for_attach_file = BoxAttachFile(spacing=dp(5))
            self.form_for_messages.add_widget(self.box_for_attach_file)
        attach = SingleIconItem(icon='file',
                                text=os.path.split(path_to_attach)[1],
                                events_callback=self.remove_attach)
        attach.ids._lbl_primary.font_style = 'Caption'
        self.box_for_attach_file.add_widget(attach)

    def write_last_path_manager(self, path_to_file_folder, path_to_file):
        self.config.set('General', 'last_path', path_to_file_folder)
        self.config.write()
        self.last_path = path_to_file_folder

    def tap_on_file_in_filemanager(self, path_to_file):
        self.window_file_manager.dismiss()
        self.window_file_manager_open = False
        path_to_file_folder, name_file = os.path.split(path_to_file)
        self.write_last_path_manager(path_to_file_folder, path_to_file)
        if os.path.splitext(name_file)[1] not in self.POSSIBLE_FILES:
            toast(self.message_about_files_mismatch[self.flag_attach])
        else:
            if self.flag_attach == 'FILE':
                self.attach_file.append(path_to_file)
                self.add_preview_attached_file(path_to_file)
            else:
                self.attach_image.append(path_to_file)
            self.add_preview_attached_image(path_to_file)

    def set_avatar(self, path_to_avatar):
        self.nav_drawer.ids.avatar.source = path_to_avatar
        self.nav_drawer.ids.avatar.reload()

    def choice_avatar_user(self):
        '''Выводит файловый менеджер для выбора аватара
        и устанавливает его в качестве аватара пользователя.'''
        def on_select(path_to_avatar):
            self.window_file_manager.dismiss()

            if os.path.splitext(path_to_avatar)[1] \
                    not in self.POSSIBLE_FILES[:3]:
                toast(self.translation._('This is not image!'))
            else:
                new_path_to_avatar = \
                    self.directory + '/data/images/avatar.png'
                create_previous_portrait(path_to_avatar, new_path_to_avatar)
                self.set_avatar(new_path_to_avatar)
                toast(self.translation._('Аватар изменён'))
                self.nav_drawer.state = 'open'

        self.show_manager(self.last_path, on_select)

    def show_plugins(self, *args):
        self.instance_list_user_plugins.show_plugins()

    def show_manager(self, directory, callback=None):
        ''''Выводит на экран файловый менеджер.'''

        if self.file_manager_not_opening:
            self.load_dialog.open()
            self.file_manager_not_opening = False

        self.window_file_manager_open = True
        Clock.schedule_once(self.window_file_manager.open, .2)
        if callback:
            self.file_manager.select_path = callback
        self.file_manager.show(directory)

    def show_posts(self, instance_tab=None):

        self.current_screen_tab.clear_widgets()
        if not self.box_posts:
            self.box_posts = BoxPosts(_app=self)
        self.box_posts.clear()
        self.box_posts.show_posts(increment=False)

    def events_program(self, instance, keyboard, keycode, text, modifiers):
        '''Вызывается при нажатии кнопки Меню или Back Key
        на мобильном устройстве.'''

        if keyboard in (1001, 27):
            if self.nav_drawer.state == 'open':
                self.nav_drawer.toggle_nav_drawer()
            self.back_screen(keyboard)
        elif keyboard in (282, 319):
            pass

        return True

    def back_screen(self, event=None):
        '''Менеджер экранов. Вызывается при нажатии Back Key
        и шеврона "Назад" в ToolBar.'''

        current_screen = self.manager.current
        if not self.window_file_manager_open:
            self.clear_box_for_attach()
        # Нажата BackKey.
        if event in (1001, 27):
            # Если информация с сервера о группе ещё не получена -
            # идёт авторизация либо данные в процессе загрузки.
            if not self.group_info:
                return
            if self.name_press_tab == 'Wall_posts' and self.form_for_messages.visible and \
                    not self.window_file_manager_open:
                self.form_for_messages.hide()
                return
            if self.window_file_manager_open:
                self.file_manager.back()
                return
            if current_screen == 'navigation button':
                if hasattr(self, 'previous_image'):
                    if self.previous_image.previous_open:
                        self.previous_image.dismiss()
                        return
                if self.box_posts and self.box_posts.show_comments:
                    self.box_posts.show_comments = False
                    self.show_posts()
                    return
                self.dialog_exit()
                return
        if current_screen == 'show license':
            self.manager.current = self.manager.screens[-1].name
        elif current_screen == 'user info':
            self.manager.current = 'navigation button'
        else:
            if not self.login or not self.password:
                self.dialog_exit()
                return
            self.manager.current = self.manager.previous()

    def dialog_exit(self):
        def check_interval_press(interval):
            self.exit_interval += interval
            if self.exit_interval > 5:
                self.exit_interval = False
                Clock.unschedule(check_interval_press)

        if self.exit_interval:
            sys.exit(0)

        Clock.schedule_interval(check_interval_press, 1)
        toast(self.translation._('Нажмите еще раз для выхода'))

    def show_form_for_messages(self, **kwargs):
        '''Выводит форму для ввода комментариев.'''

        self.form_for_messages = FormForMessages(
            callback=self.callback_for_input_text, kwargs=kwargs)

        self.form_for_messages.show(
            parent_instance=self.current_screen_tab if self.manager.current == 'navigation button' \
            else self.screen.ids.user_info.ids.float_layout
        )
        if kwargs.get('whom_name') == '':
            self.form_for_messages.visible = False

    def form_for_messages_hide(self):
        if self.form_for_messages.visible and not self.form_for_messages.ids.text_form.focus:
            self.form_for_messages.hide()

    def show_about(self, *args):
        self.nav_drawer.toggle_nav_drawer()
        self.screen.ids.load_screen.ids.status.text = \
            self.translation._(
                '[size=20][b]VKGroups[/b][/size]\n\n'
                '[b]Версия:[/b] {version}\n'
                '[b]Лицензия:[/b] MIT\n\n'
                '[size=20][b]Разработчики[/b][/size]\n\n'
                '[b]Backend:[/b] [ref=https://m.vk.com/fogapod]'
                '[color={link_color}]Евгений Ершов[/color][/ref]\n'
                '[b]Frontend:[/b] [ref=https://m.vk.com/heattheatr]'
                '[color={link_color}]Иванов Юрий[/color][/ref]\n\n'
                '[b]Исходный код:[/b] '
                '[ref=https://github.com/HeaTTheatR/VKGroups]'
                '[color={link_color}]GitHub[/color][/ref]').format(
                version=__version__,
                link_color=get_hex_from_color(self.theme_cls.primary_color)
            )
        self.screen.ids.load_screen.ids.spinner.active = False
        self.manager.current = 'load screen'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen()]]

    def show_license(self, *args):
        self.screen.ids.show_license.ids.text_license.text = \
            self.translation._('%s') % open(
                '%s/license/license_en.txt' %
                    self.directory, encoding='utf-8').read()
        self.nav_drawer._toggle()
        self.manager.current = 'show license'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen()]]
        self.screen.ids.action_bar.title = \
            self.translation._('MIT LICENSE')

    def exit_manager(self, *args):
        '''Закрывает окно файлового менеджера.'''

        self.window_file_manager.dismiss()
        self.window_file_manager_open = False

    def select_locale(self, *args):
        '''Выводит окно со списком имеющихся языковых локализаций для
        установки языка приложения.'''
        def select_locale(name_locale):
            '''Устанавливает выбранную локализацию.'''

            for locale in self.dict_language.keys():
                if name_locale == self.dict_language[locale]:
                    self.lang = locale
                    self.config.set('General', 'language', self.lang)
                    self.config.write()

        dict_info_locales = {}
        for locale in self.dict_language.keys():
            dict_info_locales[self.dict_language[locale]] = \
                ['locale', locale == self.lang]

        if not self.window_language:
            self.window_language = card(Lists(dict_items=dict_info_locales,
                                              events_callback=select_locale,
                                              flag='one_select_check'),
                                        size=(.85, .55))
        self.window_language.open()

    def set_chevron_back_screen(self):
        '''Устанавливает шеврон возврата к предыдущему экрану в ToolBar.'''

        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(self.manager.current)]]

    def download_progress_hide(self, instance_progress, value):
        '''Скрывает виджет прогресса загрузки файлов.'''

        instance_progress.dismiss()
        self.screen.ids.action_bar.right_action_items = \
            [['download', lambda x: self.download_progress_show(instance_progress)]]

    def download_progress_show(self, instance_progress):
        self.screen.ids.action_bar.right_action_items = []
        instance_progress.open()
        instance_progress.animation_progress_from_fade()

    def download_complete(self, result):
        self.screen.ids.action_bar.right_action_items = []
        if result == 'Done':
            toast(self.translation._('Загружено'))
        else:
            toast(self.translation._('Ошибка загрузки'))

    def tap_on_icon_user(self, user_id):
        '''Вызывается при тапе по иконке пользователя в списках постов/комментариев.
        Получает, устанавливает и выводит на экран информацию о выбранном пользователе.
        '''
        def unschedule(error=False):
            if error:
                toast(self.translation._('Данные не получены'))
            Clock.unschedule(wait_result)
            self.load_dialog.dismiss()

        def set_value_for_screen(instance_screen_user_info):
            '''Устанавливает значения подписей в экране информации о пользователе.'''

            instance_screen_user_info.ids.avatar.source = result['photo_200']
            instance_screen_user_info.ids.user_name.text = instance_screen_user_info.ids.user_name.text % (
                result['first_name'] + ' ' + result['last_name'],
                self.translation._('Последний раз был в сети в ') +
                time.strftime("%H:%M",
                              time.localtime(result['last_seen']['time'])))
            instance_screen_user_info.ids.label_user_status.text = \
                instance_screen_user_info.ids.label_user_status.text % result['status']
            instance_screen_user_info.ids.message_button.bind(
                on_release=lambda kwargs: self.show_form_for_messages(
                    kwargs={'user_id': user_id}))

            data = {
                'bdate': {
                    'result': result['bdate'] if 'bdate' in result else None,
                    'bad_result': self.translation._('скрыта'),
                    'label_instance':
                    instance_screen_user_info.ids.label_bdate,
                    'label_text': self.translation._('Дата рождения')
                },
                'city': {
                    'result':
                    result['city']['title'] if 'city' in result else None,
                    'bad_result': self.translation._('не указан'),
                    'label_instance': instance_screen_user_info.ids.label_city,
                    'label_text': self.translation._('Город')
                },
                'relation': {
                    'result':
                    self.RELATION[result['relation']]
                    if 'relation' in result else None,
                    'bad_result':
                    self.translation._('не указано'),
                    'label_instance':
                    instance_screen_user_info.ids.label_marital_status,
                    'label_text':
                    self.translation._('Семейное положение')
                }
            }

            for key in data:
                text_for_label = data[key]['result'] if data[key][
                    'result'] else data[key]['bad_result']
                data[key]['label_instance'].text = data[key][
                    'label_instance'].text % (data[key]['label_text'],
                                              text_for_label)
            unschedule()

        def wait_result(interval):
            '''Ожидает информации от сервера.'''

            if result:
                self.manager.current = 'user info'
                set_value_for_screen(self.screen.ids.user_info)

        if user_id == int(self.group_id):
            return
        result = None
        self.load_dialog.open()
        Clock.schedule_once(wait_result, 0)
        result, text_error = get_user_info(user_id=user_id)
        if text_error:
            unschedule(error=True)

    def load_all_kv_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            with open(os.path.join(directory_kv_files, kv_file),
                      encoding='utf-8') as kv:
                Builder.load_string(kv.read())

    def on_tab_press(self, instance_tab):
        '''Вызывается при выборе одного из пунктов BottomNavigation.'''

        self.clear_box_for_attach()
        self.name_press_tab = instance_tab.name
        self.current_screen_tab = instance_tab
        self.current_tab_manager = instance_tab.parent_widget.ids.tab_manager

        if self.current_tab_manager.current == self.name_press_tab:
            return

        # Вкладка 'Записи группы'.
        if self.name_press_tab == 'Wall_posts':
            self.show_posts(instance_tab)
            self.form_for_messages.hide()
        elif self.name_press_tab == 'Home_page':
            self.box_posts = None
            self.show_form_for_messages(whom_name='')

    def on_lang(self, instance, lang):
        self.translation.switch_lang(lang)

    def on_pause(self):
        '''Ставит приложение на 'паузу' при сворачивании его в трей.'''

        self.config.set('General', 'last_screen', self.manager.current)
        self.config.write()

        return True

    def on_start(self):
        '''Вызывается при открытии стартового экрана приложения.'''

        # Восстанавливаем форму ввода логина и пароля.
        if self.last_screen == 'load screen' and \
                not self.login and not self.password:
            if os.path.exists('%s/data/state.txt' % self.directory):
                data_state = \
                    ast.literal_eval(open('%s/data/state.txt' % self.directory).read())
                self.password_form.ids.login.text = \
                    data_state['login'] if 'login' in data_state else ''
                self.password_form.ids.password.text = \
                    data_state['password'] if 'password' in data_state else ''
                self.password_form.ids.group_id.text = data_state['group']
class name_project(App):
    '''Функционал программы.'''

    title = 'name_project'
    icon = 'icon.png'
    nav_drawer = ObjectProperty()
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'Blue'
    lang = StringProperty('en')

    def __init__(self, **kvargs):
        super(name_project, self).__init__(**kvargs)
        Window.bind(on_keyboard=self.events_program)
        Window.soft_input_mode = 'below_target'

        self.list_previous_screens = ['base']
        self.window = Window
        self.plugin = ShowPlugins(self)
        self.config = ConfigParser()
        self.manager = None
        self.window_language = None
        self.exit_interval = False
        self.dict_language = literal_eval(
            open(
                os.path.join(self.directory, 'data', 'locales', 'locales.txt')).read()
        )
        self.translation = Translation(
            self.lang, 'Ttest', os.path.join(self.directory, 'data', 'locales')
        )

    def get_application_config(self):
        return super(name_project, self).get_application_config(
                        '{}/%(appname)s.ini'.format(self.directory))

    def build_config(self, config):
        '''Создаёт файл настроек приложения name_project.ini.'''

        config.adddefaultsection('General')
        config.setdefault('General', 'language', 'en')

    def set_value_from_config(self):
        '''Устанавливает значения переменных из файла настроек name_project.ini.'''

        self.config.read(os.path.join(self.directory, 'name_project.ini'))
        self.lang = self.config.get('General', 'language')

    def build(self):
        self.set_value_from_config()
        self.load_all_kv_files(os.path.join(self.directory, 'libs', 'uix', 'kv'))
        self.screen = StartScreen()  # главный экран программы
        self.manager = self.screen.ids.manager
        self.nav_drawer = self.screen.ids.nav_drawer

        return self.screen

    def load_all_kv_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            kv_file = os.path.join(directory_kv_files, kv_file)
            if os.path.isfile(kv_file):
                if not PY2:
                    with open(kv_file, encoding='utf-8') as kv:
                        Builder.load_string(kv.read())
                else:
                    Builder.load_file(kv_file)

    def events_program(self, instance, keyboard, keycode, text, modifiers):
        '''Вызывается при нажатии кнопки Меню или Back Key
        на мобильном устройстве.'''

        if keyboard in (1001, 27):
            if self.nav_drawer.state == 'open':
                self.nav_drawer.toggle_nav_drawer()
            self.back_screen(event=keyboard)
        elif keyboard in (282, 319):
            pass

        return True

    def back_screen(self, event=None):
        '''Менеджер экранов. Вызывается при нажатии Back Key
        и шеврона "Назад" в ToolBar.'''

        # Нажата BackKey.
        if event in (1001, 27):
            if self.manager.current == 'base':
                self.dialog_exit()
                return
            try:
                self.manager.current = self.list_previous_screens.pop()
            except:
                self.manager.current = 'base'
            self.screen.ids.action_bar.title = self.title
            self.screen.ids.action_bar.left_action_items = \
                [['menu', lambda x: self.nav_drawer._toggle()]]

    def show_plugins(self, *args):
        '''Выводит на экран список плагинов.'''

        self.plugin.show_plugins()

    def show_about(self, *args):
        self.nav_drawer.toggle_nav_drawer()
        self.screen.ids.about.ids.label.text = \
            self.translation._(
                u'[size=20][b]name_project[/b][/size]\n\n'
                u'[b]Version:[/b] {version}\n'
                u'[b]License:[/b] MIT\n\n'
                u'[size=20][b]Developer[/b][/size]\n\n'
                u'[ref=SITE_PROJECT]'
                u'[color={link_color}]name_author[/color][/ref]\n\n'
                u'[b]Source code:[/b] '
                u'[ref=repo_project_on_github]'
                u'[color={link_color}]GitHub[/color][/ref]').format(
                version=__version__,
                link_color=get_hex_from_color(self.theme_cls.primary_color)
            )
        self.manager.current = 'about'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def show_license(self, *args):
        if not PY2:
            self.screen.ids.license.ids.text_license.text = \
                self.translation._('%s') % open(
                    os.path.join(self.directory, 'LICENSE'), encoding='utf-8').read()
        else:
            self.screen.ids.license.ids.text_license.text = \
                self.translation._('%s') % open(
                    os.path.join(self.directory, 'LICENSE')).read()
        self.nav_drawer._toggle()
        self.manager.current = 'license'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen()]]
        self.screen.ids.action_bar.title = \
            self.translation._('MIT LICENSE')

    def select_locale(self, *args):
        '''Выводит окно со списком имеющихся языковых локализаций для
        установки языка приложения.'''

        def select_locale(name_locale):
            '''Устанавливает выбранную локализацию.'''

            for locale in self.dict_language.keys():
                if name_locale == self.dict_language[locale]:
                    self.lang = locale
                    self.config.set('General', 'language', self.lang)
                    self.config.write()

        dict_info_locales = {}
        for locale in self.dict_language.keys():
            dict_info_locales[self.dict_language[locale]] = \
                ['locale', locale == self.lang]

        if not self.window_language:
            self.window_language = card(
                Lists(
                    dict_items=dict_info_locales,
                    events_callback=select_locale, flag='one_select_check'
                ),
                size=(.85, .55)
            )
        self.window_language.open()

    def dialog_exit(self):
        def check_interval_press(interval):
            self.exit_interval += interval
            if self.exit_interval > 5:
                self.exit_interval = False
                Clock.unschedule(check_interval_press)

        if self.exit_interval:
            sys.exit(0)
            
        Clock.schedule_interval(check_interval_press, 1)
        toast(self.translation._('Press Back to Exit'))
    def on_lang(self, instance, lang):
        self.translation.switch_lang(lang)
示例#8
0
class Migren(App):
    title = 'Дневник головной боли'
    icon = 'icon.png'
    nav_drawer = ObjectProperty()
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'Grey'
    lang = StringProperty('en')


    def __init__(self, **kvargs):
        super(Migren, self).__init__(**kvargs)
        Window.bind(on_keyboard=self.events_program)
        Window.soft_input_mode = 'below_target'

        self.list_previous_screens = ['base']
        self.window = Window
        self.plugin = ShowPlugins(self)
        self.config = ConfigParser()
        self.manager = None
        self.window_language = None
        self.exit_interval = False
        self.dict_language = literal_eval(
            open(
                os.path.join(self.directory, 'data', 'locales', 'locales.txt')).read()
        )
        self.translation = Translation(
            self.lang, 'Ttest', os.path.join(self.directory, 'data', 'locales')
        )

    def get_application_config(self):
        return super(Migren, self).get_application_config(
                        '{}/%(appname)s.ini'.format(self.directory))

    def build_config(self, config):

        config.adddefaultsection('General')
        config.setdefault('General', 'language', 'en')

    def set_value_from_config(self):
        self.config.read(os.path.join(self.directory, 'migren.ini'))
        self.lang = self.config.get('General', 'language')

    def build(self):
        self.set_value_from_config()
        self.load_all_kv_files(os.path.join(self.directory, 'libs', 'uix', 'kv'))
        self.screen = StartScreen()
        self.manager = self.screen.ids.manager
        self.nav_drawer = self.screen.ids.nav_drawer

        return self.screen

    def load_all_kv_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            kv_file = os.path.join(directory_kv_files, kv_file)
            if os.path.isfile(kv_file):
                with open(kv_file, encoding='utf-8') as kv:
                    Builder.load_string(kv.read())

    def events_program(self, instance, keyboard, keycode, text, modifiers):
        if keyboard in (1001, 27):
            if self.nav_drawer.state == 'open':
                self.nav_drawer.toggle_nav_drawer()
            self.back_screen(event=keyboard)
        elif keyboard in (282, 319):
            pass

        return True

    def back_screen(self, event=None):
        if event in (1001, 27):
            if self.manager.current == 'base':
                self.dialog_exit()
                return
            try:
                self.manager.current = self.list_previous_screens.pop()
            except:
                self.manager.current = 'base'
            self.screen.ids.action_bar.title = self.title
            self.screen.ids.action_bar.left_action_items = \
                [['menu', lambda x: self.nav_drawer._toggle()]]


    def add_note(self, *args):
        #self.screen.ids.base.add_name_previous_screen()
        #self.nav_drawer.toggle_nav_drawer()
        self.manager.current = 'note'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def add_note2(self, *args):
        #self.screen.ids.base.add_name_previous_screen()
        #self.nav_drawer.toggle_nav_drawer()
        self.manager.current = 'note2'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def add_note3(self, *args):
        #self.screen.ids.base.add_name_previous_screen()
        #self.nav_drawer.toggle_nav_drawer()
        self.manager.current = 'note3'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]



    def show_diary(self, *args):
        self.nav_drawer.toggle_nav_drawer()
        self.manager.current = 'diary'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

    def send_email(self, *args):
        addr_from = "*****@*****.**"  # Отправитель
        password = "******"  # Пароль

        msg = MIMEMultipart()  # Создаем сообщение
        msg['From'] = addr_from  # Адресат
        msg['To'] = self.screen.ids.diary.ids.mail_to.text  # Получатель
        msg['Subject'] = self.screen.ids.diary.ids.theme.text  # Тема сообщения
        users_name = self.screen.ids.diary.ids.user_name.text.replace(' ', '_')
        name = 'diary.xls'
        filepath = os.path.join(os.getcwd(), name)
        filename = os.path.basename(filepath)
        if os.path.isfile(filepath):
            rb = xlrd.open_workbook(filepath, formatting_info=True)
            wb = copy(rb)
            wb.save(users_name+'_diary.xls')
            os.remove(filepath)
        name = users_name+'_diary.xls'
        filepath = os.path.join(os.getcwd(), name)
        filename = os.path.basename(filepath)

        body = 'Дневник головной боли:\n\nПациент: '+ self.screen.ids.diary.ids.user_name.text + '\n'+\
                'Комментарий пациента: ' + self.screen.ids.diary.ids.comment.text + '\n\n' +\
            'Адресс для обратной связи: ' + self.screen.ids.diary.ids.add_mail.text  # Текст сообщения
        msg.attach(MIMEText(body, 'plain'))  # Добавляем в сообщение текст

        if os.path.isfile(filepath):  # Если файл существует
            ctype, encoding = mimetypes.guess_type(filepath)  # Определяем тип файла на основе его расширения
            if ctype is None or encoding is not None:  # Если тип файла не определяется
                ctype = 'application/octet-stream'  # Будем использовать общий тип
            maintype, subtype = ctype.split('/', 1)  # Получаем тип и подтип
            with open(filepath, 'rb') as fp:
                file = MIMEBase(maintype, subtype)  # Используем общий MIME-тип
                file.set_payload(fp.read())  # Добавляем содержимое общего типа (полезную нагрузку)
                fp.close()
            encoders.encode_base64(file)  # Содержимое должно кодироваться как Base64
            file.add_header('Content-Disposition', 'attachment', filename=filename)  # Добавляем заголовки
            msg.attach(file)  # Присоединяем файл к сообщению

        server = smtplib.SMTP_SSL('smtp.yandex.ru', 465)  # Создаем объект SMTP
        server.login(addr_from, password)  # Получаем доступ
        server.send_message(msg)  # Отправляем сообщение
        server.quit()  # Выходим

        rb = xlrd.open_workbook(filepath, formatting_info=True)
        wb = copy(rb)
        wb.save(users_name+'_old_diary.xls')

        self.create_new_table(name)

        toast(self.translation._('Дневник отправлен'))
        self.screen.ids.diary.ids.mail_to.text = ''
        self.screen.ids.diary.ids.theme.text = ''
        self.screen.ids.diary.ids.comment.text = ''

    def create_new_table(self, name, *args):
        font0 = xlwt.Font()
        font0.name = 'Times New Roman'
        font0.bold = True
        style0 = xlwt.XFStyle()
        style0.font = font0
        style1 = xlwt.XFStyle()
        style1.num_format_str = 'D-MMM-YY'
        sheet = str(datetime.now())
        sheet = sheet[0:10]
        wb = xlwt.Workbook()
        ws = wb.add_sheet(sheet)
        Col = ws.col(0)
        Col.width = 256 * 21
        ws.write(0, 0, 'Дата и время', style0)
        Col = ws.col(1)
        Col.width = 256 * 41
        ws.write(0, 1, 'Тип боли', style0)
        Col = ws.col(2)
        Col.width = 256 * 41
        ws.write(0, 2, 'Локализация боли', style0)
        Col = ws.col(3)
        Col.width = 256 * 23
        ws.write(0, 3, 'Продолжительность боли', style0)
        Col = ws.col(4)
        Col.width = 256 * 65
        ws.write(0, 4, 'Комментарий пациента', style0)
        wb.save(name)



    def show_plugins(self, *args):
        self.plugin.show_plugins()

    def show_about(self, *args):
        self.nav_drawer.toggle_nav_drawer()
        self.screen.ids.about.ids.label.text = \
            self.translation._(
                u'[size=20][b]PyConversations[/b][/size]\n\n'
                u'[b]Version:[/b] {version}\n'
                u'[b]License:[/b] MIT\n\n'
                u'[size=20][b]Developer[/b][/size]\n\n'
                u'[ref=SITE_PROJECT]'
                u'[color={link_color}]Pechka and Bomj[/color][/ref]\n\n'
                u'[b]Source code:[/b] '
                u'[ref=https://github.com/NikolaevVR/Migrebot]'
                u'[color={link_color}]GitHub[/color][/ref]').format(
                version=__version__,
                link_color=get_hex_from_color(self.theme_cls.primary_color)
            )
        self.manager.current = 'about'
        self.screen.ids.action_bar.left_action_items = \
            [['chevron-left', lambda x: self.back_screen(27)]]

   # def show_license(self, *args):
   #     self.screen.ids.license.ids.text_license.text = \
   #         self.translation._('%s') % open(
   #             os.path.join(self.directory, 'LICENSE'), encoding='utf-8').read()
    #    self.nav_drawer._toggle()
    #    self.manager.current = 'license'
    #    self.screen.ids.action_bar.left_action_items = \
    #        [['chevron-left', lambda x: self.back_screen()]]
    #    self.screen.ids.action_bar.title = \
    #        self.translation._('MIT LICENSE')
    #    self.screen.ids.action_bar.left_action_items = \
    #        [['chevron-left', lambda x: self.back_screen(27)]]


    def select_locale(self, *args):

        def select_locale(name_locale):

            for locale in self.dict_language.keys():
                if name_locale == self.dict_language[locale]:
                    self.lang = locale
                    self.config.set('General', 'language', self.lang)
                    self.config.write()

        dict_info_locales = {}
        for locale in self.dict_language.keys():
            dict_info_locales[self.dict_language[locale]] = \
                ['locale', locale == self.lang]

        if not self.window_language:
            self.window_language = card(
                Lists(
                    dict_items=dict_info_locales,
                    events_callback=select_locale, flag='one_select_check'
                ),
                size=(.85, .55)
            )
        self.window_language.open()

    def dialog_exit(self):
        def check_interval_press(interval):
            self.exit_interval += interval
            if self.exit_interval > 5:
                self.exit_interval = False
                Clock.unschedule(check_interval_press)

        if self.exit_interval:
            sys.exit(0)
            
        Clock.schedule_interval(check_interval_press, 1)
        toast(self.translation._('Press Back to Exit'))
    def on_lang(self, instance, lang):
        self.translation.switch_lang(lang)
示例#9
0
class hodl_mobile(App):
    def show_popup(self):
        p = Theme_Popup()
        p.open()

    '''Функционал программы.'''

    title = 'hodl_mobile'
    icon = 'icon.png'
    nav_drawer = ObjectProperty()
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'Blue'
    lang = StringProperty('en')
    cont = StringProperty()

    def __init__(self, **kvargs):
        super(hodl_mobile, self).__init__(**kvargs)
        Window.bind(on_keyboard=self.events_program)
        Window.soft_input_mode = 'below_target'

        self.list_previous_screens = ['base']
        self.window = Window
        self.plugin = ShowPlugins(self)
        self.config = ConfigParser()
        self.manager = None
        self.window_language = None
        self.window_contacts = None
        self.exit_interval = False
        self.dict_language = literal_eval(
            open(os.path.join(self.directory, 'data', 'locales',
                              'locales.txt')).read())
        self.dict_contacts = literal_eval(
            open(os.path.join(self.directory, 'data', 'contacts.txt')).read())
        self.translation = Translation(
            self.lang, 'Ttest', os.path.join(self.directory, 'data',
                                             'locales'))

    def get_application_config(self):
        return super(hodl_mobile, self).get_application_config(
            '{}/%(appname)s.ini'.format(self.directory))

    def build_config(self, config):
        '''Создаёт файл настроек приложения hodl_mobile.ini.'''

        config.adddefaultsection('General')
        config.setdefault('General', 'language', 'en')

    def set_value_from_config(self):
        '''Устанавливает значения переменных из файла настроек hodl_mobile.ini.'''

        self.config.read(os.path.join(self.directory, 'hodl_mobile.ini'))
        self.lang = self.config.get('General', 'language')

    def build(self):
        self.set_value_from_config()
        self.load_all_kv_files(
            os.path.join(self.directory, 'libs', 'uix', 'kv'))
        self.screen = StartScreen()  # main screen
        self.manager = self.screen.ids.manager
        self.nav_drawer = self.screen.ids.nav_drawer

        return self.screen

    def load_all_kv_files(self, directory_kv_files):
        for kv_file in os.listdir(directory_kv_files):
            kv_file = os.path.join(directory_kv_files, kv_file)
            if os.path.isfile(kv_file):
                with open(kv_file, encoding='utf-8') as kv:
                    Builder.load_string(kv.read())

    def events_program(self, instance, keyboard, keycode, text, modifiers):
        '''On menu button press.'''

        if keyboard in (1001, 27):
            if self.nav_drawer.state == 'open':
                self.nav_drawer.toggle_nav_drawer()
            self.back_screen(event=keyboard)
        elif keyboard in (282, 319):
            pass

        return True

    def back_screen(self):
        '''Менеджер экранов. Вызывается при нажатии Back Key
        и шеврона "Назад" в ToolBar.'''

        # Нажата BackKey.
        if self.manager.current == 'base':
            self.dialog_exit()
            return
        try:
            self.manager.current = self.list_previous_screens.pop()
        except:
            self.manager.current = 'base'
        self.screen.ids.action_bar.title = self.title
        self.screen.ids.action_bar.left_action_items = \
            [['menu', lambda x: self.nav_drawer._toggle()]]

    def select_locale(self, *args):
        '''Выводит окно со списком имеющихся языковых локализаций для
        установки языка приложения.'''
        def select_locale(name_locale):
            '''Устанавливает выбранную локализацию.'''

            for locale in self.dict_language.keys():
                if name_locale == self.dict_language[locale]:
                    self.lang = locale
                    self.config.set('General', 'language', self.lang)
                    self.config.write()

        dict_info_locales = {}
        for locale in self.dict_language.keys():
            dict_info_locales[self.dict_language[locale]] = \
                ['locale', locale == self.lang]

        if not self.window_language:
            self.window_language = card(Lists(dict_items=dict_info_locales,
                                              events_callback=select_locale,
                                              flag='one_select_check'),
                                        size=(.85, .55))
        self.window_language.open()

    def show_plugins(self, *args):
        '''Выводит на экран список плагинов.'''
        self.plugin.show_plugins()

    def show_about(self, *args):
        self.nav_drawer.toggle_nav_drawer()
        self.screen.ids.about.ids.label.text = \
            self.translation._(
                u'[size=20][b]hodl_mobile[/b][/size]\n\n'
                u'[b]Version:[/b] {version}\n'
                u'[b]License:[/b] MIT\n\n'
                u'[size=20][b]Developer[/b][/size]\n\n'
                u'HODL is a cryptocurrency with smart contracts.\n\n'
                u'[ref=SITE_PROJECT]'
                u'[color={link_color}]NAME_AUTHOR[/color][/ref]\n\n'
                u'[b]Source code:[/b] '
                u'[ref=https://github.com/hodleum/hodl_mobile.git]'
                u'[color={link_color}]GitHub[/color][/ref]').format(
                version=__version__,
                link_color=get_hex_from_color(self.theme_cls.primary_color)
            )
        self.manager.current = 'about'
        self.screen.ids.action_bar.title = \
            self.translation._('About')

    def show_buy_new_tokens(self, *args):
        self.manager.current = 'buy_new_tokens'
        self.screen.ids.action_bar.title = \
            self.translation._('Buy tokens')
        self.screen.ids.action_bar.left_action_items = \
            [['arrow-left', lambda x: self.show_back_trade()]]

    def show_scan(self, *args):
        self.manager.current = 'scan'
        self.screen.ids.action_bar.title = \
            self.translation._('Scan')
        self.screen.ids.action_bar.left_action_items = \
            [['arrow-left', lambda x: self.show_back_main()]]

    def show_find_new_contact(self, *args):
        self.manager.current = 'find_new_contact'
        self.screen.ids.action_bar.title = \
            self.translation._('Find new contact')
        self.screen.ids.action_bar.left_action_items = \
            [['arrow-left', lambda x: self.show_back_contacts()]]

    def show_new_transaction(self, *args):
        self.manager.current = 'new_transaction'
        self.screen.ids.action_bar.title = \
            self.translation._('New transaction')
        self.screen.ids.action_bar.left_action_items = \
            [['arrow-left', lambda x: self.show_back_main()]]

    def show_tokens_info(self, *args):
        self.manager.current = 'tokens_info'
        self.screen.ids.action_bar.title = \
            self.translation._('DVT')
        self.screen.ids.action_bar.left_action_items = \
            [['arrow-left', lambda x: self.show_back_trade()]]

    def show_back_tokens_info(self, *args):
        self.manager.current = 'tokens_info'
        self.screen.ids.action_bar.title = \
            self.translation._('DVT')
        self.screen.ids.action_bar.left_action_items = \
            [['arrow-left', lambda x: self.show_back_trade()]]

    def show_choose_from_contacts(self, *args):
        def select_contact(name_contact):
            for contact in self.dict_contacts.keys():
                if name_contact == self.dict_contacts[contact]:
                    self.window_contacts.dismiss()
                    self.cont = name_contact
                    text_cont = name_contact
                    print(text_cont)
                    self.window_contacts.dismiss()

        dict_info_contacts = {}
        for contact in self.dict_contacts.keys():
            dict_info_contacts[self.dict_contacts[contact]] = \
                ['contact', contact == self.cont]

        if not self.window_contacts:
            self.window_contacts = card(Lists(dict_items=dict_info_contacts,
                                              events_callback=select_contact,
                                              flag='one_select_check'),
                                        size=(.85, .55))
        self.window_contacts.open()

    def show_invoicecreation(self, *args):
        self.manager.current = 'invoicecreation'
        self.screen.ids.action_bar.title = \
            self.translation._('Invoice creation')
        self.screen.ids.action_bar.left_action_items = \
            [['arrow-left', lambda x: self.show_back_main()]]

    def show_set(self, *args):
        self.manager.current = 'set'
        self.nav_drawer._toggle()
        self.screen.ids.action_bar.title = \
            self.translation._('Settings')

    def show_trade(self, *args):
        self.manager.current = 'trade'
        self.nav_drawer._toggle()
        self.screen.ids.action_bar.title = \
            self.translation._('Trade')

    def show_new_wallet(self, *args):
        self.manager.current = 'new_wallet'
        self.nav_drawer._toggle()
        self.screen.ids.action_bar.title = \
            self.translation._('New wallet')
        self.screen.ids.action_bar.left_action_items = \
            [['arrow-left', lambda x: self.show_back_main()]]

    def show_back_trade(self, *args):
        self.manager.current = 'trade'
        self.screen.ids.action_bar.title = \
            self.translation._('Trade')
        self.screen.ids.action_bar.left_action_items = \
            [['menu', lambda x: self.nav_drawer._toggle()]]

    def show_buy_more1(self, *args):
        self.manager.current = 'buy_more1'
        self.screen.ids.action_bar.title = \
            self.translation._('Buy')
        self.screen.ids.action_bar.left_action_items = \
            [['arrow-left', lambda x: self.show_back_trade()]]

    def show_sell1(self, *args):
        self.manager.current = 'sell1'
        self.screen.ids.action_bar.title = \
            self.translation._('Sell')
        self.screen.ids.action_bar.left_action_items = \
            [['arrow-left', lambda x: self.show_back_trade()]]

    def show_contacts(self, *args):
        self.manager.current = 'contacts'
        self.nav_drawer._toggle()
        self.screen.ids.action_bar.title = \
            self.translation._('Contacts')

    def show_back_contacts(self, *args):
        self.manager.current = 'contacts'
        self.screen.ids.action_bar.title = \
            self.translation._('Contacts')
        self.screen.ids.action_bar.left_action_items = \
            [['menu', lambda x: self.nav_drawer._toggle()]]

    def show_main(self, *args):
        self.manager.current = 'base'
        self.nav_drawer._toggle()
        self.screen.ids.action_bar.title = \
            self.translation._('Home screen')

    def show_back_main(self, *args):
        self.manager.current = 'base'
        self.screen.ids.action_bar.title = \
            self.translation._('Home screen')
        self.screen.ids.action_bar.left_action_items = \
            [['menu', lambda x: self.nav_drawer._toggle()]]

    def show_network(self, *args):
        self.manager.current = 'network'
        self.nav_drawer._toggle()
        self.screen.ids.action_bar.title = \
            self.translation._('Network')

    def show_license(self, *args):
        self.screen.ids.license.ids.text_license.text = \
            self.translation._('%s') % open(
                os.path.join(self.directory, 'LICENSE'), encoding='utf-8').read()
        self.nav_drawer._toggle()
        self.manager.current = 'license'
        self.screen.ids.action_bar.title = \
            self.translation._('MIT LICENSE')

    def dialog_exit(self):
        def check_interval_press(interval):
            self.exit_interval += interval
            if self.exit_interval > 5:
                self.exit_interval = False
                Clock.unschedule(check_interval_press)

        if self.exit_interval:
            sys.exit(0)

        Clock.schedule_interval(check_interval_press, 1)
        toast(self.translation._('Press Back to Exit'))

    def change_theme(self, value):
        self.theme_cls.primary_palette = value
        return value

    def on_lang(self, instance, lang):
        self.translation.switch_lang(lang)

    def to_start(self):

        self.screen = StartScreen()
        return self.screen