add ifind command feature/find
authorfrostbane <frostbane@programmer.net>
Tue, 24 May 2016 08:16:38 +0900
branchfeature/find
changeset 82 b143f9dcfb8a
parent 81 a2bd41e34aca
child 83 9cfc87967a44
add ifind command
.gitignore
.hgignore
artemis/find.py
artemis/main.py
--- a/.gitignore	Fri Mar 04 09:51:09 2016 +0900
+++ b/.gitignore	Tue May 24 08:16:38 2016 +0900
@@ -1,8 +1,7 @@
+*.pyo
 *.pyc
 artemis.egg-info/
 build/
 dist/
 .idea/*
-*.pyo
-*.pyc
 pycharm-debug.egg
--- a/.hgignore	Fri Mar 04 09:51:09 2016 +0900
+++ b/.hgignore	Tue May 24 08:16:38 2016 +0900
@@ -1,11 +1,10 @@
 syntax:glob
+*.pyo
 *.pyc
 artemis.egg-info/
 build/
 dist/
 .idea/*
-*.pyo
-*.pyc
 pycharm-debug.egg
 syntax: glob
 artemis.iml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artemis/find.py	Tue May 24 08:16:38 2016 +0900
@@ -0,0 +1,154 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+                                   ..  ┌  -:  .
+                                  ([]▄▄├]▄▄▄¿┐`,
+    .¡                      ,. D{}▓███████████░█∩^}                       =
+    ,▓¼                      .▐▓▓█████████████▓██▓▓,                     (▓
+     █▌╕.                   ─@╣████████████████████▄─                   ./▀'
+    :▓█▌Ç...             └ '└▓██████████████████████Ö'`            ..  '└`'┌
+     \ g█████▄Ü┌        C g██████████████████████████╗─  ²   ²   t▄▓██ù- `╞
+      ¡ Å╠█▌╠█▓▓▓▓▌N@▌|   ╓██████████████████████████▌  .╣▓▓▓▓▓▓▓]▐╙ÅÅ∩!
+      ` ` ╫½┌▀▀▀▀▀▀░╫▌┘    ▀▀▀▀▀██████████████████▀█▀C  :╝▀▀▀▀▀▀▀.   `:
+          L ─ⁿ'``' ─       ''' ─█████████████████▌─█=       ╞º'`'º──
+                                    ..Å╫█╛.             ~
+                                   ╔▄▄▓██▓▄¿        .
+                            -   ]:├╣██▀▀▀▀▀▒Q¿.¿{  )=
+                            <─Ω╚█░▓███'──h⌂▓██╡██▌Ü─4
+                           `┌ ▐▓█▄▓██╟≤¡yQ▄███▓█▌▐▓M
+                           ^. └▀▀▄▓██▄▒▄▄╬▄████▀ └▀╙`
+                            «-'''█▓▓█▓██▓██▓▓██└``'⌐
+                                 #╛ µ:▓▄;xφ(⌠ █
+                                 ╗µ  ~╙'`   `┌█╣
+                                 ╝Ñ. ''  '   L▀▀
+                                    ~         ,
+                                    ^
+                                    ÷        ─.
+                                    N.   ∩ts  )
+                                 ∩    ²⌠  `   ─
+                                   (@   ~▒▒  .
+                                 ` └▀/ .>▀▀  :
+                                     ⌐   ,⌐
+
+
+
+ Artemis
+ Version 0.5.1
+ Copyright (c) 2016 Frostbane Ac
+ Apache-2.0, APL-1.0, 0BSD Licensed. ?
+ www.??.com
+ ?? inspired script.
+
+"""
+
+from mercurial import hg, util, commands, cmdutil
+import sys, os, time, random, mailbox, glob, socket, ConfigParser, re
+from properties import ArtemisProperties
+from artemis import Artemis
+
+__author__ = 'frostbane'
+__date__ = '2016/05/24'
+
+
+class ArtemisFind:
+    commands = [
+        # dashed options will be resolved to underscores
+        # case-sensitive => case_sensitive
+        ('p', 'property', "subject", 'Issue property to match. '
+                                     '[state, from, subject, date, '
+                                     'priority, resolution, etc..]'),
+        ('c', 'case-sensitive', None, 'Case sensitive search.'),
+        ('r', 'regex', None, 'Use regular expressions. '
+                             'Exact option will be ignored.'),
+        ('e', 'exact', None, 'Use exact comparison. '
+                             'Like comparison is used if exact is'
+                             'uspecified.'),
+    ]
+
+    usage = 'hg ifind [OPTIONS] QUERY'
+    ui = None
+    repo = None
+    opts = []
+
+    def __init__(self):
+        pass
+
+    def __is_hit(self, query, search_string):
+        opts = self.opts
+
+        exact_comp = opts["exact"] and not opts["regex"]
+        regexp_comp = opts["regex"]
+
+        if regexp_comp:
+            re_pattern = re.compile(query)
+            return re_pattern.search(search_string)
+        elif exact_comp:
+            return query == search_string
+        else:
+            return query in search_string
+
+        return False
+
+    def __search_issues(self, query):
+        ui = self.ui
+        repo = self.repo
+        opts = self.opts
+
+        case_sens = opts["case_sensitive"]
+        query_filter = opts["property"]
+
+        if not case_sens:
+            query = query.lower()
+
+        hits = []
+
+        mboxes = Artemis.get_all_mboxes(ui, repo)
+        for mbox in mboxes:
+
+            root = Artemis.find_root_key(mbox)
+            # print mbox[root].get_date()
+            # print mbox[root].get_info()
+
+            search_string = mbox[root][query_filter]
+            # non existing property
+            if not search_string:
+                continue
+
+            if not case_sens:
+                search_string = search_string.lower()
+
+            if self.__is_hit(query, search_string):
+                hits.append(mbox.issue)
+
+        return hits
+
+    def __show_results(self, issues):
+        ui = self.ui
+        repo = self.repo
+
+        for issue in issues:
+            mbox = mailbox.Maildir(issue,
+                                   factory=mailbox.MaildirMessage)
+            root = Artemis.find_root_key(mbox)
+            # print mbox.items()
+            # print mbox.keys()
+            # print mbox.values()
+
+            num_replies = str(len(mbox.keys()) - 1)
+            # print mbox[root]["message-id"]
+            ui.write("%s (%s) [%s]: %s\n" %
+                     (
+                         Artemis.get_issue_id(ui, repo, issue),
+                         num_replies.rjust(3, " "),
+                         mbox[root]["state"],
+                         mbox[root]["subject"])
+                     )
+
+    def find(self, ui, repo, query, **opts):
+        self.opts = opts
+        self.ui = ui
+        self.repo = repo
+
+        issues = self.__search_issues(query)
+        self.__show_results(issues)
--- a/artemis/main.py	Fri Mar 04 09:51:09 2016 +0900
+++ b/artemis/main.py	Tue May 24 08:16:38 2016 +0900
@@ -17,21 +17,25 @@
 from list import ArtemisList
 from add import ArtemisAdd
 from show import ArtemisShow
+from find import ArtemisFind
 
 __author__ = 'frostbane'
 __date__ = '2016/03/02'
 
 
 cmdtable = {
-    'ilist'       : (ArtemisList().list,
-                     ArtemisList.commands,
-                     _(ArtemisList.usage)),
-    'iadd'        : (ArtemisAdd().add,
-                     ArtemisAdd.commands,
-                     _(ArtemisAdd.usage)),
-    'ishow'       : (ArtemisShow().show,
-                     ArtemisShow.commands,
-                     _(ArtemisShow.usage)),
+    'ilist' : (ArtemisList().list,
+               ArtemisList.commands,
+               _(ArtemisList.usage)),
+    'iadd'  : (ArtemisAdd().add,
+               ArtemisAdd.commands,
+               _(ArtemisAdd.usage)),
+    'ishow' : (ArtemisShow().show,
+               ArtemisShow.commands,
+               _(ArtemisShow.usage)),
+    'ifind' : (ArtemisFind().find,
+              ArtemisFind.commands,
+              _(ArtemisFind.usage)),
 }
 
 if __name__ == "__main__":